001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023: import javax.servlet.ServletContext;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: /**
028: * This class is a ThreadLocal class that contains the servlet information. In addition,
029: * it supports a Map that allows other aspects of the framework to create place
030: * their own context objects into the thread local context.
031: */
032: public class PageFlowContext {
033: private static ThreadLocal pageflowContext = new PageFlowContextThreadLocal();
034:
035: // The context will provide access to the Servlet objects
036: // @todo: do we want to abstract these further to provide an abstraction removing the Servlet API?
037: private HttpServletRequest _request;
038: private HttpServletResponse _response;
039: private ServletContext _servletContext;
040:
041: // map containin other context objects
042: private Map _contexts = new HashMap();
043:
044: /**
045: * Factory method that will return the <code>PageFlowContext</code> object to the caller.
046: * @return A <code>PageFlowContext</code> that is associated with the tread.
047: */
048: public static PageFlowContext getContext() {
049: PageFlowContext ctxt = (PageFlowContext) pageflowContext.get();
050: if (ctxt == null) {
051: ctxt = new PageFlowContext();
052: pageflowContext.set(ctxt);
053: }
054: return ctxt;
055: }
056:
057: /**
058: * Return the request object.
059: * @return The <code>HttpServletRequest</code> object.
060: */
061: public HttpServletRequest getRequest() {
062: return _request;
063: }
064:
065: /**
066: * Set the request object.
067: * @param request The <code>HttpServletRequst</code>
068: */
069: public void setRequest(HttpServletRequest request) {
070: _request = request;
071: }
072:
073: /**
074: * Return the response object to the caller.
075: * @return The <code>HttpServletResponse</code> object.
076: */
077: public HttpServletResponse getResponse() {
078: return _response;
079: }
080:
081: /**
082: * Set the response object on the context.
083: * @param response The <code>HttpServletResponse</code> object.
084: */
085: public void setResponse(HttpServletResponse response) {
086: _response = response;
087: }
088:
089: /**
090: * Return the servlet context.
091: * @return The <code>ServletContext</code> object for this request.
092: */
093: public ServletContext getServletContext() {
094: return _servletContext;
095: }
096:
097: /**
098: * Set the servlet context for this request.
099: * @param servletContext The <code>ServletContext</code> object.
100: */
101: public void setServletContext(ServletContext servletContext) {
102: _servletContext = servletContext;
103: }
104:
105: /**
106: * This method will lookup a named object and return it. The object is
107: * looked up by name. If the object doesn't exist, the activator object is
108: * called to create a new instance of the object before it is returned.
109: * @param name The name of the object to return
110: * @param activator An <code>PageFlowContextActivator</code> that will create the new object if
111: * it doesn't exist.
112: * @return The object stored by the name.
113: */
114: public Object get(String name, PageFlowContextActivator activator) {
115: assert (name != null) : "Parameter 'name' must not be null";
116: assert (activator != null) : "Parameter 'activator' must not be null";
117:
118: Object ret = _contexts.get(name);
119: if (ret == null) {
120: ret = activator.activate();
121: _contexts.put(name, ret);
122: }
123: return ret;
124: }
125:
126: /**
127: * Place the context into it's initialized state.
128: */
129: public void init() {
130: _request = null;
131: _response = null;
132: _servletContext = null;
133: _contexts.clear();
134: }
135:
136: /**
137: * Thread local...
138: */
139: private static class PageFlowContextThreadLocal extends ThreadLocal {
140: protected Object initializeValue() {
141: return new PageFlowContext();
142: }
143: }
144: }
|