KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > servlet > ServletBeanContext


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

19
20 import java.util.Stack JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.beans.beancontext.BeanContextChild JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.ServletResponse JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.beehive.controls.runtime.bean.ControlContainerContext;
33
34 /**
35  * The ServletBeanContext provides a ControlBeanContext implementation that offers services
36  * and a resource management context that is appropriate to web tier usage of controls.
37  */

38 public class ServletBeanContext extends ControlContainerContext
39 {
40     private static class RequestContext
41     {
42         RequestContext(ServletContext JavaDoc context, ServletRequest JavaDoc req, ServletResponse JavaDoc resp)
43         {
44             _context = context;
45             _request = req;
46             _response = resp;
47         }
48
49         ServletContext JavaDoc _context;
50         ServletResponse JavaDoc _response;
51         ServletRequest JavaDoc _request;
52     }
53
54     public ServletBeanContext()
55     {
56         super();
57     }
58
59     /**
60      * Called by BeanContextSupport superclass during construction and deserialization to
61      * initialize subclass transient state
62      */

63     public void initialize()
64     {
65         super.initialize();
66
67         //
68
// Register the ServletService classes associated with the ServletServiceProvider
69
//
70
ServletServiceProvider ssp = ServletServiceProvider.getProvider();
71         addService(ServletContext JavaDoc.class, ssp);
72         addService(ServletRequest JavaDoc.class, ssp);
73         addService(ServletResponse JavaDoc.class, ssp);
74         addService(HttpServletRequest JavaDoc.class, ssp);
75         addService(HttpServletResponse JavaDoc.class, ssp);
76     }
77
78     /**
79      * Begins a new execution context, associated with a specific ServletRequest
80      */

81     public void beginContext(ServletContext JavaDoc context, ServletRequest JavaDoc req, ServletResponse JavaDoc resp)
82     {
83         pushRequestContext(context, req, resp);
84         super.beginContext();
85     }
86
87     /**
88      * Ends the current execution context, and resetes the current active ServletRequest.
89      */

90     public void endContext()
91     {
92         super.endContext();
93         popRequestContext();
94     }
95
96     private Stack JavaDoc<RequestContext> getRequestStack()
97     {
98         if (_reqStack == null)
99             _reqStack = new Stack JavaDoc<RequestContext>();
100
101         return _reqStack;
102     }
103
104     /**
105      * Pushes the current request context onto the stack
106      */

107     private synchronized void pushRequestContext(ServletContext JavaDoc context, ServletRequest JavaDoc req,
108                                                  ServletResponse JavaDoc resp)
109     {
110         getRequestStack().push(new RequestContext(context, req, resp));
111     }
112
113     /**
114      * Pops the current request context from the stack
115      */

116     private synchronized void popRequestContext()
117     {
118         getRequestStack().pop();
119     }
120
121     /**
122      * Returns the current request context, or null is none is available
123      */

124     private synchronized RequestContext peekRequestContext()
125     {
126         Stack JavaDoc<RequestContext> reqStack = getRequestStack();
127         if (reqStack.empty())
128             return null;
129
130         return reqStack.peek();
131     }
132
133     /**
134      * Returns the ServletContext associated with this context (or null if not currently
135      * processing a request)
136      */

137     public ServletContext JavaDoc getServletContext()
138     {
139         RequestContext reqContext = peekRequestContext();
140         if (reqContext == null)
141             return null;
142
143         return reqContext._context;
144     }
145
146     /**
147      * Returns the ServletRequest associated with this context (or null if not currently
148      * processing a request)
149      */

150     public ServletRequest JavaDoc getServletRequest()
151     {
152         RequestContext reqContext = peekRequestContext();
153         if (reqContext == null)
154             return null;
155
156         return reqContext._request;
157     }
158
159     /**
160      * Returns the ServletResponse associated with this context (or null if not currently
161      * processing a request)
162      */

163     public ServletResponse JavaDoc getServletResponse()
164     {
165         RequestContext reqContext = peekRequestContext();
166         if (reqContext == null)
167             return null;
168
169         return reqContext._response;
170     }
171
172     /**
173      * Enables/disable the use of request/response wrappers for this context. By default,
174      * wrappers are enabled if this API is not invoked.
175      */

176     public void setWrappers(boolean useWrappers)
177     {
178         _useWrappers = useWrappers;
179     }
180
181     /**
182      * Override BeanContext.getResourceAsStream() so it delegates to the current ServletContext.
183      *
184      * @param name the resource name
185      * @param bcc the specified child
186      * @return an <code>InputStream</code> for reading the resource,
187      * or <code>null</code> if the resource could not
188      * be found.
189      * @throws <code>IllegalArgumentException</code> if
190      * the resource is not valid
191      */

192     public InputStream JavaDoc getResourceAsStream(String JavaDoc name, BeanContextChild JavaDoc bcc) throws IllegalArgumentException JavaDoc
193     {
194         ServletContext JavaDoc sc = getServletContext();
195         if ( sc != null )
196             return sc.getResourceAsStream( name );
197         
198         return null;
199     }
200
201     /**
202      * Override BeanContext.getResource() so it delegates to the current ServletContext.
203      *
204      * @param name the resource name
205      * @param bcc the specified child
206      * @return a <code>URL</code> for the named
207      * resource for the specified child
208      * @throws <code>IllegalArgumentException</code>
209      * if the resource is not valid
210      */

211     public URL JavaDoc getResource(String JavaDoc name, BeanContextChild JavaDoc bcc) throws IllegalArgumentException JavaDoc
212     {
213         ServletContext JavaDoc sc = getServletContext();
214         if ( sc != null )
215         {
216             try
217             {
218                 return sc.getResource( name );
219             }
220             catch ( MalformedURLException JavaDoc mue )
221             {
222                 throw new IllegalArgumentException JavaDoc( mue.getMessage() );
223             }
224         }
225
226         return null;
227     }
228
229     protected boolean useWrappers() { return _useWrappers; }
230
231     private boolean _useWrappers = true;
232     transient private Stack JavaDoc<RequestContext> _reqStack;
233 }
234
Popular Tags