9 Replies Latest reply on Nov 2, 2010 7:14 AM by philani

    Interceptor not firing

    philani

      Hi guys

       

      My EJB interceptors are not firing, am I missing something? Ive pasted the relevant code/details below.

       

       

      Environment:

      --------------------

       

      Jboss 5.0.1 GA

      EJB 3.0

      Win XP

      JDK_1.6.0_13, set to 1.5 code compliance (long story)

       

       

      package za.co.switchonline.beans.dao;
      import java.util.List;
      import javax.annotation.security.RolesAllowed;
      import javax.ejb.Stateless;
      import javax.interceptor.Interceptors;
      import javax.persistence.Query;
      import org.jboss.ejb3.annotation.SecurityDomain;
      import za.co.switchonline.beans.interfaces.IQediUserDAO;
      import za.co.switchonline.beans.interfaces.QediUserDAOInterceptor;
      import za.co.switchonline.beans.vo.QediUser;
      @Stateless
      @SuppressWarnings("unchecked")
      @SecurityDomain("other")
      @Interceptors(value=QediUserDAOInterceptor.class)
      public class QediUserDAO extends SuperDAO implements IQediUserDAO {
      public static final String PASSWORD = "password";
      @RolesAllowed({"admin"})
      public void save(QediUser entity) {
      try {
      SuperDAO.getManager().persist(entity);
      } catch (RuntimeException re) {
      throw re;
      }
      }
      @RolesAllowed({"admin"})
      public void createQediUser(QediUser user,String hostName, String terminalName)
      {
      save(user);
      }
      @RolesAllowed({"admin"})
      public void deleteQediUser(QediUser user, String hostName,String terminalName)
      {
      delete(user);
      }
      @RolesAllowed({"admin"})
      public void updateQediUser(QediUser user, String hostName,     String terminalName)
      {
      update(user);
      }
      @RolesAllowed({"admin"})
      public void delete(QediUser entity) {
      try {
      entity = SuperDAO.getManager().getReference(QediUser.class,
      entity.getId());
      SuperDAO.getManager().remove(entity);
      } catch (RuntimeException re) {
      throw re;
      }
      }
      @RolesAllowed({"admin"})
      public QediUser update(QediUser entity) {
      try {
      QediUser result = SuperDAO.getManager().merge(entity);
      return result;
      } catch (RuntimeException re) {
      throw re;
      }
      }
      public QediUser findById(String id) {
      try {
      QediUser instance = SuperDAO.getManager().find(QediUser.class, id);
      return instance;
      } catch (RuntimeException re) {
      throw re;
      }
      }
      public List<QediUser> findByProperty(String propertyName, final Object value) {
      try {
      final String queryString = "select model from QediUser model where model."
      + propertyName + "= :propertyValue";
      Query query =SuperDAO.getManager().createQuery(queryString);
      query.setParameter("propertyValue", value);
      return query.getResultList();
      } catch (RuntimeException re) {
      throw re;
      }
      }
      public List<QediUser> findAll() {
      try {
      final String queryString = "select model from QediUser model";
      Query query = SuperDAO.getManager().createQuery(queryString);
      return query.getResultList();
      } catch (RuntimeException re) {
      throw re;
      }
      }
      }

      EJB class:

      ----------------

       

      {code}

      import java.util.List;

      import javax.annotation.security.RolesAllowed;

      import javax.ejb.Stateless;

      import javax.interceptor.Interceptors;

      import javax.persistence.Query;

      import org.jboss.ejb3.annotation.SecurityDomain;

      import za.co.switchonline.beans.interfaces.IQediUserDAO;

      import za.co.switchonline.beans.interfaces.QediUserDAOInterceptor;

      import za.co.switchonline.beans.vo.QediUser;

       

      @Stateless

       

      @SuppressWarnings("unchecked")

       

      @SecurityDomain("other")

       

      @Interceptors(value=QediUserDAOInterceptor.class)

       

      public class QediUserDAO extends SuperDAO implements IQediUserDAO {

       

      public static final String PASSWORD = "password";

       

      @RolesAllowed({"admin"})

      public void save(QediUser entity) {

      try {

      SuperDAO.getManager().persist(entity);

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

      @RolesAllowed({"admin"})

      public void createQediUser(QediUser user,String hostName, String terminalName)

      {

      save(user);

       

      }

       

      @RolesAllowed({"admin"})

      public void deleteQediUser(QediUser user, String hostName,String terminalName)

      {

      delete(user);

      }

       

      @RolesAllowed({"admin"})

      public void updateQediUser(QediUser user, String hostName, String terminalName)

      {

      update(user);

       

      }

       

      @RolesAllowed({"admin"})

      public void delete(QediUser entity) {

      try {

      entity = SuperDAO.getManager().getReference(QediUser.class,

      entity.getId());

      SuperDAO.getManager().remove(entity);

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

      @RolesAllowed({"admin"})

      public QediUser update(QediUser entity) {

      try {

      QediUser result = SuperDAO.getManager().merge(entity);

      return result;

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

      public QediUser findById(String id) {

      try {

      QediUser instance = SuperDAO.getManager().find(QediUser.class, id);

      return instance;

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

      public List<QediUser> findByProperty(String propertyName, final Object value) {

      try {

      final String queryString = "select model from QediUser model where model."

      + propertyName + "= :propertyValue";

      Query query =SuperDAO.getManager().createQuery(queryString);

      query.setParameter("propertyValue", value);

      return query.getResultList();

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

      public List<QediUser> findAll() {

      try {

      final String queryString = "select model from QediUser model";

      Query query = SuperDAO.getManager().createQuery(queryString);

      return query.getResultList();

      } catch (RuntimeException re) {

      throw re;

      }

      }

       

       

      }

       

       

       

      Interceptor:

      ------------------

       

      import javax.annotation.Resource;

      import javax.ejb.SessionContext;

      import javax.interceptor.AroundInvoke;

      import javax.interceptor.InvocationContext;

      import javax.transaction.TransactionSynchronizationRegistry;

       

       

      public class QediUserDAOInterceptor

      {

      private String principal = " ";

      Object[] params = null;

       

       

      @Resource

      private SessionContext sessionContext;

       

      @javax.annotation.Resource( mappedName = "java:comp/TransactionSynchronizationRegistry" )

          private TransactionSynchronizationRegistry txRegistry;

       

       

      @AroundInvoke

        public Object intercept(InvocationContext invocation) throws Exception

        {

      InterceptorHelper helper = new InterceptorHelper();

       

      this.principal = helper.extractPrincipal(principal, sessionContext);

      helper.storePrincipalInTxRegistry(principal, txRegistry);

      this.params = helper.extractInterceptedMethodParams(invocation,params);

      helper.extractSecurityDetailsForSave(invocation, params, txRegistry, 1, 2, "createQediUser");

      helper.extractSecurityDetailsForUpdate(invocation, params, txRegistry, 1, 2, "updateQediUser");

      helper.extractSecurityDetailsForDelete(invocation, params, txRegistry, 1, 2, "deleteQediUser");

      return invocation.proceed();

         }

       

       

      }

       

      {code}

      Where could I be going wrong?

        • 1. Re: Interceptor not firing
          wdfink

          For me it looks correct,

          but I use @Interceptors(xyz.class) or @Interceptors( {A.class, B.class} )

           

          Do you check with logging or debugger?

          • 2. Re: Interceptor not firing
            philani

            Hi Wolf

             

            Im still kinda new to JBOSS

             

            how do i enable logging, the interceptor just fails without throwing an exception

            • 3. Re: Interceptor not firing
              jaikiran

              How do you get hold of the EJB? Do you do a JNDI lookup? Please post that relevant code. And by the way, if you want to use JBoss AS5, then please use JBoss AS 5.1.0 (and not 5.0.1).

              • 4. Re: Interceptor not firing
                philani

                I use the below code to login to the security context & to perform JNDI lookup.

                The interceptors were working fine a couple of days ago, i dont know what went wrong.

                 

                {code}

                package za.co.switchonline.beans.test;
                import java.util.Properties;
                import javax.naming.Context;
                import javax.naming.InitialContext;
                import javax.naming.NameNotFoundException;
                import javax.naming.NamingException;
                import org.apache.log4j.Logger;
                import org.jboss.security.client.SecurityClient;
                import org.jboss.security.client.SecurityClientFactory;
                import za.co.switchonline.beans.interfaces.IDataSetsServicesLDAO;
                import za.co.switchonline.beans.vo.DataSetsServicesL;
                import za.co.switchonline.beans.vo.DataSetsServicesLId;
                import junit.framework.TestCase;
                /**
                *
                * @author Philani Dlamini
                * @sice   30 Aug 2010
                */
                @SuppressWarnings("unused")
                public class TestUtil
                {
                public Properties initJNDI(Properties properties, String ip) throws Exception
                {
                       properties.put(Context.SECURITY_PRINCIPAL, "caller");
                       properties.put(Context.SECURITY_CREDENTIALS, "password123");
                properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
                properties.put(Context.PROVIDER_URL, "jnp://"+ip+"");
                return properties;
                }
                public SecurityClient loginToSecurityContext(SecurityClient client,String userName, String password)
                {
                try
                {
                client = SecurityClientFactory.getSecurityClient();
                client.setSimple(userName, password);
                client.login();
                } catch (Exception e)
                {
                       e.printStackTrace();
                }
                return client;
                  }
                    public Object lookup(String objectName, Properties properties) throws NameNotFoundException
                    {
                         Object obj = null;
                        try {
                                Context ctx = new InitialContext(properties);
                                obj = ctx.lookup(objectName);
                } catch (NamingException e) {
                e.printStackTrace();
                }
                return obj;
                    }
                }

                 

                package za.co.switchonline.beans.test;

                 

                import java.util.Properties;

                import javax.naming.Context;

                import javax.naming.InitialContext;

                import javax.naming.NameNotFoundException;

                import javax.naming.NamingException;

                import org.apache.log4j.Logger;

                import org.jboss.security.client.SecurityClient;

                import org.jboss.security.client.SecurityClientFactory;

                import za.co.switchonline.beans.interfaces.IDataSetsServicesLDAO;

                import za.co.switchonline.beans.vo.DataSetsServicesL;

                import za.co.switchonline.beans.vo.DataSetsServicesLId;

                import junit.framework.TestCase;

                /**

                *

                * @author Philani Dlamini

                * @sice   30 Aug 2010

                */

                @SuppressWarnings("unused")

                public class TestUtil

                {

                 

                public Properties initJNDI(Properties properties, String ip) throws Exception

                {

                        properties.put(Context.SECURITY_PRINCIPAL, "caller");

                        properties.put(Context.SECURITY_CREDENTIALS, "password123");

                properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");

                properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

                properties.put(Context.PROVIDER_URL, "jnp://"+ip+"");

                return properties;

                 

                }

                 

                public SecurityClient loginToSecurityContext(SecurityClient client,String userName, String password)

                {

                try

                {

                  client = SecurityClientFactory.getSecurityClient();

                  client.setSimple(userName, password);

                  client.login();

                } catch (Exception e)

                {

                   e.printStackTrace();

                }

                return client;

                  }

                 

                    public Object lookup(String objectName, Properties properties) throws NameNotFoundException

                    {

                    Object obj = null;

                        try {

                      Context ctx = new InitialContext(properties);

                      obj = ctx.lookup(objectName);

                } catch (NamingException e) {

                e.printStackTrace();

                }

                return obj;

                    }

                 

                 

                }

                 

                 

                {code}

                • 5. Re: Interceptor not firing
                  philani

                  Sorry I use JBOSS 5.1.0 jaikiran

                  • 6. Re: Interceptor not firing
                    wdfink

                    As I see you use Log4j as logging framework.

                    So put a logger into your interceptor and bean code to log for example the method-entry.

                    Edit the server/.../conf/jboss-log4j.xml and add a category for your class (with package) similar to the existing entries.

                    Then you should see your log-statements if the code is invoked.

                    • 7. Re: Interceptor not firing
                      philani

                      Hello guys

                       

                      Just t update you

                       

                      I've enabled the logging on the EJB methods to be intercepted and the interceptor itself. It just goes through to executing the EJB business methods without invoking the interceptor. I mean shouldnt it this API/JBOSS throw some sort of exception ,i.e ClassNotFounException or IllegalStateException , iI read somewhere that intercetpors fail wit JDK 6 and that Ione must update jboss-3jb3* jars. This is not cool and its gonna be mission to trouble shoot.

                      • 8. Re: Interceptor not firing
                        wdfink

                        You annotate 'QediUserDAO' with the interceptor. How do you call a method from this class?

                        You have to use (within an other SLSB):

                        @EJB

                        IQediUserDAO xy;

                        ....

                        xy.method(...)

                         

                        or call direct via remote RMI, only in this case the container will able to intercept.

                        • 9. Re: Interceptor not firing
                          philani

                          Hi guys I got this working eventually.

                           

                          I refactored my interceptor name and made the name shorter, emptied the  tmp folder on %JBOSS_HOME%\server\default\tmp where jar deployements are exploded and it works fine now.

                          Thanks a lot Wolf, Jaikiran.