View Javadoc

1   package org.apache.velocity.tools.view;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.velocity.util.introspection.AbstractChainableUberspector;
23  import org.apache.velocity.util.introspection.Info;
24  import org.apache.velocity.util.introspection.Introspector;
25  import org.apache.velocity.util.introspection.VelMethod;
26  import org.apache.velocity.util.introspection.VelPropertyGet;
27  import org.apache.velocity.util.introspection.VelPropertySet;
28  import org.apache.velocity.runtime.parser.node.AbstractExecutor;
29  import org.apache.velocity.runtime.parser.node.SetExecutor;
30  import org.apache.velocity.runtime.log.Log;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpSession;
34  import javax.servlet.ServletContext;
35  import java.lang.reflect.InvocationTargetException;
36  
37  /**
38   * <p>This custom uberspector allows getAttribute() and setAttribute() as standard
39   * getters and setters for the "request","session" and "application" keys.
40   * <p>It allows VTL statements like:
41   * <pre>
42   * #set($session.foo = 'youpi')
43   * session parameter 'foo' has value: $session.foo
44   * </pre>
45   * </p>
46   * <p>This uberspector requires Velocity 1.6+ ; to use it, you must specify <code>org.apache.velocity.tools.view.WebappUberspector</code>
47   * as the last uberspector to the <code>runtime.introspector.uberspect</code> property in you <code>velocity.properties</code> file.</p>
48   *
49   * <p>For instance:</p>
50   *
51   * <p><code>runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl,org.apache.velocity.tools.view.WebappUberspector</code></p>
52   *
53   * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
54   * @version $Id: WebappUberspector.java $  */
55  
56  public class WebappUberspector extends AbstractChainableUberspector
57  {
58  
59      /**
60       * Property getter
61       * @param obj
62       * @param identifier
63       * @param i
64       * @return A Velocity Getter Method.
65       * @throws Exception
66       */
67      public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
68              throws Exception
69      {
70          VelPropertyGet ret = super.getPropertyGet(obj,identifier,i);
71          if(ret == null)
72          {
73              Class claz = obj.getClass();
74              if(obj instanceof HttpServletRequest
75                  || obj instanceof HttpSession
76                  || obj instanceof ServletContext)
77              {
78                  AbstractExecutor executor = new GetAttributeExecutor(log, introspector, claz, identifier);
79                  ret = executor.isAlive() ? new VelGetterImpl(executor) : null;
80              }
81          }
82          return ret;
83      }
84  
85      /**
86       * init method
87       */
88      @Override
89      public void init()
90      {
91          try
92          {
93              super.init();
94          }
95          catch (RuntimeException re)
96          {
97              throw re;
98          }
99          catch (Exception e)
100         {
101             throw new RuntimeException(e);
102         }
103 
104         // we need our own introspector since the inner one is hidden by the Uberspect interface
105         introspector = new Introspector(log);
106     }
107 
108     /**
109      * Property setter
110      * @param obj
111      * @param identifier
112      * @param arg
113      * @param i
114      * @return A Velocity Setter method.
115      * @throws Exception
116      */
117     public VelPropertySet getPropertySet(Object obj, String identifier,
118                                          Object arg, Info i)
119             throws Exception
120     {
121         VelPropertySet ret = super.getPropertySet(obj,identifier,arg,i);
122         if(ret == null) {
123             Class claz = obj.getClass();
124             if(obj instanceof HttpServletRequest
125                 || obj instanceof HttpSession
126                 || obj instanceof ServletContext)
127             {
128                 SetExecutor executor = new SetAttributeExecutor(log, introspector, claz, arg, identifier);
129                 ret = executor.isAlive() ? new VelSetterImpl(executor) : null;
130             }
131         }
132         return ret;
133     }
134 
135 
136     /**
137      * Executor for getAttribute(name) method.
138      */
139     public class GetAttributeExecutor extends AbstractExecutor
140     {
141         private final Introspector introspector;
142 
143         // This is still threadsafe because this object is only read except in the C'tor.
144         private Object [] params;
145 
146         /**
147          * @param log
148          * @param introspector
149          * @param clazz
150          * @param property
151          */
152         public GetAttributeExecutor(final Log log, final Introspector introspector,
153                 final Class clazz, final String property)
154         {
155             this.log = log;
156             this.introspector = introspector;
157             this.params = new Object[] { property };
158 
159             discover(clazz);
160         }
161 
162         protected void discover(final Class clazz)
163         {
164             try
165             {
166                 setMethod(introspector.getMethod(clazz, "getAttribute", params));
167             }
168             /**
169              * pass through application level runtime exceptions
170              */
171             catch( RuntimeException e )
172             {
173                 throw e;
174             }
175             catch(Exception e)
176             {
177                 log.error("While looking for getAttribute('" + params[0] + "') method:", e);
178             }
179         }
180 
181         /**
182          * @see org.apache.velocity.runtime.parser.node.AbstractExecutor#execute(java.lang.Object)
183          */
184         public Object execute(final Object o)
185             throws IllegalAccessException, InvocationTargetException
186         {
187             return isAlive() ? getMethod().invoke(o, params) : null;
188         }
189     }
190 
191     /**
192      * Executor for setAttribute(name,value) method
193      */
194     public class SetAttributeExecutor extends SetExecutor
195     {
196         private final Introspector introspector;
197         private final String property;
198 
199         /**
200          * @param log
201          * @param introspector
202          * @param clazz
203          * @param arg
204          * @param property
205          */
206         public SetAttributeExecutor(final Log log, final Introspector introspector,
207                 final Class clazz, final Object arg, final String property)
208         {
209             this.log = log;
210             this.introspector = introspector;
211             this.property = property;
212 
213             discover(clazz, arg);
214         }
215 
216         /**
217          * @param clazz
218          * @param arg
219          */
220         protected void discover(final Class clazz, final Object arg)
221         {
222             Object [] params = new Object[] { property, arg };
223 
224             try
225             {
226                 setMethod(introspector.getMethod(clazz, "setAttribute", params));
227             }
228             /**
229              * pass through application level runtime exceptions
230              */
231             catch( RuntimeException e )
232             {
233                 throw e;
234             }
235             catch(Exception e)
236             {
237                 log.error("While looking for put('" + params[0] + "') method:", e);
238             }
239         }
240 
241         /**
242          * @see org.apache.velocity.runtime.parser.node.SetExecutor#execute(java.lang.Object, java.lang.Object)
243          */
244         public Object execute(final Object o, final Object value)
245             throws IllegalAccessException,  InvocationTargetException
246         {
247             Object [] params;
248 
249             if (isAlive())
250             {
251                 params = new Object [] { property, value };
252                 return getMethod().invoke(o, params);
253             }
254 
255             return null;
256         }
257     }
258 }