Passed
Pull Request — master (#1108)
by Tim
06:37
created

DeploymentDescriptorParser::getBeanContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * \AppserverIo\Appserver\PersistenceContainer\DependencyInjection\DeploymentDescriptorParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\PersistenceContainer\DependencyInjection;
22
23
use AppserverIo\Psr\Di\ObjectManagerInterface;
24
use AppserverIo\Appserver\Core\Api\Node\EpbNode;
25
use AppserverIo\Configuration\Interfaces\NodeInterface;
26
use AppserverIo\Appserver\DependencyInjectionContainer\AbstractDeploymentDescriptorParser;
27
28
/**
29
 * Parser to parse a deployment descriptor for beans.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/appserver
35
 * @link      http://www.appserver.io
36
 */
37
class DeploymentDescriptorParser extends AbstractDeploymentDescriptorParser
38
{
39
40
    /**
41
     * Parses the bean context's deployment descriptor file for beans
42
     * that has to be registered in the object manager.
43
     *
44
     * @return void
45
     */
46
    public function parse()
47
    {
48
49
        // load the deployment descriptors that has to be parsed
50
        $deploymentDescriptors = $this->loadDeploymentDescriptors();
51
52
        // parse the deployment descriptors from the conf.d and the application's META-INF directory
53
        foreach ($deploymentDescriptors as $deploymentDescriptor) {
54
            // query whether we found epb.xml deployment descriptor file
55
            if (file_exists($deploymentDescriptor) === false) {
56
                continue;
57
            }
58
59
            // validate the passed configuration file
60
            /** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
61
            $configurationService = $this->getApplication()->newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
0 ignored issues
show
Bug introduced by
The method newService() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            $configurationService = $this->getApplication()->/** @scrutinizer ignore-call */ newService('AppserverIo\Appserver\Core\Api\ConfigurationService');
Loading history...
62
            $configurationService->validateFile($deploymentDescriptor, null, true);
63
64
            // prepare and initialize the configuration node
65
            $epbNode = new EpbNode();
66
            $epbNode->initFromFile($deploymentDescriptor);
0 ignored issues
show
Bug introduced by
$deploymentDescriptor of type string is incompatible with the type AppserverIo\Lang\String expected by parameter $filename of AppserverIo\Description\...actNode::initFromFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            $epbNode->initFromFile(/** @scrutinizer ignore-type */ $deploymentDescriptor);
Loading history...
67
68
            // query whether or not the deployment descriptor contains any beans
69
            /** @var \AppserverIo\Appserver\Core\Api\Node\EnterpriseBeansNode $enterpriseBeans */
70
            if ($enterpriseBeans = $epbNode->getEnterpriseBeans()) {
71
                // parse the session beans of the deployment descriptor
72
                /** @var \AppserverIo\Appserver\Core\Api\Node\SessionNode $sessionNode */
73
                foreach ($enterpriseBeans->getSessions() as $sessionNode) {
74
                    $this->processConfigurationNode($sessionNode);
75
                }
76
                // parse the message driven beans from the deployment descriptor
77
                /** @var \AppserverIo\Appserver\Core\Api\Node\MessageDrivenNode $messageDrivenNode */
78
                foreach ($enterpriseBeans->getMessageDrivens() as $messageDrivenNode) {
79
                    $this->processConfigurationNode($messageDrivenNode);
80
                }
81
            }
82
        }
83
    }
84
85
    /**
86
     * Creates a new descriptor instance from the data of the passed configuration node
87
     * and add's it to the object manager.
88
     *
89
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The node to process
90
     *
91
     * @return void
92
     */
93
    protected function processConfigurationNode(NodeInterface $node)
94
    {
95
96
        // load the object manager instance
97
        /** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */
98
        $objectManager = $this->getApplication()->search(ObjectManagerInterface::IDENTIFIER);
99
100
        // iterate over all configured descriptors and try to load object description
101
        /** \AppserverIo\Appserver\Core\Api\Node\DescriptorNode $descriptor */
102
        foreach ($this->getDescriptors() as $descriptor) {
103
            try {
104
                // load the descriptor class
105
                $descriptorClass = $descriptor->getNodeValue()->getValue();
106
107
                // load the object descriptor, initialize the servlet mappings and add it to the object manager
108
                /** \AppserverIo\Psr\Deployment\DescriptorInterface $objectDescriptor */
109
                if ($objectDescriptor = $descriptorClass::newDescriptorInstance()->fromConfiguration($node)) {
110
                    $objectManager->addObjectDescriptor($objectDescriptor, true);
111
                }
112
113
                // proceed with the next descriptor
114
                continue;
115
116
                // if class can not be reflected continue with next class
117
            } catch (\Exception $e) {
118
                // log an error message
119
                $this->getApplication()->getInitialContext()->getSystemLogger()->error($e->__toString());
0 ignored issues
show
Bug introduced by
The method getInitialContext() does not exist on AppserverIo\Psr\Application\ApplicationInterface. It seems like you code against a sub-type of AppserverIo\Psr\Application\ApplicationInterface such as AppserverIo\Appserver\Application\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
                $this->getApplication()->/** @scrutinizer ignore-call */ getInitialContext()->getSystemLogger()->error($e->__toString());
Loading history...
120
121
                // proceed with the next descriptor
122
                continue;
123
            }
124
        }
125
    }
126
}
127