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

1   /*
2    * $Id: ScopeELResolver.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.ArrayList;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.el.ELContext;
31  import javax.el.ELResolver;
32  
33  import org.apache.tiles.request.Request;
34  
35  /**
36   * Resolves beans in request, session and application scope.
37   *
38   * @version $Rev: 1049676 $ $Date: 2010-12-16 06:38:54 +1100 (Thu, 16 Dec 2010) $
39   * @since 2.2.1
40   */
41  public class ScopeELResolver extends ELResolver {
42  
43      /**
44       * The length of the suffix: "Scope".
45       */
46      private static final int SUFFIX_LENGTH = 5;
47  
48      /** {@inheritDoc} */
49      @Override
50      public Class<?> getCommonPropertyType(ELContext context, Object base) {
51          // only resolve at the root of the context
52          if (base != null) {
53              return null;
54          }
55  
56          return Map.class;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
62              Object base) {
63          if (base != null) {
64              List<FeatureDescriptor> retValue = Collections.emptyList();
65              return retValue.iterator();
66          }
67  
68          List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
69  
70          Request request = (Request) context
71                  .getContext(Request.class);
72          for (String scope : request.getAvailableScopes()) {
73              FeatureDescriptor descriptor = new FeatureDescriptor();
74              descriptor.setDisplayName(scope + "Scope");
75              descriptor.setExpert(false);
76              descriptor.setHidden(false);
77              descriptor.setName(scope + "Scope");
78              descriptor.setPreferred(true);
79              descriptor.setShortDescription("");
80              descriptor.setValue("type", Map.class);
81              descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE);
82              list.add(descriptor);
83          }
84  
85          return list.iterator();
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public Class<?> getType(ELContext context, Object base, Object property) {
91          if (base != null || property == null || !(property instanceof String)
92                  || !((String) property).endsWith("Scope")) {
93              return null;
94          }
95  
96          return Map.class;
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public Object getValue(ELContext context, Object base, Object property) {
102         if (base != null) {
103             return null;
104         }
105 
106         Object retValue = null;
107         String propertyString = (String) property;
108         if (property instanceof String && propertyString.endsWith("Scope")) {
109             Request request = (Request) context
110                     .getContext(Request.class);
111             retValue = request.getContext(propertyString.substring(0,
112                     propertyString.length() - SUFFIX_LENGTH));
113         }
114 
115         if (retValue != null) {
116             context.setPropertyResolved(true);
117         }
118 
119         return retValue;
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public boolean isReadOnly(ELContext context, Object base, Object property) {
125         if (context == null) {
126             throw new NullPointerException();
127         }
128 
129         return true;
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public void setValue(ELContext context, Object base, Object property,
135             Object value) {
136         // Does nothing for the moment.
137     }
138 }