KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 /**
25  * Introspects handlers using Java Reflection to call methods matching
26  * a XML-RPC call.
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  */

32 public class Invoker implements XmlRpcHandler
33 {
34     private Object JavaDoc invokeTarget;
35     private Class JavaDoc targetClass;
36
37     public Invoker(Object JavaDoc target)
38     {
39         invokeTarget = target;
40         targetClass = (invokeTarget instanceof Class JavaDoc) ? (Class JavaDoc) invokeTarget :
41                 invokeTarget.getClass();
42         if (XmlRpc.debug)
43         {
44             System.out.println("Target object is " + targetClass);
45         }
46     }
47
48     /**
49      * main method, sucht methode in object, wenn gefunden dann aufrufen.
50      */

51     public Object JavaDoc execute(String JavaDoc methodName, Vector JavaDoc params) throws Exception JavaDoc
52     {
53         // Array mit Classtype bilden, ObjectAry mit Values bilden
54
Class JavaDoc[] argClasses = null;
55         Object JavaDoc[] argValues = null;
56         if (params != null)
57         {
58             argClasses = new Class JavaDoc[params.size()];
59             argValues = new Object JavaDoc[params.size()];
60             for (int i = 0; i < params.size(); i++)
61             {
62                 argValues[i] = params.elementAt(i);
63                 if (argValues[i] instanceof Integer JavaDoc)
64                 {
65                     argClasses[i] = Integer.TYPE;
66                 }
67                 else if (argValues[i] instanceof Double JavaDoc)
68                 {
69                     argClasses[i] = Double.TYPE;
70                 }
71                 else if (argValues[i] instanceof Boolean JavaDoc)
72                 {
73                     argClasses[i] = Boolean.TYPE;
74                 }
75                 else
76                 {
77                     argClasses[i] = argValues[i].getClass();
78                 }
79             }
80         }
81
82         // Methode da ?
83
Method JavaDoc method = null;
84
85         // The last element of the XML-RPC method name is the Java
86
// method name.
87
int dot = methodName.lastIndexOf('.');
88         if (dot > -1 && dot + 1 < methodName.length())
89         {
90             methodName = methodName.substring(dot + 1);
91         }
92
93         if (XmlRpc.debug)
94         {
95             System.out.println("Searching for method: " + methodName +
96                                " in class " + targetClass.getName());
97             for (int i = 0; i < argClasses.length; i++)
98             {
99                 System.out.println("Parameter " + i + ": " + argValues[i]
100                         + " (" + argClasses[i] + ')');
101             }
102         }
103
104         try
105         {
106             method = targetClass.getMethod(methodName, argClasses);
107         }
108         // Wenn nicht da dann entsprechende Exception returnen
109
catch(NoSuchMethodException JavaDoc nsm_e)
110         {
111             throw nsm_e;
112         }
113         catch(SecurityException JavaDoc s_e)
114         {
115             throw s_e;
116         }
117
118         // Our policy is to make all public methods callable except
119
// the ones defined in java.lang.Object.
120
if (method.getDeclaringClass() == Object JavaDoc.class)
121         {
122             throw new XmlRpcException(0, "Invoker can't call methods "
123                     + "defined in java.lang.Object");
124         }
125
126         // invoke
127
Object JavaDoc returnValue = null;
128         try
129         {
130             returnValue = method.invoke(invokeTarget, argValues);
131         }
132         catch(IllegalAccessException JavaDoc iacc_e)
133         {
134             throw iacc_e;
135         }
136         catch(IllegalArgumentException JavaDoc iarg_e)
137         {
138             throw iarg_e;
139         }
140         catch(InvocationTargetException JavaDoc it_e)
141         {
142             if (XmlRpc.debug)
143             {
144                 it_e.getTargetException().printStackTrace();
145             }
146             // check whether the thrown exception is XmlRpcException
147
Throwable JavaDoc t = it_e.getTargetException();
148             if (t instanceof XmlRpcException)
149             {
150                 throw (XmlRpcException) t;
151             }
152             // It is some other exception
153
throw new Exception JavaDoc(t.toString());
154         }
155         if (returnValue == null && method.getReturnType() == Void.TYPE)
156         {
157             // Not supported by the spec.
158
throw new IllegalArgumentException JavaDoc
159                 ("void return types for handler methods not supported");
160         }
161         return returnValue;
162     }
163 }
164
Popular Tags