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);
}
}
}
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);
}
}
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());
}
}
Aggregations