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.binding.file.framework;
030:
031: import com.sun.jbi.binding.file.FileBindingResources;
032: import com.sun.jbi.binding.file.util.StringTranslator;
033:
034: import com.sun.jbi.binding.file.FileBindingContext;
035: import java.util.Hashtable;
036: import java.util.logging.Logger;
037:
038: /**
039: * This class manages the work requests and dispatches the requests to be
040: * executed in a free thread.
041: *
042: * @author Sun Microsystems, Inc.
043: */
044: public final class WorkManager implements FileBindingResources {
045: /**
046: * Handle to the list of Work Manager instances.
047: */
048: private static Hashtable sWorkManagerBucket = new Hashtable();
049:
050: /**
051: * Internal handle to the logger instance.
052: */
053: private static Logger sLog;
054:
055: /**
056: * Default value for max threads.
057: */
058: private static final int DEFAULT_MAX_THREADS = 10;
059:
060: /**
061: * Default value for min threads.
062: */
063: private static final int DEFAULT_MIN_THREADS = 2;
064:
065: /**
066: * Localization translator object.
067: */
068: private static StringTranslator sTranslator;
069:
070: /**
071: * Handle to store the Work Manager state. Valid values are "INIT", "READY"
072: * and "STOP".
073: */
074: private String mState;
075:
076: /**
077: * Handle to the WorkThreadPool instance.
078: */
079: private WorkThreadPool mThreadPool;
080:
081: /**
082: * Value for maximum number of threads.
083: */
084: private int mMaxNumberOfThreads = DEFAULT_MAX_THREADS;
085:
086: /**
087: * Value for minimum number of threads.
088: */
089: private int mMinNumberOfThreads = DEFAULT_MIN_THREADS;
090:
091: /**
092: * Creates a new instance of WorkManager.
093: */
094: private WorkManager() {
095: mState = "PARKED";
096: init();
097: }
098:
099: /**
100: * This method can be used to set the maximum threads in the Pool.
101: *
102: * @param count number of threads.
103: */
104: public void setMaxThreads(int count) {
105: mMaxNumberOfThreads = count;
106: }
107:
108: /**
109: * Sets the minimum number of threads in the Thread pool.
110: *
111: * @param count number of threads.
112: */
113: public void setMinThreads(int count) {
114: mMinNumberOfThreads = count;
115: }
116:
117: /**
118: * Returns a handle to the Work Manager instance for a unique service .
119: *
120: * @param name Some unique name to identify this work manager.
121: *
122: * @return a work manager instance
123: */
124: public static WorkManager getWorkManager(String name) {
125: WorkManager manager = null;
126:
127: String serviceName = name;
128: sLog = FileBindingContext.getInstance().getLogger();
129: sTranslator = new StringTranslator();
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(sTranslator.getString(FBC_THREADS_WM_FAILED));
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: // Stop the thread pool.
160: if (mState.equals("INIT") || mState.equals("STARTED")) {
161: try {
162: mThreadPool.stop();
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: }
167:
168: setState("STOPPED");
169: }
170:
171: /**
172: * This method is invoked to complete all the work running in the threads
173: * that are active.
174: */
175: public void finishWork() {
176: mThreadPool.exitWhenBusyThreadsDone();
177: }
178:
179: /**
180: * Initializes the Work Manager.
181: */
182: public void init() {
183: if (mState.equals("INIT")) {
184: return;
185: }
186: sWorkManagerBucket = new Hashtable();
187: mThreadPool = new WorkThreadPool();
188: mThreadPool.setMinThreads(mMinNumberOfThreads);
189: mThreadPool.setMaxThreads(mMaxNumberOfThreads);
190: mThreadPool.init();
191: setState("INIT");
192: }
193:
194: /**
195: * Process the Command in a different thread. The method places the command
196: * in its internal cache and returns control to the invoking thread.
197: *
198: * @param command - command to be processed.
199: *
200: * @return true if a free thread is available, false otherwise
201: */
202: public boolean processCommand(Command command) {
203: WorkThread workerThread;
204: workerThread = mThreadPool.getFreeThread();
205:
206: boolean status = false;
207:
208: if (workerThread != null) {
209: sLog.fine(sTranslator.getString(FBC_THREADS_WM_PROCESS));
210: workerThread.setCommand(command);
211: status = true;
212: } else {
213: sLog.severe(sTranslator
214: .getString(FBC_THREADS_WM_NOFREETHREAD));
215: }
216:
217: return status;
218: }
219:
220: /**
221: * Starts the Work Manager.
222: */
223: public void start() {
224: try {
225: mThreadPool.start();
226: } catch (Exception e) {
227: sLog.severe(sTranslator.getString(
228: FBC_THREADS_WM_THREADPOOL_FAILED, e.getMessage()));
229: }
230:
231: setState("STARTED");
232: }
233:
234: /**
235: * Sets the state of the work manager.
236: *
237: * @param state - state of the work manager.
238: */
239: protected void setState(String state) {
240: mState = state;
241: }
242: }
|