Hi,

this is a first attempt (having never written interceptors or annotation 
before). Seems to work, but not ideal. 

1. In my Interceptor I have no which interceptors need to be in Around and 
Within
2. Ideally I would like to add my annotation to the method and not the type, 
but for some reason I was unable to get that working
3. Ideally I would like to be able to specify the page where it should go. 
Right now it stays on the same page, requiring a cancel from the user.
4. Massive unknows about killing the transaction and clearing the entitymanager 
(I looked at Gavin's SeamExceptionFilter)

My annotation:

package csir.interceptor.seam;
  | 
  | import java.lang.annotation.ElementType;
  | import java.lang.annotation.Retention;
  | import java.lang.annotation.RetentionPolicy;
  | import java.lang.annotation.Target;
  | 
  | import javax.ejb.Interceptors;
  | 
  | @Target( { ElementType.TYPE })
  | @Retention(RetentionPolicy.RUNTIME)
  | @Interceptors(IfNAPExceptionInterceptor.class)
  | public @interface IfNAPException {
  | }
  | 
  | 
My interceptor

  | package csir.interceptor.seam;
  | 
  | import javax.ejb.AroundInvoke;
  | import javax.ejb.InvocationContext;
  | import javax.persistence.EntityManager;
  | 
  | import org.apache.log4j.Logger;
  | import org.jboss.seam.Component;
  | import org.jboss.seam.annotations.Around;
  | import org.jboss.seam.annotations.Within;
  | import org.jboss.seam.interceptors.BijectionInterceptor;
  | import org.jboss.seam.interceptors.BusinessProcessInterceptor;
  | import org.jboss.seam.interceptors.ConversationInterceptor;
  | import org.jboss.seam.interceptors.RemoveInterceptor;
  | import org.jboss.seam.interceptors.ValidationInterceptor;
  | import org.jboss.seam.util.Transactions;
  | 
  | import csir.common.seam.NAPException;
  | 
  | @Around( { BijectionInterceptor.class, ValidationInterceptor.class,
  |             ConversationInterceptor.class, BusinessProcessInterceptor.class 
})
  | @Within(RemoveInterceptor.class)
  | public class IfNAPExceptionInterceptor {
  | 
  |     static final Logger logger = Logger
  |                     .getLogger(IfNAPExceptionInterceptor.class);
  | 
  |     @AroundInvoke
  |     public Object customInterceptor(InvocationContext ctx) throws Exception 
{
  |             logger.info("*** BEFORE INTERCEPTION ***");
  |             Object object = null;
  |             try {
  |                     object = ctx.proceed();
  |             } catch (NAPException e) {
  |                     EntityManager em = (EntityManager) 
Component.getInstance(
  |                                     "napEntityManager", false);
  |                     logger.error("Got NAPException");
  |                     rollbackAfterException();
  |                     em.clear();
  |             }
  |             logger.info("*** AFTER INTERCEPTION ***");
  |             return object;
  |     }
  | 
  |     private void rollbackAfterException() {
  |             try {
  |                     if (Transactions.isTransactionActiveOrMarkedRollback()) 
{
  |                             logger.info("killing transaction");
  |                             Transactions.getUserTransaction().rollback();
  |                     }
  |             } catch (Exception te) {
  |                     logger.error("could not roll back transaction", te);
  |             }
  |     }
  | 
  | }
  | 

as applied in the Stateful bean:
Stateful
  | @IfNAPException
  | @Name("serviceManagementBean")
  | @Interceptors(SeamInterceptor.class)
  | public class ServiceManagementBean implements Serializable, 
ServiceManagement {
  | .
  | .
  | 

and the method throwing the NAPException
@IfInvalid(outcome = Outcome.REDISPLAY)
  |     public String save() throws Exception {
  |             try {
  |                     logger.info("Saving edited/created service.");
  |                     em.persist(service);
  |                     em.flush();
  |                     return "save";
  |             } catch (StaleObjectStateException e) {
  |                     logger.error("Failed to persist service", e);
  |                     FacesMessage facesMessage = new 
FacesMessage(e.getMessage());
  |                     facesContext.addMessage(null, facesMessage);
  |                     throw new NAPException(e);
  |             }
  |     }
  | 
  | 

and finally the NAPException.


  | package csir.common.seam;
  | 
  | import java.io.Serializable;
  | 
  | import javax.ejb.ApplicationException;
  | 
  | @ApplicationException(rollback=true)
  | public class NAPException extends Exception implements Serializable {
  | 
  |     /**
  |      * 
  |      */
  |     private static final long serialVersionUID = -5617117494284445988L;
  | 
  |     public NAPException() {
  |             super();
  |     }
  | 
  |     public NAPException(String message) {
  |             super(message); 
  |     }
  | 
  |     public NAPException(String message, Throwable cause) {
  |             super(message, cause);
  |     }
  | 
  |     public NAPException(Throwable cause) {
  |             super(cause);
  |     }
  | 
  | }

I am sure this thing can be improved extensively.

Later

Louis

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3919567#3919567

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3919567


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to