1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.access;
22
23 import java.util.Map;
24
25 import org.apache.tiles.NoSuchContainerException;
26 import org.apache.tiles.TilesContainer;
27 import org.apache.tiles.request.ApplicationContext;
28 import org.apache.tiles.request.Request;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38 public final class TilesAccess {
39
40
41
42
43 public static final String CURRENT_CONTAINER_ATTRIBUTE_NAME =
44 "org.apache.tiles.servlet.context.ServletTilesRequestContext.CURRENT_CONTAINER_KEY";
45
46
47
48
49 private TilesAccess() {
50 }
51
52
53
54
55
56 public static final String CONTAINER_ATTRIBUTE =
57 "org.apache.tiles.CONTAINER";
58
59
60
61
62
63
64
65
66 public static void setContainer(ApplicationContext context,
67 TilesContainer container) {
68 setContainer(context, container, CONTAINER_ATTRIBUTE);
69 }
70
71
72
73
74
75
76
77
78
79 public static void setContainer(ApplicationContext context,
80 TilesContainer container, String key) {
81 Logger log = LoggerFactory.getLogger(TilesAccess.class);
82 if (key == null) {
83 key = CONTAINER_ATTRIBUTE;
84 }
85
86 if (container == null) {
87 if (log.isInfoEnabled()) {
88 log.info("Removing TilesContext for context: " + context.getClass().getName());
89 }
90 context.getApplicationScope().remove(key);
91 } else {
92 if (log.isInfoEnabled()) {
93 log.info("Publishing TilesContext for context: " + context.getClass().getName());
94 }
95 context.getApplicationScope().put(key, container);
96 }
97 }
98
99
100
101
102
103
104
105
106 public static TilesContainer getContainer(ApplicationContext context) {
107 return getContainer(context, CONTAINER_ATTRIBUTE);
108 }
109
110
111
112
113
114
115
116
117
118 public static TilesContainer getContainer(ApplicationContext context,
119 String key) {
120 if (key == null) {
121 key = CONTAINER_ATTRIBUTE;
122 }
123
124 return (TilesContainer) context.getApplicationScope().get(key);
125 }
126
127
128
129
130
131
132
133
134 public static void setCurrentContainer(Request request,
135 String key) {
136 ApplicationContext applicationContext = request.getApplicationContext();
137 TilesContainer container = getContainer(applicationContext, key);
138 if (container != null) {
139 request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
140 } else {
141 throw new NoSuchContainerException("The container with the key '"
142 + key + "' cannot be found");
143 }
144 }
145
146
147
148
149
150
151
152
153 public static void setCurrentContainer(Request request,
154 TilesContainer container) {
155 if (container != null) {
156 request.getContext("request").put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
157 } else {
158 throw new NullPointerException("The container cannot be null");
159 }
160 }
161
162
163
164
165
166
167
168
169 public static TilesContainer getCurrentContainer(Request request) {
170 ApplicationContext context = request.getApplicationContext();
171 Map<String, Object> requestScope = request.getContext("request");
172 TilesContainer container = (TilesContainer) requestScope.get(CURRENT_CONTAINER_ATTRIBUTE_NAME);
173 if (container == null) {
174 container = getContainer(context);
175 requestScope.put(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
176 }
177
178 return container;
179 }
180 }