001: /**
002: * $Id: ReusableProviderGetter.java,v 1.15 2005/09/21 10:44:02 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.desktop.context;
014:
015: import com.sun.portal.providers.context.ContainerProviderContext;
016: import com.sun.portal.desktop.DesktopThreadLocalManager;
017: import com.sun.portal.providers.Provider;
018: import com.sun.portal.log.common.PortalLogger;
019: import com.sun.common.pool.PartitionObject;
020: import com.sun.common.util.LockWithMemory;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletContext;
026: import java.util.logging.Logger;
027: import java.util.logging.Level;
028: import java.util.logging.LogRecord;
029:
030: public class ReusableProviderGetter extends Thread implements
031: PartitionObject {
032: private HttpServletRequest request = null;
033: private HttpServletResponse response = null;
034: private HttpServletRequest origRequest = null;
035: private HttpServletResponse origResponse = null;
036: private ContainerProviderContext providerContext = null;
037: private Provider provider = null;
038: private String providerName = null;
039: private String parent = null;
040: private Monitor monitor = null;
041: private boolean initialized = false;
042: private boolean running = true;
043: private LockWithMemory begin = new LockWithMemory();
044: private LockWithMemory end = new LockWithMemory();
045: private ProviderGetterPool pool = null;
046: private DesktopAppContext desktopAppContext = null;
047: private DesktopContext desktopContext = null;
048: private ServletContext servletContext = null;
049: private ServletConfig servletConfig = null;
050: private int partition = 0;
051:
052: // Create a logger for this class
053: private static Logger debugLogger = PortalLogger
054: .getLogger(ReusableProviderGetter.class);
055:
056: public ReusableProviderGetter() {
057: setDaemon(true);
058: setName(getClass().getName());
059: desktopAppContext = DesktopAppContextThreadLocalizer.get();
060: //desktopAppContext.debugError("ProviderGetter.ProviderGetter(): newed ...");
061: }
062:
063: void init(DesktopAppContext dac, DesktopContext dc,
064: ContainerProviderContext cpc, String parent,
065: String providerName, Monitor m, HttpServletRequest req,
066: HttpServletResponse res, HttpServletRequest origReq,
067: HttpServletResponse origRes, ServletContext sc,
068: ProviderGetterPool getterPool) {
069: this .providerContext = cpc;
070: this .monitor = m;
071: this .request = req;
072: this .response = res;
073: this .origRequest = origReq;
074: this .origResponse = origRes;
075: this .parent = parent;
076: this .providerName = providerName;
077: this .desktopAppContext = dac;
078: this .desktopContext = dc;
079: this .servletConfig = servletConfig;
080: this .servletContext = servletContext;
081: this .pool = getterPool;
082:
083: initialized = true;
084:
085: //desktopAppContext.debugError("ProviderGetter.init(): initialized ...");
086: }
087:
088: public void setPartition(int partition) {
089: this .partition = partition;
090: }
091:
092: public int getPartition() {
093: return this .partition;
094: }
095:
096: void clear() {
097: initialized = false;
098:
099: this .providerContext = null;
100: this .monitor = null;
101: this .parent = null;
102: this .providerName = null;
103: this .request = null;
104: this .response = null;
105: this .origRequest = null;
106: this .origResponse = null;
107: this .desktopAppContext = null;
108: this .desktopContext = null;
109: this .servletConfig = null;
110: this .servletContext = null;
111:
112: //desktopAppContext.debugError("ProviderGetter.init(): cleared ...");
113: }
114:
115: public void execute() {
116: //desktopAppContext.debugError("ProviderGetter.execute(): start ...");
117: begin.signal();
118: //desktopAppContext.debugError("ProviderGetter.execute(): end ...");
119: }
120:
121: void finish() {
122: running = false;
123: begin.signal();
124: }
125:
126: public void run() {
127: //desktopAppContext.debugError("ProviderGetter.run(): entered ...");
128: try {
129: while (running) {
130: try {
131: //desktopAppContext.debugError("ProviderGetter.run(): waiting at begin ...");
132: try {
133: begin.waitFor();
134: } catch (InterruptedException ie) {
135: // probably interrupt from PSContainerProviderContext
136: // to try to free the thread, ignore it.
137: continue;
138: }
139: //desktopAppContext.debugError("ProviderGetter.run(): passed begin, executing main ...");
140: if (!running) {
141: return;
142: }
143:
144: DesktopThreadLocalManager
145: .init(desktopAppContext, request, response,
146: origRequest, origResponse,
147: servletConfig, servletContext);
148: DesktopThreadLocalManager.init(desktopContext,
149: providerContext);
150: try {
151: if (!initialized) {
152: throw new ContextError(
153: "ProviderGetter.run(): attmpted un-initialized loop!");
154: }
155:
156: ProviderContextThreadLocalizer
157: .set(providerContext);
158:
159: //
160: // getting the provider from the manager does an init. we must make sure
161: // to do this in the thread so it's parralelized.
162: //
163: provider = providerContext.getProvider(request,
164: parent, providerName);
165:
166: // insert provider into monitor
167: monitor.put(providerName, provider);
168: debugLogger.log(Level.FINE, "PSDT_CSPDC0026",
169: providerName);
170:
171: } catch (Throwable e) {
172: //
173: // we're doomed. insert null. it's up to the client to do something
174: // when they see the null value.
175: //
176: if (debugLogger.isLoggable(Level.INFO)) {
177: LogRecord logRecord = new LogRecord(
178: Level.INFO, "PSDT_CSPDC0027");
179: logRecord.setLoggerName(debugLogger
180: .getName());
181: logRecord
182: .setParameters(new String[] { providerName });
183: logRecord.setThrown(e);
184: debugLogger.log(logRecord);
185: }
186: monitor.put(providerName, null);
187: }
188: end.signal();
189: DesktopThreadLocalManager.release();
190: pool.releaseGetter(this );
191: //desktopAppContext.debugError("ProviderGetter.run(): done executing main ...");
192: } catch (Throwable t1) {
193: throw new ContextError("ProviderGetter.run()", t1);
194: }
195: }
196: } catch (Throwable t2) {
197: throw new ContextError("ProviderGetter.run()", t2);
198: }
199: }
200: }
|