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