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="" xmlns:xsi=""
   <interceptor-class> de.xmlsicherheit.AuditInterceptor</interceptor-class>



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).