1
2
3
4 package org.rcfaces.core.internal.taglib;
5
6 import javax.el.ValueExpression;
7 import javax.faces.component.UIComponent;
8 import javax.faces.context.FacesContext;
9 import javax.faces.webapp.UIComponentClassicTagBase;
10 import javax.servlet.jsp.JspException;
11 import javax.servlet.jsp.tagext.TagSupport;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.rcfaces.core.internal.util.ClassLocator;
16
17
18
19
20
21
22 public abstract class AbstractListenerTag extends TagSupport {
23
24 private static final Log LOG = LogFactory.getLog(AbstractListenerTag.class);
25
26 private ValueExpression type = null;
27
28 public void setType(ValueExpression type) {
29 this.type = type;
30 }
31
32 public void release() {
33 this.type = null;
34 super.release();
35 }
36
37 public int doStartTag() throws JspException {
38 UIComponentClassicTagBase tag = UIComponentClassicTagBase
39 .getParentUIComponentClassicTagBase(pageContext);
40 if (tag == null) {
41
42 throw new JspException("Invalid parent tag !");
43 }
44
45 if (!tag.getCreated()) {
46 if (LOG.isDebugEnabled()) {
47 LOG.debug("Tag is not created !");
48 }
49 return SKIP_BODY;
50 }
51
52 UIComponent component = tag.getComponentInstance();
53 if (component == null) {
54 throw new JspException("Component is NULL");
55 }
56
57 if (type == null) {
58 throw new JspException("Invalid type parameter.");
59 }
60
61 if (LOG.isDebugEnabled()) {
62 LOG.debug("Prepare " + getListenerName() + " listener (type="
63 + type + ") for component '" + component.getId() + "'.");
64 }
65
66 FacesContext facesContext = FacesContext.getCurrentInstance();
67
68 String stype;
69 if (type.isLiteralText() == false) {
70 Object result = type.getValue(facesContext.getELContext());
71
72 if ((result instanceof String) == false) {
73 throw new JspException("Invalid value binding evaluation ! ("
74 + result + ")");
75 }
76
77 if (LOG.isDebugEnabled()) {
78 LOG.debug("Bound value=" + result + " expression=" + type);
79 }
80
81 stype = (String) result;
82
83 } else {
84 stype = type.getExpressionString();
85 }
86
87 Class listenerClass;
88 try {
89 listenerClass = ClassLocator.load(stype, this, facesContext);
90
91 } catch (ClassNotFoundException e) {
92 throw new JspException("Can not get class '" + stype + "'.", e);
93 }
94
95 Object listener;
96 try {
97 listener = listenerClass.newInstance();
98
99 } catch (Throwable th) {
100 throw new JspException("Can not instanciate listener class '"
101 + listenerClass + "'.", th);
102 }
103
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("Add " + getListenerName() + " listener '" + listener
106 + "' to component '" + component.getId() + "'.");
107 }
108
109 addListener(listener, component);
110
111 return SKIP_BODY;
112
113 }
114
115 protected abstract String getListenerName();
116
117 protected abstract void addListener(Object type, UIComponent component)
118 throws JspException;
119 }