Using interceptors with version 2.1 Enterprise Beans

I recently hit the requirement to use Interceptors with some EJB 2.1 beans. Those beans should be migrated to 3.1, and tracing should make their complex flow easier to understand. As this blog post points out, it is possible to use interceptors with old EJB versions. Simply update the deployment descriptor to 3.0 or 3.1 and integrate the interceptors.

However, the code mentioned in the post did not work without modifications (at least in my environment with GlassFish 3.1.2). The described AuditInterceptor class required one little additional != null check in the first if to avoid NullPointerExceptions:

if (ic.getParameters() != null && ic.getParameters().length != 0) {

The deployment descriptor required some more changes, maybe only for the GlassFish 3.1.2 environment in my case:


<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="3.1"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
 <enterprise-beans>
  <session>...</session>
 </enterprise-beans>
 <interceptors>
  <interceptor>
   <interceptor-class>de.xmlsicherheit.AuditInterceptor</interceptor-class>
   <around-invoke>
    <method-name>logMethodCall</method-name>
   </around-invoke>
  </interceptor>
 </interceptors>
 <assembly-descriptor>
  <interceptor-binding>
   <ejb-name>*</ejb-name>
   <interceptor-class> de.xmlsicherheit.AuditInterceptor</interceptor-class>
  </interceptor-binding>
 </assembly-descriptor>
</ejb-jar>

Using

<method>
 <method-name>*</method-name>
</method>

in the interceptor-binding did not work out. However, omitting this element completely results into intercepting all EJB methods anyway.

Using this approach brings you the benefit of intercepting EJB 2.1 beans in a 3.1 environment without touching the actual classes and minimal changes to the deployment descriptor(s).


Posted

in

by

Tags: