1 /*
2 * $Id: TilesContextBeanVariableResolverFactory.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
22 package org.apache.tiles.mvel;
23
24 import org.apache.tiles.context.TilesRequestContextHolder;
25 import org.apache.tiles.request.Request;
26 import org.mvel2.integration.VariableResolver;
27
28 /**
29 * Resolves beans stored in request, session and application scopes.
30 *
31 * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
32 * @since 2.2.0
33 */
34 public class TilesContextBeanVariableResolverFactory extends
35 ReadOnlyVariableResolverFactory {
36
37 /**
38 * Constructor.
39 *
40 * @param requestHolder The Tiles request holder.
41 * @since 2.2.0
42 */
43 public TilesContextBeanVariableResolverFactory(TilesRequestContextHolder requestHolder) {
44 super(requestHolder);
45 }
46
47 /** {@inheritDoc} */
48 @Override
49 public VariableResolver createVariableResolver(String name) {
50 return new TilesContextBeanVariableResolver(name);
51 }
52
53 /** {@inheritDoc} */
54 public boolean isTarget(String name) {
55 Request request = requestHolder.getTilesRequestContext();
56 for (String scope : request.getAvailableScopes()) {
57 if (request.getContext(scope).containsKey(name)) {
58 return true;
59 }
60 }
61 return false;
62 }
63
64 /**
65 * Resolves a single attribute stored in request, session or application scope.
66 *
67 * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $
68 * @since 2.2.0
69 */
70 private class TilesContextBeanVariableResolver extends ReadOnlyVariableResolver {
71
72 /**
73 * Constructor.
74 *
75 * @param name The name of the attribute.
76 * @since 2.2.0
77 */
78 public TilesContextBeanVariableResolver(String name) {
79 super(name);
80 }
81
82 /** {@inheritDoc} */
83 @SuppressWarnings("rawtypes")
84 public Class getType() {
85 Object value = getValue();
86 if (value != null) {
87 return value.getClass();
88 }
89 return Object.class;
90 }
91
92 /** {@inheritDoc} */
93 public Object getValue() {
94 Request request = requestHolder.getTilesRequestContext();
95 for (String scope : request.getAvailableScopes()) {
96 Object value = request.getContext(scope).get(name);
97 if (value != null) {
98 return value;
99 }
100 }
101 return null;
102 }
103 }
104 }