/**
* Create connection policy using properties.
* Default connection policy created if no connection properties specified.
*/
protected void createConnectionPolicy() {
ConnectionPolicy policy = serverSession.getDefaultConnectionPolicy();
if (properties == null || properties.isEmpty()) {
this.connectionPolicy = policy;
return;
}
// Search only the properties map - serverSession's properties have been
// already processed.
ConnectionPolicy policyFromProperties = (ConnectionPolicy) properties.get(EntityManagerProperties.CONNECTION_POLICY);
if (policyFromProperties != null) {
policy = policyFromProperties;
}
// Note that serverSession passed into the methods below only because it
// carries the SessionLog into which the debug info should be written.
// The property is search for in the passed properties map only (not in
// serverSession, not in System.properties).
ConnectionPolicy newPolicy = null;
String isLazyString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerProperties.EXCLUSIVE_CONNECTION_IS_LAZY, properties, serverSession, false);
if (isLazyString != null) {
boolean isLazy = Boolean.parseBoolean(isLazyString);
if (policy.isLazy() != isLazy) {
if (newPolicy == null) {
newPolicy = (ConnectionPolicy) policy.clone();
}
newPolicy.setIsLazy(isLazy);
}
}
ConnectionPolicy.ExclusiveMode exclusiveMode = EntityManagerSetupImpl.getConnectionPolicyExclusiveModeFromProperties(properties, serverSession, false);
if (exclusiveMode != null) {
if (!exclusiveMode.equals(policy.getExclusiveMode())) {
if (newPolicy == null) {
newPolicy = (ConnectionPolicy) policy.clone();
}
newPolicy.setExclusiveMode(exclusiveMode);
}
}
String user = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerProperties.JDBC_USER, properties, serverSession, false);
String password = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerProperties.JDBC_PASSWORD, properties, serverSession, false);
String driver = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerProperties.JDBC_DRIVER, properties, serverSession, false);
String connectionString = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug(EntityManagerProperties.JDBC_URL, properties, serverSession, false);
// find the jta datasource
Object jtaDataSourceObj = EntityManagerFactoryProvider.getConfigPropertyLogDebug(EntityManagerProperties.JTA_DATASOURCE, properties, serverSession, false);
DataSource jtaDataSource = null;
String jtaDataSourceName = null;
if (jtaDataSourceObj != null) {
if (jtaDataSourceObj instanceof DataSource) {
jtaDataSource = (DataSource) jtaDataSourceObj;
} else if (jtaDataSourceObj instanceof String) {
jtaDataSourceName = (String) jtaDataSourceObj;
}
}
// find the non jta datasource
Object nonjtaDataSourceObj = EntityManagerFactoryProvider.getConfigPropertyLogDebug(EntityManagerProperties.NON_JTA_DATASOURCE, properties, serverSession, false);
DataSource nonjtaDataSource = null;
String nonjtaDataSourceName = null;
if (nonjtaDataSourceObj != null) {
if (nonjtaDataSourceObj instanceof DataSource) {
nonjtaDataSource = (DataSource) nonjtaDataSourceObj;
} else if (nonjtaDataSourceObj instanceof String) {
nonjtaDataSourceName = (String) nonjtaDataSourceObj;
}
}
if (user != null || password != null || driver != null || connectionString != null || jtaDataSourceObj != null || nonjtaDataSourceObj != null) {
// Validation: Can't specify jdbcDriver, connectionString with a
// DataSource
boolean isDefaultConnectorRequired = isPropertyToBeAdded(driver) || isPropertyToBeAdded(connectionString);
boolean isJNDIConnectorRequired = isPropertyToBeAdded(jtaDataSource, jtaDataSourceName) || isPropertyToBeAdded(nonjtaDataSource, nonjtaDataSourceName);
if (isDefaultConnectorRequired && isJNDIConnectorRequired) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("entity_manager_properties_conflict_default_connector_vs_jndi_connector", new Object[] {}));
}
DatasourceLogin login = (DatasourceLogin) policy.getLogin();
if (login == null) {
if (policy.getPoolName() != null) {
login = (DatasourceLogin) serverSession.getConnectionPool(policy.getPoolName()).getLogin();
} else {
login = (DatasourceLogin) serverSession.getDatasourceLogin();
}
}
// Validation: Can't specify jdbcDriver, connectionString if
// externalTransactionController is used - this requires
// externalConnectionPooling
if (login.shouldUseExternalTransactionController() && isDefaultConnectorRequired) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("entity_manager_properties_conflict_default_connector_vs_external_transaction_controller", new Object[] {}));
}
javax.sql.DataSource dataSource = null;
String dataSourceName = null;
if (isJNDIConnectorRequired) {
if (login.shouldUseExternalTransactionController()) {
if (isPropertyToBeAdded(jtaDataSource, jtaDataSourceName)) {
dataSource = jtaDataSource;
dataSourceName = jtaDataSourceName;
}
// validation: Can't change externalTransactionController
// state - will ignore data source that doesn't match the
// flag.
if (isPropertyToBeAdded(nonjtaDataSource, nonjtaDataSourceName)) {
serverSession.log(SessionLog.WARNING, SessionLog.PROPERTIES, "entity_manager_ignores_nonjta_data_source");
}
} else {
if (isPropertyToBeAdded(nonjtaDataSource, nonjtaDataSourceName)) {
dataSource = nonjtaDataSource;
dataSourceName = nonjtaDataSourceName;
}
// validation: Can't change externalTransactionController
// state - will ignore data source that doesn't match the
// flag.
if (isPropertyToBeAdded(jtaDataSource, jtaDataSourceName)) {
serverSession.log(SessionLog.WARNING, SessionLog.PROPERTIES, "entity_manager_ignores_jta_data_source");
}
}
}
// isNew...Required == null means no change required; TRUE -
// newValue substitute oldValue by newValue; FALSE - remove
// oldValue.
Boolean isNewUserRequired = isPropertyValueToBeUpdated(login.getUserName(), user);
// if isNewUserRequired==null then isNewPasswordRequired==null, too:
// don't create a new ConnectionPolicy if the same user/password passed to both createEMF and createEM
Boolean isNewPasswordRequired = null;
// if user name should be removed from properties then password
// should be removed, too.
if (isNewUserRequired != null) {
if(isNewUserRequired) {
if(password != null) {
// can't compare the passed (un-encrypted) password with the existing encrypted one, therefore
// use the new password if it's not an empty string.
isNewPasswordRequired = password.length() > 0;
}
} else {
// user should be removed -> remove password as well
isNewPasswordRequired = Boolean.FALSE;
}
}
DefaultConnector oldDefaultConnector = null;
if (login.getConnector() instanceof DefaultConnector) {
oldDefaultConnector = (DefaultConnector) login.getConnector();
}
boolean isNewDefaultConnectorRequired = oldDefaultConnector == null && isDefaultConnectorRequired;
JNDIConnector oldJNDIConnector = null;
if (login.getConnector() instanceof JNDIConnector) {
oldJNDIConnector = (JNDIConnector) login.getConnector();
}
boolean isNewJNDIConnectorRequired = oldJNDIConnector == null && isJNDIConnectorRequired;
Boolean isNewDriverRequired = null;
Boolean isNewConnectionStringRequired = null;
if (isNewDefaultConnectorRequired) {
isNewDriverRequired = isPropertyValueToBeUpdated(null, driver);
isNewConnectionStringRequired = isPropertyValueToBeUpdated(null, connectionString);
} else {
if (oldDefaultConnector != null) {
isNewDriverRequired = isPropertyValueToBeUpdated(oldDefaultConnector.getDriverClassName(), driver);
isNewConnectionStringRequired = isPropertyValueToBeUpdated(oldDefaultConnector.getConnectionString(), connectionString);
}
}
Boolean isNewDataSourceRequired = null;
if (isNewJNDIConnectorRequired) {
isNewDataSourceRequired = Boolean.TRUE;
} else {
if (oldJNDIConnector != null) {
if (dataSource != null) {
if (!dataSource.equals(oldJNDIConnector.getDataSource())) {
isNewDataSourceRequired = Boolean.TRUE;
}
} else if (dataSourceName != null) {
if (!dataSourceName.equals(oldJNDIConnector.getName())) {
isNewDataSourceRequired = Boolean.TRUE;
}
}
}
}
if (isNewUserRequired != null || isNewPasswordRequired != null || isNewDriverRequired != null || isNewConnectionStringRequired != null || isNewDataSourceRequired != null) {
// a new login required - so a new policy required, too.
if (newPolicy == null) {
newPolicy = (ConnectionPolicy) policy.clone();
}
// the new policy must have a new login - not to override the
// existing one in the original ConnectionPolicy that is likely
// shared.
DatasourceLogin newLogin = (DatasourceLogin) newPolicy.getLogin();
// sometimes ConnectionPolicy.clone clones the login , too -
// sometimes it doesn't.
if (newPolicy.getLogin() == null || newPolicy.getLogin() == policy.getLogin()) {
newLogin = (DatasourceLogin) login.clone();
newPolicy.setLogin(newLogin);
}
// because it uses a new login the connection policy should not
// be pooled.
newPolicy.setPoolName(null);
if (isNewUserRequired != null) {
if (isNewUserRequired) {
newLogin.setProperty("user", user);
} else {