/**
* Deploys the available root directory datasources.
*
* @return void
*/
protected function deployDatasources()
{
// load the container
$container = $this->getContainer();
// load the container and check if we actually have datasource files to work with
if ($datasourceFiles = $this->getDatasourceFiles()) {
// load the naming directory instance
$namingDirectory = $container->getNamingDirectory();
// create a subdirectory for the container's datasoruces
$namingDirectory->createSubdirectory(sprintf('php:env/%s/ds', $this->getContainer()->getName()));
// iterate through all provisioning files (*-ds.xml), validate them and attach them to the configuration
/** @var \AppserverIo\Appserver\Core\Api\ConfigurationService $configurationService */
$configurationService = $this->getConfigurationService();
foreach ($datasourceFiles as $datasourceFile) {
try {
// explode the filename, context name and webapp path
list($filename, $webappPath, $contextName) = $datasourceFile;
// validate the file, but skip it if validation fails
$configurationService->validateFile($filename);
// load the system properties
$systemProperties = $this->getDatasourceService()->getSystemProperties($container->getContainerNode());
// append the application specific properties
$systemProperties->add(SystemPropertyKeys::WEBAPP, $webappPath);
$systemProperties->add(SystemPropertyKeys::WEBAPP_NAME, $contextName);
// load the datasources from the file and replace the properties
$datasourcesNode = new DatasourcesNode();
$datasourcesNode->initFromFile($filename);
$datasourcesNode->replaceProperties($systemProperties);
// store the datasource in the system configuration
/** @var \AppserverIo\Appserver\Core\Api\Node\DatasourceNode $datasourceNode */
foreach ($datasourcesNode->getDatasources() as $datasourceNode) {
// add the datasource to the system configuration
$this->getDatasourceService()->persist($datasourceNode);
// bind the datasource to the naming directory
$namingDirectory->bind(sprintf('php:env/%s/ds/%s', $container->getName(), $datasourceNode->getName()), $datasourceNode);
// log a message that the datasource has been deployed
$this->getInitialContext()->getSystemLogger()->info(sprintf('Successfully deployed datasource %s', $datasourceNode->getName()));
}
// log a message and continue with the next datasource node
} catch (\Exception $e) {
// load the logger and log the XML validation errors
$systemLogger = $this->getInitialContext()->getSystemLogger();
$systemLogger->error($e->__toString());
// additionally log a message that DS will be missing
$systemLogger->critical(sprintf('Will skip reading configuration in %s, datasources might be missing.', $filename));
}
}
}
}