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