KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > processors > wsdl2 > internal > ParameterProcessor


1 package org.objectweb.celtix.tools.processors.wsdl2.internal;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.jws.soap.SOAPBinding;
10 import javax.wsdl.Message;
11 import javax.wsdl.Part;
12 import javax.xml.namespace.QName JavaDoc;
13
14 import com.sun.codemodel.JType;
15 import com.sun.tools.xjc.api.Property;
16
17 import org.objectweb.celtix.tools.common.ProcessorEnvironment;
18 import org.objectweb.celtix.tools.common.ToolException;
19 import org.objectweb.celtix.tools.common.model.JavaAnnotation;
20 import org.objectweb.celtix.tools.common.model.JavaMethod;
21 import org.objectweb.celtix.tools.common.model.JavaParameter;
22 import org.objectweb.celtix.tools.common.model.JavaReturn;
23 import org.objectweb.celtix.tools.common.model.JavaType;
24 import org.objectweb.celtix.tools.utils.ProcessorUtil;
25
26 public class ParameterProcessor extends AbstractProcessor {
27
28     public ParameterProcessor(ProcessorEnvironment penv) {
29         super(penv);
30     }
31
32     public void process(JavaMethod method, Message inputMessage, Message outputMessage,
33                         boolean isRequestResponse, List JavaDoc<String JavaDoc> parameterOrder) throws ToolException {
34
35         boolean parameterOrderPresent = false;
36
37         if (parameterOrder != null && !parameterOrder.isEmpty()) {
38             parameterOrderPresent = true;
39         }
40
41         if (parameterOrderPresent && isValidOrdering(parameterOrder, inputMessage, outputMessage)
42             && !method.isWrapperStyle()) {
43             buildParamModelsWithOrdering(method, inputMessage, outputMessage, isRequestResponse,
44                                          parameterOrder);
45         } else {
46             buildParamModelsWithoutOrdering(method, inputMessage, outputMessage, isRequestResponse);
47         }
48     }
49
50     /**
51      * This method will be used by binding processor to change existing
52      * generated java method of porttype
53      *
54      * @param method
55      * @param part
56      * @param style
57      * @throws ToolException
58      */

59     public JavaParameter addParameterFromBinding(JavaMethod method, Part part, JavaType.Style style)
60         throws ToolException {
61         return addParameter(method, getParameterFromPart(method, part, style));
62     }
63
64     private JavaParameter getParameterFromPart(JavaMethod method, Part part, JavaType.Style style) {
65         String JavaDoc name = ProcessorUtil.resolvePartName(part);
66         String JavaDoc namespace = ProcessorUtil.resolvePartNamespace(part);
67         String JavaDoc type = ProcessorUtil.resolvePartType(part, this.env);
68
69         JavaParameter parameter = new JavaParameter(name, type, namespace);
70         parameter.setPartName(part.getName());
71         parameter.setQName(ProcessorUtil.getElementName(part));
72
73         parameter.setClassName(ProcessorUtil.getFullClzName(part, env, this.collector));
74
75         if (style == JavaType.Style.INOUT || style == JavaType.Style.OUT) {
76             parameter.setHolder(true);
77             parameter.setHolderName(javax.xml.ws.Holder.class.getName());
78             parameter.setHolderClass(ProcessorUtil.getFullClzName(part, env, true, this.collector));
79         }
80         parameter.setStyle(style);
81         return parameter;
82     }
83
84     private JavaParameter addParameter(JavaMethod method, JavaParameter parameter) throws ToolException {
85         JavaAnnotation webParamAnnotation = new JavaAnnotation("WebParam");
86         String JavaDoc name = parameter.getName();
87         String JavaDoc targetNamespace = method.getInterface().getNamespace();
88         String JavaDoc partName = null;
89
90         if (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT) {
91             targetNamespace = parameter.getTargetNamespace();
92             if (parameter.getQName() != null) {
93                 name = parameter.getQName().getLocalPart();
94             }
95             if (!method.isWrapperStyle()) {
96                 partName = parameter.getPartName();
97             }
98         }
99
100         if (method.getSoapStyle() == SOAPBinding.Style.RPC) {
101             name = parameter.getPartName();
102             partName = parameter.getPartName();
103         }
104
105         if (partName != null) {
106             webParamAnnotation.addArgument("partName", partName);
107         }
108         if (parameter.getStyle() == JavaType.Style.OUT || parameter.getStyle() == JavaType.Style.INOUT) {
109             webParamAnnotation.addArgument("mode", "Mode." + parameter.getStyle().toString(), "");
110         }
111         webParamAnnotation.addArgument("name", name);
112         if (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT) {
113             webParamAnnotation.addArgument("targetNamespace", targetNamespace);
114         }
115
116         parameter.setAnnotation(webParamAnnotation);
117
118         method.addParameter(parameter);
119
120         return parameter;
121     }
122
123     private void processReturn(JavaMethod method, Part part) {
124         String JavaDoc name = part == null ? "return" : part.getName();
125         String JavaDoc type = part == null ? "void" : ProcessorUtil.resolvePartType(part, this.env);
126         String JavaDoc namespace = part == null ? null : ProcessorUtil.resolvePartNamespace(part);
127
128         JavaReturn returnType = new JavaReturn(name, type, namespace);
129         returnType.setQName(ProcessorUtil.getElementName(part));
130         returnType.setStyle(JavaType.Style.OUT);
131         if (namespace != null && type != null && !"void".equals(type)) {
132             returnType.setClassName(ProcessorUtil.getFullClzName(part, env, this.collector));
133         }
134         method.setReturn(returnType);
135     }
136
137     @SuppressWarnings JavaDoc("unchecked")
138     private void processInput(JavaMethod method, Message inputMessage) throws ToolException {
139         Map JavaDoc<String JavaDoc, Part> inputPartsMap = inputMessage.getParts();
140         Collection JavaDoc<Part> inputParts = inputPartsMap.values();
141         for (Part part : inputParts) {
142             addParameter(method, getParameterFromPart(method, part, JavaType.Style.IN));
143         }
144     }
145
146     @SuppressWarnings JavaDoc("unchecked")
147     private void processWrappedInput(JavaMethod method, Message inputMessage) throws ToolException {
148         Map JavaDoc<String JavaDoc, Part> inputPartsMap = inputMessage.getParts();
149         Collection JavaDoc<Part> inputParts = inputPartsMap.values();
150         if (inputParts.size() > 1) {
151             processInput(method, inputMessage);
152             return;
153         } else if (inputParts.isEmpty()) {
154             return;
155         }
156         Part part = inputParts.iterator().next();
157
158         List JavaDoc<? extends Property> block = ProcessorUtil.getBlock(part, env);
159         if (block != null) {
160             if (block.size() == 0) {
161                 // complete
162
}
163             for (Property item : block) {
164                 addParameter(method, getParameterFromProperty(item, JavaType.Style.IN, part));
165             }
166         }
167     }
168
169     @SuppressWarnings JavaDoc("unchecked")
170     private void processOutput(JavaMethod method, Message inputMessage, Message outputMessage,
171                                boolean isRequestResponse) throws ToolException {
172         Map JavaDoc<String JavaDoc, Part> inputPartsMap = inputMessage.getParts();
173         Map JavaDoc<String JavaDoc, Part> outputPartsMap = outputMessage.getParts();
174         Collection JavaDoc<Part> outputParts = outputPartsMap.values();
175         // figure out output parts that are not present in input parts
176
List JavaDoc<Part> outParts = new ArrayList JavaDoc<Part>();
177         if (isRequestResponse) {
178
179             for (Part outpart : outputParts) {
180                 Part inpart = inputPartsMap.get(outpart.getName());
181                 if (inpart == null) {
182                     outParts.add(outpart);
183                     continue;
184                 } else if (isSamePart(inpart, outpart)) {
185                     addParameter(method, getParameterFromPart(method, outpart, JavaType.Style.INOUT));
186                     continue;
187                 } else if (!isSamePart(inpart, outpart)) {
188                     outParts.add(outpart);
189                     continue;
190                 }
191                 // outParts.add(outpart);
192
}
193         }
194
195         if (isRequestResponse && outParts.size() == 1) {
196             processReturn(method, outputParts.iterator().next());
197             return;
198         } else {
199             processReturn(method, null);
200         }
201         if (isRequestResponse) {
202             for (Part part : outParts) {
203                 addParameter(method, getParameterFromPart(method, part, JavaType.Style.OUT));
204             }
205         }
206     }
207
208     @SuppressWarnings JavaDoc("unchecked")
209     private void processWrappedOutput(JavaMethod method, Message inputMessage, Message outputMessage,
210                                       boolean isRequestResponse) throws ToolException {
211         Map JavaDoc<String JavaDoc, Part> inputPartsMap = inputMessage.getParts();
212         Map JavaDoc<String JavaDoc, Part> outputPartsMap = outputMessage.getParts();
213         Collection JavaDoc<Part> outputParts = outputPartsMap.values();
214         Collection JavaDoc<Part> inputParts = inputPartsMap.values();
215
216         if (inputPartsMap.size() > 1 || outputPartsMap.size() > 1) {
217             processOutput(method, inputMessage, outputMessage, isRequestResponse);
218             return;
219         }
220
221         Part inputPart = inputParts.iterator().next();
222         Part outputPart = outputParts.iterator().next();
223         List JavaDoc<? extends Property> inputBlock = ProcessorUtil.getBlock(inputPart, env);
224         List JavaDoc<? extends Property> outputBlock = ProcessorUtil.getBlock(outputPart, env);
225
226         if (outputBlock == null || outputBlock.size() == 0) {
227             addVoidReturn(method);
228             return;
229         }
230         method.setReturn(null);
231         if (outputBlock.size() == 1) {
232             Property outElement = outputBlock.iterator().next();
233             boolean sameWrapperChild = false;
234             for (Property inElement : inputBlock) {
235                 if (isSameWrapperChild(inElement, outElement)) {
236                     addParameter(method, getParameterFromProperty(outElement, JavaType.Style.INOUT,
237                                                                   outputPart));
238                     sameWrapperChild = true;
239                     if (method.getReturn() == null) {
240                         addVoidReturn(method);
241                     }
242                     break;
243                 }
244             }
245             if (!sameWrapperChild) {
246                 method.setReturn(getReturnFromProperty(outElement, outputPart));
247             }
248             return;
249         }
250         method.setReturn(null);
251         for (Property outElement : outputBlock) {
252             if ("return".equals(outElement.elementName().getLocalPart())) {
253                 if (method.getReturn() != null) {
254                     org.objectweb.celtix.common.i18n.Message msg =
255                         new org.objectweb.celtix.common.i18n.Message("WRAPPER_STYLE_TWO_RETURN_TYPES", LOG);
256                     throw new ToolException(msg);
257                 }
258                 method.setReturn(getReturnFromProperty(outElement, outputPart));
259                 continue;
260             }
261             boolean sameWrapperChild = false;
262             for (Property inElement : inputBlock) {
263                 if (isSameWrapperChild(inElement, outElement)) {
264                     addParameter(method, getParameterFromProperty(outElement, JavaType.Style.INOUT,
265                                                                   outputPart));
266                     sameWrapperChild = true;
267                     break;
268                 }
269             }
270             if (!sameWrapperChild) {
271                 addParameter(method, getParameterFromProperty(outElement, JavaType.Style.OUT, outputPart));
272             }
273         }
274         if (method.getReturn() == null) {
275             addVoidReturn(method);
276         }
277     }
278
279     private void addVoidReturn(JavaMethod method) {
280         JavaReturn returnType = new JavaReturn("return", "void", null);
281         method.setReturn(returnType);
282     }
283
284     private boolean isSameWrapperChild(Property in, Property out) {
285         if (!in.name().equals(out.name())) {
286             return false;
287         }
288         if (!in.type().fullName().equals(out.type().fullName())) {
289             return false;
290         }
291         if (!in.elementName().getNamespaceURI().equals(out.elementName().getNamespaceURI())) {
292             return false;
293         }
294         return true;
295     }
296
297     private JavaParameter getParameterFromProperty(Property property, JavaType.Style style, Part part) {
298         JType t = property.type();
299         String JavaDoc targetNamespace = ProcessorUtil.resolvePartNamespace(part);
300         if (targetNamespace == null) {
301             targetNamespace = property.elementName().getNamespaceURI();
302         }
303         JavaParameter parameter = new JavaParameter(property.name(), t.fullName(), targetNamespace);
304         parameter.setStyle(style);
305         parameter.setQName(property.elementName());
306         if (style == JavaType.Style.OUT || style == JavaType.Style.INOUT) {
307             parameter.setHolder(true);
308             parameter.setHolderName(javax.xml.ws.Holder.class.getName());
309             parameter.setHolderClass(t.boxify().fullName());
310         }
311         return parameter;
312     }
313
314     private JavaReturn getReturnFromProperty(Property property, Part part) {
315         JType t = property.type();
316         String JavaDoc targetNamespace = ProcessorUtil.resolvePartNamespace(part);
317         if (targetNamespace == null) {
318             targetNamespace = property.elementName().getNamespaceURI();
319         }
320         JavaReturn returnType = new JavaReturn(property.name(), t.fullName(), targetNamespace);
321         returnType.setQName(property.elementName());
322         returnType.setStyle(JavaType.Style.OUT);
323         return returnType;
324     }
325
326     private void buildParamModelsWithoutOrdering(JavaMethod method, Message inputMessage,
327                                                  Message outputMessage, boolean isRequestResponse)
328         throws ToolException {
329         if (inputMessage != null) {
330             if (method.isWrapperStyle()) {
331                 processWrappedInput(method, inputMessage);
332             } else {
333                 processInput(method, inputMessage);
334             }
335         }
336         if (outputMessage == null) {
337             processReturn(method, null);
338         } else {
339             if (method.isWrapperStyle()) {
340                 processWrappedOutput(method, inputMessage, outputMessage, isRequestResponse);
341             } else {
342                 processOutput(method, inputMessage, outputMessage, isRequestResponse);
343             }
344         }
345     }
346
347     @SuppressWarnings JavaDoc("unchecked")
348     private void buildParamModelsWithOrdering(JavaMethod method, Message inputMessage, Message outputMessage,
349                                               boolean isRequestResponse, List JavaDoc<String JavaDoc> parameterList)
350         throws ToolException {
351         Map JavaDoc<String JavaDoc, Part> inputPartsMap = inputMessage.getParts();
352         Map JavaDoc<String JavaDoc, Part> outputPartsMap = outputMessage.getParts();
353
354         Collection JavaDoc<Part> inputParts = inputPartsMap.values();
355         Collection JavaDoc<Part> outputParts = outputPartsMap.values();
356
357         List JavaDoc<Part> inputUnlistedParts = new ArrayList JavaDoc<Part>();
358         List JavaDoc<Part> outputUnlistedParts = new ArrayList JavaDoc<Part>();
359
360         for (Part part : inputParts) {
361             if (!parameterList.contains(part.getName())) {
362                 inputUnlistedParts.add(part);
363             }
364         }
365
366         if (isRequestResponse) {
367             for (Part part : outputParts) {
368                 if (!parameterList.contains(part.getName())) {
369                     Part inpart = inputMessage.getPart(part.getName());
370                     if (inpart == null || (inpart != null && !isSamePart(inpart, part))) {
371                         outputUnlistedParts.add(part);
372                     }
373                 }
374             }
375
376             if (outputUnlistedParts.size() == 1) {
377                 processReturn(method, outputUnlistedParts.get(0));
378                 outputPartsMap.remove(outputUnlistedParts.get(0));
379                 outputUnlistedParts.clear();
380             } else {
381                 processReturn(method, null);
382             }
383         }
384
385         // now create list of paramModel with parts
386
// first for the ordered list
387
int index = 0;
388         int size = parameterList.size();
389         while (index < size) {
390             String JavaDoc partName = parameterList.get(index);
391             Part part = inputPartsMap.get(partName);
392             JavaType.Style style = JavaType.Style.IN;
393             if (part == null) {
394                 part = outputPartsMap.get(partName);
395                 style = JavaType.Style.OUT;
396             } else if (outputPartsMap.get(partName) != null
397                 && isSamePart(part, outputPartsMap.get(partName))) {
398                 style = JavaType.Style.INOUT;
399             }
400             if (part != null) {
401                 addParameter(method, getParameterFromPart(method, part, style));
402             }
403             index++;
404         }
405         // now from unlisted input parts
406
for (Part part : inputUnlistedParts) {
407             addParameter(method, getParameterFromPart(method, part, JavaType.Style.IN));
408         }
409         // now from unlisted output parts
410
for (Part part : outputUnlistedParts) {
411             addParameter(method, getParameterFromPart(method, part, JavaType.Style.INOUT));
412         }
413     }
414
415     private boolean isSamePart(Part part1, Part part2) {
416         QName JavaDoc qname1 = part1.getElementName();
417         QName JavaDoc qname2 = part2.getElementName();
418         if (qname1 != null && qname2 != null) {
419             return qname1.equals(qname2);
420         }
421         qname1 = part1.getTypeName();
422         qname2 = part2.getTypeName();
423         if (qname1 != null && qname2 != null) {
424             return qname1.equals(qname2);
425         }
426         return false;
427     }
428
429     @SuppressWarnings JavaDoc("unchecked")
430     private boolean isValidOrdering(List JavaDoc<String JavaDoc> parameterOrder,
431                                     Message inputMessage, Message outputMessage) {
432         Iterator JavaDoc<String JavaDoc> params = parameterOrder.iterator();
433
434         Collection JavaDoc<Part> inputParts = inputMessage.getParts().values();
435         Collection JavaDoc<Part> outputParts = outputMessage.getParts().values();
436
437         boolean partFound = false;
438
439         while (params.hasNext()) {
440             String JavaDoc param = params.next();
441             partFound = false;
442             for (Part part : inputParts) {
443                 if (param.equals(part.getName())) {
444                     partFound = true;
445                     break;
446                 }
447             }
448             // if not found, check output parts
449
if (!partFound) {
450                 for (Part part : outputParts) {
451                     if (param.equals(part.getName())) {
452                         partFound = true;
453                         break;
454                     }
455                 }
456             }
457             if (!partFound) {
458                 break;
459             }
460         }
461         return partFound;
462     }
463 }
464
Popular Tags