View Javadoc

1   /*
2    * $Id: AbstractListenerHandler.java,v 1.3 2007/09/13 08:38:13 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.facelets;
5   
6   import javax.faces.FacesException;
7   import javax.faces.component.UIComponent;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.rcfaces.core.internal.util.ClassLocator;
12  
13  import com.sun.facelets.FaceletContext;
14  import com.sun.facelets.tag.TagAttribute;
15  import com.sun.facelets.tag.TagConfig;
16  import com.sun.facelets.tag.TagException;
17  import com.sun.facelets.tag.TagHandler;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
22   * @version $Revision: 1.3 $ $Date: 2007/09/13 08:38:13 $
23   */
24  public abstract class AbstractListenerHandler extends TagHandler {
25      private static final String REVISION = "$Revision: 1.3 $";
26  
27      private static final Log LOG = LogFactory
28              .getLog(AbstractListenerHandler.class);
29  
30      private final TagAttribute typeParam;
31  
32      public AbstractListenerHandler(TagConfig config) {
33          super(config);
34          this.typeParam = this.getRequiredAttribute("type");
35      }
36  
37      public final void apply(FaceletContext facesContext, UIComponent parent) {
38          if (parent == null) {
39              throw new TagException(this.tag, "Parent UIComponent was null");
40          }
41  
42          // only process if the parent is new to the tree
43          if (parent.getParent() != null) {
44              return;
45          }
46  
47          String type = typeParam.getValue(facesContext);
48          if (type == null || type.length() == 0) {
49              throw new TagException(this.tag,
50                      "Type of listener is null or empty");
51          }
52  
53          Class listenerClass;
54          try {
55              listenerClass = ClassLocator.load(type, this, facesContext);
56  
57          } catch (ClassNotFoundException e) {
58              throw new FacesException("Can not get class '" + type + "'.", e);
59          }
60  
61          Object listener;
62          try {
63              listener = listenerClass.newInstance();
64  
65          } catch (Throwable th) {
66              throw new FacesException("Can not instanciate listener class '"
67                      + listenerClass + "'.", th);
68          }
69  
70          if (LOG.isDebugEnabled()) {
71              LOG.debug("Add " + getListenerName() + " listener '" + listener
72                      + "' to component '" + parent.getId() + "'.");
73          }
74  
75          addListener(listener, parent);
76      }
77  
78      protected abstract void addListener(Object listener, UIComponent component);
79  
80      protected abstract String getListenerName();
81  }