KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > ksoap > ConversionSoapHelper


1 /*
2  * Copyright (C) 2002 - INRIA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): Nicolas Tachker (ScalAgent)
20  * Contributor(s):
21  */

22 package com.scalagent.kjoram.ksoap;
23
24 import com.scalagent.kjoram.excepts.MessageFormatException;
25 import com.scalagent.kjoram.jms.*;
26 import com.scalagent.ksoap.SoapObject;
27
28 import java.lang.*;
29 import java.util.Vector JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 /**
33  * The <code>ConversionSoapHelper</code> class allows the conversion
34  * from an AbstractJmsRequest(or AbstractJmsReply) to a SoapObject and vice versa.
35  */

36 public class ConversionSoapHelper {
37   
38   public static final String JavaDoc NAMESPACE = "urn:ProxyService";
39
40   /**
41    * convert an AbstractJmsRequest to a SoapObject
42    */

43   public static SoapObject getSoapObject(AbstractJmsRequest request,
44                                          String JavaDoc name,
45                                          int cnxId)
46     throws MessageFormatException {
47     SoapObject sO = null;
48
49     if (request instanceof GetReply) {
50       sO = getSoapObject((GetReply)request);
51     } else {
52       sO = new SoapObject(NAMESPACE, "send");
53       sO.addProperty("name",name);
54       sO.addProperty("cnxId",new Integer JavaDoc(cnxId));
55       Hashtable JavaDoc h = request.soapCode();
56       if (h != null)
57         sO.addProperty("map",h);
58     }
59     return sO;
60   }
61
62   /**
63    * convert a SoapObject to Object
64    */

65   public static Object JavaDoc getObject(SoapObject sO)
66     throws MessageFormatException {
67
68     Hashtable JavaDoc h = null;
69     String JavaDoc nameSpace = sO.getNamespace();
70     String JavaDoc name = sO.getName();
71
72     if (nameSpace.equals("urn:ProxyService")) {
73       if (name.equals("getReplyResponse")) {
74         Object JavaDoc o = sO.getProperty("return");
75         h = (Hashtable JavaDoc) sO.getProperty("return");
76       } else if (name.equals("setConnectionResponse")) {
77         return new Integer JavaDoc(getSetCnx(sO));
78       } else if (name.equals("sendResponse")) {
79         return null;
80       } else {
81         throw new MessageFormatException("SoapObject " + name
82                                          + " can't be converted to a Hashtable.");
83       }
84     } else {
85       throw new MessageFormatException("SoapObject " + nameSpace
86                                        + " != urn:ProxyService.");
87     }
88
89     String JavaDoc className = (String JavaDoc) h.get("className");
90     if (className == null)
91       throw new MessageFormatException( "SoapObject " + name
92                                         + " no className found.");
93
94       if (className.equals("org.objectweb.joram.shared.client.CnxConnectReply")){
95       return CnxConnectReply.soapDecode(h);
96     } else if (className.equals("org.objectweb.joram.shared.client.ServerReply")){
97       return ServerReply.soapDecode(h);
98     } else if (className.equals("org.objectweb.joram.shared.client.MomExceptionReply")){
99       return MomExceptionReply.soapDecode(h);
100     } else if (className.equals("org.objectweb.joram.shared.client.ProducerMessages")){
101       return ProducerMessages.soapDecode(h);
102     } else if (className.equals("org.objectweb.joram.shared.client.QBrowseReply")){
103       return QBrowseReply.soapDecode(h);
104     } else if (className.equals("org.objectweb.joram.shared.client.SessCreateTDReply")){
105       return SessCreateTDReply.soapDecode(h);
106     } else if (className.equals("org.objectweb.joram.shared.client.CnxCloseReply")){
107       return CnxCloseReply.soapDecode(h);
108     } else if (className.equals("org.objectweb.joram.shared.client.CnxConnectReply")){
109       return CnxConnectReply.soapDecode(h);
110     } else if (className.equals("org.objectweb.joram.shared.client.ConsumerMessages")){
111       return ConsumerMessages.soapDecode(h);
112     } else if (className.equals("org.objectweb.joram.shared.client.GetAdminTopicReply")){
113       return GetAdminTopicReply.soapDecode(h);
114     } else {
115       throw new MessageFormatException( "SoapObject " + className
116                                         + " can't be converted to an Object.");
117     }
118   }
119   
120   static SoapObject getSoapObject(SetCnx request) {
121     SoapObject sO = new SoapObject(NAMESPACE, "setConnection");
122     sO.addProperty("name", request.name);
123     sO.addProperty("password", request.password);
124     sO.addProperty("timeout", request.timeout);
125     return sO;
126   }
127
128   static int getSetCnx(SoapObject sO) {
129     int ret = -1;
130     try {
131       ret = ((Integer JavaDoc) sO.getProperty("return")).intValue();
132       if (ret < 0)
133         ret = -1;
134     } catch (Exception JavaDoc e) {ret = -1;}
135     return ret;
136   }
137
138   static private SoapObject getSoapObject(GetReply request) {
139     SoapObject sO = new SoapObject(NAMESPACE, "getReply");
140     sO.addProperty("name", request.name);
141     sO.addProperty("cnxId", new Integer JavaDoc(request.cnxId));
142     return sO;
143   }
144 }
145
Popular Tags