1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Appserver\PersistenceContainer\BeanLocator |
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; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Psr\Di\ProviderInterface; |
24
|
|
|
use AppserverIo\Psr\Di\ObjectManagerInterface; |
25
|
|
|
use AppserverIo\Psr\EnterpriseBeans\BeanContextInterface; |
26
|
|
|
use AppserverIo\Psr\EnterpriseBeans\ResourceLocatorInterface; |
27
|
|
|
use AppserverIo\Psr\EnterpriseBeans\EnterpriseBeansException; |
28
|
|
|
use AppserverIo\Psr\EnterpriseBeans\Description\MessageDrivenBeanDescriptorInterface; |
29
|
|
|
use AppserverIo\Psr\EnterpriseBeans\Description\StatefulSessionBeanDescriptorInterface; |
30
|
|
|
use AppserverIo\Psr\EnterpriseBeans\Description\SingletonSessionBeanDescriptorInterface; |
31
|
|
|
use AppserverIo\Psr\EnterpriseBeans\Description\StatelessSessionBeanDescriptorInterface; |
32
|
|
|
use AppserverIo\Psr\EnterpriseBeans\Description\BeanDescriptorInterface; |
33
|
|
|
use AppserverIo\Appserver\Core\Environment; |
34
|
|
|
use AppserverIo\Appserver\Core\Utilities\EnvironmentKeys; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The bean resource locator implementation. |
38
|
|
|
* |
39
|
|
|
* @author Tim Wagner <[email protected]> |
40
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
41
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
42
|
|
|
* @link https://github.com/appserver-io/appserver |
43
|
|
|
* @link http://www.appserver.io |
44
|
|
|
*/ |
45
|
|
|
class BeanLocator implements ResourceLocatorInterface |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Runs a lookup for the session bean with the passed class name |
50
|
|
|
* |
51
|
|
|
* If the passed class name is a session bean an instance |
52
|
|
|
* will be returned. |
53
|
|
|
* |
54
|
|
|
* @param \AppserverIo\Psr\EnterpriseBeans\BeanContextInterface $beanManager The bean manager instance |
55
|
|
|
* @param string $className The name of the session bean's class |
56
|
|
|
* @param array $args The arguments passed to the session beans constructor |
57
|
|
|
* |
58
|
|
|
* @return object The requested session bean |
59
|
|
|
*/ |
60
|
|
|
public function lookup(BeanContextInterface $beanManager, $className, array $args = array()) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// load the object manager |
64
|
|
|
/** @var \AppserverIo\Psr\Di\ObjectManagerInterface $objectManager */ |
65
|
|
|
$objectManager = $beanManager->getApplication()->search(ObjectManagerInterface::IDENTIFIER); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// load the session ID from the environment |
68
|
|
|
$sessionId = Environment::singleton()->getAttribute(EnvironmentKeys::SESSION_ID); |
69
|
|
|
|
70
|
|
|
// query whether or not an object descriptor is available |
71
|
|
|
if ($objectManager->hasObjectDescriptor($className)) { |
72
|
|
|
// load the bean descriptor |
73
|
|
|
$descriptor = $objectManager->getObjectDescriptor($className); |
74
|
|
|
|
75
|
|
|
// query whether we've a SFSB |
76
|
|
|
if ($descriptor instanceof StatefulSessionBeanDescriptorInterface) { |
77
|
|
|
// try to load the stateful session bean from the bean manager |
78
|
|
|
if ($instance = $beanManager->lookupStatefulSessionBean($sessionId, $className)) { |
|
|
|
|
79
|
|
|
// load the object manager and re-inject the dependencies |
80
|
|
|
/** @var \AppserverIo\Psr\Di\ProviderInterface $provider */ |
81
|
|
|
$provider = $beanManager->getApplication()->search(ProviderInterface::IDENTIFIER); |
82
|
|
|
$provider->injectDependencies($descriptor, $instance); |
83
|
|
|
|
84
|
|
|
// we've to check for post-detach callbacks |
85
|
|
|
foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) { |
86
|
|
|
$instance->$postDetachCallback(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// return the instance |
90
|
|
|
return $instance; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// if not create a new instance and return it |
94
|
|
|
$instance = $beanManager->get($className); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// we've to check for post-construct callbacks |
97
|
|
|
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) { |
98
|
|
|
$instance->$postConstructCallback(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// return the instance |
102
|
|
|
return $instance; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// query whether we've a SSB |
106
|
|
|
if ($descriptor instanceof SingletonSessionBeanDescriptorInterface) { |
107
|
|
|
// try to load the singleton session bean from the bean manager |
108
|
|
|
if ($instance = $beanManager->lookupSingletonSessionBean($className)) { |
|
|
|
|
109
|
|
|
// load the object manager and re-inject the dependencies |
110
|
|
|
/** @var \AppserverIo\Psr\Di\ProviderInterface $provider */ |
111
|
|
|
$provider = $beanManager->getApplication()->search(ProviderInterface::IDENTIFIER); |
112
|
|
|
$provider->injectDependencies($descriptor, $instance); |
113
|
|
|
|
114
|
|
|
// we've to check for post-detach callbacks |
115
|
|
|
foreach ($descriptor->getPostDetachCallbacks() as $postDetachCallback) { |
116
|
|
|
$instance->$postDetachCallback(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// return the instance |
120
|
|
|
return $instance; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// singleton session beans MUST extends \Stackable |
124
|
|
|
if (is_subclass_of($descriptor->getClassName(), '\Stackable') === false) { |
125
|
|
|
throw new EnterpriseBeansException(sprintf('Singleton session bean %s MUST extend \Stackable', $descriptor->getClassName())); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// if not create a new instance and return it |
129
|
|
|
$instance = $beanManager->newSingletonSessionBeanInstance($className); |
|
|
|
|
130
|
|
|
|
131
|
|
|
// add the singleton session bean to the container |
132
|
|
|
$beanManager->getSingletonSessionBeans()->set($className, $instance); |
|
|
|
|
133
|
|
|
|
134
|
|
|
// we've to check for post-construct callback |
135
|
|
|
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) { |
136
|
|
|
$instance->$postConstructCallback(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// return the instance |
140
|
|
|
return $instance; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// query whether we've a SLSB |
144
|
|
|
if ($descriptor instanceof StatelessSessionBeanDescriptorInterface) { |
145
|
|
|
// if not create a new instance and return it |
146
|
|
|
$instance = $beanManager->get($className); |
147
|
|
|
|
148
|
|
|
// we've to check for post-construct callback |
149
|
|
|
foreach ($descriptor->getPostConstructCallbacks() as $postConstructCallback) { |
150
|
|
|
$instance->$postConstructCallback(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// return the instance |
154
|
|
|
return $instance; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// query whether we've a MDB |
158
|
|
|
if ($descriptor instanceof MessageDrivenBeanDescriptorInterface) { |
159
|
|
|
// create a new instance and return it |
160
|
|
|
return $beanManager->get($className); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// query whether we simply have a bean |
164
|
|
|
if ($descriptor instanceof BeanDescriptorInterface) { |
165
|
|
|
// create the bean instance without the factory |
166
|
|
|
return $beanManager->get($className); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// finally try to let the container create a new instance of the bean |
171
|
|
|
return $beanManager->newInstance($className); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|