public function initialize(ApplicationInterface $application)
{
// build up META-INF directory var
$metaInfDir = $application->getWebappPath() . DIRECTORY_SEPARATOR . 'META-INF';
// check if we've found a valid directory
if (is_dir($metaInfDir) === false) {
return;
}
// load the timer service executor and timer factories
$timerFactory = $this->getTimerFactory();
$calendarTimerFactory = $this->getCalendarTimerFactory();
$timerServiceExecutor = $this->getTimerServiceExecutor();
// load the service to iterate over application folders
/** @var \AppserverIo\Appserver\Core\Api\DeploymentService $service */
$service = $application->newService('AppserverIo\\Appserver\\Core\\Api\\DeploymentService');
$phpFiles = $service->globDir($metaInfDir . DIRECTORY_SEPARATOR . '*.php');
// iterate all php files
foreach ($phpFiles as $phpFile) {
try {
// cut off the META-INF directory and replace OS specific directory separators
$relativePathToPhpFile = str_replace(DIRECTORY_SEPARATOR, '\\', str_replace($metaInfDir, '', $phpFile));
// now cut off the first directory, that will be '/classes' by default
$pregResult = preg_replace('%^(\\\\*)[^\\\\]+%', '', $relativePathToPhpFile);
$className = substr($pregResult, 0, -4);
// create the reflection class instance
$reflectionClass = new \ReflectionClass($className);
// initialize the timed object instance with the data from the reflection class
$timedObject = TimedObject::fromPhpReflectionClass($reflectionClass);
// check if we have a bean with a @Stateless, @Singleton or @MessageDriven annotation
if ($timedObject->hasAnnotation(Stateless::ANNOTATION) === false && $timedObject->hasAnnotation(Singleton::ANNOTATION) === false && $timedObject->hasAnnotation(MessageDriven::ANNOTATION) === false) {
continue;
// if not, we don't care here!
}
// initialize the stackable for the timeout methods
$timeoutMethods = new StackableStorage();
// create the timed object invoker
$timedObjectInvoker = new TimedObjectInvoker();
$timedObjectInvoker->injectApplication($application);
$timedObjectInvoker->injectTimedObject($timedObject);
$timedObjectInvoker->injectTimeoutMethods($timeoutMethods);
$timedObjectInvoker->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS);
// initialize the stackable for the timers
$timers = new StackableStorage();
// initialize the timer service
$timerService = new TimerService();
$timerService->injectTimers($timers);
$timerService->injectTimerFactory($timerFactory);
$timerService->injectTimedObjectInvoker($timedObjectInvoker);
$timerService->injectCalendarTimerFactory($calendarTimerFactory);
$timerService->injectTimerServiceExecutor($timerServiceExecutor);
$timerService->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS);
// register the initialized timer service
$this->register($timerService);
// log a message that the timer service has been registered
$application->getInitialContext()->getSystemLogger()->info(sprintf('Successfully registered timer service for bean %s', $reflectionClass->getName()));
// if class can not be reflected continue with next class
} catch (\Exception $e) {
// log an error message
$application->getInitialContext()->getSystemLogger()->error($e->__toString());
// proceed with the next bean
continue;
}
}
}