AppserverIo\Appserver\Core\Listeners\StartContainersListener::handle PHP Method

handle() public method

Handle an event.
See also: League\Event\ListenerInterface::handle()
public handle ( League\Event\EventInterface $event, integer $runlevel = ApplicationServerInterface::NETWORK ) : void
$event League\Event\EventInterface The triggering event
$runlevel integer The actual runlevel
return void
    public function handle(EventInterface $event, $runlevel = ApplicationServerInterface::NETWORK)
    {
        try {
            // load the application server instance
            /** @var \AppserverIo\Appserver\Core\Interfaces\ApplicationServerInterface $applicationServer */
            $applicationServer = $this->getApplicationServer();
            // write a log message that the event has been invoked
            $applicationServer->getSystemLogger()->info($event->getName());
            // initialize the service to load the container configurations
            /** @var \AppserverIo\Appserver\Core\Api\DeploymentService $deploymentService */
            $deploymentService = $this->getDeploymentService();
            $applicationServer->setSystemConfiguration($systemConfiguration = $deploymentService->loadContainerInstances());
            // we also have to re-attach the system configuration to the initial context, because it's not a \Stackable
            /** @var \AppserverIo\Appserver\Application\Interfaces\ContextInterface */
            $initialContext = $applicationServer->getInitialContext();
            $initialContext->setSystemConfiguration($systemConfiguration);
            $applicationServer->setInitialContext($initialContext);
            // load the naming directory
            /** @var \AppserverIo\Appserver\Naming\NamingDirectory $namingDirectory */
            $namingDirectory = $applicationServer->getNamingDirectory();
            // initialize the environment variables
            $namingDirectory->bind('php:env/tmpDirectory', $deploymentService->getBaseDirectory(DirectoryKeys::TMP));
            $namingDirectory->bind('php:env/baseDirectory', $deploymentService->getBaseDirectory());
            $namingDirectory->bind('php:env/umask', $applicationServer->getSystemConfiguration()->getUmask());
            $namingDirectory->bind('php:env/user', $applicationServer->getSystemConfiguration()->getUser());
            $namingDirectory->bind('php:env/group', $applicationServer->getSystemConfiguration()->getGroup());
            // and initialize a container thread for each container
            /** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
            foreach ($applicationServer->getSystemConfiguration()->getContainers() as $containerNode) {
                // load the factory class name
                /** @var \AppserverIo\Appserver\Core\Interfaces\ContainerFactoryInterface $containerFactory */
                $containerFactory = $containerNode->getFactory();
                // use the factory to create a new container instance
                /** @var \AppserverIo\Appserver\Core\Interfaces\ContainerInterface $container */
                $container = $containerFactory::factory($applicationServer, $containerNode, $runlevel);
                $container->start();
                // wait until all servers has been bound to their ports and addresses
                while ($container->hasServersStarted() === false) {
                    // sleep to avoid cpu load
                    usleep(10000);
                }
                // register the container as service
                $applicationServer->bindService($runlevel, $container);
            }
        } catch (\Exception $e) {
            $applicationServer->getSystemLogger()->error($e->__toString());
        }
    }
StartContainersListener