/**
* Initializes the context instance for the passed webapp path.
*
* @param \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode The container to load the context for
* @param string $webappPath The path to the web application
*
* @return \AppserverIo\Appserver\Core\Api\Node\ContextNode The initialized context instance
*/
public function loadContextInstance(ContainerNodeInterface $containerNode, $webappPath)
{
// prepare the context path
$contextPath = basename($webappPath);
// load the system properties
$properties = $this->getSystemProperties($containerNode);
// append the application specific properties
$properties->add(SystemPropertyKeys::WEBAPP, $webappPath);
$properties->add(SystemPropertyKeys::WEBAPP_NAME, $contextPath);
// validate the base context file
/** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
$configurationService = $this->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
$configurationService->validateFile($baseContextPath = $this->getConfdDir('context.xml'), null);
//load it as default if validation succeeds
$context = new ContextNode();
$context->initFromFile($baseContextPath);
$context->replaceProperties($properties);
// set the context webapp path
$context->setWebappPath($webappPath);
// try to load a context configuration (from appserver.xml) for the context path
if ($contextToMerge = $containerNode->getHost()->getContext($contextPath)) {
$contextToMerge->replaceProperties($properties);
$context->merge($contextToMerge);
}
// iterate through all context configurations (context.xml), validate and merge them
foreach ($this->globDir(AppEnvironmentHelper::getEnvironmentAwareGlobPattern($webappPath, 'META-INF/context')) as $contextFile) {
try {
// validate the application specific context
$configurationService->validateFile($contextFile, null);
// create a new context node instance and replace the properties
$contextInstance = new ContextNode();
$contextInstance->initFromFile($contextFile);
$contextInstance->replaceProperties($properties);
// merge it into the default configuration
$context->merge($contextInstance);
} catch (ConfigurationException $ce) {
// load the logger and log the XML validation errors
$systemLogger = $this->getInitialContext()->getSystemLogger();
$systemLogger->error($ce->__toString());
// additionally log a message that DS will be missing
$systemLogger->critical(sprintf('Will skip app specific context file %s, configuration might be faulty.', $contextFile));
}
}
// set the real context name
$context->setName($contextPath);
$context->setEnvironmentName(AppEnvironmentHelper::getEnvironmentModifier($webappPath));
// return the initialized context instance
return $context;
}