/**
  * Provisions all web applications.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
  *
  * @return void
  */
 public function provision(ApplicationInterface $application)
 {
     // check if the webapps directory exists
     if (is_dir($webappPath = $application->getWebappPath())) {
         // prepare the glob expression with the application's directories to parse
         $applicationDirectories = AppEnvironmentHelper::getEnvironmentAwareGlobPattern($webappPath, '{WEB-INF,META-INF}/provision', GLOB_BRACE);
         // load the service instance
         /** @var \AppserverIo\Appserver\Core\Api\ProvisioningService $service */
         $service = $this->getService();
         // load the configuration service instance
         /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
         $configurationService = $this->getInitialContext()->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
         // load the container node to initialize the system properties
         /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
         $containerNode = $application->getContainer()->getContainerNode();
         // iterate through all provisioning files (provision.xml), validate them and attach them to the configuration
         foreach ($service->globDir($applicationDirectories, GLOB_BRACE) as $provisionFile) {
             try {
                 // validate the file, but skip it if the validation fails
                 $configurationService->validateFile($provisionFile, null);
                 // load the system properties
                 $properties = $service->getSystemProperties($containerNode);
                 // append the application specific properties
                 $properties->add(SystemPropertyKeys::WEBAPP, $webappPath);
                 $properties->add(SystemPropertyKeys::WEBAPP_NAME, basename($webappPath));
                 $properties->add(SystemPropertyKeys::WEBAPP_CACHE, $application->getCacheDir());
                 $properties->add(SystemPropertyKeys::WEBAPP_SESSION, $application->getSessionDir());
                 // create a new provision node instance and replace the properties
                 $provisionNode = new ProvisionNode();
                 $provisionNode->initFromFile($provisionFile);
                 $provisionNode->replaceProperties($properties);
                 // query whether we've a datasource configured or not
                 if ($datasource = $provisionNode->getDatasource()) {
                     // try to load the datasource from the system configuration
                     $datasourceNode = $service->findByName($datasource->getName());
                     // try to inject the datasource node if available
                     if ($datasourceNode != null) {
                         $provisionNode->injectDatasource($datasourceNode);
                     }
                 }
                 /* Re-provision the provision.xml (reinitialize).
                  *
                  * ATTENTION: The re-provisioning is extremely important, because
                  * this allows dynamic replacement of placeholders by using the
                  * XML file as a template that will reinterpreted with the PHP
                  * interpreter!
                  */
                 $provisionNode->reprovision($provisionFile);
                 // execute the provisioning workflow
                 $this->executeProvision($application, $provisionNode, new \SplFileInfo($webappPath));
             } 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 reading provisioning steps in %s, provisioning might not have been done.', $provisionFile));
             }
         }
     }
 }
 /**
  * Visitor method that registers the class loaders in the application.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface             $application   The application instance to register the class loader with
  * @param \AppserverIo\Appserver\Core\Api\Node\ClassLoaderNodeInterface $configuration The class loader configuration
  *
  * @return void
  */
 public static function visit(ApplicationInterface $application, ClassLoaderNodeInterface $configuration)
 {
     // load the web application path we want to register the class loader for
     $webappPath = $application->getWebappPath();
     // initialize the class path and the enforcement directories
     $classPath = array();
     $enforcementDirs = array();
     // add the possible class path if folder is available
     foreach ($configuration->getDirectories() as $directory) {
         if (is_dir($webappPath . $directory->getNodeValue())) {
             array_push($classPath, $webappPath . $directory->getNodeValue());
             if ($directory->isEnforced()) {
                 array_push($enforcementDirs, $webappPath . $directory->getNodeValue());
             }
         }
     }
     // initialize the arrays of different omit possibilities
     $omittedEnforcement = array();
     $omittedAutoLoading = array();
     // iterate over all namespaces and check if they are omitted in one or the other way
     foreach ($configuration->getNamespaces() as $namespace) {
         // is the enforcement omitted for this namespace?
         if ($namespace->omitEnforcement()) {
             $omittedEnforcement[] = $namespace->getNodeValue()->__toString();
         }
         // is the autoloading omitted for this namespace?
         if ($namespace->omitAutoLoading()) {
             $omittedAutoLoading[] = $namespace->getNodeValue()->__toString();
         }
     }
     // initialize the class loader configuration
     $config = new Config();
     // set the environment mode we want to use
     $config->setValue('environment', $configuration->getEnvironment());
     // set the cache directory
     $config->setValue('cache/dir', $application->getCacheDir());
     // set the default autoloader values
     $config->setValue('autoloader/dirs', $classPath);
     // collect the omitted namespaces (if any)
     $config->setValue('autoloader/omit', $omittedAutoLoading);
     $config->setValue('enforcement/omit', $omittedEnforcement);
     // set the default enforcement configuration values
     $config->setValue('enforcement/dirs', $enforcementDirs);
     $config->setValue('enforcement/enforce-default-type-safety', $configuration->getTypeSafety());
     $config->setValue('enforcement/processing', $configuration->getProcessing());
     $config->setValue('enforcement/level', $configuration->getEnforcementLevel());
     $config->setValue('enforcement/logger', $application->getInitialContext()->getSystemLogger());
     // create a autoloader instance and initialize it
     $classLoader = new DgClassLoader($config);
     $classLoader->init(new StackableStructureMap($config->getValue('autoloader/dirs'), $config->getValue('enforcement/dirs'), $config), new AspectRegister());
     // add the autoloader to the manager
     $application->addClassLoader($classLoader, $configuration);
 }
 /**
  * Registers the entity managers at startup.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
  *
  * @return void
  */
 public function registerEntityManagers(ApplicationInterface $application)
 {
     // build up META-INF directory var
     $metaInfDir = $this->getWebappPath() . DIRECTORY_SEPARATOR . 'META-INF';
     // check if we've found a valid directory
     if (is_dir($metaInfDir) === false) {
         return;
     }
     // check META-INF + subdirectories for XML files with MQ definitions
     /** @var \AppserverIo\Appserver\Core\Api\DeploymentService $service */
     $service = $application->newService('AppserverIo\\Appserver\\Core\\Api\\DeploymentService');
     $xmlFiles = $service->globDir(AppEnvironmentHelper::getEnvironmentAwareGlobPattern($this->getWebappPath(), 'META-INF' . DIRECTORY_SEPARATOR . 'persistence'));
     // load the configuration service instance
     /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
     $configurationService = $application->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
     // load the container node to initialize the system properties
     /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
     $containerNode = $application->getContainer()->getContainerNode();
     // gather all the deployed web applications
     foreach ($xmlFiles as $file) {
         try {
             // validate the file here, but skip if the validation fails
             $configurationService->validateFile($file, null, true);
             // load the system properties
             $properties = $service->getSystemProperties($containerNode);
             // append the application specific properties
             $properties->add(SystemPropertyKeys::WEBAPP, $webappPath = $application->getWebappPath());
             $properties->add(SystemPropertyKeys::WEBAPP_NAME, basename($webappPath));
             $properties->add(SystemPropertyKeys::WEBAPP_CACHE, $application->getCacheDir());
             $properties->add(SystemPropertyKeys::WEBAPP_SESSION, $application->getSessionDir());
             // create a new persistence manager node instance and replace the properties
             $persistenceNode = new PersistenceNode();
             $persistenceNode->initFromFile($file);
             $persistenceNode->replaceProperties($properties);
             // register the entity managers found in the configuration
             foreach ($persistenceNode->getPersistenceUnits() as $persistenceUnitNode) {
                 $this->registerEntityManager($application, $persistenceUnitNode);
             }
         } catch (InvalidConfigurationException $e) {
             // try to load the system logger instance
             /** @var \Psr\Log\LoggerInterface $systemLogger */
             if ($systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger()) {
                 $systemLogger->error($e->getMessage());
                 $systemLogger->critical(sprintf('Persistence configuration file %s is invalid, needed entity managers might be missing.', $file));
             }
         } catch (\Exception $e) {
             // try to load the system logger instance
             /** @var \Psr\Log\LoggerInterface $systemLogger */
             if ($systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger()) {
                 $systemLogger->error($e->__toString());
             }
         }
     }
 }
示例#4
0
 /**
  * Clean up the the directories for the webapp, e. g. to delete cached stuff
  * that has to be recreated after a restart.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application to clean up the directories for
  *
  * @return void
  */
 public function cleanUpFolders(ApplicationInterface $application)
 {
     // create the directory we want to store the sessions in
     $cleanUpFolders = array(new \SplFileInfo($application->getCacheDir()));
     // create the applications temporary directories
     foreach ($cleanUpFolders as $cleanUpFolder) {
         $this->cleanUpDir($cleanUpFolder);
     }
 }