001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WorkManager.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt.framework;
030:
031: import java.util.Hashtable;
032: import java.util.logging.Logger;
033: import com.sun.jbi.engine.xslt.TransformationEngineContext;
034:
035: /**
036: * This class manages the work requests and dispatches the requests to be
037: * executed in a free thread.
038: *
039: * @author Sun Microsystems, Inc.
040: */
041: public final class WorkManager {
042: /**
043: * Handle to the list of Work Manager instances.
044: */
045: private static Hashtable sWorkManagerBucket = new Hashtable();
046:
047: /**
048: * Internal handle to the logger instance.
049: */
050: private static Logger sLog;
051:
052: /**
053: * Default value for max threads.
054: */
055: private static final int DEFAULT_MAX_THREADS = 10;
056:
057: /**
058: * Default value for min threads.
059: */
060: private static final int DEFAULT_MIN_THREADS = 2;
061:
062: /**
063: * Handle to store the Work Manager state. Valid values are "INIT", "READY"
064: * and "STOP".
065: */
066: private String mState;
067:
068: /**
069: * Handle to the WorkThreadPool instance.
070: */
071: private WorkThreadPool mThreadPool;
072:
073: /**
074: * Value for max threads.
075: */
076: private int mMaxNumberOfThreads = DEFAULT_MAX_THREADS;
077:
078: /**
079: * Value for min threads.
080: */
081: private int mMinNumberOfThreads = DEFAULT_MIN_THREADS;
082:
083: /**
084: * Creates a new instance of WorkManager.
085: */
086: private WorkManager() {
087: mState = "PARKED";
088: init();
089: }
090:
091: /**
092: * Sets the log file.
093: *
094: * @param logFile log file.
095: */
096: public void setLogger(String logFile) {
097: sLog = sLog.getLogger(logFile);
098: }
099:
100: /**
101: * This method can be used to set the maximum threads in the Pool.
102: *
103: * @param count DOCUMENT ME!
104: */
105: public void setMaxThreads(int count) {
106: mMaxNumberOfThreads = count;
107: }
108:
109: /**
110: * Sets the minimu threads in the Thread pool.
111: *
112: * @param count DOCUMENT ME!
113: */
114: public void setMinThreads(int count) {
115: mMinNumberOfThreads = count;
116: }
117:
118: /**
119: * Returns a handle to the Work Manager instance for a unique service .
120: *
121: * @param name DOCUMENT ME!
122: *
123: * @return a work manager instance
124: */
125: public static WorkManager getWorkManager(String name) {
126: WorkManager manager = null;
127:
128: String serviceName = name;
129:
130: if (serviceName != null) {
131: manager = (WorkManager) sWorkManagerBucket.get(serviceName);
132:
133: if (manager == null) {
134: manager = new WorkManager();
135:
136: sWorkManagerBucket.put(serviceName, manager);
137: }
138: } else {
139: sLog.severe("service name is null can get work manager");
140: }
141:
142: return manager;
143: }
144:
145: /**
146: * Returns the number of wokring threads in the pool.
147: *
148: * @return count of number of busy threads
149: */
150: public int getBusyThreads() {
151: return mThreadPool.getBusyThreads();
152: }
153:
154: /**
155: * Cleans up the workmanager. It notifies the workthread pool to shutdown
156: * all its threads.
157: */
158: public void cease() {
159: sLog.info("WorkManager is being stopped" + mState);
160:
161: // Stop the thread pool.
162: if (mState.equals("INIT") || mState.equals("STARTED")) {
163: try {
164: mThreadPool.stop();
165: } catch (Exception e) {
166: e.printStackTrace();
167: }
168: }
169:
170: setState("STOPPED");
171: sLog.info("WorkManager cease finished , now state" + mState);
172: }
173:
174: /**
175: *
176: */
177: public void finishWork() {
178: mThreadPool.exitWhenBusyThreadsDone();
179: }
180:
181: /**
182: * Initializes the Work Manager.
183: */
184: public void init() {
185: if (mState.equals("INIT")) {
186: return;
187: }
188:
189: sLog = TransformationEngineContext.getInstance().getLogger("");
190:
191: if (sWorkManagerBucket == null) {
192: sWorkManagerBucket = new Hashtable();
193: }
194:
195: mThreadPool = new WorkThreadPool();
196: mThreadPool.setMinThreads(mMinNumberOfThreads);
197: mThreadPool.setMaxThreads(mMaxNumberOfThreads);
198: mThreadPool.init();
199: setState("INIT");
200: }
201:
202: /**
203: * Process the Command in a different thread. The method places the command
204: * in its internal cache and returns control to the invoking thread.
205: *
206: * @param command - command to be processed.
207: *
208: * @return true if a free thread is available, false otherwise
209: */
210: public boolean processCommand(Command command) {
211: WorkThread workerThread;
212: workerThread = mThreadPool.getFreeThread();
213:
214: boolean status = false;
215:
216: if (workerThread != null) {
217: sLog.info("WorkManager passing request to worker thread");
218: workerThread.setCommand(command);
219: status = true;
220: } else {
221: // Worker thread pool has been instructed to cleanup
222: sLog.info("Could not obtain free thread");
223: }
224:
225: return status;
226: }
227:
228: /**
229: * Starts the Work Manager.
230: */
231: public void start() {
232: mThreadPool.start();
233: setState("STARTED");
234: sLog.info("WorkManager is started" + mState);
235: }
236:
237: /**
238: * Sets the state of the work manager.
239: *
240: * @param state - state of the work manager.
241: */
242: protected void setState(String state) {
243: mState = state;
244: }
245: }
|