Skip to content

Commit

Permalink
Merge pull request #12 from wagnert/master
Browse files Browse the repository at this point in the history
Allow connection configuration by passing properties file to QueueCon…
  • Loading branch information
wagnert committed Mar 14, 2016
2 parents 0ac5207 + aaf8c43 commit 184a3e3
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 71 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
language: php

php:
- 7.0
- 5.6
- 5.5
- 5.4


matrix:
allow_failures:
- php: 7.0

before_install:
- pecl install pthreads-beta
- pecl install pthreads-2.0.10
- pecl install xdebug
- pyrus install pear/PHP_CodeSniffer
- phpenv rehash
- wget https://scrutinizer-ci.com/ocular.phar

Expand All @@ -22,4 +26,4 @@ script:

notifications:
email: info@appserver.io
hipchat: 95d47a72c5372d4a0fef20048c3200@Appserver
hipchat: 95d47a72c5372d4a0fef20048c3200@Appserver
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Version 2.0.0

## Bugfixes

* None

## Features

* Allow connection configuration by passing properties file to QueueConnection constructor

# Version 1.0.1

## Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion build.default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
#--------------------------------------------------------------------------------

# ---- Module Release Settings --------------------------------------------------
release.version = 1.0.1
release.version = 2.0.0
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"php" : ">=5.4.0",
"rhumsaa/uuid" : "~2.4",
"guzzle/guzzle" : "3.9.*",
"appserver-io/properties" : "~1.0",
"appserver-io-psr/pms" : "~1.0",
"appserver-io-psr/socket" : "~1.0",
"appserver-io-psr/application" : "~1.0"
Expand All @@ -37,4 +38,4 @@
"keywords" : [
"messaging pms components"
]
}
}
132 changes: 70 additions & 62 deletions src/AppserverIo/Messaging/QueueConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Guzzle\Http\Client;
use Guzzle\Http\Exception\CurlException;
use AppserverIo\Psr\Pms\MessageInterface;
use AppserverIo\Properties\PropertiesInterface;
use AppserverIo\Properties\Properties;
use AppserverIo\Messaging\Utils\PropertyKeys;

/**
* A connection implementation that handles the connection to the message queue.
Expand All @@ -41,14 +44,14 @@ class QueueConnection
*
* @var string
*/
const DEFAULT_SCHEME = 'http';
const DEFAULT_TRANSPORT = 'http';

/**
* The default client sockets IP address.
*
* @var string
*/
const DEFAULT_HOST = '127.0.0.1';
const DEFAULT_ADDRESS = '127.0.0.1';

/**
* The default client sockets port.
Expand All @@ -58,32 +61,18 @@ class QueueConnection
const DEFAULT_PORT = 8587;

/**
* The name of the webapp using this client connection.
*
* @var string
*/
protected $appName;

/**
* The default transport to use.
* The default index file.
*
* @var string
*/
protected $transport = QueueConnection::DEFAULT_SCHEME;
const DEFAULT_INDEX_FILE = 'index.mq';

/**
* The client socket's IP address.
* The name of the webapp using this client connection.
*
* @var string
*/
protected $address = QueueConnection::DEFAULT_HOST;

/**
* The client socket's port.
*
* @var integer
*/
protected $port = QueueConnection::DEFAULT_PORT;
protected $appName;

/**
* Holds an ArrayList with the initialized sessions.
Expand All @@ -106,12 +95,25 @@ class QueueConnection
*/
protected $client;

/**
* The default properties for the context configuration.
*
* @var array
*/
protected $defaultProperties = array(
'transport' => QueueConnection::DEFAULT_TRANSPORT,
'address' => QueueConnection::DEFAULT_ADDRESS,
'port' => QueueConnection::DEFAULT_PORT,
'indexFile' => QueueConnection::DEFAULT_INDEX_FILE
);

/**
* Initializes the connection.
*
* @param string $appName Name of the webapp using this client connection
* @param string $appName Name of the webapp using this client connection
* @param \AppserverIo\Properties\PropertiesInterface $properties The properties containing the connection parameters
*/
public function __construct($appName = '')
public function __construct($appName = '', PropertiesInterface $properties = null)
{

// set the application name
Expand All @@ -120,6 +122,40 @@ public function __construct($appName = '')
// initialize the message queue parser and the session
$this->parser = new MessageQueueParser();
$this->sessions = new \ArrayObject();

// initialize the default properties if no ones has been passed
if ($properties == null) {
// initialize the default properties
$properties = new Properties();
foreach ($this->defaultProperties as $key => $value) {
$properties->setProperty($key, $value);
}
}

// inject the properties
$this->injectProperties($properties);
}

/**
* The properties used to create the connection.
*
* @param \AppserverIo\Properties\PropertiesInterface $properties The connection properties
*
* @return void
*/
public function injectProperties(PropertiesInterface $properties)
{
$this->properties = $properties;
}

/**
* Return's the properties used to create the connection.
*
* @return \AppserverIo\Properties\PropertiesInterface The connection properties
*/
public function getProperties()
{
return $this->properties;
}

/**
Expand Down Expand Up @@ -154,19 +190,6 @@ public function getAppName()
return $this->appName;
}

/**
* Sets the IP address or domain name of the server the
* message queue is running on.
*
* @param string $address Holds the server to connect to
*
* @return void
*/
public function setAddress($address)
{
$this->address = $address;
}

/**
* Returns the IP address or domain name of the server the
* message queue is running on.
Expand All @@ -175,19 +198,7 @@ public function setAddress($address)
*/
public function getAddress()
{
return $this->address;
}

/**
* Sets the port for the connection.
*
* @param integer $port Holds the port for the connection
*
* @return void
*/
public function setPort($port)
{
$this->port = $port;
return $this->getProperties()->getProperty(PropertyKeys::ADDRESS);
}

/**
Expand All @@ -197,29 +208,27 @@ public function setPort($port)
*/
public function getPort()
{
return $this->port;
return $this->getProperties()->getProperty(PropertyKeys::PORT);
}

/**
* Sets the transport to use.
*
* @param integer $transport The transport to use
* Returns the transport to use.
*
* @return void
* @return string The transport to use.
*/
public function setTransport($transport)
public function getTransport()
{
$this->transport = $transport;
return $this->getProperties()->getProperty(PropertyKeys::TRANSPORT);
}

/**
* Returns the transport to use.
* Returns the index file to use.
*
* @return integer The transport to use.
* @return string The index file to use.
*/
public function getTransport()
public function getIndexFile()
{
return $this->transport;
return $this->getProperties()->getProperty(PropertyKeys::INDEX_FILE);
}

/**
Expand Down Expand Up @@ -305,7 +314,7 @@ public function send(MessageInterface $message, $validateResponse = false)
*/
protected function getPath()
{
return sprintf('/%s/index.mq', $this->getAppName());
return sprintf('/%s/%s', $this->getAppName(), $this->getIndexFile());
}

/**
Expand All @@ -316,8 +325,7 @@ protected function getPath()
*/
protected function getBaseUrl()
{
// initialize the requested URL with the default connection values
return $this->getTransport() . '://' . $this->getAddress() . ':' . $this->getPort();
return sprintf('%s://%s:%s', $this->getTransport(), $this->getAddress(), $this->getPort());
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/AppserverIo/Messaging/QueueConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace AppserverIo\Messaging;

use AppserverIo\Properties\PropertiesInterface;

/**
* A factory implementation for creating queue connections.
*
Expand All @@ -42,12 +44,13 @@ protected function __construct()
/**
* Returns the queue connection instance as singleton.
*
* @param string $appName Name of the webapp using this client connection
* @param string $appName Name of the webapp using this client connection
* @param \AppserverIo\Properties\PropertiesInterface $properties The properties containing the connection parameters
*
* @return \AppserverIo\Messaging\QueueConnection The singleton instance
*/
public static function createQueueConnection($appName)
public static function createQueueConnection($appName = '', PropertiesInterface $properties = null)
{
return new QueueConnection($appName);
return new QueueConnection($appName, $properties);
}
}
70 changes: 70 additions & 0 deletions src/AppserverIo/Messaging/Utils/PropertyKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* AppserverIo\Messaging\Utils\PropertyKeys
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @author Tim Wagner <tw@appserver.io>
* @copyright 2015 TechDivision GmbH <info@appserver.io>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/appserver-io/messaging
* @link http://www.appserver.io
*/

namespace AppserverIo\Messaging\Utils;

/**
* This class holds the property keys used to create the host connection.
*
* @author Tim Wagner <tw@appserver.io>
* @copyright 2015 TechDivision GmbH <info@appserver.io>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/appserver-io/messaging
* @link http://www.appserver.io
*/
class PropertyKeys
{

/**
* The property name with the host address.
*
* @var string
*/
const ADDRESS = 'address';

/**
* The property name with the host port.
*
* @var string
*/
const PORT = 'port';

/**
* The property name with the transport to use.
*
* @var string
*/
const TRANSPORT = 'transport';

/**
* The property name with the index file to use.
*
* @var string
*/
const INDEX_FILE = 'indexFile';

/**
* Private constructor for marking the class as utility.
*/
final protected function __construct()
{
/* Class is a utility class */
}
}

0 comments on commit 184a3e3

Please sign in to comment.