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: package org.apache.jetspeed.request;
018:
019: import java.lang.reflect.Constructor;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.servlet.ServletConfig;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.jetspeed.PortalReservedParameters;
030: import org.apache.jetspeed.aggregator.CurrentWorkerContext;
031: import org.apache.jetspeed.userinfo.UserInfoManager;
032:
033: /**
034: * JetspeedRequestContextComponent
035: *
036: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
037: * @version $Id: JetspeedRequestContextComponent.java 587064 2007-10-22 11:54:11Z woonsan $
038: */
039: public class JetspeedRequestContextComponent implements
040: RequestContextComponent {
041: private String contextClassName = null;
042: private Class contextClass = null;
043: /** The user info manager. */
044: private UserInfoManager userInfoMgr;
045: private ThreadLocal tlRequestContext = new ThreadLocal();
046: private Map requestContextObjects;
047:
048: private final static Log log = LogFactory
049: .getLog(JetspeedRequestContextComponent.class);
050:
051: public JetspeedRequestContextComponent(String contextClassName) {
052: this .contextClassName = contextClassName;
053: this .requestContextObjects = new HashMap();
054: }
055:
056: public JetspeedRequestContextComponent(String contextClassName,
057: UserInfoManager userInfoMgr) {
058: this .contextClassName = contextClassName;
059: this .userInfoMgr = userInfoMgr;
060: this .requestContextObjects = new HashMap();
061: }
062:
063: public JetspeedRequestContextComponent(String contextClassName,
064: UserInfoManager userInfoMgr, Map requestContextObjects) {
065: this .contextClassName = contextClassName;
066: this .userInfoMgr = userInfoMgr;
067: this .requestContextObjects = requestContextObjects;
068: }
069:
070: public RequestContext create(HttpServletRequest req,
071: HttpServletResponse resp, ServletConfig config) {
072: RequestContext context = null;
073:
074: try {
075: if (null == contextClass) {
076: contextClass = Class.forName(contextClassName);
077: }
078:
079: Constructor constructor = contextClass
080: .getConstructor(new Class[] {
081: HttpServletRequest.class,
082: HttpServletResponse.class,
083: ServletConfig.class, UserInfoManager.class,
084: Map.class });
085: context = (RequestContext) constructor
086: .newInstance(new Object[] { req, resp, config,
087: userInfoMgr, requestContextObjects });
088:
089: } catch (Exception e) {
090: String msg = "JetspeedRequestContextComponent: Failed to create a Class object for RequestContext: "
091: + e.toString();
092: log.error(msg);
093: }
094: tlRequestContext.set(context);
095: return context;
096: }
097:
098: public void release(RequestContext context) {
099: tlRequestContext.set(null);
100: }
101:
102: /**
103: * The servlet request can always get you back to the Request Context if you need it
104: * This static accessor provides this capability
105: *
106: * @param request
107: * @return RequestContext
108: */
109: public RequestContext getRequestContext(HttpServletRequest request) {
110: RequestContext rc = (RequestContext) request
111: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
112: if (rc != null) {
113: return rc;
114: } else {
115: log
116: .error("Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread.");
117: throw new IllegalStateException(
118: "Cannot call getRequestContext(HttpServletRequest request) before it has been created and set for this thread.");
119: }
120: }
121:
122: public RequestContext getRequestContext() {
123: RequestContext rc = null;
124:
125: if (CurrentWorkerContext.getParallelRenderingMode()) {
126: rc = (RequestContext) CurrentWorkerContext
127: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
128: } else {
129: rc = (RequestContext) tlRequestContext.get();
130: }
131:
132: if (rc != null) {
133: return rc;
134: } else {
135: log
136: .error("Cannot call getRequestContext() before it has been created and set for this thread.");
137: throw new IllegalStateException(
138: "Cannot call getRequestContext() before it has been created and set for this thread.");
139: }
140: }
141:
142: }
|