001: package org.jacorb.poa;
002:
003: import org.apache.avalon.framework.configuration.ConfigurationException;
004: import org.apache.avalon.framework.logger.Logger;
005: import org.jacorb.config.Configuration;
006: import org.jacorb.orb.ORB;
007:
008: /**
009: * factory class to create instances of RPPoolManager
010: * depending on the configuration of the ORB this
011: * factory will return a new instance for every call
012: * or return a shared instance.
013: *
014: * @author Alphonse Bendt
015: * @version $Id: RPPoolManagerFactory.java,v 1.5 2006/07/07 12:09:50 alphonse.bendt Exp $
016: */
017: public class RPPoolManagerFactory {
018: private final ORB orb;
019: private final Logger logger;
020: private final Configuration configuration;
021: private final int threadPoolMin;
022: private final int threadPoolMax;
023: private final FactoryDelegate delegate;
024:
025: public RPPoolManagerFactory(ORB orb) throws ConfigurationException {
026: this .orb = orb;
027: configuration = orb.getConfiguration();
028: logger = configuration.getNamedLogger("jacorb.poa.controller");
029:
030: threadPoolMin = configuration.getAttributeAsInteger(
031: "jacorb.poa.thread_pool_min", 5);
032:
033: if (threadPoolMin < 1) {
034: throw new ConfigurationException(
035: "jacorb.poa.thread_pool_min must be >= 1");
036: }
037:
038: threadPoolMax = configuration.getAttributeAsInteger(
039: "jacorb.poa.thread_pool_max", 20);
040:
041: if (threadPoolMax < threadPoolMin) {
042: throw new ConfigurationException(
043: "jacorb.poa.thread_pool_max must be >= "
044: + threadPoolMin
045: + "(jacorb.poa.thread_pool_min)");
046: }
047:
048: boolean poolsShouldBeShared = configuration
049: .getAttributeAsBoolean("jacorb.poa.thread_pool_shared",
050: false);
051:
052: if (logger.isDebugEnabled()) {
053: logger
054: .debug("RequestProcessorPoolFactory settings: thread_pool_min="
055: + threadPoolMin
056: + " thread_pool_max="
057: + threadPoolMax
058: + " thread_pool_shared="
059: + poolsShouldBeShared);
060: }
061:
062: if (poolsShouldBeShared) {
063: delegate = new SharedPoolFactory();
064: } else {
065: delegate = new DefaultPoolFactory();
066: }
067: }
068:
069: public void destroy() {
070: delegate.destroy();
071: }
072:
073: /**
074: * factory method to create a RPPoolManager instance.
075: *
076: * @param isSingleThreaded if true the returned poolmanager will only use one thread
077: */
078: public RPPoolManager newRPPoolManager(boolean isSingleThreaded) {
079: if (isSingleThreaded) {
080: return new RPPoolManager(orb.getPOACurrent(), 1, 1, logger,
081: configuration) {
082: void destroy() {
083: // allow destruction by clients
084: destroy(true);
085: }
086:
087: protected void warnPoolIsEmpty() {
088: // disable the warning
089: // as this Pool is single threaded
090: // by definition there's no point
091: // in issueing a warning that you should increase
092: // the pool size
093: }
094: };
095: }
096:
097: return delegate.newRPPoolManager();
098: }
099:
100: private interface FactoryDelegate {
101: RPPoolManager newRPPoolManager();
102:
103: void destroy();
104: }
105:
106: private class SharedPoolFactory implements FactoryDelegate {
107: private final RPPoolManager sharedInstance = new RPPoolManager(
108: orb.getPOACurrent(), threadPoolMin, threadPoolMax,
109: logger, configuration) {
110: void destroy() {
111: // ignore request as this is a shared pool.
112: // the pool will be destroyed with the enclosing factory.
113: }
114: };
115:
116: public RPPoolManager newRPPoolManager() {
117: return sharedInstance;
118: }
119:
120: public void destroy() {
121: sharedInstance.destroy(true);
122: }
123: }
124:
125: private class DefaultPoolFactory implements FactoryDelegate {
126: public RPPoolManager newRPPoolManager() {
127: return new RPPoolManager(orb.getPOACurrent(),
128: threadPoolMin, threadPoolMax, logger, configuration) {
129: void destroy() {
130: // allow destruction by clients
131: destroy(true);
132: }
133: };
134: }
135:
136: public void destroy() {
137: // nothing to do. each created poolManager is associated to exactly one
138: // poa which will destroy it as necessary
139: }
140: }
141: }
|