use of org.eclipse.persistence.sessions.JNDIConnector in project eclipselink by eclipse-ee4j.
the class EntityManagerImpl method createConnectionPolicy.
/**
* Create connection policy using properties.
* Default connection policy created if no connection properties specified.
*/
protected static ConnectionPolicy createConnectionPolicy(ServerSession serverSession, Map properties) {
ConnectionPolicy policy = serverSession.getDefaultConnectionPolicy();
if (properties == null || properties.isEmpty()) {
return policy;
}
// 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();
}
}
// 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;
}
// 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;
}
// 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;
// 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 it doesn't.
if (newPolicy.getLogin() == null || newPolicy.getLogin() == policy.getLogin()) {
newLogin = 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 {
newLogin.getProperties().remove("user");
}
}
if (isNewPasswordRequired != null) {
if (isNewPasswordRequired) {
newLogin.setProperty("password", password);
} else {
newLogin.getProperties().remove("password");
}
}
if (isNewDefaultConnectorRequired) {
newLogin.setConnector(new DefaultConnector());
newLogin.setUsesExternalConnectionPooling(false);
} else if (isNewJNDIConnectorRequired) {
newLogin.setConnector(new JNDIConnector());
newLogin.setUsesExternalConnectionPooling(true);
}
if (isDefaultConnectorRequired) {
DefaultConnector defaultConnector = (DefaultConnector) newLogin.getConnector();
if (isNewDriverRequired != null) {
if (isNewDriverRequired) {
defaultConnector.setDriverClassName(driver);
} else {
defaultConnector.setDriverClassName(null);
}
}
if (isNewConnectionStringRequired != null) {
if (isNewConnectionStringRequired) {
defaultConnector.setDatabaseURL(connectionString);
} else {
defaultConnector.setDatabaseURL(null);
}
}
} else if (isNewDataSourceRequired != null) {
JNDIConnector jndiConnector = (JNDIConnector) newLogin.getConnector();
if (isNewDataSourceRequired) {
if (dataSource != null) {
jndiConnector.setDataSource(dataSource);
} else {
// dataSourceName != null
jndiConnector.setDataSource(null);
jndiConnector.setName(dataSourceName);
}
}
}
}
}
if (newPolicy != null) {
return newPolicy;
} else {
return policy;
}
}
use of org.eclipse.persistence.sessions.JNDIConnector in project eclipselink by eclipse-ee4j.
the class JUnitTestCase method usingTheSameDatabase.
/**
* Indicates whether two sessions are connected to the same db
*/
public static boolean usingTheSameDatabase(AbstractSession session1, AbstractSession session2) {
Connector conn1 = session1.getLogin().getConnector();
Connector conn2 = session2.getLogin().getConnector();
if (conn1 instanceof DefaultConnector && conn2 instanceof DefaultConnector) {
return ((DefaultConnector) conn1).getDatabaseURL().equals(((DefaultConnector) conn2).getDatabaseURL());
} else if (conn1 instanceof JNDIConnector && conn2 instanceof JNDIConnector) {
String name1 = ((JNDIConnector) conn1).getName();
String name2 = ((JNDIConnector) conn2).getName();
if (name1 != null && name1.equals(name2)) {
return true;
}
return ((JNDIConnector) conn1).getDataSource().equals(((JNDIConnector) conn2).getDataSource());
}
return false;
}
use of org.eclipse.persistence.sessions.JNDIConnector in project helidon by oracle.
the class CDISEPlatform method initializeExternalTransactionController.
/**
* Overrides the {@link
* ServerPlatformBase#initializeExternalTransactionController()}
* method to {@linkplain #disableJTA() disable JTA} if there is no
* {@link TransactionManager} bean present in CDI before invoking
* the {@linkplain
* ServerPlatformBase#initializeExternalTransactionController()
* superclass implementation}.
*
* <p>This method also acquires a {@link DataSource} from
* {@linkplain CDI CDI} proactively, and {@linkplain
* JNDIConnector#setDataSource(DataSource) installs it} to
* preëmpt any JNDI operations.</p>
*
* @exception ValidationException if a {@link DataSource} could
* not be acquired
*
* @see ServerPlatformBase#initializeExternalTransactionController()
*
* @see Session#getDatasourceLogin()
*
* @see DatasourceLogin#getConnector()
*
* @see JNDIConnector
*
* @see JNDIConnector#getName()
*
* @see JNDIConnector#setDataSource(DataSource)
*/
@Override
public void initializeExternalTransactionController() {
final CDI<Object> cdi = CDI.current();
assert cdi != null;
if (cdi.select(TransactionManager.class).isUnsatisfied()) {
this.disableJTA();
}
super.initializeExternalTransactionController();
// See https://github.com/oracle/helidon/issues/949. This is
// the only spot where we can actually change the Connector
// that is used by EclipseLink to look up a data source during
// JPA "SE mode" persistence unit acquisition such that it
// doesn't get overwritten by other EclipseLink internals.
final Session session = this.getDatabaseSession();
if (session != null) {
final Object login = session.getDatasourceLogin();
if (login instanceof DatasourceLogin) {
final Object connector = ((DatasourceLogin) login).getConnector();
if (connector instanceof JNDIConnector) {
final JNDIConnector jndiConnector = (JNDIConnector) connector;
final String dataSourceName = jndiConnector.getName();
if (dataSourceName != null) {
try {
jndiConnector.setDataSource(cdi.select(DataSource.class, NamedLiteral.of(dataSourceName)).get());
} catch (final InjectionException injectionExceptionOfAnyKind) {
throw ValidationException.cannotAcquireDataSource(dataSourceName, injectionExceptionOfAnyKind);
}
}
}
}
}
}
Aggregations