in modules/commons/src/main/java/org/apache/synapse/commons/datasource/JNDIBasedDataSourceRepository.java [76:239]
public void register(DataSourceInformation information) {
validateInitialized();
String dataSourceName = information.getDatasourceName();
validateDSName(dataSourceName);
Properties properties = information.getProperties();
InitialContext context = null;
Properties jndiEvn = null;
if (properties == null || properties.isEmpty()) {
if (initialContext != null) {
context = initialContext;
if (log.isDebugEnabled()) {
log.debug("Empty JNDI properties for datasource " + dataSourceName);
log.debug("Using system-wide jndi properties : " + jndiProperties);
}
jndiEvn = jndiProperties;
}
}
if (context == null) {
jndiEvn = createJNDIEnvironment(properties, information.getAlias());
context = createInitialContext(jndiEvn);
if (context == null) {
validateInitialContext(initialContext);
context = initialContext;
if (log.isDebugEnabled()) {
log.debug("Cannot create a name context with provided jndi properties : " +
jndiEvn);
log.debug("Using system-wide JNDI properties : " + jndiProperties);
}
jndiEvn = jndiProperties;
} else {
perDataSourceICMap.put(dataSourceName, context);
}
}
String dsType = information.getType();
String driver = information.getDriver();
String url = information.getUrl();
String user = information.getSecretInformation().getUser();
String password = information.getSecretInformation().getResolvedSecret();
String maxActive = String.valueOf(information.getMaxActive());
String maxIdle = String.valueOf(information.getMaxIdle());
String maxWait = String.valueOf(information.getMaxWait());
//populates context tree
populateContextTree(context, dataSourceName);
if (DataSourceInformation.BASIC_DATA_SOURCE.equals(dsType)) {
Reference ref = new Reference("javax.sql.DataSource",
"org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER_CLS_NAME,
driver));
ref.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
ref.add(new StringRefAddr(SecurityConstants.PROP_USER_NAME, user));
ref.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD, password));
ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_ACTIVE, maxActive));
ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_IDLE, maxIdle));
ref.add(new StringRefAddr(DataSourceConstants.PROP_MAX_WAIT, maxWait));
// set BasicDataSource specific parameters
setBasicDataSourceParameters(ref, information);
//set default jndiProperties for reference
setCommonParameters(ref, information);
try {
if (log.isDebugEnabled()) {
log.debug("Registering a DataSource with name : " +
dataSourceName + " in the JNDI tree with jndiProperties : " + jndiEvn);
}
context.rebind(dataSourceName, ref);
} catch (NamingException e) {
String msg = " Error binding name ' " + dataSourceName + " ' to " +
"the DataSource(BasicDataSource) reference";
throw new SynapseCommonsException(msg, e, log);
}
} else if (DataSourceInformation.PER_USER_POOL_DATA_SOURCE.equals(dsType)) {
// Construct DriverAdapterCPDS reference
String className = (String) information.getParameter(
DataSourceConstants.PROP_CPDS_ADAPTER +
DataSourceConstants.DOT_STRING +
DataSourceConstants.PROP_CPDS_CLASS_NAME);
String factory = (String) information.getParameter(
DataSourceConstants.PROP_CPDS_ADAPTER +
DataSourceConstants.DOT_STRING +
DataSourceConstants.PROP_CPDS_FACTORY);
String name = (String) information.getParameter(
DataSourceConstants.PROP_CPDS_ADAPTER +
DataSourceConstants.DOT_STRING +
DataSourceConstants.PROP_CPDS_NAME);
Reference cpdsRef =
new Reference(className, factory, null);
cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_DRIVER, driver));
cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_URL, url));
cpdsRef.add(new StringRefAddr(DataSourceConstants.PROP_USER, user));
cpdsRef.add(new StringRefAddr(SecurityConstants.PROP_PASSWORD,
password));
try {
context.rebind(name, cpdsRef);
} catch (NamingException e) {
String msg = "Error binding name '" + name + "' to " +
"the DriverAdapterCPDS reference";
throw new SynapseCommonsException(msg, e, log);
}
// Construct PerUserPoolDataSource reference
Reference ref =
new Reference("org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
"org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory",
null);
ref.add(new BinaryRefAddr(
DataSourceConstants.PROP_JNDI_ENV,
MiscellaneousUtil.serialize(jndiEvn)));
ref.add(new StringRefAddr(
DataSourceConstants.PROP_DATA_SOURCE_NAME, name));
ref.add(new StringRefAddr(
DataSourceConstants.PROP_DEFAULT_MAX_ACTIVE, maxActive));
ref.add(new StringRefAddr(
DataSourceConstants.PROP_DEFAULT_MAX_IDLE, maxIdle));
ref.add(new StringRefAddr(
DataSourceConstants.PROP_DEFAULT_MAX_WAIT, maxWait));
//set default jndiProperties for reference
setCommonParameters(ref, information);
try {
if (log.isDebugEnabled()) {
log.debug("Registering a DataSource with name : " +
dataSourceName + " in the JNDI tree with jndiProperties : " + jndiEvn);
}
context.rebind(dataSourceName, ref);
} catch (NamingException e) {
String msg = "Error binding name ' " + dataSourceName + " ' to " +
"the PerUserPoolDataSource reference";
throw new SynapseCommonsException(msg, e, log);
}
} else {
throw new SynapseCommonsException("Unsupported data source type : " + dsType, log);
}
cachedNameList.add(dataSourceName);
}