KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.context;
14
15 import info.magnolia.cms.beans.runtime.File;
16 import info.magnolia.cms.beans.runtime.MultipartForm;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.core.HierarchyManager;
19 import info.magnolia.cms.core.search.QueryManager;
20 import info.magnolia.cms.i18n.Messages;
21 import info.magnolia.cms.security.AccessManager;
22 import info.magnolia.cms.security.User;
23 import info.magnolia.cms.util.FactoryUtil;
24
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35 /**
36  * This class enables to get the current Request without passing the request around the world. A ThreadLocal is used to
37  * manage this.
38  * <p>
39  * In a later version this class should not depend on servlets. The core should use the context to get and set
40  * attributes instead of using the request or session object directly. Magnolia could run then in a neutral and
41  * configurable context.
42  * @author Philipp Bracher
43  * @version $Revision: 6989 $ ($Author: gjoseph $)
44  */

45
46 public class MgnlContext {
47
48     /**
49      * Logger
50      */

51     public static Logger log = LoggerFactory.getLogger(MgnlContext.class);
52
53     /**
54      * The thread local variable holding the current context
55      */

56     private static ThreadLocal JavaDoc localContext = new ThreadLocal JavaDoc();
57
58     /**
59      * Do not instantiate this class. The constructor must be public to use discovery
60      */

61     public MgnlContext() {
62     }
63
64     /**
65      * A short cut for the current user.
66      * @return the current user
67      */

68     public static User getUser() {
69         return getInstance().getUser();
70     }
71
72     /**
73      * Set the locale for the current context.
74      * @param locale
75      */

76     public static void setLocale(Locale JavaDoc locale) {
77         getInstance().setLocale(locale);
78     }
79
80     /**
81      * Get the contexts locale object
82      * @return the current locale
83      */

84     public static Locale JavaDoc getLocale() {
85         return getInstance().getLocale();
86     }
87
88     public static Messages getMessages() {
89         return getInstance().getMessages();
90     }
91
92     public static Messages getMessages(String JavaDoc basename) {
93         return getInstance().getMessages(basename);
94     }
95
96     /**
97      * Set current user
98      * @param user
99      */

100     public static void setUser(User user) {
101         getInstance().setUser(user);
102     }
103
104     /**
105      * Get hierarchy manager initialized for this user
106      * @param repositoryId
107      * @return hierarchy manager
108      */

109     public static HierarchyManager getHierarchyManager(String JavaDoc repositoryId) {
110         return getInstance().getHierarchyManager(repositoryId);
111     }
112
113     /**
114      * Get hierarchy manager initialized for this user
115      * @param repositoryId
116      * @param workspaceId
117      * @return hierarchy manager
118      */

119     public static HierarchyManager getHierarchyManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
120         return getInstance().getHierarchyManager(repositoryId, workspaceId);
121     }
122
123     /**
124      * Get access manager for the specified repository on default workspace
125      * @param repositoryId
126      * @return access manager
127      */

128     public static AccessManager getAccessManager(String JavaDoc repositoryId) {
129         return getInstance().getAccessManager(repositoryId);
130     }
131
132     /**
133      * Get access manager for the specified repository on the specified workspace
134      * @param repositoryId
135      * @param workspaceId
136      * @return access manager
137      */

138     public static AccessManager getAccessManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
139         return getInstance().getAccessManager(repositoryId, workspaceId);
140     }
141
142     /**
143      * Get QueryManager created for this user on the specified repository
144      * @param repositoryId
145      * @return query manager
146      */

147     public static QueryManager getQueryManager(String JavaDoc repositoryId) {
148         return getInstance().getQueryManager(repositoryId);
149     }
150
151     /**
152      * Get QueryManager created for this user on the specified repository and workspace
153      * @param repositoryId
154      * @param workspaceId
155      * @return query manager
156      */

157     public static QueryManager getQueryManager(String JavaDoc repositoryId, String JavaDoc workspaceId) {
158         return getInstance().getQueryManager(repositoryId, workspaceId);
159     }
160
161     /**
162      * Get currently active page
163      * @return content object
164      */

165     public static Content getActivePage() {
166
167         WebContext ctx = getWebContextIfExisting(getInstance());
168         if (ctx != null) {
169             return ctx.getActivePage();
170         }
171         return null;
172     }
173
174     /**
175      * Get aggregated file, its used from image templates to manipulate
176      * @return file object
177      */

178     public static File getFile() {
179         WebContext ctx = getWebContextIfExisting(getInstance());
180         if (ctx != null) {
181             return ctx.getFile();
182         }
183         return null;
184     }
185
186     /**
187      * Get form object assembled by <code>MultipartRequestFilter</code>
188      * @return multipart form object
189      */

190     public static MultipartForm getPostedForm() {
191         WebContext ctx = getWebContextIfExisting(getInstance());
192         if (ctx != null) {
193             return ctx.getPostedForm();
194         }
195         return null;
196     }
197
198     /**
199      * Get parameter value as string
200      * @param name
201      * @return parameter value
202      */

203     public static String JavaDoc getParameter(String JavaDoc name) {
204         WebContext ctx = getWebContextIfExisting(getInstance());
205         if (ctx != null) {
206             return ctx.getParameter(name);
207         }
208         return null;
209
210     }
211
212     /**
213      * Get parameter value as string
214      * @return parameter values
215      */

216     public static Map JavaDoc getParameters() {
217         WebContext ctx = getWebContextIfExisting(getInstance());
218         if (ctx != null) {
219             return ctx.getParameters();
220         }
221         return null;
222     }
223
224     /**
225      * @return the context path
226      */

227     public static String JavaDoc getContextPath() {
228         WebContext ctx = getWebContextIfExisting(getInstance());
229         if (ctx != null) {
230             return ctx.getContextPath();
231         }
232         return StringUtils.EMPTY;
233     }
234
235     /**
236      * Set attribute value, scope of the attribute is defined
237      * @param name is used as a key
238      * @param value
239      */

240     public static void setAttribute(String JavaDoc name, Object JavaDoc value) {
241         getInstance().setAttribute(name, value, Context.LOCAL_SCOPE);
242     }
243
244     /**
245      * Set attribute value, scope of the attribute is defined
246      * @param name is used as a key
247      * @param value
248      * @param scope , highest level of scope from which this attribute is visible
249      */

250     public static void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
251         getInstance().setAttribute(name, value, scope);
252     }
253
254     /**
255      * Get attribute value
256      * @param name to which value is associated to
257      * @return attribute value
258      */

259     public static Object JavaDoc getAttribute(String JavaDoc name) {
260         return getInstance().getAttribute(name);
261     }
262
263     /**
264      * Get the attribute from the specified scope
265      * @param name
266      * @param scope
267      * @return the value
268      */

269     public static Object JavaDoc getAttribute(String JavaDoc name, int scope) {
270         return getInstance().getAttribute(name, scope);
271     }
272
273     /**
274      * Set context implementation instance
275      * @param context
276      */

277     public static void setInstance(Context context) {
278         localContext.set(context);
279     }
280
281     /**
282      * Get the current context of this thread
283      * @return the context
284      */

285     public static Context getInstance() {
286         Context context = (Context) localContext.get();
287         // It should never fall back, We need to fix all false callers instead
288
if (context == null) {
289             log.error("MgnlContext is not initialized, This could happen if the request does not go through magnolia " +
290                     "default filters");
291             log.error("See : MgnlContext.setInstance(Context context)");
292             throw new IllegalStateException JavaDoc("MgnlContext is not set for this thread");
293         }
294         return context;
295     }
296
297     /**
298      * Used to check if an instance is already set since getInstance() will always return a context.
299      * @return true if an instance was set
300      */

301     public static boolean hasInstance() {
302         return localContext.get() != null;
303     }
304
305     /**
306      * Get magnolia system context, Note : this context have full rights over all repositories/ workspaces
307      * @return system context
308      */

309     public static Context getSystemContext() {
310         return (SystemContext) FactoryUtil.getSingleton(SystemContext.class);
311     }
312
313     /**
314      * Sets this context as a web context.
315      * @param request
316      */

317     public static void initAsWebContext(HttpServletRequest JavaDoc request) {
318         WebContext ctx = (WebContext) FactoryUtil.newInstance(WebContext.class);
319         ctx.init(request);
320         setInstance(ctx);
321     }
322
323     /**
324      * Returns the web context, also if eventually wrapped in a ContextDecorator.
325      * @param ctx
326      * @return WebContext instance or null if no web context is set
327      */

328     private static WebContext getWebContextIfExisting(Context ctx) {
329         if (ctx instanceof WebContext) {
330             return (WebContext) ctx;
331         }
332         else if (ctx instanceof ContextDecorator) {
333             return getWebContextIfExisting(((ContextDecorator) ctx).getWrappedContext());
334         }
335         return null;
336     }
337 }
Popular Tags