下面列出了怎么用javax.ejb.ApplicationException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
/**
* For http://issues.apache.org/jira/browse/OPENEJB-980
*/
public void applicationExceptionInheritanceTest() throws Exception {
EjbModule ejbModule = testModule();
final AnnotationDeployer.DiscoverAnnotatedBeans discvrAnnBeans = new AnnotationDeployer.DiscoverAnnotatedBeans();
ejbModule = discvrAnnBeans.deploy(ejbModule);
final AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();
org.apache.openejb.jee.ApplicationException appEx =
assemblyDescriptor.getApplicationException(BusinessException.class);
assertThat(appEx, notNullValue());
assertThat(appEx.getExceptionClass(), is(BusinessException.class.getName()));
assertThat(appEx.isRollback(), is(true));
//inheritance is now handled at runtime, only explicitly mentioned exceptions are in the assembly descriptor
appEx = assemblyDescriptor.getApplicationException(ValueRequiredException.class);
assertThat(appEx, nullValue());
}
@Override
public boolean isApplicationException(Exception e) {
for (Class<?> declaredEx : interfaceMethod.getExceptionTypes()) {
if (declaredEx.isInstance(e)) {
return true;
}
}
if (e.getClass().getAnnotation(ApplicationException.class) != null) {
return true;
}
return false;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean newTransaction = beginTransaction();
try {
return method.invoke(delegate, args);
} catch (Throwable throwable) {
if (throwable instanceof InvocationTargetException) {
InvocationTargetException invocationTargetException = (InvocationTargetException) throwable;
ApplicationException applicationException = invocationTargetException.getTargetException().getClass().getAnnotation(ApplicationException.class);
if (applicationException == null || applicationException.rollback()) {
rollbackTransaction();
}
} else {
rollbackTransaction();
}
throw throwable.getCause();
} finally {
endTransaction(newTransaction);
}
}
public ExceptionType getExceptionType(final Throwable e) {
// Errors are always system exceptions
if (!(e instanceof Exception)) {
return ExceptionType.SYSTEM;
}
// check the registered app exceptions
Class<?> exceptionClass = e.getClass();
boolean inherited = false;
while (exceptionClass != Object.class) {
final ExceptionType type = exceptions.get(exceptionClass);
if (type == ExceptionType.APPLICATION || type == ExceptionType.APPLICATION_ROLLBACK) {
return type;
}
if (type != null) {
if (inherited) {
return ExceptionType.SYSTEM;
}
if (type == ExceptionType.APPLICATION_NOT_INHERITED) {
return ExceptionType.APPLICATION;
}
return ExceptionType.APPLICATION_ROLLBACK;
}
exceptionClass = exceptionClass.getSuperclass();
inherited = true;
}
// Unregistered - runtime exceptions are system exception and the rest are application exceptions
final Class<? extends Throwable> eClass = e.getClass();
final ApplicationException applicationException = eClass.getAnnotation(ApplicationException.class);
if (applicationException != null) {
addApplicationException(eClass, applicationException.rollback(), applicationException.inherited());
return getExceptionType(e);
}
if (e instanceof RuntimeException) {
return ExceptionType.SYSTEM;
}
return ExceptionType.APPLICATION;
}
@Override
public boolean rollbackOn(Throwable ex) {
ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
return (ann != null ? ann.rollback() : super.rollbackOn(ex));
}
@Override
public boolean rollbackOn(Throwable ex) {
ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
return (ann != null ? ann.rollback() : super.rollbackOn(ex));
}
@Override
public boolean rollbackOn(Throwable ex) {
ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
return (ann != null ? ann.rollback() : super.rollbackOn(ex));
}
@Override
public boolean rollbackOn(Throwable ex) {
ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class);
return (ann != null ? ann.rollback() : super.rollbackOn(ex));
}
private static boolean hasRollbackAnnotation(Exception ex) {
ApplicationException annotation = ex.getClass().getAnnotation(
ApplicationException.class);
return annotation != null && annotation.rollback();
}