/**
  * Initializes the available application contexts and returns them.
  *
  * @param \AppserverIo\Appserver\Core\Interfaces\ContainerInterface $container The container we want to add the applications to
  *
  * @return array The array with the application contexts
  */
 public function loadContextInstancesByContainer(ContainerInterface $container)
 {
     try {
         // initialize the array for the context instances
         $contextInstances = array();
         // 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
         $baseContext = new ContextNode();
         $baseContext->initFromFile($baseContextPath);
     } 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('Problems validating base context file %s, this might affect app configurations badly.', $baseContextPath));
     }
     // iterate over all applications and create the context configuration
     foreach (glob($container->getAppBase() . '/*', GLOB_ONLYDIR) as $webappPath) {
         // prepare the context path
         $contextPath = basename($webappPath);
         // start with a fresh clone of the base context configuration
         $context = clone $baseContext;
         // try to load a context configuration (from appserver.xml) for the context path
         if ($contextToMerge = $container->getContainerNode()->getHost()->getContext($contextPath)) {
             $context->merge($contextToMerge);
         }
         // iterate through all context configurations (context.xml), validate and merge them
         foreach ($this->globDir($webappPath . '/META-INF/context.xml') as $contextFile) {
             try {
                 // validate the application specific context
                 $configurationService->validateFile($contextFile, null);
                 // create a new context node instance
                 $contextInstance = new ContextNode();
                 $contextInstance->initFromFile($contextFile);
                 // 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);
         // attach the context to the context instance
         $contextInstances[$contextPath] = $context;
     }
     // return the array with the context instances
     return $contextInstances;
 }
 /**
  * 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;
 }