2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.faces;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import javax.faces.FacesException;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.EvaluationException;
25 import javax.faces.el.VariableResolver;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29
30 /***
31 * <p>Shale-specific VariableResolver for evaluating JavaServer Faces
32 * value binding and method binding expressions. The following special
33 * variable names are recognized, and evaluated as indicated:</p>
34 * <ul>
35 * <li><strong>jndi</strong> - Returns the JNDI context at name
36 * <code>java:comp/env</code> (relative to the initial context
37 * supplied by the container.</li>
38 * </ul>
39 * <p>All other evaluations are delegated to the previous implementation
40 * that was passed to our constructor.</p>
41 *
42 * <p>Since 1.0.1, if the optional <code>shale-tiger.jar</code> file
43 * (containing the Shale Tiger Extensions) is available to this web
44 * application, the extra variable resolver implementation found there
45 * will be interposed between this instance and the previous
46 * implementation instance passed to our constructor.</p>
47 *
48 * $Id: ShaleVariableResolver.java 464373 2006-10-16 04:21:54Z rahul $
49 */
50 public class ShaleVariableResolver extends VariableResolver {
51
52
53
54
55
56 /***
57 * <p>Construct a new {@link ShaleVariableResolver} instance.</p>
58 *
59 * @param original Original resolver to delegate to.
60 */
61 public ShaleVariableResolver(VariableResolver original) {
62
63
64 VariableResolver delegate = null;
65 try {
66
67 ClassLoader cl = Thread.currentThread().getContextClassLoader();
68 Class clazz = cl.loadClass(TIGER_DELEGATE);
69
70 Constructor constructor = clazz.getConstructor(VARIABLE_RESOLVER_ARGS);
71 delegate =
72 (VariableResolver) constructor.newInstance(new Object[] { original });
73 } catch (ClassNotFoundException e) {
74 delegate = null;
75 } catch (RuntimeException e) {
76 throw e;
77 } catch (InvocationTargetException e) {
78 Throwable cause = e.getCause();
79 if (cause instanceof RuntimeException) {
80 throw (RuntimeException) cause;
81 } else {
82 throw new FacesException(cause);
83 }
84 } catch (Exception e) {
85 throw new FacesException(e);
86 }
87
88 if (delegate != null) {
89
90 this.original = delegate;
91 } else {
92
93 this.original = original;
94 }
95
96 }
97
98
99
100
101
102 /***
103 * <p>The original <code>VariableResolver</code> passed to our constructor.</p>
104 */
105 private VariableResolver original = null;
106
107
108
109
110
111 /***
112 * <p>Variable name to be resoved to our JNDI environment context.</p>
113 */
114 private static final String JNDI_VARIABLE_NAME = "jndi";
115
116
117 /***
118 * <p>Fully qualified class name of the variable resolver implementation
119 * in the Shale Tiger Extensions library.</p>
120 */
121 private static final String TIGER_DELEGATE =
122 "org.apache.shale.tiger.faces.VariableResolverImpl";
123
124
125 /***
126 * <p>Constructor signature for a VariableResolver that takes a
127 * VariableResolver as a parameter.</p>
128 */
129 private static final Class VARIABLE_RESOLVER_ARGS[] =
130 { VariableResolver.class };
131
132
133
134
135
136 /***
137 * <p>Resolve variable names known to this resolver; otherwise, delegate to
138 * the original resolver passed to our constructor.</p>
139 *
140 * @param context FacesContext for the current request
141 * @param name Variable name to be resolved
142 *
143 * @exception EvaluationException if the JNDI naming context
144 * throws a naming exception
145 */
146 public Object resolveVariable(FacesContext context, String name)
147 throws EvaluationException {
148
149 if (JNDI_VARIABLE_NAME.equals(name)) {
150 try {
151 InitialContext ic = new InitialContext();
152 return (Context) ic.lookup("java:comp/env");
153 } catch (NamingException e) {
154 throw new EvaluationException(e);
155 }
156 } else {
157 return original.resolveVariable(context, name);
158 }
159
160 }
161
162
163 }