Search in sources :

Example 6 with CreateException

use of javax.ejb.CreateException in project spring-framework by spring-projects.

the class LocalSlsbInvokerInterceptor method invokeInContext.

/**
	 * This implementation "creates" a new EJB instance for each invocation.
	 * Can be overridden for custom invocation strategies.
	 * <p>Alternatively, override {@link #getSessionBeanInstance} and
	 * {@link #releaseSessionBeanInstance} to change EJB instance creation,
	 * for example to hold a single shared EJB instance.
	 */
@Override
public Object invokeInContext(MethodInvocation invocation) throws Throwable {
    Object ejb = null;
    try {
        ejb = getSessionBeanInstance();
        Method method = invocation.getMethod();
        if (method.getDeclaringClass().isInstance(ejb)) {
            // directly implemented
            return method.invoke(ejb, invocation.getArguments());
        } else {
            // not directly implemented
            Method ejbMethod = ejb.getClass().getMethod(method.getName(), method.getParameterTypes());
            return ejbMethod.invoke(ejb, invocation.getArguments());
        }
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (logger.isDebugEnabled()) {
            logger.debug("Method of local EJB [" + getJndiName() + "] threw exception", targetEx);
        }
        if (targetEx instanceof CreateException) {
            throw new EjbAccessException("Could not create local EJB [" + getJndiName() + "]", targetEx);
        } else {
            throw targetEx;
        }
    } catch (NamingException ex) {
        throw new EjbAccessException("Failed to locate local EJB [" + getJndiName() + "]", ex);
    } catch (IllegalAccessException ex) {
        throw new EjbAccessException("Could not access method [" + invocation.getMethod().getName() + "] of local EJB [" + getJndiName() + "]", ex);
    } finally {
        if (ejb instanceof EJBLocalObject) {
            releaseSessionBeanInstance((EJBLocalObject) ejb);
        }
    }
}
Also used : EJBLocalObject(javax.ejb.EJBLocalObject) NamingException(javax.naming.NamingException) Method(java.lang.reflect.Method) CreateException(javax.ejb.CreateException) EJBLocalObject(javax.ejb.EJBLocalObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with CreateException

use of javax.ejb.CreateException in project spring-framework by spring-projects.

the class SimpleRemoteStatelessSessionProxyFactoryBeanTests method testCreateExceptionWithLocalBusinessInterface.

@Test
public void testCreateExceptionWithLocalBusinessInterface() throws Exception {
    final String jndiName = "foo";
    final CreateException cex = new CreateException();
    final MyHome home = mock(MyHome.class);
    given(home.create()).willThrow(cex);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) {
            // parameterize
            assertTrue(name.equals(jndiName));
            return home;
        }
    };
    SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
    fb.setBusinessInterface(MyLocalBusinessMethods.class);
    assertEquals(fb.getBusinessInterface(), MyLocalBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyLocalBusinessMethods mbm = (MyLocalBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    try {
        mbm.getValue();
        fail("Should have failed to create EJB");
    } catch (RemoteAccessException ex) {
        assertTrue(ex.getCause() == cex);
    }
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) RemoteAccessException(org.springframework.remoting.RemoteAccessException) CreateException(javax.ejb.CreateException) Test(org.junit.Test)

Example 8 with CreateException

use of javax.ejb.CreateException in project spring-framework by spring-projects.

the class LocalStatelessSessionProxyFactoryBeanTests method testCreateException.

@Test
public void testCreateException() throws Exception {
    final String jndiName = "foo";
    final CreateException cex = new CreateException();
    final MyHome home = mock(MyHome.class);
    given(home.create()).willThrow(cex);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        public Object lookup(String name) throws NamingException {
            // parameterize
            assertTrue(name.equals(jndiName));
            return home;
        }
    };
    LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
    fb.setJndiName(jndiName);
    // no java:comp/env prefix
    fb.setResourceRef(false);
    fb.setBusinessInterface(MyBusinessMethods.class);
    assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
    fb.setJndiTemplate(jt);
    // Need lifecycle methods
    fb.afterPropertiesSet();
    MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
    assertTrue(Proxy.isProxyClass(mbm.getClass()));
    try {
        mbm.getValue();
        fail("Should have failed to create EJB");
    } catch (EjbAccessException ex) {
        assertSame(cex, ex.getCause());
    }
}
Also used : JndiTemplate(org.springframework.jndi.JndiTemplate) CreateException(javax.ejb.CreateException) Test(org.junit.Test)

Aggregations

CreateException (javax.ejb.CreateException)8 Test (org.junit.Test)3 JndiTemplate (org.springframework.jndi.JndiTemplate)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PostConstruct (javax.annotation.PostConstruct)2 RemoteException (java.rmi.RemoteException)1 EJBLocalObject (javax.ejb.EJBLocalObject)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 JVTAlarmMonitorHome (javax.oss.fm.monitor.JVTAlarmMonitorHome)1 InvocationType (org.jboss.as.ee.component.interceptors.InvocationType)1 RemoteAccessException (org.springframework.remoting.RemoteAccessException)1