KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > webservices > DynamicWebservice


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.deliver.util.webservices;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.xml.namespace.QName JavaDoc;
33 import javax.xml.rpc.ParameterMode JavaDoc;
34
35 import org.apache.axis.client.Call;
36 import org.apache.axis.client.Service;
37 import org.apache.axis.encoding.XMLType;
38 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
39 import org.apache.axis.encoding.ser.BeanSerializerFactory;
40 import org.apache.log4j.Logger;
41 import org.infoglue.cms.security.InfoGluePrincipal;
42
43 /**
44  *
45  */

46 class Parameter
47 {
48     public final String JavaDoc name;
49     public final QName JavaDoc type;
50     
51     public Parameter(final String JavaDoc name, final QName JavaDoc type)
52     {
53         this.name = name;
54         this.type = type;
55     }
56 }
57
58 /**
59  *
60  */

61 public class DynamicWebservice
62 {
63     /**
64      * The class logger.
65      */

66     private final static Logger logger = Logger.getLogger(DynamicWebservice.class.getName());
67     
68     /**
69      *
70      */

71     private static final String JavaDoc PRINCIPAL_ARGUMENT_NAME = "principal";
72     
73     /**
74      *
75      */

76     private static final String JavaDoc DEFAULT_NAMESPACE_URI = "http://soapinterop.org/";
77     
78     /**
79      *
80      */

81     private String JavaDoc targetEndpointAddress;
82     
83     /**
84      *
85      */

86     private String JavaDoc operationName;
87     
88     /**
89      *
90      */

91     private QName JavaDoc returnType;
92     
93     /**
94      *
95      */

96     private final List JavaDoc parameters = new ArrayList JavaDoc();
97     
98     /**
99      *
100      */

101     private final List JavaDoc arguments = new ArrayList JavaDoc();
102
103     /**
104      *
105      */

106     private final Map JavaDoc mappings = new HashMap JavaDoc(); // <Class> -> <QName>
107

108     /**
109      *
110      */

111     private final Map JavaDoc standardMappings = new HashMap JavaDoc(); // <Class> -> <QName>
112

113     /**
114      *
115      */

116     private Service service;
117     
118     /**
119      *
120      */

121     private Call call;
122     
123     /**
124      *
125      */

126     private Object JavaDoc result;
127     
128     /**
129      *
130      */

131     private DynamicWebserviceSerializer serializer;
132     
133     
134     /**
135      *
136      */

137     public DynamicWebservice(final InfoGluePrincipal remotePrincipal)
138     {
139         super();
140         configureStandardMappings();
141         serializer = new DynamicWebserviceSerializer(standardMappings);
142         addArgument(PRINCIPAL_ARGUMENT_NAME, remotePrincipal.getName());
143     }
144
145     /**
146      *
147      */

148     public void callService()
149     {
150         try
151         {
152             service = new Service();
153             call = (Call) service.createCall();
154             
155             configureBasic();
156             configureMappings();
157             configureParameters();
158             
159             result = call.invoke(arguments.toArray());
160         }
161         catch (Exception JavaDoc e)
162         {
163             e.printStackTrace();
164         }
165     }
166     
167     /**
168      *
169      */

170     private void configureStandardMappings()
171     {
172         standardMappings.put(Boolean JavaDoc.class, XMLType.XSD_BASE64);
173         standardMappings.put(Boolean JavaDoc.class, XMLType.XSD_BOOLEAN);
174         standardMappings.put(Double JavaDoc.class, XMLType.XSD_DOUBLE);
175         standardMappings.put(Float JavaDoc.class, XMLType.XSD_FLOAT);
176         standardMappings.put(Integer JavaDoc.class, XMLType.XSD_INT);
177         standardMappings.put(Long JavaDoc.class, XMLType.XSD_LONG);
178         standardMappings.put(String JavaDoc.class, XMLType.XSD_STRING);
179     }
180     
181     /**
182      *
183      */

184     private void configureBasic()
185     {
186         call.setTargetEndpointAddress(targetEndpointAddress);
187         call.setOperationName(new QName JavaDoc(DEFAULT_NAMESPACE_URI, operationName));
188         call.setEncodingStyle(Call.ENCODINGSTYLE_URI_PROPERTY);
189         call.setReturnType(returnType);
190     }
191     
192     /**
193      *
194      */

195     private void configureParameters()
196     {
197         for(final Iterator JavaDoc i = parameters.iterator(); i.hasNext(); )
198         {
199             final Parameter parameter = (Parameter) i.next();
200             call.addParameter(parameter.name, parameter.type, ParameterMode.IN);
201         }
202     }
203     
204     /**
205      *
206      */

207     private void configureMappings()
208     {
209         for(final Iterator JavaDoc i = mappings.keySet().iterator(); i.hasNext(); )
210         {
211             final Class JavaDoc clazz = (Class JavaDoc) i.next();
212             final QName JavaDoc type = (QName JavaDoc) mappings.get(clazz);
213             call.registerTypeMapping(clazz, type, BeanSerializerFactory.class, BeanDeserializerFactory.class);
214         }
215     }
216     
217     /**
218      *
219      */

220     public Object JavaDoc getResult()
221     {
222         return result;
223     }
224     
225     /**
226      *
227      */

228     public void setTargetEndpointAddress(final String JavaDoc targetEndpointAddress)
229     {
230         logger.debug("targetEndpointAddress=[" + targetEndpointAddress + "]");
231         this.targetEndpointAddress = targetEndpointAddress;
232     }
233
234     /**
235      *
236      */

237     public void setOperationName(final String JavaDoc operationName)
238     {
239         logger.debug("operationName=[" + operationName + "]");
240         this.operationName = operationName;
241     }
242     
243     /**
244      *
245      */

246     public void setReturnType(final Class JavaDoc c)
247     {
248         logger.debug("returnType=[" + (c==null ? "null" : getClassName(c)) + "]");
249         returnType = mappingForClass(c); // null is ok
250
}
251     
252     /**
253      *
254      */

255     public void addArgument(final String JavaDoc name, final Object JavaDoc value)
256     {
257         assertNameNotNull(name);
258         logger.debug("addArgument=[" + name + "," + value + "]");
259         addArgument(name, mappingForClass(value.getClass()), value);
260     }
261     
262     /**
263      *
264      */

265     public void addArgument(final String JavaDoc name, final Map JavaDoc value)
266     {
267         assertNameNotNull(name);
268
269         logger.debug("addArgument=[" + name + "," + value + "] (Map)");
270         addArgument(name, XMLType.SOAP_ARRAY, serializer.serializeMap(value).toArray());
271     }
272     
273     /**
274      *
275      */

276     public void addArgument(final String JavaDoc name, final Collection JavaDoc value)
277     {
278         assertNameNotNull(name);
279
280         logger.debug("addArgument=[" + name + "," + value + "] (Collection)");
281         addArgument(name, XMLType.SOAP_ARRAY, serializer.serializeCollection(value).toArray());
282     }
283     
284     /**
285      *
286      */

287     private void assertNameNotNull(final String JavaDoc argument)
288     {
289         if(argument == null)
290         {
291             throw new IllegalArgumentException JavaDoc("A parameter name can't be null.");
292         }
293     }
294     
295     /**
296      *
297      */

298     private void addArgument(final String JavaDoc name, final QName JavaDoc type, final Object JavaDoc value)
299     {
300         parameters.add(new Parameter(name, type));
301         arguments.add(value);
302     }
303
304     /**
305      *
306      */

307     private QName JavaDoc mappingForClass(final Class JavaDoc c)
308     {
309         if(c == null)
310         {
311             return null;
312         }
313         if(standardMappings.containsKey(c))
314         {
315             return (QName JavaDoc) standardMappings.get(c);
316         }
317         if(mappings.containsKey(c))
318         {
319             return (QName JavaDoc) mappings.get(c);
320         }
321         return addMapping(c);
322     }
323
324     /**
325      *
326      */

327     private QName JavaDoc addMapping(final Class JavaDoc c) {
328         final String JavaDoc className = getClassName(c);
329         final QName JavaDoc type = new QName JavaDoc(DEFAULT_NAMESPACE_URI + className, className);
330         mappings.put(c, type);
331         logger.debug("addMapping=[" + c.getName() + "," + type + "]");
332         return type;
333     }
334     
335     /**
336      *
337      */

338     private String JavaDoc getClassName(final Class JavaDoc c)
339     {
340         final int firstChar = c.getName().lastIndexOf('.') + 1;
341         return (firstChar > 0) ? c.getName().substring(firstChar) : c.getName();
342     }
343 }
Popular Tags