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

1   /*
2    * $Id: TilesContextPropertyAccessorDelegateFactory.java 1049696 2010-12-15 20:30:10Z 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.ognl;
23  
24  import ognl.PropertyAccessor;
25  
26  import org.apache.tiles.request.ApplicationContext;
27  import org.apache.tiles.request.Request;
28  import org.apache.tiles.util.CombinedBeanInfo;
29  
30  /**
31   * Decides the appropriate {@link PropertyAccessor} for the given property name
32   * and {@link Request}.
33   *
34   * @version $Rev: 1049696 $ $Date: 2010-12-16 07:30:10 +1100 (Thu, 16 Dec 2010) $
35   * @since 2.2.0
36   */
37  public class TilesContextPropertyAccessorDelegateFactory implements
38          PropertyAccessorDelegateFactory<Request> {
39  
40      /**
41       * The plain object property accessor, to be used directly for
42       * {@link Request}.
43       */
44      private PropertyAccessor objectPropertyAccessor;
45  
46      /**
47       * The application context property accessor.
48       */
49      private PropertyAccessor applicationContextPropertyAccessor;
50  
51      /**
52       * The request scope property accessor.
53       */
54      private PropertyAccessor anyScopePropertyAccessor;
55  
56      /**
57       * The session scope property accessor.
58       */
59      private PropertyAccessor scopePropertyAccessor;
60  
61      /**
62       * The bean info of {@link Request} and
63       * {@link org.apache.tiles.request.ApplicationContext}.
64       */
65      private CombinedBeanInfo beanInfo;
66  
67      /**
68       * Constructor.
69       *
70       * @param objectPropertyAccessor The plain object property accessor, to be
71       * used directly for {@link Request}.
72       * @param applicationContextPropertyAccessor The application context
73       * property accessor.
74       * @param anyScopePropertyAccessor The request scope property accessor.
75       * @param scopePropertyAccessor The session scope property accessor.
76       * @since 2.2.0
77       */
78      public TilesContextPropertyAccessorDelegateFactory(
79              PropertyAccessor objectPropertyAccessor,
80              PropertyAccessor applicationContextPropertyAccessor,
81              PropertyAccessor anyScopePropertyAccessor,
82              PropertyAccessor scopePropertyAccessor) {
83          beanInfo = new CombinedBeanInfo(Request.class, ApplicationContext.class);
84          this.objectPropertyAccessor = objectPropertyAccessor;
85          this.applicationContextPropertyAccessor = applicationContextPropertyAccessor;
86          this.anyScopePropertyAccessor = anyScopePropertyAccessor;
87          this.scopePropertyAccessor = scopePropertyAccessor;
88      }
89  
90      /** {@inheritDoc} */
91      public PropertyAccessor getPropertyAccessor(String propertyName,
92              Request request) {
93          PropertyAccessor retValue;
94          if (propertyName.endsWith("Scope")) {
95              String scopeName = propertyName.substring(0, propertyName.length()
96                      - ScopePropertyAccessor.SCOPE_SUFFIX_LENGTH);
97              if (request.getContext(scopeName) != null) {
98                  return scopePropertyAccessor;
99              }
100         }
101         if (beanInfo.getMappedDescriptors(Request.class)
102                 .containsKey(propertyName)) {
103             retValue = objectPropertyAccessor;
104         } else if (beanInfo.getMappedDescriptors(ApplicationContext.class)
105                 .containsKey(propertyName)) {
106             retValue = applicationContextPropertyAccessor;
107         } else {
108             return anyScopePropertyAccessor;
109         }
110         return retValue;
111     }
112 }