KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlRpcServer


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
17
18 package org.apache.xmlrpc;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.EmptyStackException JavaDoc;
22 import java.util.Stack JavaDoc;
23
24 /**
25  * A multithreaded, reusable XML-RPC server object. The name may be misleading
26  * because this does not open any server sockets. Instead it is fed by passing
27  * an XML-RPC input stream to the execute method. If you want to open a
28  * HTTP listener, use the WebServer class instead.
29  *
30  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
31  * @author Daniel L. Rall
32  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
33  */

34 public class XmlRpcServer
35 {
36     private Stack JavaDoc pool;
37     private int nbrWorkers;
38
39     /**
40      * The maximum number of threads which can be used concurrently, by defaut use the one defined
41      * in XmlRpc
42      */

43     private int maxThreads = -1;
44
45     /**
46      * We want the <code>$default</code> handler to always be
47      * available.
48      */

49     private DefaultHandlerMapping handlerMapping;
50
51     /**
52      * Construct a new XML-RPC server. You have to register handlers
53      * to make it do something useful.
54      */

55     public XmlRpcServer()
56     {
57         pool = new Stack JavaDoc();
58         nbrWorkers = 0;
59         handlerMapping = new DefaultHandlerMapping();
60     }
61
62     /**
63      * @see org.apache.xmlrpc.DefaultHandlerMapping#addHandler(String, Object)
64      */

65     public void addHandler(String JavaDoc handlerName, Object JavaDoc handler)
66     {
67         handlerMapping.addHandler(handlerName, handler);
68     }
69
70     /**
71      * @see org.apache.xmlrpc.DefaultHandlerMapping#removeHandler(String)
72      */

73     public void removeHandler(String JavaDoc handlerName)
74     {
75         handlerMapping.removeHandler(handlerName);
76     }
77
78     /**
79      * Return the current XmlRpcHandlerMapping.
80      */

81     public XmlRpcHandlerMapping getHandlerMapping()
82     {
83         return handlerMapping;
84     }
85
86     /**
87      * Set the MaxThreads for this Client
88      */

89     public void setMaxThreads(int maxThreads)
90     {
91         this.maxThreads = maxThreads;
92     }
93     
94     /**
95      * Get the MaxThreads for this Server
96      */

97     public int getMaxThreads()
98     {
99         if (maxThreads == -1)
100             return (XmlRpc.getMaxThreads());
101         
102         return (maxThreads);
103     }
104     
105     /**
106      * Parse the request and execute the handler method, if one is
107      * found. Returns the result as XML. The calling Java code
108      * doesn't need to know whether the call was successful or not
109      * since this is all packed into the response. No context information
110      * is passed.
111      */

112     public byte[] execute(InputStream JavaDoc is)
113     {
114         return execute(is, new DefaultXmlRpcContext(null, null, getHandlerMapping()));
115     }
116
117     /**
118      * Parse the request and execute the handler method, if one is
119      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
120      * use the credentials to authenticate the user. No context information
121      * is passed.
122      */

123     public byte[] execute(InputStream JavaDoc is, String JavaDoc user, String JavaDoc password)
124     {
125         return execute(is, new DefaultXmlRpcContext(user, password, getHandlerMapping()));
126     }
127     
128     /**
129      * Parse the request and execute the handler method, if one is
130      * found. If the invoked handler is AuthenticatedXmlRpcHandler,
131      * use the credentials to authenticate the user. Context information
132      * is passed to the worker, and may be passed to the request handler.
133      */

134     public byte[] execute(InputStream JavaDoc 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     /**
148      * Hands out pooled workers.
149      *
150      * @return A worker (never <code>null</code>).
151      * @throws RuntimeException If the server exceeds its maximum
152      * number of allowed requests.
153      */

154     protected XmlRpcWorker getWorker()
155     {
156         try
157         {
158             return (XmlRpcWorker) pool.pop();
159         }
160         catch(EmptyStackException JavaDoc 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 JavaDoc("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