2009-06-24 34 views
2

我有一個使用(本地)會話bean公開服務的JEE5應用程序。如何從EJB客戶端隱藏RuntimeException細節?

在服務執行過程中發生內部錯誤時,RuntimeException被JBoss(5.0.1)拋出並封裝在javax.ejb.EJBTransactionRolledbackException中。

問題是,接收到此EJBTransactionRolledbackException的客戶端應用程序可以訪問有關原因運行時異常的詳細信息,從而暴露了我的應用程序的內部體系結構。我不想那樣。

相反,我希望JBoss總是將由暴露的會話bean引發的RuntimeException封裝爲單個(並且簡單)的TechnicalException(無理由)。

達到此目的的最佳方法是什麼? (使用攔截器?使用JBoss配置?)

回答

4

最後,基於前面的回答和我個人的研究,我保留了如下因素的解決方案。

我創建了專門用於管理服務器故障的攔截:

public class FaultBarrierInterceptor { 

@AroundInvoke 
public Object intercept(final InvocationContext invocationContext) throws Exception { 
    try { 
     return invocationContext.proceed(); 
    } catch (final RuntimeException e) { 
     final Logger logger = Logger.getLogger(invocationContext.getMethod().getDeclaringClass()); 
     logger.error("A fault occured during service invocation:" + 
       "\n-METHOD: " + invocationContext.getMethod() + 
       "\n-PARAMS: " + Arrays.toString(invocationContext.getParameters()), e); 
     throw new TechnicalException(); 
    } 
}} 

拋出的異常的技術延伸EJBException的和不公開的原因的RuntimeException:

public class TechnicalException extends EJBException {} 

我使用這個攔截所有公共服務:

@Stateless 
@Interceptors({FaultBarrierInterceptor.class}) 
public class ShoppingCardServicesBean implements ShoppingCardServices { ... 

這是一個implementationatio n的Fault Barrier pattern

捕獲,記錄任何運行時異常並使用TechnicalException向客戶端發送故障(沒有內部詳細信息)。檢查的異常被忽略。

RuntimeException處理是集中的,並與任何業務方法分開。

1

任何RuntimeException擴展java.lang.Exception。

的EJB規範提供處理2種類型的異常(應用程序和系統)

如果你想拋出系統異常,你通常會做它像這樣:

try { 
.... your code ... 
}catch(YourApplicationException ae) { 
    throw ae; 
}catch(Exception e) { 
    throw new EJBException(e); //here's where you need to change. 
} 

要隱藏你的系統異常的內部細節,只需更換:

throw new EJBException(e); 

有:

throw new EJBException(new TechnicalException("Technical Fault")); 

希望這是你正在尋找。

乾杯