2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // ------------------------------------------------------------- Constructor
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          // Determine if our optional resolver is present
64          VariableResolver delegate = null;
65          try {
66              // Attempt to load the Shale Tiger Extensions class
67              ClassLoader cl = Thread.currentThread().getContextClassLoader();
68              Class clazz = cl.loadClass(TIGER_DELEGATE);
69              // Instantiate a new instance, passing the original resolver
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              // Interpose our delegate into the chain
90              this.original = delegate;
91          } else {
92              // Use the provided instance directly
93              this.original = original;
94          }
95  
96      }
97  
98  
99      // ------------------------------------------------------ Instance Variables
100 
101 
102     /***
103      * <p>The original <code>VariableResolver</code> passed to our constructor.</p>
104      */
105     private VariableResolver original = null;
106 
107 
108     // ------------------------------------------------------ Manifest Constants
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     // ------------------------------------------------ VariableResolver Methods
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 }