Search in sources :

Example 46 with ResourceException

use of javax.resource.ResourceException 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());
    }
}
Also used : ResourceInfo(org.glassfish.resourcebase.resources.api.ResourceInfo) ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) XAResource(javax.transaction.xa.XAResource) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) PasswordCredential(javax.resource.spi.security.PasswordCredential) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Subject(javax.security.auth.Subject) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException) ManagedConnectionFactory(javax.resource.spi.ManagedConnectionFactory) XAResource(javax.transaction.xa.XAResource) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ManagedConnection(javax.resource.spi.ManagedConnection) ResourceException(javax.resource.ResourceException) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal) ConnectorRuntime(com.sun.enterprise.connectors.ConnectorRuntime)

Example 47 with ResourceException

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

the class ConnectionManagerImpl method allocateConnection.

public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo, String jndiNameToUse, Object conn) throws ResourceException {
    validateResourceAndPool();
    PoolManager poolmgr = ConnectorRuntime.getRuntime().getPoolManager();
    boolean resourceShareable = true;
    ResourceReferenceDescriptor ref = poolmgr.getResourceReference(jndiNameToUse, logicalName);
    if (ref != null) {
        String shareableStr = ref.getSharingScope();
        if (shareableStr.equals(ref.RESOURCE_UNSHAREABLE)) {
            resourceShareable = false;
        }
    }
    // TODO V3 refactor all the 3 cases viz, no res-ref, app-auth, cont-auth.
    if (ref == null) {
        if (getLogger().isLoggable(Level.FINE)) {
            getLogger().log(Level.FINE, "poolmgr.no_resource_reference", jndiNameToUse);
        }
        return internalGetConnection(mcf, defaultPrin, cxRequestInfo, resourceShareable, jndiNameToUse, conn, true);
    }
    String auth = ref.getAuthorization();
    if (auth.equals(ResourceReferenceDescriptor.APPLICATION_AUTHORIZATION)) {
        if (cxRequestInfo == null) {
            String msg = getLocalStrings().getString("con_mgr.null_userpass");
            throw new ResourceException(msg);
        }
        ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolInfo);
        return internalGetConnection(mcf, null, cxRequestInfo, resourceShareable, jndiNameToUse, conn, false);
    } else {
        ResourcePrincipal prin = null;
        Set principalSet = null;
        Principal callerPrincipal = null;
        SecurityContext securityContext = null;
        ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
        // TODO V3 is SecurityContext.getCurrent() the right way ? Does it need to be injected ?
        if (connectorRuntime.isServer() && (securityContext = SecurityContext.getCurrent()) != null && (callerPrincipal = securityContext.getCallerPrincipal()) != null && (principalSet = securityContext.getPrincipalSet()) != null) {
            AuthenticationService authService = connectorRuntime.getAuthenticationService(rarName, poolInfo);
            if (authService != null) {
                prin = (ResourcePrincipal) authService.mapPrincipal(callerPrincipal, principalSet);
            }
        }
        if (prin == null) {
            prin = ref.getResourcePrincipal();
            if (prin == null) {
                if (getLogger().isLoggable(Level.FINE)) {
                    getLogger().log(Level.FINE, "default-resource-principal not" + "specified for " + jndiNameToUse + ". Defaulting to" + " user/password specified in the pool");
                }
                prin = defaultPrin;
            } else if (!prin.equals(defaultPrin)) {
                ConnectorRuntime.getRuntime().switchOnMatching(rarName, poolInfo);
            }
        }
        return internalGetConnection(mcf, prin, cxRequestInfo, resourceShareable, jndiNameToUse, conn, false);
    }
}
Also used : Set(java.util.Set) SecurityContext(com.sun.enterprise.security.SecurityContext) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) ResourceException(javax.resource.ResourceException) ResourceReferenceDescriptor(com.sun.enterprise.deployment.ResourceReferenceDescriptor) PoolManager(com.sun.enterprise.resource.pool.PoolManager) AuthenticationService(com.sun.enterprise.connectors.authentication.AuthenticationService) ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) Principal(java.security.Principal)

Example 48 with ResourceException

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

the class ConnectorConnectionPoolAdminServiceImpl method getDefaultSubject.

/**
 * Utility method that is used to get the default subject for the
 * specified mcf and resource principal.
 * @param poolInfo
 * @param mcf
 * @param prin
 * @return
 * @throws javax.resource.ResourceException
 */
protected Subject getDefaultSubject(PoolInfo poolInfo, ManagedConnectionFactory mcf, ResourcePrincipal prin) throws ResourceException {
    ResourcePrincipal resourcePrincipal = null;
    if (prin == null) {
        try {
            resourcePrincipal = getDefaultResourcePrincipal(poolInfo, mcf);
        } catch (NamingException ne) {
            _logger.log(Level.WARNING, "jdbc.pool_not_reachable", ne.getMessage());
            String l10nMsg = localStrings.getString("pingpool.name_not_bound", poolInfo);
            ResourceException e = new ResourceException(l10nMsg + poolInfo);
            e.initCause(ne);
            throw e;
        }
    } else {
        resourcePrincipal = prin;
    }
    final Subject defaultSubject = ConnectionPoolObjectsUtils.createSubject(mcf, resourcePrincipal);
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("using subject: " + defaultSubject);
    }
    return defaultSubject;
}
Also used : ResourcePrincipal(com.sun.enterprise.deployment.ResourcePrincipal) NamingException(javax.naming.NamingException) ResourceException(javax.resource.ResourceException) Subject(javax.security.auth.Subject)

Example 49 with ResourceException

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

the class ConnectorConnectionPoolAdminServiceImpl method getConnection.

/**
 * Get a sql connection from the DataSource specified by the jdbcJndiName.
 * This API is intended to be used in the DAS. The motivation for having this
 * API is to provide the CMP backend a means of acquiring a connection during
 * the codegen phase. If a user is trying to deploy an app on a remote server,
 * without this API, a resource reference has to be present both in the DAS
 * and the server instance. This makes the deployment more complex for the
 * user since a resource needs to be forcibly created in the DAS Too.
 * This API will mitigate this need.
 *
 * @param resourceInfo the jndi name of the resource being used to get Connection from
 *                 This resource can either be a pmf resource or a jdbc resource
 * @return a java.sql.Connection
 * @throws java.sql.SQLException in case of errors
 */
public Connection getConnection(ResourceInfo resourceInfo) throws SQLException {
    java.sql.Connection con = null;
    try {
        // DASResourcesUtil.setAdminConfigContext();
        PoolInfo poolInfo = getPoolNameFromResourceJndiName(resourceInfo);
        if (poolInfo == null) {
            throw new SQLException("No pool by name exists ");
        }
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine("ConnectorRuntime.getConnection :: poolName : " + poolInfo);
        }
        con = (java.sql.Connection) getUnpooledConnection(poolInfo, null, true);
        if (con == null) {
            String i18nMsg = localStrings.getString("ccp_adm.null_unpooled_connection");
            throw new SQLException(i18nMsg);
        }
    } catch (ResourceException re) {
        SQLException sqle = new SQLException(re.getMessage());
        sqle.initCause(re);
        _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine("Exception : " + re);
        }
        throw sqle;
    } catch (Exception ex) {
        SQLException sqle = new SQLException(ex.getMessage());
        sqle.initCause(ex);
        _logger.log(Level.WARNING, "jdbc.exc_get_conn", ex.getMessage());
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine(" getConnection in ConnectorRuntime failed : " + ex);
        }
        throw sqle;
    }
    return con;
}
Also used : Connection(java.sql.Connection) SQLException(java.sql.SQLException) PoolInfo(org.glassfish.resourcebase.resources.api.PoolInfo) ResourceException(javax.resource.ResourceException) PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)

Example 50 with ResourceException

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

the class ConnectorAllocator method createResource.

public ResourceHandle createResource() throws PoolingException {
    try {
        ManagedConnection mc = mcf.createManagedConnection(subject, reqInfo);
        ResourceHandle resource = createResourceHandle(mc, spec, this, info);
        ConnectionEventListener l = new ConnectionListenerImpl(resource);
        mc.addConnectionEventListener(l);
        return resource;
    } catch (ResourceException ex) {
        Object[] params = new Object[] { spec.getPoolInfo(), ex.toString() };
        _logger.log(Level.WARNING, "poolmgr.create_resource_error", params);
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, "Resource Exception while creating resource", ex);
        }
        if (ex.getLinkedException() != null) {
            _logger.log(Level.WARNING, "poolmgr.create_resource_linked_error", ex.getLinkedException().toString());
        }
        throw new PoolingException(ex);
    }
}
Also used : PoolingException(com.sun.appserv.connectors.internal.api.PoolingException) ResourceHandle(com.sun.enterprise.resource.ResourceHandle) ResourceException(javax.resource.ResourceException)

Aggregations

ResourceException (javax.resource.ResourceException)163 TranslatorException (org.teiid.translator.TranslatorException)26 SQLException (java.sql.SQLException)18 IOException (java.io.IOException)17 PoolingException (com.sun.appserv.connectors.internal.api.PoolingException)14 ManagedConnection (javax.resource.spi.ManagedConnection)13 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)13 NamingException (javax.naming.NamingException)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 SObject (com.sforce.soap.partner.sobject.SObject)9 ConnectionException (com.sforce.ws.ConnectionException)9 UnexpectedErrorFault (com.sforce.soap.partner.fault.UnexpectedErrorFault)8 ArrayList (java.util.ArrayList)8 ResourceHandle (com.sun.enterprise.resource.ResourceHandle)7 InvalidSObjectFault (com.sforce.soap.partner.fault.InvalidSObjectFault)6 ResourcePrincipal (com.sun.enterprise.deployment.ResourcePrincipal)6 Set (java.util.Set)6 MessageEndpoint (javax.resource.spi.endpoint.MessageEndpoint)6 XAResource (javax.transaction.xa.XAResource)6 InvalidFieldFault (com.sforce.soap.partner.fault.InvalidFieldFault)5