KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > jxpath > JXPathCocoonContexts


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.jxpath;
17
18 import org.apache.avalon.framework.component.Component;
19 import org.apache.avalon.framework.context.Contextualizable;
20 import org.apache.avalon.framework.thread.ThreadSafe;
21
22 import org.apache.cocoon.components.ContextHelper;
23 import org.apache.cocoon.environment.Context;
24 import org.apache.cocoon.environment.Cookie;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.environment.Session;
28
29 import org.apache.commons.jxpath.JXPathContext;
30 import org.apache.commons.jxpath.JXPathContextFactory;
31 import org.apache.commons.jxpath.JXPathIntrospector;
32 import org.apache.commons.jxpath.servlet.Constants;
33 import org.apache.commons.jxpath.servlet.KeywordVariables;
34
35 import java.io.InputStream JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.security.Principal JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * Component that allocate and cache JXPathContexts bound to VariableContext,
45  * Cocoon Request, Cocoon Session and Cocoon Context.
46  *
47  * <p>
48  * If you need to limit the attibute lookup to just one scope, you can use the
49  * pre-definded variables "request", "session" and "application".
50  * For example, the expression "$session/foo" extracts the value of the
51  * session attribute named "foo".</p>
52  *
53  * <p>
54  * Following are some implementation details.
55  * There is a separate JXPathContext for each of the four scopes. These contexts are chained
56  * according to the nesting of the scopes. So, the parent of the "variable"
57  * JXPathContext is a "request" JXPathContext, whose parent is a "session"
58  * JXPathContext (that is if there is a session), whose parent is an "application"
59  * context.</p>
60  *
61  * <p>
62  * Since JXPath chains lookups for variables and extension functions, variables
63  * and extension function declared in the outer scopes are also available in
64  * the inner scopes.</p>
65  *
66  * <p>
67  * The "session" variable will be undefined if there is no session for this servlet.
68  * JXPath does not automatically create sessions.</p>
69  *
70  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
71  * @version $Id: JXPathCocoonContexts.java 202262 2005-06-28 18:02:55Z vgritsenko $
72  */

73 public final class JXPathCocoonContexts implements Component, Contextualizable, ThreadSafe {
74
75     public static final String JavaDoc ROLE = JXPathCocoonContexts.class.getName();
76
77     private static final String JavaDoc VARCONTEXT = Constants.JXPATH_CONTEXT + "/VAR";
78
79     private static JXPathContextFactory factory;
80     private org.apache.avalon.framework.context.Context context;
81
82     static {
83         factory = JXPathContextFactory.newInstance();
84         JXPathIntrospector.registerDynamicClass(RequestProxy.class, CocoonRequestHandler.class);
85         JXPathIntrospector.registerDynamicClass(SessionProxy.class, CocoonSessionHandler.class);
86         JXPathIntrospector.registerDynamicClass(ContextProxy.class, CocoonContextHandler.class);
87     }
88
89     public void contextualize(org.apache.avalon.framework.context.Context context) {
90         this.context = context;
91     }
92
93     public final JXPathContext getVariableContext() {
94         final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
95
96         Request request = ObjectModelHelper.getRequest(objectModel);
97         JXPathContext context = (JXPathContext) request.getAttribute(VARCONTEXT);
98         if (context == null) {
99             context = factory.newContext(getRequestContext(), null);
100             request.setAttribute(VARCONTEXT, context);
101         }
102         return context;
103     }
104
105     /**
106      * Returns a JXPathContext bound to the "request" scope.
107      * Caches that context within the request itself.
108      */

109     public final JXPathContext getRequestContext() {
110         final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
111
112         Request request = ObjectModelHelper.getRequest(objectModel);
113         JXPathContext context = (JXPathContext) request.getAttribute(Constants.JXPATH_CONTEXT);
114         if (context == null) {
115             Context envContext = ObjectModelHelper.getContext(objectModel);
116             JXPathContext parentContext = null;
117
118             Session session = request.getSession(false);
119             if (session != null) {
120                 parentContext = getSessionContext(session, envContext);
121             } else {
122                 parentContext = getApplicationContext(envContext);
123             }
124
125             request = new RequestProxy(request);
126             context = factory.newContext(parentContext, request);
127             context.setVariables(new KeywordVariables(Constants.REQUEST_SCOPE, request));
128             request.setAttribute(Constants.JXPATH_CONTEXT, context);
129         }
130         return context;
131     }
132
133     /**
134      * Returns a JXPathContext bound to the "session" scope.
135      * Caches that context within the session itself.
136      */

137     public final JXPathContext getSessionContext(Session session, Context envContext) {
138         JXPathContext context = (JXPathContext) session.getAttribute(Constants.JXPATH_CONTEXT);
139         if (context == null) {
140             JXPathContext parentContext = getApplicationContext(envContext);
141             session = new SessionProxy(session);
142             context = factory.newContext(parentContext, session);
143             context.setVariables(new KeywordVariables(Constants.SESSION_SCOPE, session));
144             session.setAttribute(Constants.JXPATH_CONTEXT, context);
145         }
146         return context;
147     }
148
149     /**
150      * Returns a JXPathContext bound to the "application" scope. Caches that context
151      * within the servlet context itself.
152      */

153     public final JXPathContext getApplicationContext(Context envContext) {
154         JXPathContext context = (JXPathContext) envContext.getAttribute(Constants.JXPATH_CONTEXT);
155         if (context == null) {
156             envContext = new ContextProxy(envContext);
157             context = factory.newContext(null, envContext);
158             context.setVariables(new KeywordVariables(Constants.APPLICATION_SCOPE, envContext));
159             envContext.setAttribute(Constants.JXPATH_CONTEXT, context);
160         }
161         return context;
162     }
163
164     public static final class RequestProxy implements Request {
165         private Request delegate;
166
167         public RequestProxy (Request delegate) {
168             this.delegate = delegate;
169         }
170
171         public Object JavaDoc get(String JavaDoc name) {
172             return this.delegate.get(name);
173         }
174
175         public Object JavaDoc getAttribute(String JavaDoc name) {
176             return this.delegate.getAttribute(name);
177         }
178
179         public Enumeration JavaDoc getAttributeNames() {
180             return this.delegate.getAttributeNames();
181         }
182
183         public String JavaDoc getCharacterEncoding() {
184             return this.delegate.getCharacterEncoding();
185         }
186
187         public void setCharacterEncoding(String JavaDoc enc)
188         throws java.io.UnsupportedEncodingException JavaDoc {
189             this.delegate.setCharacterEncoding(enc);
190         }
191
192         public int getContentLength() {
193             return this.delegate.getContentLength();
194         }
195
196         public String JavaDoc getContentType() {
197             return this.delegate.getContentType();
198         }
199
200         public String JavaDoc getParameter(String JavaDoc name) {
201             return this.delegate.getParameter(name);
202         }
203
204         public Enumeration JavaDoc getParameterNames() {
205             return this.delegate.getParameterNames();
206         }
207
208         public String JavaDoc[] getParameterValues(String JavaDoc name) {
209             return this.delegate.getParameterValues(name);
210         }
211
212         public String JavaDoc getProtocol() {
213             return this.delegate.getProtocol();
214         }
215
216         public String JavaDoc getScheme() {
217             return this.delegate.getScheme();
218         }
219
220         public String JavaDoc getServerName() {
221             return this.delegate.getServerName();
222         }
223
224         public int getServerPort() {
225             return this.delegate.getServerPort();
226         }
227
228         public String JavaDoc getRemoteAddr() {
229             return this.delegate.getRemoteAddr();
230         }
231
232         public String JavaDoc getRemoteHost() {
233             return this.delegate.getRemoteHost();
234         }
235
236         public void setAttribute(String JavaDoc name, Object JavaDoc o) {
237             this.delegate.setAttribute(name, o);
238         }
239
240         public void removeAttribute(String JavaDoc name) {
241             this.delegate.removeAttribute(name);
242         }
243
244         public Locale JavaDoc getLocale() {
245             return this.delegate.getLocale();
246         }
247
248         public Enumeration JavaDoc getLocales() {
249             return this.delegate.getLocales();
250         }
251
252         public boolean isSecure() {
253             return this.delegate.isSecure();
254         }
255
256         public Cookie[] getCookies() {
257             return this.delegate.getCookies();
258         }
259
260         public Map JavaDoc getCookieMap() {
261             return this.delegate.getCookieMap();
262         }
263
264         public long getDateHeader(String JavaDoc name) {
265             return this.delegate.getDateHeader(name);
266         }
267
268         public String JavaDoc getHeader(String JavaDoc name) {
269             return this.delegate.getHeader(name);
270         }
271
272         public Enumeration JavaDoc getHeaders(String JavaDoc name) {
273             return this.delegate.getHeaders(name);
274         }
275
276         public Enumeration JavaDoc getHeaderNames() {
277             return this.delegate.getHeaderNames();
278         }
279
280         public String JavaDoc getMethod() {
281             return this.delegate.getMethod();
282         }
283
284         public String JavaDoc getPathInfo() {
285             return this.delegate.getPathInfo();
286         }
287
288         public String JavaDoc getPathTranslated() {
289             return this.delegate.getPathTranslated();
290         }
291
292         public String JavaDoc getContextPath() {
293             return this.delegate.getContextPath();
294         }
295
296         public String JavaDoc getQueryString() {
297             return this.delegate.getQueryString();
298         }
299
300         public String JavaDoc getRemoteUser() {
301             return this.delegate.getRemoteUser();
302         }
303
304         public String JavaDoc getRequestedSessionId() {
305             return this.delegate.getRequestedSessionId();
306         }
307
308         public String JavaDoc getRequestURI() {
309             return this.delegate.getRequestURI();
310         }
311
312         public String JavaDoc getSitemapURI() {
313             return this.delegate.getSitemapURI();
314         }
315
316         public String JavaDoc getSitemapURIPrefix() {
317             return this.delegate.getSitemapURIPrefix();
318         }
319
320         public String JavaDoc getServletPath() {
321             return this.delegate.getServletPath();
322         }
323
324         public Session getSession(boolean create) {
325             return this.delegate.getSession(create);
326         }
327
328         public Session getSession() {
329             return this.delegate.getSession();
330         }
331
332         public boolean isRequestedSessionIdValid() {
333             return this.delegate.isRequestedSessionIdValid();
334         }
335
336         public boolean isRequestedSessionIdFromCookie() {
337             return this.delegate.isRequestedSessionIdFromCookie();
338         }
339
340         public boolean isRequestedSessionIdFromURL() {
341             return this.delegate.isRequestedSessionIdFromURL();
342         }
343
344         public Principal JavaDoc getUserPrincipal() {
345             return this.delegate.getUserPrincipal();
346         }
347
348         public boolean isUserInRole(String JavaDoc role) {
349             return this.delegate.isUserInRole(role);
350         }
351
352         public String JavaDoc getAuthType() {
353             return this.delegate.getAuthType();
354         }
355     }
356
357     public class SessionProxy implements Session {
358         private Session delegate;
359
360         public SessionProxy(Session delegate) {
361             this.delegate = delegate;
362         }
363
364         public long getCreationTime() {
365             return this.delegate.getCreationTime();
366         }
367
368         public String JavaDoc getId() {
369             return this.delegate.getId();
370         }
371
372         public long getLastAccessedTime() {
373             return this.delegate.getLastAccessedTime();
374         }
375
376         public void setMaxInactiveInterval(int interval) {
377             this.delegate.setMaxInactiveInterval(interval);
378         }
379
380         public int getMaxInactiveInterval() {
381             return this.delegate.getMaxInactiveInterval();
382         }
383
384         public Object JavaDoc getAttribute(String JavaDoc name) {
385             return this.delegate.getAttribute(name);
386         }
387
388         public Enumeration JavaDoc getAttributeNames() {
389             return this.delegate.getAttributeNames();
390         }
391
392         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
393             this.delegate.setAttribute(name, value);
394         }
395
396         public void removeAttribute(String JavaDoc name) {
397             this.delegate.removeAttribute(name);
398         }
399
400         public void invalidate() {
401             this.delegate.invalidate();
402         }
403
404         public boolean isNew() {
405             return this.delegate.isNew();
406         }
407     }
408
409     public class ContextProxy implements Context {
410         private Context delegate;
411
412         public ContextProxy(org.apache.cocoon.environment.Context delegate) {
413             this.delegate = delegate;
414         }
415
416         public Object JavaDoc getAttribute(String JavaDoc name) {
417             return this.delegate.getAttribute(name);
418         }
419
420         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
421             this.delegate.setAttribute(name, value);
422         }
423
424         public void removeAttribute(String JavaDoc name) {
425             this.delegate.removeAttribute(name);
426         }
427
428         public Enumeration JavaDoc getAttributeNames() {
429             return this.delegate.getAttributeNames();
430         }
431
432         public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
433             return this.delegate.getResource(path);
434         }
435
436         public String JavaDoc getRealPath(String JavaDoc path) {
437             return this.delegate.getRealPath(path);
438         }
439
440         public String JavaDoc getMimeType(String JavaDoc file) {
441             return this.delegate.getMimeType(file);
442         }
443
444         public String JavaDoc getInitParameter(String JavaDoc name) {
445             return this.delegate.getInitParameter(name);
446         }
447
448         public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
449             return this.delegate.getResourceAsStream(path);
450         }
451     }
452 }
453
Popular Tags