KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * Provide a default handler mapping, used by the XmlRpcServer. This
25  * mapping supports the special handler name "$default" that will
26  * handle otherwise unhandled requests.
27  *
28  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
29  * @author Daniel L. Rall
30  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
31  * @see org.apache.xmlrpc.XmlRpcServer
32  * @since 1.2
33  */

34 public class DefaultHandlerMapping
35     implements XmlRpcHandlerMapping
36 {
37     private Hashtable JavaDoc handlers;
38
39     /**
40      * Create a new mapping.
41      */

42     public DefaultHandlerMapping()
43     {
44         handlers = new Hashtable JavaDoc();
45     }
46
47     /**
48      * Register a handler object with this name. Methods of this
49      * objects will be callable over XML-RPC as
50      * "handlername.methodname". For more information about XML-RPC
51      * handlers see the <a HREF="../index.html#1a">main documentation
52      * page</a>.
53      *
54      * @param handlername The name to identify the handler by.
55      * @param handler The handler itself.
56      */

57     public void addHandler(String JavaDoc handlerName, Object JavaDoc handler)
58     {
59         if (handler instanceof XmlRpcHandler ||
60                 handler instanceof AuthenticatedXmlRpcHandler ||
61                 handler instanceof ContextXmlRpcHandler)
62         {
63             handlers.put(handlerName, handler);
64         }
65         else if (handler != null)
66         {
67             handlers.put(handlerName, new Invoker(handler));
68         }
69     }
70
71     /**
72      * Remove a handler object that was previously registered with
73      * this server.
74      *
75      * @param handlerName The name identifying the handler to remove.
76      */

77     public void removeHandler(String JavaDoc handlerName)
78     {
79         handlers.remove(handlerName);
80     }
81
82     /**
83      * Find the handler and its method name for a given method.
84      * Implements the <code>XmlRpcHandlerMapping</code> interface.
85      *
86      * @param methodName The name of the XML-RPC method to find a
87      * handler for (this is <i>not</i> the Java method name).
88      * @return A handler object and method name.
89      * @see org.apache.xmlrpc.XmlRpcHandlerMapping#getHandler(String)
90      */

91     public Object JavaDoc getHandler(String JavaDoc methodName)
92         throws Exception JavaDoc
93     {
94         Object JavaDoc handler = null;
95         String JavaDoc handlerName = null;
96         int dot = methodName.lastIndexOf('.');
97         if (dot > -1)
98         {
99             // The last portion of the XML-RPC method name is the Java
100
// method name.
101
handlerName = methodName.substring(0, dot);
102             handler = handlers.get(handlerName);
103         }
104
105         if (handler == null)
106         {
107             handler = handlers.get("$default");
108
109             if (handler == null)
110             {
111                 if (dot > -1)
112                 {
113                     throw new Exception JavaDoc("RPC handler object \""
114                                         + handlerName + "\" not found and no "
115                                         + "default handler registered");
116                 }
117                 else
118                 {
119                     throw new Exception JavaDoc("RPC handler object not found for \""
120                                         + methodName
121                                         + "\": No default handler registered");
122                 }
123             }
124         }
125
126         return handler;
127     }
128 }
129
Popular Tags