Project Tiles has retired. For details please refer to its Attic page.
TilesContextBeanELResolver xref
View Javadoc

1   /*
2    * $Id: TilesContextBeanELResolver.java 1291847 2012-02-21 15:09:30Z nlebas $
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.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.el.ELContext;
30  import javax.el.ELResolver;
31  
32  import org.apache.tiles.request.Request;
33  
34  /**
35   * Resolves beans in request, session and application scope.
36   *
37   * @version $Rev: 1291847 $ $Date: 2012-02-22 02:09:30 +1100 (Wed, 22 Feb 2012) $
38   * @since 2.2.1
39   */
40  public class TilesContextBeanELResolver extends ELResolver {
41  
42      /** {@inheritDoc} */
43      @Override
44      public Class<?> getCommonPropertyType(ELContext context, Object base) {
45          // only resolve at the root of the context
46          if (base != null) {
47              return null;
48          }
49  
50          return String.class;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
56              Object base) {
57          List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
58  
59          Request request = (Request) context
60                  .getContext(Request.class);
61          for (String scope : request.getAvailableScopes()) {
62              collectBeanInfo(request.getContext(scope), list);
63          }
64          return list.iterator();
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public Class<?> getType(ELContext context, Object base, Object property) {
70          if (base != null) {
71              return null;
72          }
73  
74          Object obj = findObjectByProperty(context, property);
75          if (obj != null) {
76              context.setPropertyResolved(true);
77              return obj.getClass();
78          }
79          return null;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public Object getValue(ELContext context, Object base, Object property) {
85          if (base != null) {
86              return null;
87          }
88  
89          Object retValue = findObjectByProperty(context, property);
90  
91          if (retValue != null) {
92              context.setPropertyResolved(true);
93          }
94  
95          return retValue;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public boolean isReadOnly(ELContext context, Object base, Object property) {
101         if (context == null) {
102             throw new NullPointerException();
103         }
104 
105         return true;
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public void setValue(ELContext context, Object base, Object property,
111             Object value) {
112         // Does nothing for the moment.
113     }
114 
115     /**
116      * Collects bean infos from a map's values and filling a list.
117      *
118      * @param map The map containing the bean to be inspected.
119      * @param list The list to fill.
120      * @since 2.2.1
121      */
122     protected void collectBeanInfo(Map<String, ? extends Object> map,
123             List<FeatureDescriptor> list) {
124         if (map == null || map.isEmpty()) {
125             return;
126         }
127 
128         for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
129             FeatureDescriptor descriptor = new FeatureDescriptor();
130             descriptor.setDisplayName(entry.getKey());
131             descriptor.setExpert(false);
132             descriptor.setHidden(false);
133             descriptor.setName(entry.getKey());
134             descriptor.setPreferred(true);
135             descriptor.setShortDescription("");
136             descriptor.setValue("type", String.class);
137             descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE);
138             list.add(descriptor);
139         }
140     }
141 
142     /**
143      * Finds an object in request, session or application scope, in this order.
144      *
145      * @param context The context to use.
146      * @param property The property used as an attribute name.
147      * @return The found bean, if it exists, or <code>null</code> otherwise.
148      * @since 2.2.1
149      */
150     protected Object findObjectByProperty(ELContext context, Object property) {
151         Object retValue = null;
152 
153         Request request = (Request) context
154                 .getContext(Request.class);
155 
156         String prop = property.toString();
157 
158         String[] scopes = request.getAvailableScopes().toArray(new String[0]);
159         int i = 0;
160         do {
161             retValue = getObject(request.getContext(scopes[i]), prop);
162             i++;
163         } while (retValue == null && i < scopes.length);
164 
165         return retValue;
166     }
167 
168     /**
169      * Returns an object from a map in a null-safe manner.
170      *
171      * @param map The map to use.
172      * @param property The property to use as a key.
173      * @return The object, if present, or <code>null</code> otherwise.
174      * @since 2.2.1
175      */
176     protected Object getObject(Map<String, ? extends Object> map,
177             String property) {
178         Object retValue = null;
179         if (map != null) {
180             retValue = map.get(property);
181         }
182         return retValue;
183     }
184 }