Search in sources :

Example 31 with ResourceException

use of javax.resource.ResourceException 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);
            }
        }
    }
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) PasswordCredential(javax.resource.spi.security.PasswordCredential) Properties(java.util.Properties) XAResourceWrapper(com.sun.enterprise.transaction.api.XAResourceWrapper) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) Property(org.jvnet.hk2.config.types.Property) ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) JdbcConnectionPool(org.glassfish.jdbc.config.JdbcConnectionPool) TransactionService(com.sun.enterprise.transaction.config.TransactionService) XAResource(javax.transaction.xa.XAResource) Resource(com.sun.enterprise.config.serverbeans.Resource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Subject(javax.security.auth.Subject) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) XAResource(javax.transaction.xa.XAResource) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal) ConnectorRuntime(com.sun.appserv.connectors.internal.api.ConnectorRuntime)

Example 32 with ResourceException

use of javax.resource.ResourceException in project Payara by payara.

the class ResourcesDeployer method createAppScopedResources.

private void createAppScopedResources(Application app, List<org.glassfish.resources.api.Resource> resources, DeploymentContext dc, boolean embedded) throws ResourceException {
    try {
        if (resources != null) {
            Application application = dc.getTransientAppMetaData(ServerTags.APPLICATION, Application.class);
            Resources asc = dc.getTransientAppMetaData(APP_META_DATA_RESOURCES, Resources.class);
            if (asc == null) {
                asc = application.createChild(Resources.class);
                application.setResources(asc);
                dc.addTransientAppMetaData(APP_META_DATA_RESOURCES, asc);
                ApplicationInfo appInfo = appRegistry.get(app.getName());
                if (appInfo != null) {
                    appInfo.addTransientAppMetaData(app.getName() + "-resources", asc);
                }
            }
            createConfig(asc, resources, embedded);
            String appName = app.getName();
            preserveResources(asc, appName, appName);
        }
    } catch (Exception e) {
        Object[] params = new Object[] { app.getName(), e };
        _logger.log(Level.SEVERE, "gf.resources.app.scope.deployment.failure", params);
        throw new ResourceException(e);
    }
}
Also used : ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) ResourceException(javax.resource.ResourceException) ResourceException(javax.resource.ResourceException) IOException(java.io.IOException) DeploymentException(org.glassfish.deployment.common.DeploymentException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException)

Example 33 with ResourceException

use of javax.resource.ResourceException in project Payara by payara.

the class CustomResourceManager method createConfigBean.

public Resource createConfigBean(Resources resources, HashMap attributes, Properties properties, boolean validate) throws Exception {
    setAttributes(attributes, null);
    ResourceStatus status = null;
    if (!validate) {
        status = new ResourceStatus(ResourceStatus.SUCCESS, "");
    } else {
        status = isValid(resources, false, null);
    }
    if (status.getStatus() == ResourceStatus.SUCCESS) {
        return createConfigBean(resources, properties);
    } else {
        throw new ResourceException(status.getMessage());
    }
}
Also used : ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) ResourceException(javax.resource.ResourceException)

Example 34 with ResourceException

use of javax.resource.ResourceException in project Payara by payara.

the class ResourcesDeployer method createConfig.

private Collection<Resource> createConfig(Resources resources, Collection<org.glassfish.resources.api.Resource> resourcesToRegister, boolean embedded) throws ResourceException {
    List<Resource> resourceConfigs = new ArrayList<Resource>();
    for (org.glassfish.resources.api.Resource resource : resourcesToRegister) {
        final HashMap attrList = resource.getAttributes();
        final Properties props = resource.getProperties();
        String desc = resource.getDescription();
        if (desc != null) {
            attrList.put("description", desc);
        }
        try {
            final ResourceManager rm = resourceFactory.getResourceManager(resource);
            if (embedded && isEmbeddedResource(resource, resourcesToRegister)) {
                Resource configBeanResource = rm.createConfigBean(resources, attrList, props, false);
                resources.getResources().add(configBeanResource);
                resourceConfigs.add(configBeanResource);
            } else if (!embedded && !isEmbeddedResource(resource, resourcesToRegister)) {
                com.sun.enterprise.config.serverbeans.Resource configBeanResource = rm.createConfigBean(resources, attrList, props, true);
                resources.getResources().add(configBeanResource);
                resourceConfigs.add(configBeanResource);
            }
        } catch (Exception e) {
            throw new ResourceException(e);
        }
    }
    return resourceConfigs;
}
Also used : Resource(com.sun.enterprise.config.serverbeans.Resource) Resource(org.glassfish.resources.api.Resource) ResourceManager(org.glassfish.resources.admin.cli.ResourceManager) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) ResourceException(javax.resource.ResourceException) IOException(java.io.IOException) DeploymentException(org.glassfish.deployment.common.DeploymentException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException) Resource(org.glassfish.resources.api.Resource) ResourceException(javax.resource.ResourceException) org.glassfish.resources.api(org.glassfish.resources.api) org.jvnet.hk2.config(org.jvnet.hk2.config)

Example 35 with ResourceException

use of javax.resource.ResourceException in project Payara by payara.

the class JavaMailResourceManager method createConfigBean.

public Resource createConfigBean(Resources resources, HashMap attributes, Properties properties, boolean validate) throws Exception {
    setAttributes(attributes, null);
    ResourceStatus status = null;
    if (!validate) {
        status = new ResourceStatus(ResourceStatus.SUCCESS, "");
    } else {
        status = isValid(resources, false, null);
    }
    if (status.getStatus() == ResourceStatus.SUCCESS) {
        return createConfigBean(resources, properties);
    } else {
        throw new ResourceException(status.getMessage());
    }
}
Also used : ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) ResourceException(javax.resource.ResourceException)

Aggregations

ResourceException (javax.resource.ResourceException)165 SQLException (java.sql.SQLException)19 IOException (java.io.IOException)18 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)14 ManagedConnection (javax.resource.spi.ManagedConnection)14 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)13 TranslatorException (org.teiid.translator.TranslatorException)13 Connection (javax.resource.cci.Connection)12 Test (org.junit.Test)12 NamingException (javax.naming.NamingException)11 XAResource (javax.transaction.xa.XAResource)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 ConnectionException (com.sforce.ws.ConnectionException)9 MessageEndpoint (javax.resource.spi.endpoint.MessageEndpoint)9 UnexpectedErrorFault (com.sforce.soap.partner.fault.UnexpectedErrorFault)8 JMSException (javax.jms.JMSException)8 LocalTransactionException (javax.resource.spi.LocalTransactionException)8 ResourceHandle (com.sun.enterprise.resource.ResourceHandle)7 ArrayList (java.util.ArrayList)7 NotSupportedException (javax.resource.NotSupportedException)7