1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Appserver\Core\AbstractManager |
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
|
|
|
* @author Bernhard Wick <[email protected]> |
16
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
17
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
18
|
|
|
* @link https://github.com/appserver-io/appserver |
19
|
|
|
* @link http://www.appserver.io |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace AppserverIo\Appserver\Core; |
23
|
|
|
|
24
|
|
|
use Psr\Container\ContainerInterface; |
25
|
|
|
use AppserverIo\Storage\StorageInterface; |
26
|
|
|
use AppserverIo\Storage\GenericStackable; |
27
|
|
|
use AppserverIo\Lang\Reflection\ReflectionClass; |
28
|
|
|
use AppserverIo\Psr\Di\ProviderInterface; |
29
|
|
|
use AppserverIo\Psr\Application\ManagerInterface; |
30
|
|
|
use AppserverIo\Psr\Application\ApplicationInterface; |
31
|
|
|
use AppserverIo\Psr\Application\ManagerConfigurationInterface; |
32
|
|
|
use AppserverIo\Psr\Naming\InitialContext as NamingDirectory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Abstract manager implementation. |
36
|
|
|
* |
37
|
|
|
* @author Tim Wagner <[email protected]> |
38
|
|
|
* @author Bernhard Wick <[email protected]> |
39
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
40
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
41
|
|
|
* @link https://github.com/appserver-io/appserver |
42
|
|
|
* @link http://www.appserver.io |
43
|
|
|
* |
44
|
|
|
* @property \AppserverIo\Storage\StorageInterface $data Storage container for arbitrary data |
45
|
|
|
* @property \AppserverIo\Psr\Naming\InitialContext $initialContext The initial context of our naming directory |
46
|
|
|
* @property \AppserverIo\Psr\Application\ApplicationInterface $application The application to manage |
47
|
|
|
* @property \AppserverIo\Psr\Application\ManagerConfigurationInterface $managerConfiguration The application to manage |
48
|
|
|
*/ |
49
|
|
|
abstract class AbstractManager extends GenericStackable implements ManagerInterface, ContainerInterface |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Inject the configuration for this manager. |
54
|
|
|
* |
55
|
|
|
* @param \AppserverIo\Psr\Application\ManagerConfigurationInterface $managerConfiguration The managers configuration |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function injectManagerConfiguration(ManagerConfigurationInterface $managerConfiguration) |
60
|
|
|
{ |
61
|
|
|
$this->managerConfiguration = $managerConfiguration; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Inject the data storage. |
66
|
|
|
* |
67
|
|
|
* @param \AppserverIo\Storage\StorageInterface $data The data storage to use |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function injectData(StorageInterface $data) |
72
|
|
|
{ |
73
|
|
|
$this->data = $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Inject the application instance. |
78
|
|
|
* |
79
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function injectApplication(ApplicationInterface $application) |
84
|
|
|
{ |
85
|
|
|
$this->application = $application; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the application instance. |
90
|
|
|
* |
91
|
|
|
* @return \AppserverIo\Psr\Application\ApplicationInterface|\AppserverIo\Psr\Naming\NamingDirectoryInterface The application instance |
92
|
|
|
*/ |
93
|
|
|
public function getApplication() |
94
|
|
|
{ |
95
|
|
|
return $this->application; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the storage with the naming directory. |
100
|
|
|
* |
101
|
|
|
* @return \AppserverIo\Storage\StorageInterface The storage with the naming directory |
102
|
|
|
*/ |
103
|
|
|
public function getNamingDirectory() |
104
|
|
|
{ |
105
|
|
|
return $this->getApplication()->getNamingDirectory(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The global naming directory. |
110
|
|
|
* |
111
|
|
|
* @param \AppserverIo\Psr\Naming\InitialContext $initialContext The global naming directory |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function injectInitialContext(NamingDirectory $initialContext) |
116
|
|
|
{ |
117
|
|
|
$this->initialContext = $initialContext; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the global naming directory. |
122
|
|
|
* |
123
|
|
|
* @return \AppserverIo\Psr\Naming\InitialContext The global naming directory |
124
|
|
|
*/ |
125
|
|
|
public function getInitialContext() |
126
|
|
|
{ |
127
|
|
|
return $this->initialContext; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the absolute path to the web application. |
132
|
|
|
* |
133
|
|
|
* @return string The absolute path |
134
|
|
|
*/ |
135
|
|
|
public function getWebappPath() |
136
|
|
|
{ |
137
|
|
|
return $this->getApplication()->getWebappPath(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the absolute path to the application directory. |
142
|
|
|
* |
143
|
|
|
* @return string The absolute path to the application directory |
144
|
|
|
*/ |
145
|
|
|
public function getAppBase() |
146
|
|
|
{ |
147
|
|
|
return $this->getApplication()->getAppBase(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the absolute path to the application server's base directory. |
152
|
|
|
* |
153
|
|
|
* @param string $directoryToAppend A directory to append to the base directory |
154
|
|
|
* |
155
|
|
|
* @return string The absolute path the application server's base directory |
156
|
|
|
*/ |
157
|
|
|
public function getBaseDirectory($directoryToAppend = null) |
158
|
|
|
{ |
159
|
|
|
return $this->getApplication()->getBaseDirectory($directoryToAppend); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Query's whether or not the attribute is available. |
164
|
|
|
* |
165
|
|
|
* @param string $key The key of the attribute to query for |
166
|
|
|
* |
167
|
|
|
* @return boolean TRUE if the attribute is set, else FALSE |
168
|
|
|
*/ |
169
|
|
|
public function hasAttribute($key) |
170
|
|
|
{ |
171
|
|
|
return $this->data->has($key); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Registers the value with the passed key in the container. |
176
|
|
|
* |
177
|
|
|
* @param string $key The key to register the value with |
178
|
|
|
* @param object $value The value to register |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function setAttribute($key, $value) |
183
|
|
|
{ |
184
|
|
|
$this->data->set($key, $value); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the attribute with the passed key from the container. |
189
|
|
|
* |
190
|
|
|
* @param string $key The key the requested value is registered with |
191
|
|
|
* |
192
|
|
|
* @return mixed|null The requested value if available |
193
|
|
|
*/ |
194
|
|
|
public function getAttribute($key) |
195
|
|
|
{ |
196
|
|
|
if ($this->data->has($key)) { |
197
|
|
|
return $this->data->get($key); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns the manager's data container. |
203
|
|
|
* |
204
|
|
|
* @return \AppserverIo\Storage\StorageInterface The data container |
205
|
|
|
*/ |
206
|
|
|
public function getAttributes() |
207
|
|
|
{ |
208
|
|
|
return $this->data; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns a new reflection class intance for the passed class name. |
213
|
|
|
* |
214
|
|
|
* @param string $className The class name to return the reflection class instance for |
215
|
|
|
* |
216
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
217
|
|
|
*/ |
218
|
|
|
public function newReflectionClass($className) |
219
|
|
|
{ |
220
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->newReflectionClass($className); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return's the manager configuration. |
225
|
|
|
* |
226
|
|
|
* @return \AppserverIo\Psr\Application\ManagerConfigurationInterface The manager configuration |
227
|
|
|
*/ |
228
|
|
|
public function getManagerConfiguration() |
229
|
|
|
{ |
230
|
|
|
return $this->managerConfiguration; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Returns a reflection class intance for the passed class name. |
235
|
|
|
* |
236
|
|
|
* @param string $className The class name to return the reflection class instance for |
237
|
|
|
* |
238
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
239
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::getReflectionClass() |
240
|
|
|
*/ |
241
|
|
|
public function getReflectionClass($className) |
242
|
|
|
{ |
243
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->getReflectionClass($className); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns a reflection class intance for the passed class name. |
248
|
|
|
* |
249
|
|
|
* @param object $instance The instance to return the reflection class instance for |
250
|
|
|
* |
251
|
|
|
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance |
252
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::newReflectionClass() |
253
|
|
|
* @see \AppserverIo\Psr\Di\ProviderInterface::getReflectionClass() |
254
|
|
|
*/ |
255
|
|
|
public function getReflectionClassForObject($instance) |
256
|
|
|
{ |
257
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->getReflectionClassForObject($instance); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Returns a new instance of the passed class name. |
262
|
|
|
* |
263
|
|
|
* @param string $className The fully qualified class name to return the instance for |
264
|
|
|
* @param array $args Arguments to pass to the constructor of the instance |
265
|
|
|
* |
266
|
|
|
* @return object The instance itself |
267
|
|
|
*/ |
268
|
|
|
public function newInstance($className, array $args = array()) |
269
|
|
|
{ |
270
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->newInstance($className, $args); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Finds an entry of the container by its identifier and returns it. |
275
|
|
|
* |
276
|
|
|
* @param string $id Identifier of the entry to look for |
277
|
|
|
* |
278
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface No entry was found for **this** identifier. |
279
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface Error while retrieving the entry. |
280
|
|
|
* |
281
|
|
|
* @return mixed Entry. |
282
|
|
|
*/ |
283
|
|
|
public function get($id) |
284
|
|
|
{ |
285
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->get($id); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Returns true if the container can return an entry for the given identifier. |
290
|
|
|
* Returns false otherwise. |
291
|
|
|
* |
292
|
|
|
* `has($id)` returning TRUE does not mean that `get($id)` will not throw an exception. |
293
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
294
|
|
|
* |
295
|
|
|
* @param string $id Identifier of the entry to look for. |
296
|
|
|
* |
297
|
|
|
* @return boolean TRUE if an entroy for the given identifier exists, else FALSE |
298
|
|
|
*/ |
299
|
|
|
public function has($id) |
300
|
|
|
{ |
301
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->has($id); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Register's the passed value with the passed ID. |
306
|
|
|
* |
307
|
|
|
* @param string $id The ID of the value to add |
308
|
|
|
* @param string $value The value to add |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
* @throws \AppserverIo\Appserver\DependencyInjectionContainer\ContainerException Is thrown, if a value with the passed key has already been added |
312
|
|
|
*/ |
313
|
|
|
public function set($id, $value) |
314
|
|
|
{ |
315
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->set($id, $value); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Query's whether or not an instance of the passed already exists. |
320
|
|
|
* |
321
|
|
|
* @param string $id Identifier of the entry to look for |
322
|
|
|
* |
323
|
|
|
* @return boolean TRUE if an instance exists, else FALSE |
324
|
|
|
*/ |
325
|
|
|
public function exists($id) |
326
|
|
|
{ |
327
|
|
|
return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->exists($id); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Lifecycle callback that'll be invoked after the application has been started. |
332
|
|
|
* |
333
|
|
|
* @param \AppserverIo\Psr\Application\ApplicationInterface $application The application instance |
334
|
|
|
* |
335
|
|
|
* @return void |
336
|
|
|
* @see \AppserverIo\Psr\Application\ManagerInterface::postStartup() |
337
|
|
|
*/ |
338
|
|
|
public function postStartup(ApplicationInterface $application) |
339
|
|
|
{ |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Parse the manager's object descriptors. |
344
|
|
|
* |
345
|
|
|
* @return void |
346
|
|
|
*/ |
347
|
|
|
public function parseObjectDescriptors() |
348
|
|
|
{ |
349
|
|
|
|
350
|
|
|
// load the manager configuration |
351
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNodeInterface $managerConfiguration */ |
352
|
|
|
$managerConfiguration = $this->getManagerConfiguration(); |
353
|
|
|
|
354
|
|
|
// load the object description configuration |
355
|
|
|
$objectDescription = $managerConfiguration->getObjectDescription(); |
356
|
|
|
|
357
|
|
|
// initialize the parsers and start initializing the object descriptors |
358
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ParserNodeInterface */ |
359
|
|
|
foreach ($objectDescription->getParsers() as $parserConfiguration) { |
360
|
|
|
// query whether or not a factory has been configured |
361
|
|
|
if ($parserFactoryConfiguration = $parserConfiguration->getFactory()) { |
362
|
|
|
// create a reflection class from the parser factory |
363
|
|
|
$reflectionClass = new ReflectionClass($parserFactoryConfiguration); |
364
|
|
|
$factory = $reflectionClass->newInstance(); |
365
|
|
|
|
366
|
|
|
// create the parser instance and start parsing |
367
|
|
|
$parser = $factory->createParser($parserConfiguration, $this); |
|
|
|
|
368
|
|
|
$parser->parse(); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Returns the managers object descriptors to use. |
375
|
|
|
* |
376
|
|
|
* @return array The object descriptors |
377
|
|
|
*/ |
378
|
|
|
public function getDescriptors() |
379
|
|
|
{ |
380
|
|
|
|
381
|
|
|
// load the manager configuration |
382
|
|
|
/** @var \AppserverIo\Appserver\Core\Api\Node\ManagerNodeInterface $managerConfiguration */ |
383
|
|
|
$managerConfiguration = $this->getManagerConfiguration(); |
384
|
|
|
|
385
|
|
|
// load the object description configuration |
386
|
|
|
$objectDescription = $managerConfiguration->getObjectDescription(); |
387
|
|
|
|
388
|
|
|
// return the configured object descriptors |
389
|
|
|
return $objectDescription->getDescriptors(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* A dummy functionality to implement the stop functionality. |
394
|
|
|
* |
395
|
|
|
* @return void |
396
|
|
|
* \AppserverIo\Psr\Application\ManagerInterface::stop() |
397
|
|
|
*/ |
398
|
|
|
public function stop() |
399
|
|
|
{ |
400
|
|
|
error_log(sprintf('Now shutdown manager "%s"', $this->getIdentifier())); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|