This project has retired. For details please refer to its Attic page.
TilesContextVariableResolverFactory xref
View Javadoc

1   /*
2    * $Id: TilesContextVariableResolverFactory.java 1049688 2010-12-15 20:15:41Z apetrelli $
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  package org.apache.tiles.mvel;
23  
24  import java.beans.PropertyDescriptor;
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  
28  import org.apache.tiles.context.TilesRequestContextHolder;
29  import org.apache.tiles.request.ApplicationContext;
30  import org.apache.tiles.request.Request;
31  import org.apache.tiles.request.reflect.CannotAccessMethodException;
32  import org.apache.tiles.util.CombinedBeanInfo;
33  import org.mvel2.integration.VariableResolver;
34  
35  /**
36   * Resolves {@link org.apache.tiles.request.Request} and
37   * {@link org.apache.tiles.request.ApplicationContext} properties as variables.
38   *
39   * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
40   * @since 2.2.0
41   */
42  public class TilesContextVariableResolverFactory extends
43          ReadOnlyVariableResolverFactory {
44  
45      /**
46       * Beaninfo about {@link org.apache.tiles.request.Request} and
47       * {@link org.apache.tiles.request.ApplicationContext}.
48       */
49      private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo(
50              Request.class, ApplicationContext.class);
51  
52      /**
53       * Constructor.
54       *
55       * @param requestHolder The Tiles request holder.
56       * @since 2.2.0
57       */
58      public TilesContextVariableResolverFactory(TilesRequestContextHolder requestHolder) {
59          super(requestHolder);
60      }
61  
62      /** {@inheritDoc} */
63      public boolean isTarget(String name) {
64          return requestBeanInfo.getMappedDescriptors(Request.class).containsKey(
65                  name)
66                  || requestBeanInfo.getMappedDescriptors(
67                          ApplicationContext.class).containsKey(name);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public VariableResolver createVariableResolver(String name) {
73          VariableResolver resolver = null;
74          PropertyDescriptor descriptor = requestBeanInfo.getMappedDescriptors(Request.class).get(name);
75          if (descriptor != null) {
76              resolver = new RequestVariableResolver(name, descriptor);
77          } else {
78              descriptor = requestBeanInfo.getMappedDescriptors(ApplicationContext.class).get(name);
79              if (descriptor != null) {
80                  resolver = new ApplicationVariableResolver(name, descriptor);
81              }
82          }
83          return resolver;
84      }
85  
86      /**
87       * Resolves a {@link org.apache.tiles.request.Request} property as a variable.
88       *
89       * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
90       * @since 2.2.0
91       */
92      private class RequestVariableResolver extends ReadOnlyVariableResolver {
93  
94          /**
95           * The property descriptor.
96           */
97          private PropertyDescriptor descriptor;
98  
99          /**
100          * Constructor.
101          *
102          * @param name The name of the property.
103          * @param descriptor The property descriptor.
104          * @since 2.2.0
105          */
106         public RequestVariableResolver(String name, PropertyDescriptor descriptor) {
107             super(name);
108             this.descriptor = descriptor;
109         }
110 
111         /** {@inheritDoc} */
112         @SuppressWarnings("rawtypes")
113         public Class getType() {
114             return descriptor.getPropertyType();
115         }
116 
117         /** {@inheritDoc} */
118         public Object getValue() {
119             Method method = descriptor.getReadMethod();
120             try {
121                 return method.invoke(requestHolder.getTilesRequestContext());
122             } catch (IllegalArgumentException e) {
123                 throw new CannotAccessMethodException(
124                         "Arguments are wrong for property '"
125                                 + descriptor.getName() + "'", e);
126             } catch (IllegalAccessException e) {
127                 throw new CannotAccessMethodException(
128                         "Cannot access getter method for property '"
129                                 + descriptor.getName() + "'", e);
130             } catch (InvocationTargetException e) {
131                 throw new CannotAccessMethodException(
132                         "The getter method for property '"
133                                 + descriptor.getName() + "' threw an exception",
134                         e);
135             }
136         }
137     }
138 
139     /**
140      * Resolves a {@link org.apache.tiles.request.ApplicationContext} property as a
141      * variable.
142      *
143      * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
144      * @since 2.2.0
145      */
146     private class ApplicationVariableResolver extends ReadOnlyVariableResolver {
147 
148         /**
149          * The property descriptor.
150          *
151          * @since 2.2.0
152          */
153         private PropertyDescriptor descriptor;
154 
155         /**
156          * Constructor.
157          *
158          * @param name The name of the property.
159          * @param descriptor The property descriptor.
160          * @since 2.2.0
161          */
162         public ApplicationVariableResolver(String name, PropertyDescriptor descriptor) {
163             super(name);
164             this.descriptor = descriptor;
165         }
166 
167         /** {@inheritDoc} */
168         @SuppressWarnings("rawtypes")
169         public Class getType() {
170             return descriptor.getPropertyType();
171         }
172 
173         /** {@inheritDoc} */
174         public Object getValue() {
175             Method method = descriptor.getReadMethod();
176             try {
177                 return method.invoke(requestHolder.getTilesRequestContext()
178                         .getApplicationContext());
179             } catch (IllegalArgumentException e) {
180                 throw new CannotAccessMethodException(
181                         "Arguments are wrong for property '"
182                                 + descriptor.getName() + "'", e);
183             } catch (IllegalAccessException e) {
184                 throw new CannotAccessMethodException(
185                         "Cannot access getter method for property '"
186                                 + descriptor.getName() + "'", e);
187             } catch (InvocationTargetException e) {
188                 throw new CannotAccessMethodException(
189                         "The getter method for property '"
190                                 + descriptor.getName() + "' threw an exception",
191                         e);
192             }
193         }
194     }
195 }