KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > ContextHelper


1 /*
2  * Copyright 1999-2004 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.components;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.avalon.framework.context.ContextException;
23 import org.apache.cocoon.environment.ObjectModelHelper;
24 import org.apache.cocoon.environment.Request;
25 import org.apache.cocoon.environment.Response;
26
27 /**
28  * A set of constants and methods to access the content of the context
29  * object. Some of the constants are defined in {@link org.apache.cocoon.Constants}.
30  *
31  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
32  * @version CVS $Id: ContextHelper.java 37199 2004-08-30 13:02:54Z cziegeler $
33  */

34
35 public final class ContextHelper {
36
37     /** Application <code>Context</code> Key for the current object model */
38     public static final String JavaDoc CONTEXT_OBJECT_MODEL = "object-model";
39
40     /** Application <code>Context</code> Key for the current request object */
41     public static final String JavaDoc CONTEXT_REQUEST_OBJECT = CONTEXT_OBJECT_MODEL + '.' + ObjectModelHelper.REQUEST_OBJECT;
42
43     /** Application <code>Context</code> Key for the current response object */
44     public static final String JavaDoc CONTEXT_RESPONSE_OBJECT = CONTEXT_OBJECT_MODEL + '.' + ObjectModelHelper.RESPONSE_OBJECT;
45
46     /** Application <code>Context</code> Key for the current sitemap service manager */
47     public static final String JavaDoc CONTEXT_SITEMAP_SERVICE_MANAGER = "sitemap-service-manager";
48     
49     private ContextHelper() {
50         // Forbid instantiation
51
}
52
53     /**
54      * Return the current request
55      * @param context The component context
56      * @return The request object
57      */

58     public static final Request getRequest(Context context) {
59         // the request object is always present
60
try {
61             return (Request)context.get(CONTEXT_REQUEST_OBJECT);
62         } catch (ContextException ce) {
63             throw new CascadingRuntimeException("Unable to get the request object from the context.", ce);
64         }
65     }
66
67     /**
68      * Return the current response
69      * @param context The component context
70      * @return The response
71      */

72     public static final Response getResponse(Context context) {
73         // the response object is always present
74
try {
75             return (Response)context.get(CONTEXT_RESPONSE_OBJECT);
76         } catch (ContextException ce) {
77             throw new CascadingRuntimeException("Unable to get the response object from the context.", ce);
78         }
79     }
80
81     /**
82      * Return the current object model
83      * @param context The component context
84      * @return The object model
85      */

86     public static final Map JavaDoc getObjectModel(Context context) {
87         // the object model is always present
88
try {
89             return (Map JavaDoc)context.get(CONTEXT_OBJECT_MODEL);
90         } catch (ContextException ce) {
91             throw new CascadingRuntimeException("Unable to get the object model from the context.", ce);
92         }
93     }
94 }
95
Popular Tags