00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one 00003 * or more contributor license agreements. See the NOTICE file 00004 * distributed with this work for additional information 00005 * regarding copyright ownership. The ASF licenses this file 00006 * to you under the Apache License, Version 2.0 (the 00007 * "License"); you may not use this file except in compliance 00008 * with the License. You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, 00013 * software distributed under the License is distributed on an 00014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 00015 * KIND, either express or implied. See the License for the 00016 * specific language governing permissions and limitations 00017 * under the License. 00018 */ 00019 package org.apache.xmlrpc.common; 00020 00021 import java.util.ArrayList; 00022 import java.util.List; 00023 00024 00027 public abstract class XmlRpcWorkerFactory { 00028 private final XmlRpcWorker singleton = newWorker(); 00029 private final XmlRpcController controller; 00030 private final List pool = new ArrayList(); 00031 private int numThreads; 00032 00036 public XmlRpcWorkerFactory(XmlRpcController pController) { 00037 controller = pController; 00038 } 00039 00043 protected abstract XmlRpcWorker newWorker(); 00044 00050 public XmlRpcController getController() { 00051 return controller; 00052 } 00053 00060 public synchronized XmlRpcWorker getWorker() throws XmlRpcLoadException { 00061 int max = controller.getMaxThreads(); 00062 if (max > 0 && numThreads == max) { 00063 throw new XmlRpcLoadException("Maximum number of concurrent requests exceeded: " + max); 00064 } 00065 if (max == 0) { 00066 return singleton; 00067 } 00068 ++numThreads; 00069 if (pool.size() == 0) { 00070 return newWorker(); 00071 } else { 00072 return (XmlRpcWorker) pool.remove(pool.size() - 1); 00073 } 00074 } 00075 00080 public synchronized void releaseWorker(XmlRpcWorker pWorker) { 00081 --numThreads; 00082 int max = controller.getMaxThreads(); 00083 if (pWorker == singleton) { 00084 // Do nothing, it's the singleton 00085 } else { 00086 if (pool.size() < max) { 00087 pool.add(pWorker); 00088 } 00089 } 00090 } 00091 00095 public synchronized int getCurrentRequests() { 00096 return numThreads; 00097 } 00098 }