/** * Create and return a new instance of the session handler with * the passed configuration. * * @param \AppserverIo\Appserver\Core\Api\Node\SessionHandlerNodeInterface $sessionHandlerNode The session handler configuration * @param \AppserverIo\Appserver\ServletEngine\SessionSettingsInterface $sessionSettings The session settings * * @return \AppserverIo\Appserver\ServletEngine\Session\SessionHandlerInterface The session handler instance */ public static function create(SessionHandlerNodeInterface $sessionHandlerNode, SessionSettingsInterface $sessionSettings) { // reflect the class $reflectionClass = new ReflectionClass($sessionHandlerNode->getType()); // create and initialize the session handler instance $sessionHandler = $reflectionClass->newInstanceArgs($sessionHandlerNode->getParamsAsArray()); $sessionHandler->injectSessionSettings($sessionSettings); // return the initialzed instance return $sessionHandler; }
/** * Test @Route annotation initialization. * * @return null */ public function testGetRouteAnnotation() { // initialize the annotations to ignore and the aliases $ignore = array(); $aliases = array('Route' => 'AppserverIo\\Psr\\Servlet\\Annotations\\Route'); // we need to load the annotation instance from the mock servlet implementation $reflectionClass = new ReflectionClass('AppserverIo\\Psr\\Servlet\\Annotations\\HelloWorldServlet', $ignore, $aliases); $reflectionAnnotation = $reflectionClass->getAnnotation(Route::ANNOTATION); $routeAnnotation = $reflectionAnnotation->newInstance($reflectionAnnotation->getAnnotationName(), $reflectionAnnotation->getValues()); // define the URL patterns and the initialization parameters $urlPattern = array('/helloWorld.do', '/helloWorld.do*'); $initParams = array(array('name1', 'value1'), array('name2', 'value2')); // check te values $this->assertSame('helloWorld', $routeAnnotation->getName()); $this->assertSame('Hello World', $routeAnnotation->getDisplayName()); $this->assertSame('The Hello World! as servlet implementation.', $routeAnnotation->getDescription()); $this->assertSame($urlPattern, $routeAnnotation->getUrlPattern()); $this->assertSame($initParams, $routeAnnotation->getInitParams()); }
/** * Checks if the isAbstract() method works as expected. * * @return void */ public function testIsAbstract() { $this->assertFalse($this->reflectionClass->isAbstract()); }
/** * Check that the merge() method with descriptor containing an other * method name throws an Exception. * * @return void * @expectedException AppserverIo\Routlt\Description\DescriptorException */ public function testMergeWithNotMatchingDescriptor() { // create a reflection class and load the reflection method $reflectionClass = new ReflectionClass(__CLASS__); $reflectionMethod = $reflectionClass->getMethod('nonameAction'); // initialize the descriptor instance $this->descriptor->fromReflectionMethod($reflectionMethod); $descriptor = new ActionDescriptor(); $descriptor->setMethodName('otherNameAction'); // merge the descriptors $this->descriptor->merge($descriptor); }
/** * Returns an array of reflection method instances from the passed reflection class. * * @param \AppserverIo\Lang\Reflection\ReflectionClass $reflectionClass The reflection class to return the methods for * @param integer $filter The filter used for loading the methods * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded * @param array $annotationAliases An array with annotation aliases used when create annotation instances * * @return array An array with ReflectionMethod instances */ public static function fromReflectionClass(ReflectionClass $reflectionClass, $filter = -1, array $annotationsToIgnore = array(), array $annotationAliases = array()) { // initialize the array for the reflection methods $reflectionMethods = array(); // load the reflection methods and initialize the array with the reflection methods $phpReflectionClass = $reflectionClass->toPhpReflectionClass(); foreach ($phpReflectionClass->getMethods($filter) as $phpReflectionMethod) { $reflectionMethods[$phpReflectionMethod->getName()] = ReflectionMethod::fromPhpReflectionMethod($phpReflectionMethod, $annotationsToIgnore, $annotationAliases); } // return the array with the initialized reflection methods return $reflectionMethods; }
/** * Utility method to create a Principal for the given username. This * creates an instance of the principalClassName type if this option was * specified. If principalClassName was not specified, a SimplePrincipal * is created. * * @param \AppserverIo\Lang\String $name The name of the principal * * @return Principal The principal instance * @throws \Exception Is thrown if the custom principal type cannot be created */ public function createIdentity(string $name) { //initialize the principal $principal = null; // query whether or not a principal class name has been specified if ($this->principalClassName == null) { $principal = new SimplePrincipal($name); } else { $reflectionClass = new ReflectionClass($this->principalClassName->__toString()); $principal = $reflectionClass->newInstanceArgs(array($name)); } // return the principal instance return $principal; }
/** * Register the servlet described by the passed descriptor. * * @param \AppserverIo\Psr\Servlet\Description\ServletDescriptorInterface $descriptor The servlet descriptor * * @return void */ protected function registerServlet(ServletDescriptorInterface $descriptor) { try { // create a new reflection class instance $reflectionClass = new ReflectionClass($descriptor->getClassName()); // instantiate the servlet $instance = $reflectionClass->newInstance(); // load servlet name $servletName = $descriptor->getName(); // initialize the servlet configuration $servletConfig = new ServletConfiguration(); $servletConfig->injectServletContext($this); $servletConfig->injectServletName($servletName); // append the init params to the servlet configuration foreach ($descriptor->getInitParams() as $paramName => $paramValue) { $servletConfig->addInitParameter($paramName, $paramValue); } // initialize the servlet $instance->init($servletConfig); // the servlet is added to the dictionary using the complete request path as the key $this->addServlet($servletName, $instance); // prepend the url-pattern - servlet mapping to the servlet mappings foreach ($descriptor->getUrlPatterns() as $pattern) { $this->addServletMapping($pattern, $servletName); } // register the EPB references foreach ($descriptor->getEpbReferences() as $epbReference) { $this->registerEpbReference($epbReference); } // register the resource references foreach ($descriptor->getResReferences() as $resReference) { $this->registerResReference($resReference); } // register the persistence unit references foreach ($descriptor->getPersistenceUnitReferences() as $persistenceUnitReference) { $this->registerPersistenceUnitReference($persistenceUnitReference); } } catch (\Exception $e) { // log the exception $this->getApplication()->getInitialContext()->getSystemLogger()->critical($e->__toString()); } }
/** * Creates a new reflection class instance from the passed PHP reflection class. * * @param \ReflectionClass $reflectionClass The PHP reflection class to load the data from * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded * @param array $annotationAliases An array with annotation aliases used when create annotation instances * * @return \AppserverIo\Lang\Reflection\ReflectionClass The instance */ public static function fromPhpReflectionClass(\ReflectionClass $reflectionClass, array $annotationsToIgnore = array(), array $annotationAliases = array()) { return new ReflectionClass($reflectionClass->getName(), $annotationsToIgnore, $annotationAliases); }
/** * Parses the servlet context's deployment descriptor file for servlets * that has to be registered in the object manager. * * @return void */ public function parse() { // load the web application base directory $webappPath = $this->getAuthenticationContext()->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); // prepare and initialize the configuration node $webAppNode = new WebAppNode(); $webAppNode->initFromFile($deploymentDescriptor); // query whether or not we've a login configuration /** @var \AppserverIo\Appserver\Core\Api\Node\LoginConfigNode $loginConfig */ if ($loginConfig = $webAppNode->getLoginConfig()) { // create the authentication method instance $reflectionClass = new ReflectionClass($this->mapAuthenticator($loginConfig->getAuthMethod()->__toString())); $authenticator = $reflectionClass->newInstanceArgs(array($loginConfig, $this->getAuthenticationContext(), new Boolean(true))); // add the authentication method itself $this->getAuthenticationContext()->addAuthenticator($authenticator); // initialize the security roles, that are part of the new security subsystem /** @var \AppserverIo\Appserver\Core\Api\Node\SecurityRoleNode $securityRoleNode */ foreach ($webAppNode->getSecurityRoles() as $securityRoleNode) { // do something here } // initialize the security roles, that are part of the new security subsystem /** @var \AppserverIo\Appserver\Core\Api\Node\SecurityConstraintNode $securityContstraintNode */ foreach ($webAppNode->getSecurityConstraints() as $securityContstraintNode) { // prepare the array with the authentication constraint role names $roleNames = array(); if ($authConstraint = $securityContstraintNode->getAuthConstraint()) { $roleNames = $authConstraint->getRoleNamesAsArray(); } /** @var \AppserverIo\Appserver\Core\Api\Node\WebResourceCollectionNode $webResourceCollectionNode */ foreach ($securityContstraintNode->getWebResourceCollections() as $webResourceCollectionNode) { // prepare the arrays for the HTTP methods and the method omissions $httpMethods = $webResourceCollectionNode->getHttpMethodsAsArray(); $httpMethodOmissions = $webResourceCollectionNode->getHttpMethodOmissionsAsArray(); /** @var \AppserverIo\Appserver\Core\Api\Node\UrlPatternNode $urlPatternNode */ foreach ($webResourceCollectionNode->getUrlPatterns() as $urlPatternNode) { // prepare the URL pattern to authenticator mapping with the necessary data $mapping = new Mapping($urlPatternNode->__toString(), $authenticator->getSerial(), $roleNames, $httpMethods, $httpMethodOmissions); // add the URL pattern to authenticator mapping $this->getAuthenticationContext()->addMapping($mapping); } } } } }
/** * Returns an array of reflection property instances from the passed reflection class. * * @param \AppserverIo\Lang\Reflection\ReflectionClass $reflectionClass The reflection class to return the properties for * @param integer $filter The filter used for loading the properties * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded * @param array $annotationAliases An array with annotation aliases used when create annotation instances * * @return array An array with ReflectionProperty instances */ public static function fromReflectionClass(ReflectionClass $reflectionClass, $filter = 0, array $annotationsToIgnore = array(), array $annotationAliases = array()) { // initialize the array for the reflection properties $reflectionProperties = array(); // load the reflection properties and initialize the array with the reflection properties $phpReflectionClass = $reflectionClass->toPhpReflectionClass(); foreach ($phpReflectionClass->getProperties($filter) as $phpReflectionProperty) { $reflectionProperties[$phpReflectionProperty->getName()] = ReflectionProperty::fromPhpReflectionProperty($phpReflectionProperty, $annotationsToIgnore, $annotationAliases); } // return the array with the initialized reflection properties return $reflectionProperties; }