3 Replies Latest reply on Aug 22, 2007 7:52 AM by Pete Muir

    Interceptor for seam component

    Eric Yip Newbie

      Is there a way to create an interceptor for a seam component that's NOT an EJB? Basically, I want the interceptor to get invoke whereever a method in the seam component is called.

        • 1. Re: Interceptor for seam component
          Gavin King Master

          Yes, just use @Interceptors as a meta-annotation, as described in the docs.

          • 2. Re: Interceptor for seam component
            chen hong Newbie

            i do it as the doc,but i can't invoke the interceptor,my code is the following

            theinterceptor:
            package cn.sh.guanghua.manhour.service.manhour;

            import java.lang.reflect.Modifier;
            import java.util.Map;

            import javax.interceptor.AroundInvoke;
            import javax.interceptor.InvocationContext;

            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.framework.EntityHome;
            import org.jboss.seam.log.Log;

            import cn.sh.guanghua.manhour.model.manhour.Logs;

            @Name("logsHome")
            public class LogsHome extends EntityHome {
            @Logger
            Log log;

            public void setLogsId(Long id) {
            setId(id);
            }

            public Long getLogsId() {
            return (Long) getId();
            }

            @Override
            protected Logs createInstance() {
            Logs logs = new Logs();
            return logs;
            }

            public void wire() {
            }

            public boolean isWired() {
            return true;
            }

            public Logs getDefinedInstance() {
            return isIdDefined() ? getInstance() : null;
            }

            @AroundInvoke
            public Object doLogs(InvocationContext inv) throws Exception {
            int methodModify = inv.getMethod().getModifiers();
            String methodName = inv.getMethod().getName();
            System.out.println("1111111111111111111111111111111111111111111");
            if (methodModify == Modifier.PUBLIC
            && (methodName.startsWith("create")
            || methodName.startsWith("update")
            || methodName.startsWith("persist")
            || methodName.startsWith("remove"))) {

            Map data = inv.getContextData();
            Object target = inv.getTarget();
            log.debug("ContextData:" + data);
            log.debug("Target:" + target);
            }

            return inv.proceed();
            }

            }


            the invoke interceptor class:
            package cn.sh.guanghua.manhour.service.admin;

            import java.util.ArrayList;
            import java.util.List;

            import javax.interceptor.Interceptors;

            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.framework.EntityHome;

            import cn.sh.guanghua.manhour.model.admin.Role;
            import cn.sh.guanghua.manhour.model.admin.RoleResource;
            import cn.sh.guanghua.manhour.service.manhour.LogsHome;

            @Name("roleHome")
            @Interceptors(LogsHome.class)
            public class RoleHome extends EntityHome {

            public void setRoleId(Long id) {
            setId(id);
            }

            public Long getRoleId() {
            return (Long) getId();
            }

            @Override
            protected Role createInstance() {
            Role role = new Role();
            return role;
            }

            public void wire() {
            }

            public boolean isWired() {
            return true;
            }

            public Role getDefinedInstance() {
            return isIdDefined() ? getInstance() : null;
            }

            public List getRoleResources() {
            return getInstance() == null ? null : new ArrayList(
            getInstance().getRoleResources());
            }

            }

            but when RoleHome excute the persist method,the interceptor don't work

            thank you!

            • 3. Re: Interceptor for seam component
              Pete Muir Master

              Post readable code.