1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.InputStream ; 21 import java.util.EmptyStackException ; 22 import java.util.Stack ; 23 24 34 public class XmlRpcServer 35 { 36 private Stack pool; 37 private int nbrWorkers; 38 39 43 private int maxThreads = -1; 44 45 49 private DefaultHandlerMapping handlerMapping; 50 51 55 public XmlRpcServer() 56 { 57 pool = new Stack (); 58 nbrWorkers = 0; 59 handlerMapping = new DefaultHandlerMapping(); 60 } 61 62 65 public void addHandler(String handlerName, Object handler) 66 { 67 handlerMapping.addHandler(handlerName, handler); 68 } 69 70 73 public void removeHandler(String handlerName) 74 { 75 handlerMapping.removeHandler(handlerName); 76 } 77 78 81 public XmlRpcHandlerMapping getHandlerMapping() 82 { 83 return handlerMapping; 84 } 85 86 89 public void setMaxThreads(int maxThreads) 90 { 91 this.maxThreads = maxThreads; 92 } 93 94 97 public int getMaxThreads() 98 { 99 if (maxThreads == -1) 100 return (XmlRpc.getMaxThreads()); 101 102 return (maxThreads); 103 } 104 105 112 public byte[] execute(InputStream is) 113 { 114 return execute(is, new DefaultXmlRpcContext(null, null, getHandlerMapping())); 115 } 116 117 123 public byte[] execute(InputStream is, String user, String password) 124 { 125 return execute(is, new DefaultXmlRpcContext(user, password, getHandlerMapping())); 126 } 127 128 134 public byte[] execute(InputStream is, XmlRpcContext context) 135 { 136 XmlRpcWorker worker = getWorker(); 137 try 138 { 139 return worker.execute(is, context); 140 } 141 finally 142 { 143 pool.push(worker); 144 } 145 } 146 147 154 protected XmlRpcWorker getWorker() 155 { 156 try 157 { 158 return (XmlRpcWorker) pool.pop(); 159 } 160 catch(EmptyStackException x) 161 { 162 int maxThreads = getMaxThreads(); 163 if (nbrWorkers < maxThreads) 164 { 165 nbrWorkers += 1; 166 if (nbrWorkers >= maxThreads * .95) 167 { 168 System.out.println("95% of XML-RPC server threads in use"); 169 } 170 return createWorker(); 171 } 172 throw new RuntimeException ("System overload: Maximum number of " + 173 "concurrent requests (" + maxThreads + 174 ") exceeded"); 175 } 176 } 177 178 protected XmlRpcWorker createWorker() 179 { 180 return new XmlRpcWorker(handlerMapping); 181 } 182 } 183 | Popular Tags |