001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027:
028: import biz.hammurapi.config.Component;
029: import biz.hammurapi.config.ConfigurationException;
030: import biz.hammurapi.config.Context;
031: import biz.hammurapi.config.DomConfigurable;
032: import biz.hammurapi.config.Wrapper;
033:
034: public class ThreadPoolComponentWrapper implements Wrapper, Component,
035: DomConfigurable {
036: private ThreadPool master;
037: private int numberOfThreads = 20;
038: private int priority = Thread.NORM_PRIORITY;
039: private int maxQueue = 1000;
040: private String exceptionSinkName;
041: private Object owner;
042: private String name;
043:
044: public Object getMaster() {
045: return master;
046: }
047:
048: public void start() throws ConfigurationException {
049: ExceptionSink exceptionSink = null;
050: if (exceptionSinkName != null) {
051: if (!(owner instanceof Context)) {
052: throw new ConfigurationException(
053: "Cannot lookup exception sink - owner doesn't implement Context");
054: }
055:
056: exceptionSink = (ExceptionSink) ((Context) owner)
057: .get(exceptionSinkName);
058:
059: if (exceptionSink == null) {
060: throw new ConfigurationException(
061: "Lookup failed for exception sink '"
062: + exceptionSinkName + "'");
063: }
064: }
065:
066: master = new ThreadPool(numberOfThreads, priority, maxQueue,
067: exceptionSink, name);
068: master.start();
069: }
070:
071: public void stop() throws ConfigurationException {
072: if (master != null) {
073: master.stop();
074: }
075: }
076:
077: public void setOwner(Object owner) {
078: this .owner = owner;
079: }
080:
081: public void configure(Node configNode, Context context)
082: throws ConfigurationException {
083: Element e = (Element) configNode;
084: if (e.hasAttribute("threads")) {
085: numberOfThreads = Integer.parseInt(e
086: .getAttribute("threads"));
087: if (numberOfThreads < 1) {
088: throw new ConfigurationException(
089: "Number of threads shall be >=1");
090: }
091: }
092:
093: if (e.hasAttribute("max-queue")) {
094: maxQueue = Integer.parseInt(e.getAttribute("max-queue"));
095: }
096:
097: if (e.hasAttribute("exception-sink")) {
098: exceptionSinkName = e.getAttribute("exception-sink");
099: }
100:
101: if (e.hasAttribute("pool-name")) {
102: name = e.getAttribute("pool-name");
103: }
104:
105: if (e.hasAttribute("priority")) {
106: String pStr = e.getAttribute("priority");
107: if ("min".equalsIgnoreCase(pStr)) {
108: priority = Thread.MIN_PRIORITY;
109: } else if ("max".equalsIgnoreCase(pStr)) {
110: priority = Thread.MAX_PRIORITY;
111: } else if ("normal".equalsIgnoreCase(pStr)) {
112: priority = Thread.NORM_PRIORITY;
113: } else {
114: throw new ConfigurationException(
115: "Invalid priority value: " + pStr);
116: }
117: }
118: }
119:
120: }
|