View Javadoc

1   /*
2    * $Id: TilesContextELResolver.java 1049676 2010-12-15 19:38:54Z 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  package org.apache.tiles.el;
22  
23  import java.beans.FeatureDescriptor;
24  import java.util.Iterator;
25  
26  import javax.el.ELContext;
27  import javax.el.ELResolver;
28  
29  import org.apache.tiles.request.ApplicationContext;
30  import org.apache.tiles.request.Request;
31  import org.apache.tiles.util.CombinedBeanInfo;
32  
33  /**
34   * Resolves properties of {@link Request} and
35   * {@link ApplicationContext}.
36   *
37   * @version $Rev: 1049676 $ $Date: 2010-12-16 06:38:54 +1100 (Thu, 16 Dec 2010) $
38   * @since 2.2.1
39   */
40  public class TilesContextELResolver extends ELResolver {
41  
42      /**
43       * Internal bean resolver to resolve beans in any context.
44       */
45      private ELResolver beanElResolver;
46  
47      /**
48       * Constructor.
49       *
50       * @param beanElResolver The used bean resolver.
51       */
52      public TilesContextELResolver(ELResolver beanElResolver) {
53          this.beanElResolver = beanElResolver;
54      }
55  
56      /**
57       * The beaninfos about {@link Request} and {@link ApplicationContext}.
58       */
59      private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo(
60              Request.class, ApplicationContext.class);
61  
62      /** {@inheritDoc} */
63      @Override
64      public Class<?> getCommonPropertyType(ELContext context, Object base) {
65          // only resolve at the root of the context
66          if (base != null) {
67              return null;
68          }
69  
70          return String.class;
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
76              Object base) {
77          // only resolve at the root of the context
78          if (base != null) {
79              return null;
80          }
81  
82          return requestBeanInfo.getDescriptors().iterator();
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public Class<?> getType(ELContext context, Object base, Object property) {
88          // only resolve at the root of the context
89          if (base != null) {
90              return null;
91          }
92  
93          Class<?> retValue = null;
94          if (requestBeanInfo.getProperties(Request.class).contains(property)) {
95              Request request = (Request) context
96                      .getContext(Request.class);
97              retValue = beanElResolver.getType(context, request, property);
98          } else if (requestBeanInfo.getProperties(ApplicationContext.class).contains(property)) {
99              ApplicationContext applicationContext = (ApplicationContext) context
100                     .getContext(ApplicationContext.class);
101             retValue = beanElResolver.getType(context, applicationContext, property);
102         }
103 
104         if (retValue != null) {
105             context.setPropertyResolved(true);
106         }
107 
108         return retValue;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public Object getValue(ELContext context, Object base, Object property) {
114         // only resolve at the root of the context
115         if (base != null) {
116             return null;
117         }
118 
119         Object retValue = null;
120 
121         if (requestBeanInfo.getProperties(Request.class).contains(property)) {
122             Request request = (Request) context
123                     .getContext(Request.class);
124             retValue = beanElResolver.getValue(context, request, property);
125         } else if (requestBeanInfo.getProperties(ApplicationContext.class)
126                 .contains(property)) {
127             ApplicationContext applicationContext = (ApplicationContext) context
128                     .getContext(ApplicationContext.class);
129             retValue = beanElResolver.getValue(context, applicationContext, property);
130         }
131 
132         if (retValue != null) {
133             context.setPropertyResolved(true);
134         }
135 
136         return retValue;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public boolean isReadOnly(ELContext context, Object base, Object property) {
142         if (context == null) {
143             throw new NullPointerException();
144         }
145 
146         return true;
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     public void setValue(ELContext context, Object base, Object property,
152             Object value) {
153         // Does nothing for the moment.
154     }
155 }