/**
* Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.datasource.internal;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.commons.datasource.DataSourceInformationRepository;
import org.apache.synapse.commons.datasource.factory.DataSourceInformationRepositoryFactory;
import org.apache.synapse.commons.util.MiscellaneousUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.datasource.DataSourceInformationManager;
import org.wso2.carbon.datasource.DataSourceInformationRepositoryService;
import org.wso2.carbon.datasource.DataSourceInformationRepositoryServiceImpl;
import org.wso2.carbon.datasource.multitenancy.DataSourceInitializer;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.securevault.SecretCallbackHandlerService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.securevault.secret.SecretCallbackHandler;
import org.wso2.securevault.secret.handler.SharedSecretCallbackHandlerCache;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @scr.component name="org.wso2.carbon.datasource" immediate="true"
* @scr.reference name="registry.service"
* interface="org.wso2.carbon.registry.core.service.RegistryService" cardinality="1..1"
* policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService"
* @scr.reference name="secret.callback.handler.service"
* interface="org.wso2.carbon.securevault.SecretCallbackHandlerService"
* cardinality="1..1" policy="dynamic"
* bind="setSecretCallbackHandlerService" unbind="unsetSecretCallbackHandlerService"
* @scr.reference name="carbon.core.configurationContextService"
* interface="org.wso2.carbon.utils.ConfigurationContextService"
* cardinality="1..1" policy="dynamic" bind="setConfigurationContextService"
* unbind="unsetConfigurationContextService"
*/
public class DataSourceServiceComponent {
private static final Log log = LogFactory.getLog(DataSourceServiceComponent.class);
private static RegistryService registryService;
private static String DATA_SOURCE_PROPERTIES = "datasources.properties";
public static final String DATA_SOURCE_REPOSITORY_MAP = "repositoryMap";
private ServiceRegistration registration;
private SecretCallbackHandlerService secretCallbackHandlerService;
private static ConfigurationContext configurationContext;
private int tenantId;
private DataSourceInformationManager dataSourceInformationManager;
protected void activate(ComponentContext cmpCtx) throws Exception {
if (registryService != null) {
tenantId = SuperTenantCarbonContext.getCurrentContext(
configurationContext).getTenantId();
try {
Registry registry = registryService.getConfigSystemRegistry(tenantId);
if (registry == null) {
handleException("Registry is null.");
}
BundleContext bundleContex = cmpCtx.getBundleContext();
bundleContex.registerService(Axis2ConfigurationContextObserver.class.getName(),
new DataSourceInitializer(), null);
if (secretCallbackHandlerService != null) {
SecretCallbackHandler secretCallbackHandler =
secretCallbackHandlerService.getSecretCallbackHandler();
SharedSecretCallbackHandlerCache.getInstance()
.setSecretCallbackHandler(secretCallbackHandler);
}
dataSourceInformationManager = new DataSourceInformationManager();
dataSourceInformationManager.setRepository(getDSFromCarbonDSConfig());
dataSourceInformationManager.setRegistry(registry);
DataSourceInformationRepository repository
= dataSourceInformationManager.getRepository();
@SuppressWarnings("unchecked")
Map<Integer, DataSourceInformationRepository> repositoryMap =
(Map<Integer, DataSourceInformationRepository>) configurationContext.
getProperty(DataSourceServiceComponent.DATA_SOURCE_REPOSITORY_MAP);
if (repositoryMap == null) {
repositoryMap = new HashMap<Integer, DataSourceInformationRepository>();
repositoryMap.put(tenantId, repository);
configurationContext.setProperty(
DataSourceServiceComponent.DATA_SOURCE_REPOSITORY_MAP, repositoryMap);
}
DataSourceInformationRepositoryService repositoryServiceImpl =
new DataSourceInformationRepositoryServiceImpl(repository);
registration = cmpCtx.getBundleContext().registerService(
DataSourceInformationRepositoryService.class.getName(),
repositoryServiceImpl, null);
} catch (RegistryException e) {
handleException("Error getting SystemRegistry from Registry Service");
}
}
}
protected void deactivate(ComponentContext componentContext) throws Exception {
dataSourceInformationManager.shutDown();
if (log.isDebugEnabled()) {
log.debug("Stopping the (RuleServerManager Component");
}
componentContext.getBundleContext().ungetService(registration.getReference());
}
protected void setRegistryService(RegistryService regService) {
registryService = regService;
}
protected void unsetRegistryService(RegistryService regService) {
registryService = null;
}
public static RegistryService getRegistryService() {
return registryService;
}
private DataSourceInformationRepository getDSFromCarbonDSConfig() {
if (tenantId == 0) {
String carbonConfDir = CarbonUtils.getCarbonConfigDirPath();
String propertiesFile = carbonConfDir + File.separator +
DATA_SOURCE_PROPERTIES;
return DataSourceInformationRepositoryFactory.createDataSourceInformationRepository(
loadProperties(propertiesFile));
} else {
return new DataSourceInformationRepository();
}
}
private Properties loadProperties(String filePath) {
File dataSourceFile = new File(filePath);
if (!dataSourceFile.exists()) {
return MiscellaneousUtil.loadProperties(DATA_SOURCE_PROPERTIES);
}
Properties properties = new Properties();
InputStream in = null;
try {
in = new FileInputStream(dataSourceFile);
properties.load(in);
} catch (IOException e) {
String msg = "Error loading properties from a file at :" + filePath;
log.warn(msg, e);
return properties;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
return properties;
}
private void handleException(String msg) {
log.error(msg);
throw new IllegalArgumentException(msg);
}
protected void setSecretCallbackHandlerService(
SecretCallbackHandlerService secretCallbackHandlerService) {
if (log.isDebugEnabled()) {
log.debug("SecretCallbackHandlerService bound to the ESB initialization process");
}
this.secretCallbackHandlerService = secretCallbackHandlerService;
}
protected void unsetSecretCallbackHandlerService(
SecretCallbackHandlerService secretCallbackHandlerService) {
if (log.isDebugEnabled()) {
log.debug("SecretCallbackHandlerService unbound from the ESB environment");
}
this.secretCallbackHandlerService = null;
}
/**
* This method is used to bind the ConfigurationContext which is being used in order to access
* it in this particular class
*
* @param configurationContextService configuration context service required to grab the current
*/
public void setConfigurationContextService(
ConfigurationContextService configurationContextService) {
configurationContext = configurationContextService.getServerConfigContext();
}
/**
* This method is used to unbind the ConfigurationContext which is being used from the context
* of this particular class
*
* @param configurationContextService configuration context service required to grab the current
*/
public void unsetConfigurationContextService(
ConfigurationContextService configurationContextService) {
configurationContext = null;
}
}