001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.context;
006:
007: import com.sun.common.pool.PartitionObject;
008: import com.sun.common.util.LockWithMemory;
009: import com.sun.portal.desktop.DesktopThreadLocalManager;
010: import com.sun.portal.desktop.dp.DPHelper;
011: import com.sun.portal.desktop.dp.FQCN;
012: import com.sun.portal.desktop.monitoring.MonitoringSubsystemWrapper;
013: import com.sun.portal.log.common.PortalLogger;
014: import com.sun.portal.monitoring.utilities.ActivityTime;
015: import com.sun.portal.providers.Provider;
016: import com.sun.portal.providers.context.ContainerProviderContext;
017:
018: import javax.servlet.ServletConfig;
019: import javax.servlet.ServletContext;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024: import java.util.logging.Logger;
025: import java.util.Set;
026:
027: public class ReusableProviderCaller extends Thread implements
028: PartitionObject {
029: private static Logger logger = PortalLogger
030: .getLogger(ReusableProviderCaller.class);
031:
032: private static LogRecord getLogRecord(Level level, String message,
033: Object[] parameters, Throwable t) {
034: LogRecord result = new LogRecord(level, message);
035: result.setLoggerName(logger.getName());
036: result.setParameters(parameters);
037: result.setThrown(t);
038: return result;
039: }
040:
041: private ContainerProviderContext providerContext = null;
042: private Provider provider = null;
043: private StringBuffer content = null;
044: private HttpServletRequest request = null;
045: private HttpServletResponse response = null;
046: private HttpServletRequest origRequest = null;
047: private HttpServletResponse origResponse = null;
048: private String parent = null;
049: private Monitor monitor = null;
050: private boolean initialized = false;
051: private boolean running = true;
052: private LockWithMemory begin = new LockWithMemory();
053: private LockWithMemory end = new LockWithMemory();
054: private ProviderCallerPool pool = null;
055: private DesktopAppContext desktopAppContext = null;
056: private DesktopContext desktopContext = null;
057: private ServletContext servletContext = null;
058: private ServletConfig servletConfig = null;
059: private Set cuMembership = null;
060: private int partition = 0;
061:
062: public ReusableProviderCaller() {
063: setDaemon(true);
064: setName(getClass().getName());
065: }
066:
067: public void setPartition(int partition) {
068: this .partition = partition;
069: }
070:
071: public int getPartition() {
072: return this .partition;
073: }
074:
075: void init(DesktopAppContext dac, DesktopContext dc,
076: ContainerProviderContext cpc, Provider p, String parent,
077: Monitor m, HttpServletRequest req, HttpServletResponse res,
078: HttpServletRequest origReq, HttpServletResponse origRes,
079: ServletConfig servletConfig, ServletContext servletContext,
080: Set cuMembership, ProviderCallerPool callerPool) {
081: this .providerContext = cpc;
082: this .provider = p;
083: this .parent = parent;
084: this .monitor = m;
085: this .request = req;
086: this .response = res;
087: this .origRequest = origReq;
088: this .origResponse = origRes;
089: this .desktopAppContext = dac;
090: this .desktopContext = dc;
091: this .servletConfig = servletConfig;
092: this .servletContext = servletContext;
093: this .pool = callerPool;
094: this .cuMembership = cuMembership;
095:
096: initialized = true;
097: }
098:
099: void clear() {
100: initialized = false;
101: content = null;
102:
103: this .providerContext = null;
104: this .provider = null;
105: this .parent = null;
106: this .monitor = null;
107: this .request = null;
108: this .response = null;
109: this .origRequest = null;
110: this .origResponse = null;
111: this .desktopAppContext = null;
112: this .desktopContext = null;
113: this .servletContext = null;
114: this .servletConfig = null;
115: }
116:
117: public void execute() {
118: begin.signal();
119: }
120:
121: void finish() {
122: running = false;
123: begin.signal();
124: }
125:
126: public Provider getProvider() {
127: return provider;
128: }
129:
130: public StringBuffer getContent() {
131: return content;
132: }
133:
134: public void endOfTask(long timeout) {
135: try {
136: end.waitFor(timeout);
137: } catch (InterruptedException e) {
138: ; // ignore
139: }
140: }
141:
142: private void doFinally(String name, FQCN fqcn,
143: ActivityTime activityTime) {
144: if (name != null) {
145: monitor.put(name, content);
146: MonitoringSubsystemWrapper.getDesktopMonitoringSubsystem()
147: .stopProviderContentServiceTransaction(
148: fqcn.encoded());
149: if (activityTime != null) {
150: MonitoringSubsystemWrapper
151: .getDesktopMonitoringSubsystem()
152: .getContentChannelActionStatistic().measure(
153: activityTime, fqcn.plain());
154: }
155: }
156:
157: if (initialized) {
158: // Clean up only once
159: DesktopThreadLocalManager.release();
160: CommunityUserMembershipThreadLocalizer.reset();
161: pool.releaseCaller(this );
162: end.signal();
163: }
164: }
165:
166: public void run() {
167: String name = null;
168: FQCN fqcn = null;
169: ActivityTime activityTime = null;
170:
171: try {
172: while (running) {
173: try {
174: try {
175: begin.waitFor();
176: } catch (InterruptedException ie) {
177: continue;
178: }
179:
180: if (!running) {
181: // finishing, going out of pool or out of service.
182: break;
183: }
184:
185: if (!initialized) {
186: throw new ContextError(
187: "ProviderCaller.run(): attmpted un-initialized loop!");
188: }
189:
190: DesktopThreadLocalManager
191: .init(desktopAppContext, request, response,
192: origRequest, origResponse,
193: servletConfig, servletContext);
194: DesktopThreadLocalManager.init(desktopContext,
195: providerContext);
196: DesktopThreadLocalManager.init(parent);
197: CommunityUserMembershipThreadLocalizer.set(
198: desktopContext.getUserID(), cuMembership);
199: ProviderContextThreadLocalizer.set(providerContext);
200:
201: name = provider.getName();
202: fqcn = DPHelper.getFQCN(desktopContext.getDPRoot(),
203: name);
204: activityTime = new ActivityTime();
205: MonitoringSubsystemWrapper
206: .getDesktopMonitoringSubsystem()
207: .getContentChannelActionStatistic().mark(
208: activityTime);
209: MonitoringSubsystemWrapper
210: .getDesktopMonitoringSubsystem()
211: .startProviderContentServiceTransaction(
212: fqcn.encoded());
213: content = provider.getContent(request, response);
214: if (provider.getRefreshTime() > 0) {
215: providerContext.putCachedContent(name, content);
216: }
217: } catch (Throwable t) {
218: if (logger.isLoggable(Level.SEVERE)) {
219: logger.log(getLogRecord(Level.SEVERE,
220: "PSDT_CSPDC0025",
221: new Object[] { name }, t));
222: }
223: } finally {
224: doFinally(name, fqcn, activityTime);
225: name = null;
226: fqcn = null;
227: activityTime = null;
228: }
229: }
230: } catch (Throwable t2) {
231: if (logger.isLoggable(Level.SEVERE)) {
232: if (logger.isLoggable(Level.SEVERE)) {
233: logger
234: .log(getLogRecord(Level.SEVERE,
235: "PSDT_CSPDC0025",
236: new Object[] { name }, t2));
237: }
238: }
239: } finally {
240: doFinally(name, fqcn, activityTime);
241: }
242: }
243: }
|