Пример #1
0
 /**
  * Registers the message beans at startup.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
  *
  * @return void
  */
 protected function registerBeans(ApplicationInterface $application)
 {
     // query if the web application folder exists
     if (is_dir($folder = $this->getWebappPath()) === false) {
         // if not, do nothing
         return;
     }
     // load the directories to be parsed
     $directories = array();
     // append the directory found in the servlet managers configuration
     foreach ($this->getDirectories() as $directoryNode) {
         // prepare the custom directory defined in the servlet managers configuration
         $customDir = $folder . DIRECTORY_SEPARATOR . ltrim($directoryNode->getNodeValue()->getValue(), DIRECTORY_SEPARATOR);
         // check if the directory exists
         if (is_dir($customDir)) {
             $directories[] = $customDir;
         }
     }
     // parse the directory for annotated beans
     $directoryParser = new DirectoryParser();
     $directoryParser->injectApplication($application);
     // parse the directories for annotated servlets
     foreach ($directories as $directory) {
         $directoryParser->parse($directory);
     }
     // it's no valid application without at least the epb.xml file
     if (file_exists($deploymentDescriptor = $folder . DIRECTORY_SEPARATOR . 'META-INF' . DIRECTORY_SEPARATOR . 'epb.xml')) {
         try {
             // parse the deployment descriptor for registered beans
             $deploymentDescriptorParser = new DeploymentDescriptorParser();
             $deploymentDescriptorParser->injectApplication($application);
             $deploymentDescriptorParser->parse($deploymentDescriptor, '/a:epb/a:enterprise-beans/a:session');
             $deploymentDescriptorParser->parse($deploymentDescriptor, '/a:epb/a:enterprise-beans/a:message-driven');
         } catch (InvalidConfigurationException $e) {
             $application->getInitialContext()->getSystemLogger()->critical($e->getMessage());
         }
     }
     // load the object manager
     $objectManager = $this->getApplication()->search('ObjectManagerInterface');
     // register the beans found by annotations and the XML configuration
     foreach ($objectManager->getObjectDescriptors() as $descriptor) {
         // check if we've found a bean descriptor
         if ($descriptor instanceof BeanDescriptorInterface) {
             // register the bean
             $this->registerBean($descriptor);
         }
         // if we found a singleton session bean with a startup callback
         if ($descriptor instanceof SingletonSessionBeanDescriptorInterface && $descriptor->isInitOnStartup()) {
             $this->getApplication()->search($descriptor->getName(), array($sessionId = null, array($application)));
         }
     }
 }
Пример #2
0
 /**
  * Finds all servlets which are provided by the webapps and initializes them.
  *
  * @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance
  *
  * @return void
  *
  * @throws \AppserverIo\Appserver\ServletEngine\InvalidServletMappingException
  */
 protected function registerServlets(ApplicationInterface $application)
 {
     // query if the web application folder exists
     if (is_dir($folder = $this->getWebappPath()) === false) {
         // if not, do nothing
         return;
     }
     // load the object manager
     $objectManager = $this->getApplication()->search('ObjectManagerInterface');
     // load the directories to be parsed
     $directories = array();
     // append the directory found in the servlet managers configuration
     /** @var \AppserverIo\Appserver\Core\Api\Node\DirectoryNode $directoryNode */
     foreach ($this->getDirectories() as $directoryNode) {
         // prepare the custom directory defined in the servlet managers configuration
         $customDir = $folder . DIRECTORY_SEPARATOR . ltrim($directoryNode->getNodeValue()->getValue(), DIRECTORY_SEPARATOR);
         // check if the directory exists
         if (is_dir($customDir)) {
             $directories[] = $customDir;
         }
     }
     // initialize the directory parser
     $directoryParser = new DirectoryParser();
     $directoryParser->injectApplication($application);
     // parse the directories for annotated servlets
     foreach ($directories as $directory) {
         $directoryParser->parse($directory);
     }
     // it's no valid application without at least the web.xml file
     if (file_exists($deploymentDescriptor = $folder . DIRECTORY_SEPARATOR . 'WEB-INF' . DIRECTORY_SEPARATOR . 'web.xml')) {
         try {
             // parse the deployment descriptor for registered servlets
             $deploymentDescriptorParser = new DeploymentDescriptorParser();
             $deploymentDescriptorParser->injectApplication($application);
             $deploymentDescriptorParser->parse($deploymentDescriptor, '/a:web-app/a:servlet');
         } catch (InvalidConfigurationException $e) {
             $application->getInitialContext()->getSystemLogger()->critical($e->getMessage());
             return;
         }
         // load the application config
         $config = new \SimpleXMLElement(file_get_contents($deploymentDescriptor));
         $config->registerXPathNamespace('a', 'http://www.appserver.io/appserver');
         // initialize the servlets by parsing the servlet-mapping nodes
         foreach ($config->xpath('/a:web-app/a:servlet-mapping') as $mapping) {
             // load the url pattern and the servlet name
             $urlPattern = (string) $mapping->{'url-pattern'};
             $servletName = (string) $mapping->{'servlet-name'};
             // try to find the servlet with the configured name
             /** @var \AppserverIo\Psr\Servlet\Description\ServletDescriptorInterface $descriptor */
             foreach ($objectManager->getObjectDescriptors() as $descriptor) {
                 // query if we've a servlet and the name matches the mapped servlet name
                 if ($descriptor instanceof ServletDescriptorInterface && $descriptor->getName() === $servletName) {
                     // add the URL pattern
                     $descriptor->addUrlPattern($urlPattern);
                     // override the descriptor with the URL pattern
                     $objectManager->setObjectDescriptor($descriptor);
                     // proceed the next mapping
                     continue 2;
                 }
             }
             // the servlet is added to the dictionary using the complete request path as the key
             throw new InvalidServletMappingException(sprintf('Can\'t find servlet %s for url-pattern %s', $servletName, $urlPattern));
         }
         // initialize the security configuration by parsing the security nodes
         foreach ($config->xpath('/a:web-app/a:security') as $key => $securityParam) {
             // prepare the URL config in JSON format
             $securedUrlConfig = json_decode(json_encode($securityParam), 1);
             // add the web app path to the security config (to resolve relative filenames)
             $securedUrlConfig['webapp-path'] = $folder;
             // add the configuration to the array
             $this->securedUrlConfigs->set($key, $securedUrlConfig);
         }
         // initialize the context by parsing the context-param nodes
         foreach ($config->xpath('/a:web-app/a:context-param') as $contextParam) {
             $this->addInitParameter((string) $contextParam->{'param-name'}, (string) $contextParam->{'param-value'});
         }
         // initialize the session configuration by parsing the session-config children
         foreach ($config->xpath('/a:web-app/a:session-config') as $sessionConfig) {
             foreach ($sessionConfig as $key => $value) {
                 $this->addSessionParameter(str_replace(' ', '', ucwords(str_replace('-', ' ', (string) $key))), (string) $value);
             }
         }
     }
     // register the beans located by annotations and the XML configuration
     foreach ($objectManager->getObjectDescriptors() as $descriptor) {
         // check if we've found a servlet descriptor
         if ($descriptor instanceof ServletDescriptorInterface) {
             // register the servlet
             $this->registerServlet($descriptor);
         }
     }
 }