Skip to content

Commit

Permalink
Merge pull request #368 from wagnert/master
Browse files Browse the repository at this point in the history
Closed #284 - Refactor Application implementation/interface + Add --c start argument to change default configuration file
  • Loading branch information
wagnert committed Jan 14, 2015
2 parents f2695d2 + 8e3aefb commit a6b7ba7
Show file tree
Hide file tree
Showing 22 changed files with 496 additions and 211 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# Version 1.0.0-beta4

# Bugfixes

* Closed #350 - Creating EPB references by annotations + XML configuration
* Closed #336 - Positioning of namespace definition next to php tag
* Closed #348 - Changed determination of omitted namespaces

# Features

* Closed #350 - Creating EPB references by annotations + XML configuration
* Closed #284 - Refactor Application implementation/interface
* Closed #289 - Refactoring bean/servlet/manager registration in naming directory
* Closed #285 - Refactor servlet engine virtual host management #285
* Closed #291 - Configuration (XML configuration) based bean declaration
* Closed #300 - Timer Service doesn't support seconds as period
* Add --c start argument to change default configuration file
* Add scanner for changed files in webapps directory
* Remove some SPL Iteratators
* Remove automatic directory parsing of appserver-io/routlt package from context.xml
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"appserver-io/microcron" : "0.1.*",
"appserver-io-psr/epb" : "0.2.*",
"appserver-io-psr/servlet" : "0.7.*",
"appserver-io-psr/application" : "0.4.*",
"appserver-io-psr/messagequeueprotocol" : "0.2.*",
"appserver-io/doppelgaenger" : "0.4.*",
"appserver-io/http" : "0.2.*",
Expand Down
9 changes: 8 additions & 1 deletion etc/appserver/conf.d/context.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<context type="AppserverIo\Appserver\Application\Application">


<!--
<params>
<param name="cache.dir" type="string">/cache</param>
<param name="session.dir" type="string">/session</param>
</params>
-->

<classLoaders>
<classLoader
name="ComposerClassLoader"
Expand Down
45 changes: 37 additions & 8 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace AppserverIo\Appserver\Core;

use AppserverIo\Appserver\Core\Api\Node\ParamNode;
use AppserverIo\Appserver\Core\Utilities\DirectoryKeys;

declare (ticks = 1);
Expand All @@ -37,6 +38,13 @@
// set environmental variables in $_ENV globals per default
$_ENV = appserver_get_envs();

// define the available options
$watch = 'w';
$config = 'c';

// check if server.php has been started with -w and/or -c option
$arguments = getopt("$watch::", array("$config::"));

// define a constant with the appserver base directory
define('APPSERVER_BP', __DIR__);

Expand All @@ -46,19 +54,45 @@
// bootstrap the application
require __DIR__ . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'bootstrap.php';

// query whether a configuration file has been specified or not
if (array_key_exists($config, $arguments) && file_exists($arguments[$config])) {
// set the file passed as parameter
$filename = $arguments[$config];
} elseif (file_exists(sprintf('%s/etc/appserver/appserver.xml', APPSERVER_BP))) {
// try to load the default configuration file
$filename = sprintf('%s/etc/appserver/appserver.xml', APPSERVER_BP);
} else {
// throw an exception if we don't have a configuration file
throw new \Exception('Can\' find a configuration file');
}

// initialize configuration and schema file name
$configurationFileName = DirectoryKeys::realpath(sprintf('%s/%s/appserver.xml', APPSERVER_BP, DirectoryKeys::CONF));
$configurationFileName = DirectoryKeys::realpath($filename);
$schemaFileName = DirectoryKeys::realpath(sprintf('%s/resources/schema/appserver.xsd', APPSERVER_BP));

// initialize the DOMDocument with the configuration file to be validated
$configurationFile = new \DOMDocument();
$configurationFile->load($configurationFileName);

// create a DOMElement with the base.dir configuration
$paramElement = $configurationFile->createElement('param', APPSERVER_BP);
$paramElement->setAttribute('name', DirectoryKeys::BASE);
$paramElement->setAttribute('type', ParamNode::TYPE_STRING);

// append the base.dir DOMElement
if ($paramsNode = $configurationFile->getElementsByTagName('params')->item(0)) {
$paramsNode->appendChild($paramElement);
}

// create a new DOMDocument with the merge content => necessary because else, schema validation fails!!
$mergeDoc = new \DOMDocument();
$mergeDoc->loadXML($configurationFile->saveXML());

// activate internal error handling, necessary to catch errors with libxml_get_errors()
libxml_use_internal_errors(true);

// validate the configuration file with the schema
if ($configurationFile->schemaValidate($schemaFileName) === false) {
if ($mergeDoc->schemaValidate($schemaFileName) === false) {
foreach (libxml_get_errors() as $error) {
$message = "Found a schema validation error on line %s with code %s and message %s when validating configuration file %s";
error_log(var_export($error, true));
Expand All @@ -68,16 +102,11 @@

// initialize the SimpleXMLElement with the content XML configuration file
$configuration = new \AppserverIo\Configuration\Configuration();
$configuration->initFromFile($configurationFileName);
$configuration->addChildWithNameAndValue('baseDirectory', APPSERVER_BP);
$configuration->initFromString($mergeDoc->saveXML());

// create the server instance
$server = new Server($configuration);

// check if server.php has been started with -w option
$watch = 'w';
$arguments = getopt("$watch::");

// if -w option has been passed, watch deployment directory only
if (array_key_exists($watch, $arguments)) {
$server->watch();
Expand Down
34 changes: 18 additions & 16 deletions src/AppserverIo/Appserver/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@

namespace AppserverIo\Appserver\Application;

use AppserverIo\Lang\Reflection\ReflectionObject;
use AppserverIo\Logger\LoggerUtils;
use AppserverIo\Storage\GenericStackable;
use AppserverIo\Storage\StorageInterface;
use AppserverIo\Appserver\Core\Api\Node\ClassLoaderNodeInterface;
use AppserverIo\Appserver\Core\Interfaces\ClassLoaderInterface;
use AppserverIo\Appserver\Naming\BindingTrait;
use AppserverIo\Logger\LoggerUtils;
use AppserverIo\Appserver\Naming\NamingDirectory;
use AppserverIo\Psr\Naming\NamingException;
use AppserverIo\Psr\Naming\NamingDirectoryInterface;
use AppserverIo\Storage\GenericStackable;
use AppserverIo\Storage\StorageInterface;
use AppserverIo\Lang\Reflection\ReflectionObject;
use AppserverIo\Psr\EnterpriseBeans\Annotations\EnterpriseBean;
use AppserverIo\Psr\EnterpriseBeans\Annotations\AnnotationKeys;
use AppserverIo\Psr\Application\ManagerInterface;
use AppserverIo\Appserver\Application\Interfaces\ContextInterface;
use AppserverIo\Psr\Application\ApplicationInterface;
use AppserverIo\Psr\Application\DirectoryAwareInterface;
use AppserverIo\Psr\Application\FilesystemAwareInterface;
use AppserverIo\Appserver\Application\Interfaces\ContextInterface;
use AppserverIo\Appserver\Application\Interfaces\ManagerConfigurationInterface;

/**
Expand All @@ -60,7 +62,7 @@
* @property string $name Name of the application
* @property \AppserverIo\Psr\Naming\NamingDirectoryInterface $namingDirectory The naming directory instance
*/
class Application extends \Thread implements ApplicationInterface
class Application extends \Thread implements ApplicationInterface, DirectoryAwareInterface, FilesystemAwareInterface
{

/**
Expand Down Expand Up @@ -337,23 +339,23 @@ public function getBaseDirectory($directoryToAppend = null)
}

/**
* Returns the absolute path to the web application base directory.
* Returns the absolute path to the applications base directory.
*
* @return string The path to the webapps folder
* @return string The app base directory
*/
public function getWebappPath()
public function getAppBase()
{
return $this->getAppBase() . DIRECTORY_SEPARATOR . $this->getName();
return $this->getNamingDirectory()->search('php:env/appBase');
}

/**
* Returns the absolute path to the applications base directory.
* Returns the absolute path to the web application base directory.
*
* @return string The app base directory
* @return string The path to the webapps folder
*/
public function getAppBase()
public function getWebappPath()
{
return $this->getNamingDirectory()->search('php:env/appBase');
return $this->getNamingDirectory()->search(sprintf('php:env/%s/webappPath', $this->getName()));
}

/**
Expand All @@ -373,7 +375,7 @@ public function getTmpDir()
*/
public function getSessionDir()
{
return $this->getTmpDir() . DIRECTORY_SEPARATOR . ApplicationInterface::SESSION_DIRECTORY;
return $this->getNamingDirectory()->search(sprintf('php:env/%s/sessionDirectory', $this->getName()));
}

/**
Expand All @@ -383,7 +385,7 @@ public function getSessionDir()
*/
public function getCacheDir()
{
return $this->getTmpDir() . DIRECTORY_SEPARATOR . ApplicationInterface::CACHE_DIRECTORY;
return $this->getNamingDirectory()->search(sprintf('php:env/%s/cacheDirectory', $this->getName()));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/AppserverIo/Appserver/Core/AbstractContainerThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@

use AppserverIo\Logger\LoggerUtils;
use AppserverIo\Storage\GenericStackable;
use AppserverIo\Appserver\Naming\NamingDirectory;
use AppserverIo\Psr\Application\ApplicationInterface;
use AppserverIo\Appserver\Core\Interfaces\ContainerInterface;
use AppserverIo\Appserver\Core\Utilities\DirectoryKeys;
use AppserverIo\Appserver\Core\Utilities\ContainerStateKeys;
use AppserverIo\Appserver\Naming\NamingDirectory;

/**
* Class AbstractContainerThread
Expand Down Expand Up @@ -147,8 +146,7 @@ public function main()
// define webservers base dir
define(
'SERVER_BASEDIR',
$this->getInitialContext()->getSystemConfiguration()->getBaseDirectory()->getNodeValue()->__toString()
. DIRECTORY_SEPARATOR
$this->getInitialContext()->getSystemConfiguration()->getBaseDirectory() . DIRECTORY_SEPARATOR
);

// check if we've the old or the new directory structure
Expand Down
46 changes: 23 additions & 23 deletions src/AppserverIo/Appserver/Core/AbstractExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace AppserverIo\Appserver\Core;

use AppserverIo\Appserver\Application\Interfaces\ContextInterface;
use AppserverIo\Appserver\Core\Utilities\DirectoryKeys;
use AppserverIo\Appserver\Core\Interfaces\ExtractorInterface;
use AppserverIo\Appserver\Core\Api\Node\ExtractorNodeInterface;

Expand Down Expand Up @@ -108,9 +107,7 @@ public function deployWebapps()
}

// prepare the filename for the file with the last succesfull deployment timestamp
$successFile = $this->getService()->realpath(
DirectoryKeys::DEPLOY . DIRECTORY_SEPARATOR . ExtractorInterface::FILE_DEPLOYMENT_SUCCESSFULL
);
$successFile = $this->getService()->getDeployDir(ExtractorInterface::FILE_DEPLOYMENT_SUCCESSFULL);

// create a flag file with date of the last successfull deployment
touch($successFile);
Expand All @@ -129,7 +126,7 @@ public function flagArchive(\SplFileInfo $archive, $flag)
// delete all old flags
$this->unflagArchive($archive);
// get archives folder name from deploy dir
$deployFolderName = $this->getDeployDir() . DIRECTORY_SEPARATOR . $archive->getFilename();
$deployFolderName = $this->getDeployDir($archive->getFilename());
// flag archive
file_put_contents($deployFolderName . $flag, $archive->getFilename());
// set correct user/group for the flag file
Expand Down Expand Up @@ -165,7 +162,7 @@ public function isDeployable(\SplFileInfo $archive)
{

// prepare the deploy folder
$deployFolderName = $this->getDeployDir() . DIRECTORY_SEPARATOR . $archive->getFilename();
$deployFolderName = $this->getDeployDir($archive->getFilename());

// check if the .dodeploy flag file exists
if (file_exists($deployFolderName . ExtractorInterface::FLAG_DODEPLOY)) {
Expand All @@ -188,7 +185,7 @@ public function isUndeployable(\SplFileInfo $archive)
{

// prepare the deploy folder
$deployFolderName = $this->getDeployDir() . DIRECTORY_SEPARATOR . $archive->getFilename();
$deployFolderName = $this->getDeployDir($archive->getFilename());

// make sure that NO flag for the archive is available
foreach ($this->getFlags() as $flag) {
Expand All @@ -213,8 +210,7 @@ public function soakArchive(\SplFileInfo $archive)
{

// prepare the upload target in the deploy directory
$deployDirectory = $this->getDeployDir();
$target = $deployDirectory . DIRECTORY_SEPARATOR . $archive->getFilename();
$target = $this->getDeployDir($archive->getFilename());

// move the uploaded file from the tmp to the deploy directory
rename($archive->getPathname(), $target);
Expand All @@ -238,7 +234,7 @@ public function undeployArchive(\SplFileInfo $archive)

// create webapp folder name based on the archive's basename
$webappFolderName = new \SplFileInfo(
$this->getWebappsDir() . DIRECTORY_SEPARATOR . basename($archive->getFilename(), $this->getExtensionSuffix())
$this->getWebappsDir(basename($archive->getFilename(), $this->getExtensionSuffix()))
);

// check if app has to be undeployed
Expand All @@ -258,10 +254,10 @@ public function undeployArchive(\SplFileInfo $archive)
}

} catch (\Exception $e) {

// log error
$this->getInitialContext()
->getSystemLogger()
->error($e->__toString());
$this->getInitialContext()->getSystemLogger()->error($e->__toString());

// flag webapp as failed
$this->flagArchive($archive, ExtractorInterface::FLAG_FAILED);
}
Expand All @@ -283,10 +279,8 @@ public function restoreBackup(\SplFileInfo $archive)
}

// create tmp & webapp folder name based on the archive's basename
$webappFolderName = $this->getWebappsDir() . DIRECTORY_SEPARATOR
. basename($archive->getFilename(), $this->getExtensionSuffix());
$tmpFolderName = $this->getTmpDir() . DIRECTORY_SEPARATOR
. md5(basename($archive->getFilename(), $this->getExtensionSuffix()));
$webappFolderName = $this->getWebappsDir(basename($archive->getFilename(), $this->getExtensionSuffix()));
$tmpFolderName = $this->getTmpDir(md5(basename($archive->getFilename(), $this->getExtensionSuffix())));

// copy backup to webapp directory
$this->copyDir($tmpFolderName, $webappFolderName);
Expand Down Expand Up @@ -361,31 +355,37 @@ public function getService()
/**
* Returns the servers tmp directory.
*
* @param string $relativePathToAppend A relative path to append
*
* @return string
*/
public function getTmpDir()
public function getTmpDir($relativePathToAppend = '')
{
return $this->getService()->getTmpDir();
return $this->getService()->getTmpDir($relativePathToAppend);
}

/**
* Returns the servers deploy directory.
*
* @param string $relativePathToAppend A relative path to append
*
* @return string
*/
public function getDeployDir()
public function getDeployDir($relativePathToAppend = '')
{
return $this->getService()->getDeployDir();
return $this->getService()->getDeployDir($relativePathToAppend);
}

/**
* Returns the servers webapps directory.
*
* @param string $relativePathToAppend A relative path to append
*
* @return string The web application directory
*/
public function getWebappsDir()
public function getWebappsDir($relativePathToAppend = '')
{
return $this->getService()->getWebappsDir();
return $this->getService()->getWebappsDir($relativePathToAppend);
}

/**
Expand Down
Loading

0 comments on commit a6b7ba7

Please sign in to comment.