KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > context > AbstractContext


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.context;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.HierarchyManager;
17 import info.magnolia.cms.core.search.QueryManager;
18 import info.magnolia.cms.i18n.Messages;
19 import info.magnolia.cms.i18n.MessagesManager;
20 import info.magnolia.cms.security.AccessManager;
21 import info.magnolia.cms.security.Security;
22 import info.magnolia.cms.security.User;
23
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * Default implementation of the Context interface
37  * @author Philipp Bracher
38  * @version $Revision: 6341 $ ($Author: philipp $)
39  */

40 public abstract class AbstractContext implements Context {
41
42     /**
43      * Logger
44      */

45     private static Logger log = LoggerFactory.getLogger(AbstractContext.class);
46
47     /**
48      * user attached to this context
49      */

50     protected User user;
51
52     /**
53      * The locale for this context
54      */

55     private Locale JavaDoc locale;
56
57     /**
58      * Get attribute value
59      * @param name to which value is associated to
60      * @return attribute value
61      */

62     public Object JavaDoc getAttribute(String JavaDoc name) {
63         Object JavaDoc value = this.getAttribute(name, Context.LOCAL_SCOPE);
64         if (null == value) {
65             value = this.getAttribute(name, Context.SESSION_SCOPE);
66         }
67         if (null == value) {
68             value = this.getAttribute(name, Context.APPLICATION_SCOPE);
69         }
70         return value;
71     }
72
73     /**
74      * Merge the scopes maps
75      */

76     public Map JavaDoc getAttributes() {
77         Map JavaDoc map = new HashMap JavaDoc();
78         map.putAll(this.getAttributes(Context.LOCAL_SCOPE));
79         map.putAll(this.getAttributes(Context.SESSION_SCOPE));
80         map.putAll(this.getAttributes(Context.APPLICATION_SCOPE));
81         return map;
82     }
83
84     /**
85      * Set user instance for this context
86      * @param user
87      */

88     public void setUser(User user) {
89         this.user = user;
90         setLocale(new Locale JavaDoc(user.getLanguage()));
91     }
92
93     /**
94      * Get user as initialized
95      * @return User
96      * @see info.magnolia.cms.security.User
97      */

98     public User getUser() {
99         if (this.user == null) {
100             log.debug("JAAS Subject is null, returning Anonymous user");
101             this.user = Security.getUserManager().getAnonymousUser();
102         }
103         return this.user;
104     }
105
106     /**
107      * If not yet set try to get the locale of the user. Else use the locale of the system context
108      * @see Context#getLocale()
109      */

110     public Locale JavaDoc getLocale() {
111         if (locale == null) {
112             User user = this.getUser();
113             if (user != null) {
114                 locale = new Locale JavaDoc(user.getLanguage());
115             }
116             if (locale == null) {
117                 locale = MgnlContext.getSystemContext().getLocale();
118             }
119         }
120
121         return locale;
122     }
123
124     public void setLocale(Locale JavaDoc locale) {
125         this.locale = locale;
126     }
127
128     public Messages getMessages() {
129         return getMessages(MessagesManager.DEFAULT_BASENAME);
130     }
131
132     public Messages getMessages(String JavaDoc basename) {
133         return MessagesManager.getMessages(basename, getLocale());
134     }
135
136     public HierarchyManager getHierarchyManager(String JavaDoc repositoryId) {
137         return this.getHierarchyManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId));
138     }
139
140     public AccessManager getAccessManager(String JavaDoc repositoryId) {
141         return this.getAccessManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId));
142     }
143
144     public QueryManager getQueryManager(String JavaDoc repositoryId) {
145         return this.getQueryManager(repositoryId, ContentRepository.getDefaultWorkspace(repositoryId));
146     }
147
148     /**
149      * Map implemenation
150      */

151     public Object JavaDoc get(Object JavaDoc key) {
152         return this.getAttribute(key.toString());
153     }
154
155     /**
156      * Map implementation
157      */

158     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
159         this.setAttribute(key.toString(), value, Context.LOCAL_SCOPE);
160         return value;
161     }
162
163     /**
164      * Map implementation
165      */

166     public void clear() {
167         for (Iterator JavaDoc iter = this.getAttributes().keySet().iterator(); iter.hasNext();) {
168             String JavaDoc key = (String JavaDoc) iter.next();
169             this.removeAttribute(key, Context.LOCAL_SCOPE);
170
171         }
172         throw new UnsupportedOperationException JavaDoc("you can not clear a magnolia context");
173     }
174
175     /**
176      * Map implementation. This implementation is very slow
177      */

178     public boolean containsValue(Object JavaDoc value) {
179         return this.getAttributes().containsValue(value);
180     }
181
182     /**
183      * Map implementation
184      */

185     public Set JavaDoc entrySet() {
186         return this.getAttributes().entrySet();
187     }
188
189     /**
190      * Map implementation
191      */

192     public boolean isEmpty() {
193         return this.getAttributes().isEmpty();
194     }
195
196     /**
197      * Map implementation
198      */

199     public Set JavaDoc keySet() {
200         return this.getAttributes().keySet();
201     }
202
203     /**
204      * Map implementation
205      */

206     public void putAll(Map JavaDoc map) {
207         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
208             Entry entry = (Entry) iter.next();
209             this.setAttribute(entry.getKey().toString(), entry.getValue(), Context.LOCAL_SCOPE);
210         }
211     }
212
213     /**
214      * Map implementation
215      */

216     public Object JavaDoc remove(Object JavaDoc key) {
217         Object JavaDoc obj = this.getAttribute(key.toString());
218         this.removeAttribute(key.toString(), Context.LOCAL_SCOPE);
219         return obj;
220     }
221
222     /**
223      * Map implementation
224      */

225     public Collection JavaDoc values() {
226         return this.getAttributes().values();
227     }
228
229     /**
230      * Map implementation
231      */

232     public boolean containsKey(Object JavaDoc arg0) {
233         return this.getAttributes().containsKey(arg0);
234     }
235
236     /**
237      * Map implementation
238      */

239     public int size() {
240         return this.getAttributes().size();
241     }
242
243 }
244
Popular Tags