001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * WorkManager.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license terms
009: */
010: package com.sun.jbi.binding.file.framework;
011:
012: import com.sun.jbi.binding.file.FileBindingResources;
013: import com.sun.jbi.binding.file.util.StringTranslator;
014:
015: import java.util.Hashtable;
016: import java.util.logging.Logger;
017:
018: /**
019: * This class manages the work requests and dispatches the requests to be
020: * executed in a free thread.
021: *
022: * @author Sun Microsystems, Inc.
023: */
024: public final class WorkManager implements FileBindingResources {
025: /**
026: * Handle to the list of Work Manager instances.
027: */
028: private static Hashtable sWorkManagerBucket = new Hashtable();
029:
030: /**
031: * Internal handle to the logger instance.
032: */
033: private static Logger sLog;
034:
035: /**
036: * Default value for max threads.
037: */
038: private static final int DEFAULT_MAX_THREADS = 10;
039:
040: /**
041: * Default value for min threads.
042: */
043: private static final int DEFAULT_MIN_THREADS = 2;
044:
045: /**
046: * Localization translator object.
047: */
048: private static StringTranslator sTranslator;
049:
050: /**
051: * Handle to store the Work Manager state. Valid values are "INIT", "READY"
052: * and "STOP".
053: */
054: private String mState;
055:
056: /**
057: * Handle to the WorkThreadPool instance.
058: */
059: private WorkThreadPool mThreadPool;
060:
061: /**
062: * Value for maximum number of threads.
063: */
064: private int mMaxNumberOfThreads = DEFAULT_MAX_THREADS;
065:
066: /**
067: * Value for minimum number of threads.
068: */
069: private int mMinNumberOfThreads = DEFAULT_MIN_THREADS;
070:
071: /**
072: * Creates a new instance of WorkManager.
073: */
074: private WorkManager() {
075: mState = "PARKED";
076: init();
077: }
078:
079: /**
080: * Sets the log file.
081: *
082: * @param logFile log file.
083: */
084: public void setLogger(String logFile) {
085: sLog = sLog.getLogger(logFile);
086: }
087:
088: /**
089: * This method can be used to set the maximum threads in the Pool.
090: *
091: * @param count number of threads.
092: */
093: public void setMaxThreads(int count) {
094: mMaxNumberOfThreads = count;
095: }
096:
097: /**
098: * Sets the minimum number of threads in the Thread pool.
099: *
100: * @param count number of threads.
101: */
102: public void setMinThreads(int count) {
103: mMinNumberOfThreads = count;
104: }
105:
106: /**
107: * Returns a handle to the Work Manager instance for a unique service .
108: *
109: * @param name Some unique name to identify this work manager.
110: *
111: * @return a work manager instance
112: */
113: public static WorkManager getWorkManager(String name) {
114: WorkManager manager = null;
115:
116: String serviceName = name;
117:
118: if (serviceName != null) {
119: manager = (WorkManager) sWorkManagerBucket.get(serviceName);
120:
121: if (manager == null) {
122: manager = new WorkManager();
123:
124: sWorkManagerBucket.put(serviceName, manager);
125: }
126: } else {
127: if (sLog == null) {
128: sLog = Logger
129: .getLogger("com.sun.jbi.binding.file.framework");
130: }
131:
132: sLog.severe(sTranslator.getString(FBC_THREADS_WM_FAILED));
133: }
134:
135: return manager;
136: }
137:
138: /**
139: * Returns the number of wokring threads in the pool.
140: *
141: * @return count of number of busy threads
142: */
143: public int getBusyThreads() {
144: return mThreadPool.getBusyThreads();
145: }
146:
147: /**
148: * Cleans up the workmanager. It notifies the workthread pool to shutdown
149: * all its threads.
150: */
151: public void cease() {
152: // Stop the thread pool.
153: if (mState.equals("INIT") || mState.equals("STARTED")) {
154: try {
155: mThreadPool.stop();
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: setState("STOPPED");
162: }
163:
164: /**
165: * This method is invoked to complete all the work running in the threads
166: * that are active.
167: */
168: public void finishWork() {
169: mThreadPool.exitWhenBusyThreadsDone();
170: }
171:
172: /**
173: * Initializes the Work Manager.
174: */
175: public void init() {
176: if (mState.equals("INIT")) {
177: return;
178: }
179:
180: sLog = Logger.getLogger(this .getClass().getPackage().getName());
181: sTranslator = new StringTranslator();
182: sWorkManagerBucket = new Hashtable();
183: mThreadPool = new WorkThreadPool();
184: mThreadPool.setMinThreads(mMinNumberOfThreads);
185: mThreadPool.setMaxThreads(mMaxNumberOfThreads);
186: mThreadPool.init();
187: setState("INIT");
188: }
189:
190: /**
191: * Process the Command in a different thread. The method places the command
192: * in its internal cache and returns control to the invoking thread.
193: *
194: * @param command - command to be processed.
195: *
196: * @return true if a free thread is available, false otherwise
197: */
198: public boolean processCommand(Command command) {
199: WorkThread workerThread;
200: workerThread = mThreadPool.getFreeThread();
201:
202: boolean status = false;
203:
204: if (workerThread != null) {
205: sLog.fine(sTranslator.getString(FBC_THREADS_WM_PROCESS));
206: workerThread.setCommand(command);
207: status = true;
208: } else {
209: sLog.severe(sTranslator
210: .getString(FBC_THREADS_WM_NOFREETHREAD));
211: }
212:
213: return status;
214: }
215:
216: /**
217: * Starts the Work Manager.
218: */
219: public void start() {
220: try {
221: mThreadPool.start();
222: } catch (Exception e) {
223: sLog.severe(sTranslator.getString(
224: FBC_THREADS_WM_THREADPOOL_FAILED, e.getMessage()));
225: }
226:
227: setState("STARTED");
228: }
229:
230: /**
231: * Sets the state of the work manager.
232: *
233: * @param state - state of the work manager.
234: */
235: protected void setState(String state) {
236: mState = state;
237: }
238: }
|