use of javax.resource.spi.security.PasswordCredential in project Payara by payara.
the class JdbcRecoveryResourceHandler method loadXAResourcesAndItsConnections.
/**
* {@inheritDoc}
*/
@Override
public void loadXAResourcesAndItsConnections(List xaresList, List connList) {
// Done so as to initialize connectors-runtime before loading jdbc-resources. need a better way ?
ConnectorRuntime crt = connectorRuntimeProvider.get();
Collection<JdbcResource> jdbcResources = getAllJdbcResources();
if (jdbcResources == null || jdbcResources.size() == 0) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("loadXAResourcesAndItsConnections : no resources");
}
return;
}
List<JdbcConnectionPool> jdbcPools = new ArrayList<JdbcConnectionPool>();
for (Resource resource : jdbcResources) {
JdbcResource jdbcResource = (JdbcResource) resource;
if (getResourcesUtil().isEnabled(jdbcResource)) {
ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(jdbcResource);
JdbcConnectionPool pool = JdbcResourcesUtil.createInstance().getJdbcConnectionPoolOfResource(resourceInfo);
if (pool != null && "javax.sql.XADataSource".equals(pool.getResType())) {
jdbcPools.add(pool);
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("JdbcRecoveryResourceHandler:: loadXAResourcesAndItsConnections :: " + "adding : " + (jdbcResource.getPoolName()));
}
}
}
loadAllJdbcResources();
// Read from the transaction-service , if the replacement of
// Vendor XAResource class with our version required.
// If yes, put the mapping in the xaresourcewrappers properties.
Properties XAResourceWrappers = new Properties();
XAResourceWrappers.put("oracle.jdbc.xa.client.OracleXADataSource", "com.sun.enterprise.transaction.jts.recovery.OracleXAResource");
Config c = habitat.getService(Config.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
txService = c.getExtensionByType(TransactionService.class);
List<Property> properties = txService.getProperty();
if (properties != null) {
for (Property property : properties) {
String name = property.getName();
String value = property.getValue();
if (name.equals("oracle-xa-recovery-workaround")) {
if ("false".equals(value)) {
XAResourceWrappers.remove("oracle.jdbc.xa.client.OracleXADataSource");
}
} else if (name.equals("sybase-xa-recovery-workaround")) {
if (value.equals("true")) {
XAResourceWrappers.put("com.sybase.jdbc2.jdbc.SybXADataSource", "com.sun.enterprise.transaction.jts.recovery.SybaseXAResource");
}
}
}
}
for (JdbcConnectionPool jdbcConnectionPool : jdbcPools) {
if (jdbcConnectionPool.getResType() == null || jdbcConnectionPool.getName() == null || !jdbcConnectionPool.getResType().equals("javax.sql.XADataSource")) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("skipping pool : " + jdbcConnectionPool.getName());
}
continue;
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest(" using pool : " + jdbcConnectionPool.getName());
}
PoolInfo poolInfo = ConnectorsUtil.getPoolInfo(jdbcConnectionPool);
try {
String[] dbUserPassword = getdbUserPasswordOfJdbcConnectionPool(jdbcConnectionPool);
String dbUser = dbUserPassword[0];
String dbPassword = dbUserPassword[1];
if (dbPassword == null) {
dbPassword = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nullpassword_error", poolInfo);
}
}
if (dbUser == null) {
dbUser = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nulluser_error", poolInfo);
}
}
ManagedConnectionFactory fac = crt.obtainManagedConnectionFactory(poolInfo);
Subject subject = new Subject();
PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
pc.setManagedConnectionFactory(fac);
Principal prin = new ResourcePrincipal(dbUser, dbPassword);
subject.getPrincipals().add(prin);
subject.getPrivateCredentials().add(pc);
ManagedConnection mc = fac.createManagedConnection(subject, null);
connList.add(mc);
try {
XAResource xares = mc.getXAResource();
if (xares != null) {
// See if a wrapper class for the vendor XADataSource is
// specified if yes, replace the XAResouce class of database
// vendor with our own version
String datasourceClassname = jdbcConnectionPool.getDatasourceClassname();
String wrapperclass = (String) XAResourceWrappers.get(datasourceClassname);
if (wrapperclass != null) {
// need to load wrapper class provided by "transactions" module.
// Using connector-class-loader so as to get access to "transaction" module.
XAResourceWrapper xaresWrapper = null;
xaresWrapper = (XAResourceWrapper) crt.getConnectorClassLoader().loadClass(wrapperclass).newInstance();
xaresWrapper.init(mc, subject);
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("adding resource " + poolInfo + " -- " + xaresWrapper);
}
xaresList.add(xaresWrapper);
} else {
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("adding resource " + poolInfo + " -- " + xares);
}
xaresList.add(xares);
}
}
} catch (ResourceException ex) {
_logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
}
// ignored. Not at XA_TRANSACTION level
}
} catch (Exception ex) {
_logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
}
}
}
}
use of javax.resource.spi.security.PasswordCredential in project Payara by payara.
the class ConnectorsRecoveryResourceHandler method loadXAResourcesAndItsConnections.
/**
* {@inheritDoc}
*/
public void loadXAResourcesAndItsConnections(List xaresList, List connList) {
// Done so as to initialize connectors-runtime before loading connector-resources. need a better way ?
ConnectorRuntime crt = connectorRuntimeProvider.get();
// ApplicationLoaderService already guarantees that connectors are loaded before any other applications.
// Recovery will not start sooner than transaction is first needed on EE application startup, therefore
// it is safe to continue here without waiting for startupProvider, as that ultimately creates a deadlock
// between ApplicationLoaderService needed a transaction manager, and recovery wainting for applications
// to finish loading.
Collection<ConnectorResource> connectorResources = getAllConnectorResources();
if (connectorResources == null || connectorResources.size() == 0) {
return;
}
List<ConnectorConnectionPool> connPools = new ArrayList<ConnectorConnectionPool>();
for (Resource resource : connectorResources) {
ConnectorResource connResource = (ConnectorResource) resource;
if (getResourcesUtil().isEnabled(connResource)) {
ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(connResource);
ConnectorConnectionPool pool = ResourcesUtil.createInstance().getConnectorConnectionPoolOfResource(resourceInfo);
if (pool != null && ConnectorConstants.XA_TRANSACTION_TX_SUPPORT_STRING.equals(getTransactionSupport(pool))) {
connPools.add(pool);
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("ConnectorsRecoveryResourceHandler loadXAResourcesAndItsConnections :: " + "adding : " + connResource.getPoolName());
}
}
}
}
loadAllConnectorResources();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Recovering pools : " + connPools.size());
}
for (ConnectorConnectionPool connPool : connPools) {
PoolInfo poolInfo = ConnectorsUtil.getPoolInfo(connPool);
try {
String[] dbUserPassword = getdbUserPasswordOfConnectorConnectionPool(connPool);
if (dbUserPassword == null) {
continue;
}
String dbUser = dbUserPassword[0];
String dbPassword = dbUserPassword[1];
Subject subject = new Subject();
// username [pointbase interprets this as "root"]/password.
if (dbPassword == null) {
dbPassword = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nullpassword_error", poolInfo);
}
}
if (dbUser == null) {
dbUser = "";
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "datasource.xadatasource_nulluser_error", poolInfo);
}
}
String rarName = connPool.getResourceAdapterName();
// TODO V3 JMS-RA ??
if (ConnectorAdminServiceUtils.isJMSRA(rarName)) {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Performing recovery for JMS RA, poolName " + poolInfo);
}
ManagedConnectionFactory[] mcfs = crt.obtainManagedConnectionFactories(poolInfo);
_logger.log(Level.INFO, "JMS resource recovery has created CFs = " + mcfs.length);
for (int i = 0; i < mcfs.length; i++) {
PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
pc.setManagedConnectionFactory(mcfs[i]);
Principal prin = new ResourcePrincipal(dbUser, dbPassword);
subject.getPrincipals().add(prin);
subject.getPrivateCredentials().add(pc);
ManagedConnection mc = mcfs[i].createManagedConnection(subject, null);
connList.add(mc);
try {
XAResource xares = mc.getXAResource();
if (xares != null) {
xaresList.add(xares);
}
} catch (ResourceException ex) {
// ignored. Not at XA_TRANSACTION level
}
}
} else {
ManagedConnectionFactory mcf = crt.obtainManagedConnectionFactory(poolInfo);
PasswordCredential pc = new PasswordCredential(dbUser, dbPassword.toCharArray());
pc.setManagedConnectionFactory(mcf);
Principal prin = new ResourcePrincipal(dbUser, dbPassword);
subject.getPrincipals().add(prin);
subject.getPrivateCredentials().add(pc);
ManagedConnection mc = mcf.createManagedConnection(subject, null);
connList.add(mc);
try {
XAResource xares = mc.getXAResource();
if (xares != null) {
xaresList.add(xares);
}
} catch (ResourceException ex) {
// ignored. Not at XA_TRANSACTION level
}
}
} catch (Exception ex) {
_logger.log(Level.WARNING, "datasource.xadatasource_error", poolInfo);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "datasource.xadatasource_error_excp", ex);
}
}
}
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Total XAResources identified for recovery is " + xaresList.size());
_logger.log(Level.FINE, "Total connections identified for recovery is " + connList.size());
}
}
use of javax.resource.spi.security.PasswordCredential in project Payara by payara.
the class DMManagedConnectionFactory method createManagedConnection.
/**
* Creates a new physical connection to the underlying EIS resource
* manager.
*
* @param subject <code>Subject</code> instance passed by the application server
* @param cxRequestInfo <code>ConnectionRequestInfo</code> which may be created
* as a result of the invocation <code>getConnection(user, password)</code>
* on the <code>DataSource</code> object
* @return <code>ManagedConnection</code> object created
* @throws ResourceException if there is an error in instantiating the
* <code>DataSource</code> object used for the
* creation of the <code>ManagedConnection</code> object
* @throws SecurityException if there ino <code>PasswordCredential</code> object
* satisfying this request
*/
@Override
public javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
logFine("In createManagedConnection");
if (dsObjBuilder == null) {
dsObjBuilder = new DataSourceObjectBuilder(spec);
}
PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
try {
Class.forName(spec.getDetail(DataSourceSpec.CLASSNAME));
} catch (ClassNotFoundException cnfe) {
_logger.log(Level.SEVERE, "jdbc.exc_cnfe", cnfe);
throw new ResourceException("The driver could not be loaded: " + spec.getDetail(DataSourceSpec.CLASSNAME));
}
java.sql.Connection dsConn = null;
ManagedConnectionImpl mc = null;
Properties driverProps = new Properties();
// Will return a set of properties that would have setURL and <url> as objects
// Get a set of normal case properties
Hashtable properties = dsObjBuilder.parseDriverProperties(spec, false);
Set<Map.Entry<String, Vector>> entries = properties.entrySet();
for (Map.Entry<String, Vector> entry : entries) {
String value = "";
String key = entry.getKey();
Vector values = entry.getValue();
if (!values.isEmpty() && values.size() == 1) {
value = (String) values.firstElement();
} else if (values.size() > 1) {
logFine("More than one value for key : " + key);
}
String prop = getParsedKey(key);
driverProps.put(prop, value);
if (prop.equalsIgnoreCase("URL")) {
if (spec.getDetail(DataSourceSpec.URL) == null) {
setConnectionURL(value);
}
}
}
try {
if (cxRequestInfo != null) {
driverProps.setProperty("user", pc.getUserName());
driverProps.setProperty("password", new String(pc.getPassword()));
} else {
String user = spec.getDetail(DataSourceSpec.USERNAME);
String password = spec.getDetail(DataSourceSpec.PASSWORD);
if (user != null) {
driverProps.setProperty("user", user);
}
if (password != null) {
driverProps.setProperty("password", password);
}
}
dsConn = DriverManager.getConnection(spec.getDetail(DataSourceSpec.URL), driverProps);
} catch (java.sql.SQLException sqle) {
_logger.log(Level.SEVERE, "jdbc.exc_create_mc", sqle);
throw new javax.resource.spi.ResourceAllocationException("The connection could not be allocated: " + sqle.getMessage());
}
try {
mc = constructManagedConnection(null, dsConn, pc, this);
// GJCINT
validateAndSetIsolation(mc);
} finally {
if (mc == null) {
if (dsConn != null) {
try {
dsConn.close();
} catch (SQLException e) {
_logger.log(Level.FINEST, "Exception while closing connection : createManagedConnection" + dsConn);
}
}
}
}
return mc;
}
use of javax.resource.spi.security.PasswordCredential in project Payara by payara.
the class ManagedConnectionFactoryImpl method matchManagedConnections.
/**
* Returns a matched <code>ManagedConnection</code> from the candidate
* set of <code>ManagedConnection</code> objects.
*
* @param connectionSet <code>Set</code> of <code>ManagedConnection</code>
* objects passed by the application server
* @param subject passed by the application server
* for retrieving information required for matching
* @param cxRequestInfo <code>ConnectionRequestInfo</code> passed by the application server
* for retrieving information required for matching
* @return <code>ManagedConnection</code> that is the best match satisfying this request
* @throws ResourceException if there is an error accessing the <code>Subject</code>
* parameter or the <code>Set</code> of <code>ManagedConnection</code>
* objects passed by the application server
*/
@Override
public javax.resource.spi.ManagedConnection matchManagedConnections(java.util.Set connectionSet, javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
logFine("In matchManagedConnections");
if (connectionSet == null) {
return null;
}
PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
java.util.Iterator iter = connectionSet.iterator();
ManagedConnectionImpl mc = null;
while (iter.hasNext()) {
try {
mc = (ManagedConnectionImpl) iter.next();
} catch (java.util.NoSuchElementException nsee) {
_logger.log(Level.SEVERE, "jdbc.exc_iter");
throw new ResourceException(nsee.getMessage());
}
if (pc == null && this.equals(mc.getManagedConnectionFactory())) {
return mc;
} else if (SecurityUtils.isPasswordCredentialEqual(pc, mc.getPasswordCredential())) {
return mc;
}
}
return null;
}
use of javax.resource.spi.security.PasswordCredential in project Payara by payara.
the class CPManagedConnectionFactory method createManagedConnection.
/**
* Creates a new physical connection to the underlying EIS resource
* manager.
*
* @param subject <code>Subject</code> instance passed by the application server
* @param cxRequestInfo <code>ConnectionRequestInfo</code> which may be created
* as a result of the invocation <code>getConnection(user, password)</code>
* on the <code>DataSource</code> object
* @return <code>ManagedConnection</code> object created
* @throws ResourceException if there is an error in instantiating the
* <code>DataSource</code> object used for the
* creation of the <code>ManagedConnection</code> object
* @throws SecurityException if there ino <code>PasswordCredential</code> object
* satisfying this request
* @throws ResourceAllocationException if there is an error in allocating the
* physical connection
*/
@Override
public javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException {
logFine("In createManagedConnection");
PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo);
javax.sql.ConnectionPoolDataSource dataSource = getDataSource();
javax.sql.PooledConnection cpConn = null;
ManagedConnectionImpl mc = null;
try {
/* For the case where the user/passwd of the connection pool is
* equal to the PasswordCredential for the connection request
* get a connection from this pool directly.
* for all other conditions go create a new connection
*/
String user = getUser();
if (user == null || isEqual(pc, user, getPassword())) {
cpConn = dataSource.getPooledConnection();
} else {
cpConn = dataSource.getPooledConnection(pc.getUserName(), new String(pc.getPassword()));
}
} catch (java.sql.SQLException sqle) {
// _logger.log(Level.SEVERE, "jdbc.exc_create_ds_conn",sqle);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbc.exc_create_ds_conn", sqle);
}
StringManager sm = StringManager.getManager(DataSourceObjectBuilder.class);
String msg = sm.getString("jdbc.cannot_allocate_connection", sqle.getMessage());
ResourceAllocationException rae = new ResourceAllocationException(msg, sqle);
throw rae;
}
try {
mc = constructManagedConnection(cpConn, null, pc, this);
mc.initializeConnectionType(ManagedConnectionImpl.ISPOOLEDCONNECTION);
// GJCINT
validateAndSetIsolation(mc);
} finally {
if (mc == null) {
if (cpConn != null) {
try {
cpConn.close();
} catch (SQLException e) {
_logger.log(Level.FINEST, "Exception while closing connection : createManagedConnection" + cpConn);
}
}
}
}
return mc;
}
Aggregations