1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.context;
35
36 import info.magnolia.cms.i18n.Messages;
37 import info.magnolia.cms.i18n.MessagesManager;
38 import info.magnolia.cms.security.AccessManager;
39 import info.magnolia.cms.security.PermissionUtil;
40 import info.magnolia.cms.security.Security;
41 import info.magnolia.cms.security.User;
42
43 import java.io.Serializable;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.Locale;
48 import java.util.Map;
49 import java.util.Set;
50
51 import javax.jcr.LoginException;
52 import javax.jcr.RepositoryException;
53 import javax.jcr.Session;
54 import javax.security.auth.Subject;
55
56
57
58
59
60 public abstract class AbstractContext implements Context, Serializable {
61
62 @Override
63 public User getUser() {
64 return Security.getSystemUser();
65 }
66
67 @Override
68 public Subject getSubject() {
69 return Security.getSystemSubject();
70 }
71
72
73
74
75 protected Locale locale;
76
77 private AttributeStrategy attributeStrategy;
78
79 private JCRSessionStrategy repositoryStrategy;
80
81 public AttributeStrategy getAttributeStrategy() {
82 return attributeStrategy;
83 }
84
85 public void setAttributeStrategy(AttributeStrategy strategy) {
86 this.attributeStrategy = strategy;
87 }
88
89 public JCRSessionStrategy getRepositoryStrategy() {
90 return repositoryStrategy;
91 }
92
93 public void setRepositoryStrategy(JCRSessionStrategy strategy) {
94 this.repositoryStrategy = strategy;
95 }
96
97 @Override
98 public Object getAttribute(String name, int scope) {
99 return getAttributeStrategy().getAttribute(name, scope);
100 }
101
102 @Override
103 public Map<String, Object> getAttributes(int scope) {
104 return getAttributeStrategy().getAttributes(scope);
105 }
106
107 @Override
108 public void removeAttribute(String name, int scope) {
109 getAttributeStrategy().removeAttribute(name, scope);
110 }
111
112 @Override
113 public void setAttribute(String name, Object value, int scope) {
114 getAttributeStrategy().setAttribute(name, value, scope);
115 }
116
117 @Override
118 public AccessManager getAccessManager(String workspace) {
119 return PermissionUtil.getAccessManager(workspace, getSubject());
120 }
121
122 @Override
123 public Session getJCRSession(String workspaceName) throws LoginException, RepositoryException {
124 return getRepositoryStrategy().getSession(workspaceName);
125 }
126
127
128
129
130
131
132
133 @Override
134 public Object getAttribute(String name) {
135 Object value = this.getAttribute(name, Context.LOCAL_SCOPE);
136 if (null == value) {
137 value = this.getAttribute(name, Context.SESSION_SCOPE);
138 }
139 if (null == value) {
140 value = this.getAttribute(name, Context.APPLICATION_SCOPE);
141 }
142 return value;
143 }
144
145
146
147
148 @Override
149 public Map<String, Object> getAttributes() {
150 final Map<String, Object> map = new HashMap<String, Object>();
151 map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
152 map.putAll(this.getAttributes(Context.SESSION_SCOPE));
153 map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
154 return map;
155 }
156
157
158
159
160
161
162 @Override
163 public Locale getLocale() {
164 if (locale == null) {
165 final SystemContext sysctx = MgnlContext.getSystemContext();
166 if (this == sysctx) {
167
168 return null;
169 }
170 locale = sysctx.getLocale();
171 }
172 return locale;
173 }
174
175 @Override
176 public void setLocale(Locale locale) {
177 this.locale = locale;
178 }
179
180
181
182
183 @Override
184 public Messages getMessages() {
185 return getMessages(MessagesManager.DEFAULT_BASENAME);
186 }
187
188
189
190
191 @Override
192 public Messages getMessages(String basename) {
193 return MessagesManager.getMessages(basename, getLocale());
194 }
195
196
197
198
199
200
201 @Override
202 public Object get(Object key) {
203 return this.getAttribute(key.toString());
204 }
205
206
207
208
209 @Override
210 public Object put(Object key, Object value) {
211 this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
212 return value;
213 }
214
215
216
217
218 @Override
219 public void clear() {
220 for (String key : this.getAttributes().keySet()) {
221 this.removeAttribute(key, Context.LOCAL_SCOPE);
222 }
223 throw new UnsupportedOperationException("you can not clear a magnolia context");
224 }
225
226
227
228
229 @Override
230 public boolean containsValue(Object value) {
231 return this.getAttributes().containsValue(value);
232 }
233
234
235
236
237 @Override
238 public Set<Entry<String, Object>> entrySet() {
239 return this.getAttributes().entrySet();
240 }
241
242
243
244
245 @Override
246 public boolean isEmpty() {
247 return this.getAttributes().isEmpty();
248 }
249
250
251
252
253 @Override
254 public Set<String> keySet() {
255 return this.getAttributes().keySet();
256 }
257
258
259
260
261 @Override
262 public void putAll(Map map) {
263 for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
264 Entry entry = (Entry) iter.next();
265 this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
266 }
267 }
268
269
270
271
272 @Override
273 public Object remove(Object key) {
274 Object obj = this.getAttribute(key.toString());
275 this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
276 return obj;
277 }
278
279
280
281
282 @Override
283 public Collection<Object> values() {
284 return this.getAttributes().values();
285 }
286
287
288
289
290 @Override
291 public boolean containsKey(Object arg0) {
292 return this.getAttributes().containsKey(arg0);
293 }
294
295
296
297
298 @Override
299 public int size() {
300 return this.getAttributes().size();
301 }
302
303
304
305
306 @Override
307 public void release() {
308 getRepositoryStrategy().release();
309 }
310 }