KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > server > MessageRouter


1 package org.apache.soap.server;
2
3 import java.io.*;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import org.w3c.dom.*;
7 import org.apache.soap.util.Bean;
8 import org.apache.soap.util.MethodUtils;
9 import org.apache.soap.util.IOUtils;
10 import org.apache.soap.*;
11 import org.apache.soap.rpc.*;
12
13 /**
14  * This class is a transport independent SOAP message router. However you
15  * do it, if you get a SOAP envelope to me, I will deliver that to the
16  * right method of the object you give me to work on.
17  *
18  * @author Sanjiva Weerawarana <sanjiva@watson.ibm.com>
19  */

20 public class MessageRouter {
21   /**
22    * Check whether the message is valid - does the service publish it?
23    */

24   public static boolean validMessage (DeploymentDescriptor dd,
25                                       String JavaDoc messageName) {
26     String JavaDoc[] pubMessages = dd.getMethods ();
27     for (int i = 0; i < pubMessages.length; i++) {
28       if (messageName.equals (pubMessages[i])) {
29         return true;
30       }
31     }
32     return false;
33   }
34
35   /**
36    * Deliver the message to the appropriate method on the given target
37    * object.
38    */

39
40   public static void invoke (DeploymentDescriptor dd, Envelope env,
41                              Object JavaDoc targetObject, String JavaDoc messageName,
42                              SOAPContext reqCtx, SOAPContext resCtx)
43        throws SOAPException {
44     byte providerType = dd.getProviderType ();
45
46     try {
47       // Class[] argTypes = new Class[] { Envelope.class, PrintWriter.class };
48
// Object[] args = new Object[] { env, out };
49
Class JavaDoc[] argTypes = new Class JavaDoc[] { Envelope.class, SOAPContext.class,
50                                                        SOAPContext.class };
51       Object JavaDoc[] args = new Object JavaDoc[] { env, reqCtx, resCtx };
52
53       if (providerType == DeploymentDescriptor.PROVIDER_JAVA) {
54         Method m = MethodUtils.getMethod (targetObject, messageName,
55                                           argTypes);
56         Object JavaDoc resObj = m.invoke (targetObject, args);
57       } else {
58         // find the class that provides the BSF services (done
59
// this way via reflection to avoid a compile-time dependency on BSF)
60
Class JavaDoc bc = Class.forName ("org.apache.soap.server.InvokeBSF");
61
62         // now invoke the service
63
Class JavaDoc[] sig = {DeploymentDescriptor.class,
64                        Object JavaDoc.class,
65                        String JavaDoc.class,
66                        Object JavaDoc[].class};
67         Method m = MethodUtils.getMethod (bc, "service", sig, true);
68         m.invoke (null, new Object JavaDoc[] {dd, targetObject, messageName, args});
69       }
70     } catch (InvocationTargetException e) {
71       Throwable JavaDoc t = e.getTargetException();
72
73       if (t instanceof SOAPException) {
74         throw (SOAPException)t;
75       } else {
76         throw new SOAPException(Constants.FAULT_CODE_SERVER,
77                                 "Exception from service object: " +
78                                 t.getMessage (), t);
79       }
80     } catch (ClassNotFoundException JavaDoc e) {
81       throw new SOAPException (Constants.FAULT_CODE_SERVER,
82                                "Unable to load BSF: script services " +
83                                "unsupported without BSF", e);
84     } catch (Throwable JavaDoc t) {
85       throw new SOAPException (Constants.FAULT_CODE_SERVER,
86                                "Exception while handling service request: " +
87                                t.getMessage(), t);
88     }
89   }
90 }
91
Popular Tags