1 5 package org.exoplatform.faces.core.event; 6 7 import java.util.* ; 8 9 import javax.faces.component.UIComponent; 10 import javax.faces.context.FacesContext; 11 import javax.faces.context.ExternalContext; 12 import javax.faces.event.AbortProcessingException; 13 import javax.faces.event.ActionEvent; 14 import javax.faces.event.ActionListener; 15 import org.exoplatform.Constants; 16 import org.exoplatform.faces.context.PortletExternalContext; 17 import org.exoplatform.faces.core.Util; 18 import org.exoplatform.faces.core.component.InformationProvider; 19 20 26 abstract public class ExoActionListener implements ActionListener { 27 private static List exceptionHandlers_ ; 28 static { 29 exceptionHandlers_ = new ArrayList(10); 30 exceptionHandlers_.add(new UniqueObjectExceptionHandler()) ; 31 exceptionHandlers_.add(new MessageExceptionHandler()) ; 32 exceptionHandlers_.add(new PermissionExceptionHandler()) ; 33 exceptionHandlers_.add(new FatalExceptionHandler()) ; 34 } 35 36 private List interceptors_ ; 37 private String action_ ; 38 39 public ExoActionListener() { } 40 41 public void init() { } 42 43 public ExoActionListener setActionToListen(String action) { 44 action_ = action ; 45 return this ; 46 } 47 48 public String getActionName() { return action_ ; } 49 50 final public void processAction(ActionEvent action) throws AbortProcessingException { 51 throw new AbortProcessingException("Use processAction(ExoActionEvent action)") ; 52 } 53 54 public boolean canHandleAction(String action) { 55 return action_.equals(action) ; 56 } 57 58 final public void processAction(ExoActionEvent action) throws AbortProcessingException { 59 try { 60 if(interceptors_ != null) { 62 for(int i = 0; i < interceptors_.size(); i++) { 63 ActionInterceptor interceptor = (ActionInterceptor) interceptors_.get(i) ; 64 interceptor.preExecute(action) ; 65 } 66 } 67 execute(action) ; 68 if(interceptors_ != null) { 70 for(int i = 0; i < interceptors_.size(); i++) { 71 ActionInterceptor interceptor = (ActionInterceptor) interceptors_.get(i) ; 72 interceptor.preExecute(action) ; 73 } 74 } 75 } catch (Exception ex) { 76 ExceptionHandler handler = null; 77 for (int i = 0; i < exceptionHandlers_.size(); i++) { 78 ExceptionHandler eh = (ExceptionHandler) exceptionHandlers_.get(i) ; 79 if(eh.canHandleError(ex)) { 80 handler = eh ; 81 break ; 82 } 83 } 84 handler.handle(action, ex) ; 85 } 86 postExecute(action) ; 87 } 88 89 protected void postExecute(ExoActionEvent action) { 90 FacesContext.getCurrentInstance().renderResponse() ; 91 } 92 93 abstract public void execute(ExoActionEvent event) throws Exception ; 94 95 public ExoActionListener addInterceptor(ActionInterceptor interceptor) { 96 if(interceptors_ == null) { 97 interceptors_ = new ArrayList(3) ; 98 } 99 interceptors_.add(interceptor) ; 100 return this ; 101 } 102 103 protected String getResourceBaseName() { 104 String baseName = getClass().getName() ; 105 int idx = baseName.lastIndexOf('.') ; 106 baseName = baseName.substring(idx + 1, baseName.length()) ; 107 baseName = baseName.replace('$', '.') ; 108 return baseName ; 109 } 110 111 public ResourceBundle getApplicationResourceBundle() { 112 ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext() ; 113 if(eContext instanceof PortletExternalContext) { 114 PortletExternalContext impl = (PortletExternalContext) eContext ; 115 return impl.getApplicationResourceBundle() ; 116 } 117 return (ResourceBundle)eContext.getRequestMap().get(Constants.APPLICATION_RESOURCE) ; 118 } 119 120 protected InformationProvider findInformationProvider(UIComponent src) { 121 return Util.findInformationProvider(src) ; 122 } 123 } 124 | Popular Tags |