/** * Registers the entity managers at startup. * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance * * @return void */ protected 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($metaInfDir . DIRECTORY_SEPARATOR . 'persistence.xml'); // gather all the deployed web applications foreach ($xmlFiles as $file) { try { // try to initialize a SimpleXMLElement $sxe = new \SimpleXMLElement($file, null, true); $sxe->registerXPathNamespace('a', 'http://www.appserver.io/appserver'); // validate the file here, if it is not valid we can skip further steps try { /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */ $configurationService = $application->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService'); $configurationService->validateFile($file, null, true); } catch (InvalidConfigurationException $e) { /** @var \Psr\Log\LoggerInterface $systemLogger */ $systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger(); $systemLogger->error($e->getMessage()); $systemLogger->critical(sprintf('Message queue configuration file %s is invalid, needed queues might be missing.', $file)); return; } // initialize the entity managers found in the deployment descriptor $persistenceNode = new PersistenceNode(); $persistenceNode->initFromFile($file); foreach ($persistenceNode->getPersistenceUnits() as $persistenceUnitNode) { $this->registerEntityManager($application, $persistenceUnitNode); } // if class can not be reflected continue with next class } catch (\Exception $e) { // log an error message $application->getInitialContext()->getSystemLogger()->error($e->__toString()); // proceed with the next queue continue; } } }
/** * Registers aspects written within source files which we might encounter * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance * * @return void */ public function registerAspectXml(ApplicationInterface $application) { /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */ $configurationService = $application->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService'); // check if we even have a XMl file to read from $xmlPaths = $configurationService->globDir($this->getWebappPath() . DIRECTORY_SEPARATOR . '{WEB-INF,META-INF,common}' . DIRECTORY_SEPARATOR . self::CONFIG_FILE, GLOB_BRACE); foreach ($xmlPaths as $xmlPath) { // iterate all XML configuration files we found if (is_readable($xmlPath)) { // validate the file here, if it is not valid we can skip further steps try { $configurationService->validateFile($xmlPath, null, true); } catch (InvalidConfigurationException $e) { /** @var \Psr\Log\LoggerInterface $systemLogger */ $systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger(); $systemLogger->error($e->getMessage()); $systemLogger->critical(sprintf('Pointcuts configuration file %s is invalid, AOP functionality might not work as expected.', $xmlPath)); continue; } // load the aop config $config = new \SimpleXMLElement(file_get_contents($xmlPath)); $config->registerXPathNamespace('a', 'http://www.appserver.io/appserver'); // create us an aspect // name of the aspect will be the application name $aspect = new Aspect(); $aspect->setName($xmlPath); // check if we got some pointcuts foreach ($config->xpath('/a:pointcuts/a:pointcut') as $pointcutConfiguration) { // build up the pointcut and add it to the collection $pointcut = new Pointcut(); $pointcut->setAspectName($aspect->getName()); $pointcut->setName((string) $pointcutConfiguration->{'pointcut-name'}); $pointcut->setPointcutExpression(new PointcutExpression((string) $pointcutConfiguration->{'pointcut-pattern'})); $aspect->getPointcuts()->add($pointcut); } // check if we got some advices foreach ($config->xpath('/a:pointcuts/a:advice') as $adviceConfiguration) { // build up the advice and add it to the aspect $advice = new Advice(); $advice->setAspectName((string) $adviceConfiguration->{'advice-aspect'}); $advice->setName($advice->getAspectName() . '->' . (string) $adviceConfiguration->{'advice-name'}); $advice->setCodeHook((string) $adviceConfiguration->{'advice-type'}); $pointcutPointcut = $this->generatePointcutPointcut((array) $adviceConfiguration->{'advice-pointcuts'}->{'pointcut-name'}, $aspect); $advice->getPointcuts()->add($pointcutPointcut); // finally add the advice to our aspect (we will also add it without pointcuts of its own) $aspect->getAdvices()->add($advice); } // if the aspect contains pointcuts or advices it can be used if ($aspect->getPointcuts()->count() > 0 || $aspect->getAdvices()->count() > 0) { $this->getAspectRegister()->set($aspect->getName(), $aspect); } } } }
/** * 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()); } } } }
/** * Deploys the message queues. * * @param \AppserverIo\Psr\Application\ApplicationInterface|\AppserverIo\Psr\Naming\NamingDirectoryInterface $application The application instance * * @return void */ protected function registerMessageQueues(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($metaInfDir . DIRECTORY_SEPARATOR . 'message-queues.xml'); // initialize the array for the creating the subdirectories $this->directories = new GenericStackable(); $this->directories[] = $application->getNamingDirectory(); // gather all the deployed web applications foreach ($xmlFiles as $file) { try { // try to initialize a SimpleXMLElement $sxe = new \SimpleXMLElement($file, null, true); $sxe->registerXPathNamespace('a', 'http://www.appserver.io/appserver'); // lookup the MessageQueue's defined in the passed XML node if (($nodes = $sxe->xpath('/a:message-queues/a:message-queue')) === false) { continue; } // validate the file here, if it is not valid we can skip further steps try { /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */ $configurationService = $application->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService'); $configurationService->validateFile($file, null, true); } catch (InvalidConfigurationException $e) { /** @var \Psr\Log\LoggerInterface $systemLogger */ $systemLogger = $this->getApplication()->getInitialContext()->getSystemLogger(); $systemLogger->error($e->getMessage()); $systemLogger->critical(sprintf('Message queue configuration file %s is invalid, needed queues might be missing.', $file)); return; } // iterate over all found queues and initialize them foreach ($nodes as $node) { $this->registeMessageQueue($node); } // if class can not be reflected continue with next class } catch (\Exception $e) { // log an error message $application->getInitialContext()->getSystemLogger()->error($e->__toString()); // proceed with the next queue continue; } } }
/** * Has been automatically invoked by the container after the application * instance has been created. * * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance * * @return void * @see \AppserverIo\Psr\Application\ManagerInterface::initialize() */ public function initialize(ApplicationInterface $application) { // build up META-INF directory var $metaInfDir = $application->getWebappPath() . DIRECTORY_SEPARATOR . 'META-INF'; // check if we've found a valid directory if (is_dir($metaInfDir) === false) { return; } // load the timer service executor and timer factories $timerFactory = $this->getTimerFactory(); $calendarTimerFactory = $this->getCalendarTimerFactory(); $timerServiceExecutor = $this->getTimerServiceExecutor(); // load the service to iterate over application folders /** @var \AppserverIo\Appserver\Core\Api\DeploymentService $service */ $service = $application->newService('AppserverIo\\Appserver\\Core\\Api\\DeploymentService'); $phpFiles = $service->globDir($metaInfDir . DIRECTORY_SEPARATOR . '*.php'); // iterate all php files foreach ($phpFiles as $phpFile) { try { // cut off the META-INF directory and replace OS specific directory separators $relativePathToPhpFile = str_replace(DIRECTORY_SEPARATOR, '\\', str_replace($metaInfDir, '', $phpFile)); // now cut off the first directory, that will be '/classes' by default $pregResult = preg_replace('%^(\\\\*)[^\\\\]+%', '', $relativePathToPhpFile); $className = substr($pregResult, 0, -4); // create the reflection class instance $reflectionClass = new \ReflectionClass($className); // initialize the timed object instance with the data from the reflection class $timedObject = TimedObject::fromPhpReflectionClass($reflectionClass); // check if we have a bean with a @Stateless, @Singleton or @MessageDriven annotation if ($timedObject->hasAnnotation(Stateless::ANNOTATION) === false && $timedObject->hasAnnotation(Singleton::ANNOTATION) === false && $timedObject->hasAnnotation(MessageDriven::ANNOTATION) === false) { continue; // if not, we don't care here! } // initialize the stackable for the timeout methods $timeoutMethods = new StackableStorage(); // create the timed object invoker $timedObjectInvoker = new TimedObjectInvoker(); $timedObjectInvoker->injectApplication($application); $timedObjectInvoker->injectTimedObject($timedObject); $timedObjectInvoker->injectTimeoutMethods($timeoutMethods); $timedObjectInvoker->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS); // initialize the stackable for the timers $timers = new StackableStorage(); // initialize the timer service $timerService = new TimerService(); $timerService->injectTimers($timers); $timerService->injectTimerFactory($timerFactory); $timerService->injectTimedObjectInvoker($timedObjectInvoker); $timerService->injectCalendarTimerFactory($calendarTimerFactory); $timerService->injectTimerServiceExecutor($timerServiceExecutor); $timerService->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS); // register the initialized timer service $this->register($timerService); // log a message that the timer service has been registered $application->getInitialContext()->getSystemLogger()->info(sprintf('Successfully registered timer service for bean %s', $reflectionClass->getName())); // if class can not be reflected continue with next class } catch (\Exception $e) { // log an error message $application->getInitialContext()->getSystemLogger()->error($e->__toString()); // proceed with the next bean continue; } } }