AppserverIo\Appserver\ServletEngine\DependencyInjection\DeploymentDescriptorParser::parse PHP Method

parse() public method

Parses the servlet context's deployment descriptor file for servlets that has to be registered in the object manager.
public parse ( ) : void
return void
    public function parse()
    {
        // load the web application base directory
        $webappPath = $this->getServletContext()->getWebappPath();
        // prepare the deployment descriptor
        $deploymentDescriptor = $webappPath . DIRECTORY_SEPARATOR . 'WEB-INF' . DIRECTORY_SEPARATOR . 'web.xml';
        // query whether we found epb.xml deployment descriptor file
        if (file_exists($deploymentDescriptor) === false) {
            return;
        }
        // validate the passed configuration file
        /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
        $configurationService = $this->getApplication()->newService('AppserverIo\\Appserver\\Core\\Api\\ConfigurationService');
        $configurationService->validateFile($deploymentDescriptor, null, true);
        // load the object manager instance
        /** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ObjectManagerInterface $objectManager */
        $objectManager = $this->getApplication()->search('ObjectManagerInterface');
        // prepare and initialize the configuration node
        $webAppNode = new WebAppNode();
        $webAppNode->initFromFile($deploymentDescriptor);
        /** @var \AppserverIo\Appserver\Core\Api\Node\ServletNode $servletNode */
        foreach ($webAppNode->getServlets() as $servletNode) {
            // iterate over all configured descriptors and try to load object description
            /** \AppserverIo\Appserver\Core\Api\Node\DescriptorNode $descriptor */
            foreach ($objectManager->getConfiguredDescriptors() as $descriptor) {
                try {
                    // load the descriptor class
                    $descriptorClass = $descriptor->getNodeValue()->getValue();
                    // load the object descriptor, initialize the servlet mappings and add it to the object manager
                    /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
                    if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromConfiguration($servletNode)) {
                        /** @var \AppserverIo\Appserver\Core\Api\Node\ServletMappingNode $servletMappingNode */
                        foreach ($webAppNode->getServletMappings() as $servletMappingNode) {
                            // query whether or not we've to add the URL pattern for the servlet
                            if ((string) $servletNode->getServletName() === (string) $servletMappingNode->getServletName()) {
                                $objectDescriptor->addUrlPattern((string) $servletMappingNode->getUrlPattern());
                            }
                        }
                        // add the object descriptor for the servlet
                        $objectManager->addObjectDescriptor($objectDescriptor, true);
                    }
                    // proceed with the next descriptor
                    continue;
                    // if class can not be reflected continue with next class
                } catch (\Exception $e) {
                    // log an error message
                    $this->getApplication()->getInitialContext()->getSystemLogger()->error($e->__toString());
                    // proceed with the next descriptor
                    continue;
                }
            }
        }
        // initialize the session configuration if available
        /** @var \AppserverIo\Appserver\Core\Api\Node\SessionConfigNode $sessionConfig */
        if ($sessionConfig = $webAppNode->getSessionConfig()) {
            foreach ($sessionConfig->toArray() as $key => $value) {
                $this->getServletContext()->addSessionParameter($key, $value);
            }
        }
        // initialize the error page configuration if available
        /** @var \AppserverIo\Appserver\Core\Api\Node\ErrorPageNode $errorPageNode */
        foreach ($webAppNode->getErrorPages() as $errorPageNode) {
            $this->getServletContext()->addErrorPage((string) $errorPageNode->getErrorCodePattern(), (string) $errorPageNode->getErrorLocation());
        }
        // initialize the context with the context parameters
        /** @var \AppserverIo\Appserver\Core\Api\Node\ContextParamNode $contextParamNode */
        foreach ($webAppNode->getContextParams() as $contextParamNode) {
            $this->getServletContext()->addInitParameter((string) $contextParamNode->getParamName(), (string) $contextParamNode->getParamValue());
        }
    }

Usage Example

 /**
  * 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
  */
 public function registerServlets(ApplicationInterface $application)
 {
     // query whether or not the web application folder exists
     if (is_dir($this->getWebappPath()) === false) {
         return;
     }
     // initialize the directory parser and parse the web application's base directory for annotated servlets
     $directoryParser = new DirectoryParser();
     $directoryParser->injectServletContext($this);
     $directoryParser->parse();
     // initialize the deployment descriptor parser and parse the web application's deployment descriptor for servlets
     $deploymentDescriptorParser = new DeploymentDescriptorParser();
     $deploymentDescriptorParser->injectServletContext($this);
     $deploymentDescriptorParser->parse();
     // load the object manager instance
     /** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ObjectManagerInterface $objectManager */
     $objectManager = $this->getApplication()->search('ObjectManagerInterface');
     // register the beans located by annotations and the XML configuration
     /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
     foreach ($objectManager->getObjectDescriptors() as $descriptor) {
         // check if we've found a servlet descriptor and register the servlet
         if ($descriptor instanceof ServletDescriptorInterface) {
             $this->registerServlet($descriptor);
         }
     }
 }