Coverage Report - org.apache.xmlrpc.common.XmlRpcWorkerFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcWorkerFactory
68% 
60% 
2,333
 
 1  
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.common;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.xmlrpc.XmlRpcLoadException;
 22  
 import org.apache.xmlrpc.client.XmlRpcClientWorker;
 23  
 
 24  
 
 25  
 /** A factory for {@link org.apache.xmlrpc.client.XmlRpcClientWorker}
 26  
  * instances.
 27  
  */
 28  
 public abstract class XmlRpcWorkerFactory {
 29  518
         private final XmlRpcWorker singleton = newWorker();
 30  
         private final XmlRpcController controller;
 31  518
         private final List pool = new ArrayList();
 32  
         private int numThreads;
 33  
 
 34  
         /** Creates a new instance.
 35  
          * @param pController The client controlling the factory.
 36  
          */
 37  518
         public XmlRpcWorkerFactory(XmlRpcController pController) {
 38  518
                 controller = pController;
 39  518
         }
 40  
 
 41  
         /** Creates a new worker instance.
 42  
          * @return New instance of {@link XmlRpcClientWorker}.
 43  
          */
 44  
         protected abstract XmlRpcWorker newWorker();
 45  
 
 46  
         /** Returns the factory controller.
 47  
          * @return The controller, an instance of
 48  
          * {@link org.apache.xmlrpc.client.XmlRpcClient}, or
 49  
          * {@link org.apache.xmlrpc.server.XmlRpcServer}.
 50  
          */
 51  
         public XmlRpcController getController() {
 52  747
                 return controller;
 53  
         }
 54  
 
 55  
         /** Returns a worker for synchronous processing.
 56  
          * @return An instance of {@link XmlRpcClientWorker}, which is ready
 57  
          * for use.
 58  
          * @throws XmlRpcLoadException The clients maximum number of concurrent
 59  
          * threads is exceeded.
 60  
          */
 61  
         public synchronized XmlRpcWorker getWorker() throws XmlRpcLoadException {
 62  747
                 int max = controller.getMaxThreads();
 63  747
                 if (max > 0  &&  numThreads == max) {
 64  0
                         throw new XmlRpcLoadException("Maximum number of concurrent requests exceeded: " + max);
 65  
                 }
 66  747
                 ++numThreads;
 67  747
                 if (max == 0) {
 68  747
                         return singleton;
 69  
                 }
 70  0
                 if (pool.size() == 0) {
 71  0
                         return newWorker();
 72  
                 } else {
 73  0
                         return (XmlRpcClientWorker) pool.remove(pool.size() - 1);
 74  
                 }
 75  
         }
 76  
 
 77  
         /** Called, when the worker did its job. Frees resources and
 78  
          * decrements the number of concurrent requests.
 79  
          * @param pWorker The worker being released.
 80  
          */
 81  
         public synchronized void releaseWorker(XmlRpcClientWorker pWorker) {
 82  432
                 --numThreads;
 83  432
                 int max = controller.getMaxThreads();
 84  432
                 if (pWorker == singleton) {
 85  
                         // Do nothing, it's the singleton
 86  
                 } else {
 87  0
                         if (pool.size() < max) {
 88  0
                                 pool.add(pWorker);
 89  
                         }
 90  
                 }
 91  432
         }
 92  
 
 93  
         /** Returns the number of currently running requests.
 94  
          * @return Current number of concurrent requests.
 95  
          */
 96  
         public synchronized int getCurrentRequests() {
 97  0
                 return numThreads;
 98  
         }
 99  
 }