use of org.glassfish.jdbc.config.JdbcResource 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 org.glassfish.jdbc.config.JdbcResource in project Payara by payara.
the class JdbcRecoveryResourceHandler method loadAllJdbcResources.
private void loadAllJdbcResources() {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "loadAllJdbcResources start");
}
try {
Collection<JdbcResource> jdbcResources = getAllJdbcResources();
InitialContext ic = new InitialContext();
for (Resource resource : jdbcResources) {
JdbcResource jdbcResource = (JdbcResource) resource;
if (getResourcesUtil().isEnabled(jdbcResource)) {
try {
ic.lookup(jdbcResource.getJndiName());
} catch (Exception ex) {
_logger.log(Level.SEVERE, "error.loading.jdbc.resources.during.recovery", jdbcResource.getJndiName());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, ex.toString(), ex);
}
}
}
}
} catch (NamingException ne) {
_logger.log(Level.SEVERE, "error.loading.jdbc.resources.during.recovery", ne.getMessage());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, ne.toString(), ne);
}
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "loadAllJdbcResources end");
}
}
use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.
the class JdbcRuntimeExtension method isConnectionPoolReferredInServerInstance.
/**
* Determines if a JDBC connection pool is referred in a
* server-instance via resource-refs
* @param poolInfo pool-name
* @return boolean true if pool is referred in this server instance as well enabled, false
* otherwise
*/
@Override
public boolean isConnectionPoolReferredInServerInstance(PoolInfo poolInfo) {
Collection<JdbcResource> jdbcResources = ConnectorRuntime.getRuntime().getResources(poolInfo).getResources(JdbcResource.class);
for (JdbcResource resource : jdbcResources) {
ResourceInfo resourceInfo = ConnectorsUtil.getResourceInfo(resource);
// Have to check isReferenced here!
if ((resource.getPoolName().equalsIgnoreCase(poolInfo.getName())) && ResourcesUtil.createInstance().isReferenced(resourceInfo) && ResourcesUtil.createInstance().isEnabled(resource)) {
if (logger.isLoggable(Level.FINE)) {
logger.fine("pool " + poolInfo + "resource " + resourceInfo + " referred is referenced by this server");
logger.fine("JDBC resource " + resource.getJndiName() + "refers " + poolInfo + "in this server instance and is enabled");
}
return true;
}
}
if (logger.isLoggable(Level.FINE)) {
logger.fine("No JDBC resource refers [ " + poolInfo + " ] in this server instance");
}
return false;
}
use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.
the class JdbcRuntimeExtension method getAllSystemRAResourcesAndPools.
@Override
public Collection<Resource> getAllSystemRAResourcesAndPools() {
List<Resource> resources = new ArrayList<Resource>();
Domain domain = domainProvider.get();
if (domain != null) {
Resources allResources = domain.getResources();
for (Resource resource : allResources.getResources()) {
if (resource instanceof JdbcConnectionPool) {
resources.add(resource);
} else if (resource instanceof JdbcResource) {
resources.add(resource);
}
}
}
System.out.println("JdbcRuntimeExtension, getAllSystemRAResourcesAndPools = " + resources);
return resources;
}
use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.
the class ReferenceConstrainTest method doChangeToInValidPool.
@Test
public void doChangeToInValidPool() throws TransactionFailure {
Domain domain = habitat.getService(Domain.class);
// Find JdbcResource to chenge its values
Iterator<JdbcResource> iterator = domain.getResources().getResources(JdbcResource.class).iterator();
JdbcResource jdbc = null;
while (iterator.hasNext()) {
JdbcResource res = iterator.next();
if ("DerbyPool".equals(res.getPoolName())) {
jdbc = res;
break;
}
}
assertNotNull(jdbc);
ConfigBean poolConfig = (ConfigBean) ConfigBean.unwrap(jdbc);
Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("pool-name", "WrongPointer");
changes.put(poolConfig, configChanges);
try {
ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
cs.apply(changes);
fail("Can not reach this point");
} catch (TransactionFailure tf) {
ConstraintViolationException cv = findConstrViolation(tf);
// cv.printStackTrace(System.out);
assertNotNull(cv);
}
}
Aggregations