KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > engine > SOAPClientEngine


1 /*
2  * $Id: SOAPClientEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.engine;
26
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.rpc.ParameterMode JavaDoc;
38 import javax.xml.rpc.ServiceException JavaDoc;
39
40 import org.apache.axis.Message;
41 import org.apache.axis.client.Call;
42 import org.apache.axis.client.Service;
43 import org.apache.axis.encoding.XMLType;
44 import org.apache.axis.message.RPCElement;
45 import org.apache.axis.message.RPCParam;
46 import org.apache.axis.message.SOAPEnvelope;
47 import org.ofbiz.service.GenericServiceException;
48 import org.ofbiz.service.ModelParam;
49 import org.ofbiz.service.ModelService;
50 import org.ofbiz.service.ServiceDispatcher;
51 import org.ofbiz.base.util.Debug;
52 import org.ofbiz.base.util.UtilValidate;
53
54 /**
55  * Generic Service SOAP Interface
56  *
57  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
58  * @author <a HREF="mailto:">Andy Chen</a>
59  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
60  * @version $Rev: 5462 $
61  * @since 2.0
62  */

63 public final class SOAPClientEngine extends GenericAsyncEngine {
64     
65     public static final String JavaDoc module = SOAPClientEngine.class.getName();
66     
67     public SOAPClientEngine(ServiceDispatcher dispatcher) {
68         super(dispatcher);
69     }
70     
71     /**
72      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
73      */

74     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
75         runSync(localName, modelService, context);
76     }
77     
78     /**
79      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
80      */

81     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
82         Object JavaDoc result = serviceInvoker(modelService, context);
83         
84         if (result == null)
85             throw new GenericServiceException("Service did not return expected result");
86         if (!(result instanceof Map JavaDoc)) {
87             Map JavaDoc newResult = new HashMap JavaDoc();
88             
89             newResult.put("result", result);
90             return newResult;
91         }
92         return (Map JavaDoc) result;
93     }
94     
95     // Invoke the remote SOAP service
96
private Object JavaDoc serviceInvoker(ModelService modelService, Map JavaDoc context) throws GenericServiceException {
97         if (modelService.location == null || modelService.invoke == null)
98             throw new GenericServiceException("Cannot locate service to invoke");
99         
100         Service service = null;
101         Call call = null;
102         
103         try {
104             service = new Service();
105             call = (Call) service.createCall();
106         } catch (javax.xml.rpc.JAXRPCException JavaDoc e) {
107             throw new GenericServiceException("RPC service error", e);
108         } catch (ServiceException JavaDoc e) {//Add by Andy.Chen 2003.01.15
109
throw new GenericServiceException("RPC service error", e);
110         }
111         
112         URL JavaDoc endPoint = null;
113         
114         try {
115             endPoint = new URL JavaDoc(this.getLocation(modelService));
116         } catch (MalformedURLException JavaDoc e) {
117             throw new GenericServiceException("Location not a valid URL", e);
118         }
119         
120         List JavaDoc inModelParamList = modelService.getInModelParamList();
121         Object JavaDoc[] params = new Object JavaDoc[inModelParamList.size()];
122         
123         if (Debug.infoOn()) Debug.logInfo("[SOAPClientEngine.invoke] : Parameter length - " + params.length, module);
124         
125         call.setTargetEndpointAddress(endPoint);
126         
127         if (UtilValidate.isNotEmpty(modelService.nameSpace)){
128             call.setOperationName(new QName JavaDoc(modelService.nameSpace, modelService.invoke));
129         } else {
130             call.setOperationName(modelService.invoke);
131         }
132         
133         int i = 0;
134         
135         call.setOperation(call.getOperationName().getLocalPart());
136         Vector JavaDoc vParams = new Vector JavaDoc();
137         Iterator JavaDoc iter = inModelParamList.iterator();
138         while (iter.hasNext()) {
139             ModelParam p = (ModelParam) iter.next();
140             
141             if (Debug.infoOn()) Debug.logInfo("[SOAPClientEngine.invoke} : Parameter: " + p.name + " (" + p.mode + ") - " + i, module);
142             
143             //Exclude params that ModelServiceReader insert into
144
if(!p.name.trim().equals("userLogin") && !p.name.trim().equals("locale")) {
145                 QName JavaDoc qName = call.getParameterTypeByName(p.name); //.getTypeMapping().getTypeQName((Class) ObjectType.classNameClassMap.get(p.type));
146
call.addParameter(p.name, qName, getMode(p.mode));
147                 vParams.add(context.get(p.name));
148             }
149             
150             // if the value is null, that's fine, it will go in null...
151
params[i] = context.get(p.name);
152             
153             i++;
154         }
155         
156         call.setReturnType(XMLType.XSD_ANYTYPE);
157         params=vParams.toArray();
158         
159         Object JavaDoc result = null;
160         
161         try {
162             Debug.logInfo("[SOAPClientEngine.invoke] : Sending Call To SOAP Server", module);
163             result = call.invoke(params);
164         } catch (java.rmi.RemoteException JavaDoc e) {
165             throw new GenericServiceException("RPC error", e);
166         }
167         if (Debug.verboseOn()) {
168             Debug.log("SOAP Service Result - " + result, module);
169         }
170
171         return getResponseParams(call.getMessageContext().getResponseMessage());
172     }
173         
174     private Map JavaDoc getResponseParams(Message respMessage) {
175         Map JavaDoc mRet = new Hashtable JavaDoc();
176         try {
177             SOAPEnvelope resEnv = (SOAPEnvelope) respMessage.getSOAPEnvelope();
178             List JavaDoc bodies = resEnv.getBodyElements();
179             Iterator JavaDoc i = bodies.iterator();
180             while (i.hasNext()) {
181                 Object JavaDoc o = i.next();
182
183                 if (o instanceof RPCElement) {
184                     RPCElement body = (RPCElement) o;
185                     List JavaDoc params = null;
186                     params = body.getParams();
187
188                     Iterator JavaDoc p = params.iterator();
189                     while (p.hasNext()) {
190                         RPCParam param = (RPCParam) p.next();
191                         mRet.put(param.getName(), param.getValue());
192                         if (Debug.verboseOn()) {
193                             Debug.log("SOAP Client Param - " + param.getName() + "=" + param.getValue(), module);
194                         }
195                     }
196                 }
197             }
198         } catch (org.apache.axis.AxisFault e) {
199             Debug.logError(e, "AxisFault", module);
200         } catch (org.xml.sax.SAXException JavaDoc e) {
201             Debug.logError(e, "SAXException", module);
202         }
203         return mRet;
204     }
205         
206     private ParameterMode JavaDoc getMode(String JavaDoc sMode) {
207         if (sMode.equals("IN")) {
208             return ParameterMode.IN;
209         } else if (sMode.equals("OUT")) {
210             return ParameterMode.OUT;
211         } else if (sMode.equals("INOUT")) {
212             return ParameterMode.INOUT;
213         } else {
214             return null;
215         }
216     }
217 }
218
Popular Tags