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

1   /*
2    * $Id: ReadOnlyVariableResolverFactory.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  package org.apache.tiles.mvel;
22  
23  import java.util.HashMap;
24  
25  import org.apache.tiles.context.TilesRequestContextHolder;
26  import org.mvel2.UnresolveablePropertyException;
27  import org.mvel2.integration.VariableResolver;
28  import org.mvel2.integration.impl.BaseVariableResolverFactory;
29  
30  /**
31   * A base variable resolver factory that is read-only.
32   *
33   * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
34   */
35  public abstract class ReadOnlyVariableResolverFactory extends
36          BaseVariableResolverFactory {
37  
38      /**
39       * The Tiles request holder.
40       */
41      protected TilesRequestContextHolder requestHolder;
42  
43      /**
44       * Constructor.
45       *
46       * @param requestHolder The Tiles request holder.
47       * @since 3..0
48       */
49      public ReadOnlyVariableResolverFactory(TilesRequestContextHolder requestHolder) {
50          this.requestHolder = requestHolder;
51          variableResolvers = new HashMap<String, VariableResolver>();
52      }
53  
54      /** {@inheritDoc} */
55      public VariableResolver createVariable(String name, Object value) {
56          if (nextFactory != null) {
57              return nextFactory.createVariable(name, value);
58          }
59          throw new UnsupportedOperationException("This variable resolver factory is read only");
60      }
61  
62      /** {@inheritDoc} */
63      public VariableResolver createVariable(String name, Object value,
64              Class<?> type) {
65          variableResolvers = new HashMap<String, VariableResolver>();
66          if (nextFactory != null) {
67              return nextFactory.createVariable(name, value, type);
68          }
69          throw new UnsupportedOperationException("This variable resolver factory is read only");
70      }
71  
72      /** {@inheritDoc} */
73      public boolean isResolveable(String name) {
74          return isTarget(name) || isNextResolveable(name);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public VariableResolver getVariableResolver(String name) {
80          if (isResolveable(name)) {
81              if (variableResolvers != null && variableResolvers.containsKey(name)) {
82                  return variableResolvers.get(name);
83              } else if (isTarget(name)) {
84                  VariableResolver variableResolver = createVariableResolver(name);
85                  variableResolvers.put(name, variableResolver);
86                  return variableResolver;
87              } else if (nextFactory != null) {
88                  return nextFactory.getVariableResolver(name);
89              }
90          }
91  
92          throw new UnresolveablePropertyException("unable to resolve variable '" + name + "'");
93      }
94  
95      /**
96       * Creates a variable resolver.
97       *
98       * @param name The name of the property.
99       * @return The variable resolver.
100      * @since 3.0.0
101      */
102     public abstract VariableResolver createVariableResolver(String name);
103 
104     /**
105      * Base variable resolver.
106      *
107      * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
108      * @since 3.0.0
109      */
110     public abstract static class ReadOnlyVariableResolver implements VariableResolver {
111 
112         /**
113          * The name of the property.
114          */
115         protected String name;
116 
117         /**
118          * Constructor.
119          *
120          * @param name The name of the property.
121          * @since 2.2.0
122          */
123         public ReadOnlyVariableResolver(String name) {
124             this.name = name;
125         }
126 
127         /** {@inheritDoc} */
128         public int getFlags() {
129             return 0;
130         }
131 
132         /** {@inheritDoc} */
133         public String getName() {
134             return name;
135         }
136 
137         /** {@inheritDoc} */
138         public void setStaticType(@SuppressWarnings("rawtypes") Class type) {
139             // Does nothing for the moment.
140         }
141 
142         /** {@inheritDoc} */
143         public void setValue(Object value) {
144             throw new UnsupportedOperationException("This resolver is read-only");
145         }
146     }
147 }