Search in sources :

Example 56 with Context

use of javax.naming.Context in project mybatis-3 by mybatis.

the class JndiDataSourceFactory method setProperties.

@Override
public void setProperties(Properties properties) {
    try {
        InitialContext initCtx;
        Properties env = getEnvProperties(properties);
        if (env == null) {
            initCtx = new InitialContext();
        } else {
            initCtx = new InitialContext(env);
        }
        if (properties.containsKey(INITIAL_CONTEXT) && properties.containsKey(DATA_SOURCE)) {
            Context ctx = (Context) initCtx.lookup(properties.getProperty(INITIAL_CONTEXT));
            dataSource = (DataSource) ctx.lookup(properties.getProperty(DATA_SOURCE));
        } else if (properties.containsKey(DATA_SOURCE)) {
            dataSource = (DataSource) initCtx.lookup(properties.getProperty(DATA_SOURCE));
        }
    } catch (NamingException e) {
        throw new DataSourceException("There was an error configuring JndiDataSourceTransactionPool. Cause: " + e, e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) DataSourceException(org.apache.ibatis.datasource.DataSourceException) NamingException(javax.naming.NamingException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource)

Example 57 with Context

use of javax.naming.Context in project spring-framework by spring-projects.

the class JndiRmiClientInterceptor method invoke.

/**
	 * Fetches an RMI stub and delegates to {@link #doInvoke}.
	 * If configured to refresh on connect failure, it will call
	 * {@link #refreshAndRetry} on corresponding RMI exceptions.
	 * @see #getStub
	 * @see #doInvoke
	 * @see #refreshAndRetry
	 * @see java.rmi.ConnectException
	 * @see java.rmi.ConnectIOException
	 * @see java.rmi.NoSuchObjectException
	 */
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object stub;
    try {
        stub = getStub();
    } catch (NamingException ex) {
        throw new RemoteLookupFailureException("JNDI lookup for RMI service [" + getJndiName() + "] failed", ex);
    }
    Context ctx = (this.exposeAccessContext ? getJndiTemplate().getContext() : null);
    try {
        return doInvoke(invocation, stub);
    } catch (RemoteConnectFailureException ex) {
        return handleRemoteConnectFailure(invocation, ex);
    } catch (RemoteException ex) {
        if (isConnectFailure(ex)) {
            return handleRemoteConnectFailure(invocation, ex);
        } else {
            throw ex;
        }
    } finally {
        getJndiTemplate().releaseContext(ctx);
    }
}
Also used : Context(javax.naming.Context) RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) NamingException(javax.naming.NamingException) RemoteLookupFailureException(org.springframework.remoting.RemoteLookupFailureException) RemoteException(java.rmi.RemoteException)

Example 58 with Context

use of javax.naming.Context in project spring-framework by spring-projects.

the class SimpleRemoteSlsbInvokerInterceptorTests method doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh.

private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception {
    final RemoteInterface ejb = mock(RemoteInterface.class);
    given(ejb.targetMethod()).willThrow(new ConnectException(""));
    int lookupCount = 2;
    if (!cacheHome) {
        lookupCount++;
        if (lookupHomeOnStartup) {
            lookupCount++;
        }
    }
    final String jndiName = "foobar";
    Context mockContext = mockContext(jndiName, ejb);
    SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
    si.setRefreshHomeOnConnectFailure(true);
    si.setLookupHomeOnStartup(lookupHomeOnStartup);
    si.setCacheHome(cacheHome);
    RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
    try {
        target.targetMethod();
        fail("Should have thrown RemoteException");
    } catch (ConnectException ex) {
    // expected
    }
    verify(mockContext, times(lookupCount)).close();
    verify(ejb, times(2)).remove();
}
Also used : Context(javax.naming.Context) ConnectException(java.rmi.ConnectException)

Example 59 with Context

use of javax.naming.Context in project spring-framework by spring-projects.

the class LocalSlsbInvokerInterceptorTests method testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods.

@Test
public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception {
    Object retVal = new Object();
    LocalInterface ejb = mock(LocalInterface.class);
    given(ejb.targetMethod()).willReturn(retVal);
    String jndiName = "foobar";
    Context mockContext = mockContext(jndiName, ejb);
    LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class });
    pf.addAdvice(si);
    BusinessMethods target = (BusinessMethods) pf.getProxy();
    assertTrue(target.targetMethod() == retVal);
    verify(mockContext).close();
    verify(ejb).remove();
}
Also used : Context(javax.naming.Context) ProxyFactory(org.springframework.aop.framework.ProxyFactory) EJBLocalObject(javax.ejb.EJBLocalObject) Test(org.junit.Test)

Example 60 with Context

use of javax.naming.Context in project spring-framework by spring-projects.

the class LocalSlsbInvokerInterceptorTests method testException.

private void testException(Exception expected) throws Exception {
    LocalInterfaceWithBusinessMethods ejb = mock(LocalInterfaceWithBusinessMethods.class);
    given(ejb.targetMethod()).willThrow(expected);
    String jndiName = "foobar";
    Context mockContext = mockContext(jndiName, ejb);
    LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { LocalInterfaceWithBusinessMethods.class });
    pf.addAdvice(si);
    LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy();
    try {
        target.targetMethod();
        fail("Should have thrown exception");
    } catch (Exception thrown) {
        assertTrue(thrown == expected);
    }
    verify(mockContext).close();
}
Also used : Context(javax.naming.Context) ProxyFactory(org.springframework.aop.framework.ProxyFactory) CreateException(javax.ejb.CreateException) NamingException(javax.naming.NamingException)

Aggregations

Context (javax.naming.Context)507 InitialContext (javax.naming.InitialContext)250 Test (org.junit.Test)173 NamingException (javax.naming.NamingException)156 DataSource (javax.sql.DataSource)72 Properties (java.util.Properties)67 Connection (java.sql.Connection)62 NameNotFoundException (javax.naming.NameNotFoundException)52 UserTransaction (javax.transaction.UserTransaction)48 SQLException (java.sql.SQLException)46 IOException (java.io.IOException)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)44 Statement (java.sql.Statement)42 Name (javax.naming.Name)38 Hashtable (java.util.Hashtable)35 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)27 BeanContext (org.apache.openejb.BeanContext)27 Binding (javax.naming.Binding)25 Reference (javax.naming.Reference)25 NotContextException (javax.naming.NotContextException)23