KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > deployment > OperationDescription


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.deployment;
8
9 // $Id: OperationDescription.java,v 1.9.2.1 2004/11/27 07:28:47 tdiesler Exp $
10

11 import org.jboss.logging.Logger;
12
13 import javax.xml.namespace.QName JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18
19 /**
20  * Abstracts an Axis service description
21  *
22  * @author thomas.diesler@jboss.org
23  * @since 08-June-2004
24  */

25 public class OperationDescription
26 {
27    // provide logging
28
private final Logger log = Logger.getLogger(OperationDescription.class);
29
30    // The java name of the operation
31
private String JavaDoc name;
32    private QName JavaDoc qname;
33    private QName JavaDoc returnQName;
34    private QName JavaDoc returnType;
35    private boolean oneWay;
36
37    private ArrayList JavaDoc parameters = new ArrayList JavaDoc();
38    private ArrayList JavaDoc faults = new ArrayList JavaDoc();
39
40    public OperationDescription(String JavaDoc name, QName JavaDoc qname)
41    {
42       if (name == null)
43          throw new IllegalArgumentException JavaDoc("Operation name cannot be null");
44
45       if (qname == null)
46          qname = new QName JavaDoc(name);
47
48       this.name = name;
49       this.qname = qname;
50    }
51
52    public String JavaDoc getJavaName()
53    {
54       return name;
55    }
56
57    public String JavaDoc getWsdlName()
58    {
59       return getQName().getLocalPart();
60    }
61
62    public QName JavaDoc getQName()
63    {
64       return qname;
65    }
66
67    public QName JavaDoc getReturnQName()
68    {
69       return returnQName;
70    }
71
72    public QName JavaDoc getReturnType()
73    {
74       return returnType;
75    }
76
77    public void setReturnQName(QName JavaDoc returnQName)
78    {
79       this.returnQName = returnQName;
80    }
81
82    public void setReturnType(QName JavaDoc returnType)
83    {
84       this.returnType = returnType;
85    }
86
87    public boolean isOneWay()
88    {
89       return oneWay;
90    }
91
92    public void setOneWay(boolean oneWay)
93    {
94       this.oneWay = oneWay;
95    }
96
97    public Iterator JavaDoc getParameters()
98    {
99       return parameters.iterator();
100    }
101
102    public void setParameters(List JavaDoc newList)
103    {
104       List JavaDoc oldList = parameters;
105       parameters = new ArrayList JavaDoc();
106
107       Iterator JavaDoc itNew = newList.iterator();
108       while (itNew.hasNext())
109       {
110          Parameter param = (Parameter)itNew.next();
111          log.debug("Reordering: " + param.getName());
112          parameters.add(param);
113          oldList.remove(param);
114       }
115
116       Iterator JavaDoc itOld = oldList.iterator();
117       while (itOld.hasNext())
118       {
119          Parameter param = (Parameter)itOld.next();
120          if (param.inHeader || param.outHeader)
121          {
122             log.debug("Appending header: " + param.getName());
123             parameters.add(param);
124          }
125          else
126          {
127             throw new IllegalStateException JavaDoc("Incomplete reorder list: " + param.getName());
128          }
129       }
130    }
131
132    public void addParameter(Parameter param)
133    {
134       parameters.add(param);
135    }
136
137    public void addFault(Fault fault)
138    {
139       faults.add(fault);
140    }
141
142    public Parameter getParameterForName(String JavaDoc partName)
143    {
144       Parameter param = null;
145       Iterator JavaDoc it = parameters.iterator();
146       while (param == null && it.hasNext())
147       {
148          Parameter pa = (Parameter)it.next();
149          if (pa.getName().equals(partName))
150             param = pa;
151       }
152       return param;
153    }
154
155    public Iterator JavaDoc getFaults()
156    {
157       return faults.iterator();
158    }
159
160    public void writeWSDD(PrintWriter JavaDoc out)
161    {
162       String JavaDoc name = getJavaName();
163       QName JavaDoc qname = getQName();
164       QName JavaDoc returnQName = getReturnQName();
165       QName JavaDoc returnType = getReturnType();
166
167       log.trace("Operation: " + this);
168
169       String JavaDoc xmlns = "";
170       String JavaDoc opPrefix = qname.getPrefix();
171       if (opPrefix.length() > 0)
172       {
173          xmlns = "xmlns:" + opPrefix + "='" + qname.getNamespaceURI() + "' ";
174       }
175
176       String JavaDoc qnameAttr = WSDDGenerator.getQNameAttrValue(qname);
177
178       if (returnType != null)
179       {
180          String JavaDoc returnAttr = WSDDGenerator.getQNameAttrValue(returnQName);
181          String JavaDoc typeAttr = WSDDGenerator.getQNameAttrValue(returnType);
182
183          String JavaDoc typePrefix = returnType.getPrefix();
184          if (typePrefix.startsWith("ns") && xmlns.startsWith("xmlns:" + typePrefix) == false)
185          {
186             xmlns += "xmlns:" + typePrefix + "='" + returnType.getNamespaceURI() + "' ";
187          }
188
189          out.println(" <operation name='" + name + "' qname='" + qnameAttr + "' returnQName='" + returnAttr + "' returnType='" + typeAttr + "' " + xmlns + ">");
190       }
191       else
192       {
193          String JavaDoc onewayStr = (oneWay ? "oneway='true' " : "");
194          out.println(" <operation name='" + name + "' qname='" + qnameAttr + "' " + onewayStr + xmlns + ">");
195       }
196
197       Iterator JavaDoc itParam = getParameters();
198       while (itParam.hasNext())
199       {
200          OperationDescription.Parameter param = (OperationDescription.Parameter)itParam.next();
201          String JavaDoc paramName = param.getName();
202          QName JavaDoc paramQName = param.getQName();
203          String JavaDoc mode = param.getMode();
204          boolean inHeader = param.isInHeader();
205          boolean outHeader = param.isOutHeader();
206
207          log.trace("Parameter: " + param);
208
209          QName JavaDoc typeQName = param.getType();
210
211          String JavaDoc headers = "";
212          if (inHeader) headers += "inHeader='true' ";
213          if (outHeader) headers += "outHeader='true' ";
214
215          xmlns = "";
216
217          if (typeQName != null)
218          {
219             String JavaDoc typeAttr = WSDDGenerator.getQNameAttrValue(typeQName);
220             String JavaDoc typePrefix = typeQName.getPrefix();
221             if (typePrefix.startsWith("ns"))
222             {
223                xmlns += "xmlns:" + typePrefix + "='" + typeQName.getNamespaceURI() + "' ";
224             }
225
226             qnameAttr = WSDDGenerator.getQNameAttrValue(paramQName);
227             out.println(" <parameter name='" + paramName + "' qname='" + qnameAttr + "' mode='" + mode + "' type='" + typeAttr + "' " + headers + xmlns + "/>");
228          }
229          else
230          {
231             out.println(" <parameter name='" + paramName + "' mode='" + mode + "' " + headers + xmlns + "/>");
232          }
233       }
234
235       Iterator JavaDoc itFault = getFaults();
236       while (itFault.hasNext())
237       {
238          OperationDescription.Fault fault = (OperationDescription.Fault)itFault.next();
239          String JavaDoc faultName = fault.getName();
240          String JavaDoc javaType = fault.getJavaType();
241
242          log.trace("Fault: " + fault);
243
244          QName JavaDoc typeQName = fault.getTypeQName();
245          QName JavaDoc faultQName = fault.getQName();
246
247          String JavaDoc typeAttr = WSDDGenerator.getQNameAttrValue(typeQName);
248
249          xmlns = "";
250          String JavaDoc typePrefix = typeQName.getPrefix();
251          if (typePrefix.startsWith("ns"))
252          {
253             xmlns += "xmlns:" + typePrefix + "='" + typeQName.getNamespaceURI() + "' ";
254          }
255
256          qnameAttr = WSDDGenerator.getQNameAttrValue(faultQName);
257          out.println(" <fault name='" + faultName + "' qname='" + qnameAttr + "' type='" + typeAttr + "' class='" + javaType + "' " + xmlns + "/>");
258       }
259
260       out.println(" </operation>");
261    }
262
263    public boolean equals(Object JavaDoc obj)
264    {
265       if (obj == null) return false;
266       return toString().equals(obj.toString());
267    }
268
269    public int hashCode()
270    {
271       return toString().hashCode();
272    }
273
274    public String JavaDoc toString()
275    {
276       return "[name=" + name + ",qname=" + qname + ",returnQName=" + returnQName + ",returnType=" + returnType + "]";
277    }
278
279 // Used in wsdd operations
280
public static class Parameter
281    {
282       private String JavaDoc name;
283       private QName JavaDoc qname;
284       private String JavaDoc mode;
285       private QName JavaDoc type;
286       private boolean inHeader;
287       private boolean outHeader;
288
289       public Parameter(String JavaDoc name, QName JavaDoc qname, String JavaDoc mode, QName JavaDoc type)
290       {
291          if (name == null)
292             throw new IllegalArgumentException JavaDoc("Parameter name cannot be null");
293          if (mode == null)
294             throw new IllegalArgumentException JavaDoc("Parameter mode cannot be null");
295          if (type == null)
296             throw new IllegalArgumentException JavaDoc("Parameter type cannot be null");
297
298          if (qname == null)
299             qname = new QName JavaDoc(name);
300
301          this.name = name;
302          this.qname = qname;
303          this.mode = mode;
304          this.type = type;
305       }
306
307       public String JavaDoc getName()
308       {
309          return name;
310       }
311
312       public QName JavaDoc getQName()
313       {
314          return qname;
315       }
316
317       public QName JavaDoc getType()
318       {
319          return type;
320       }
321
322       public String JavaDoc getMode()
323       {
324          return mode;
325       }
326
327       public void setMode(String JavaDoc mode)
328       {
329          this.mode = mode;
330       }
331
332       public boolean isInHeader()
333       {
334          return inHeader;
335       }
336
337       public void setInHeader(boolean inHeader)
338       {
339          this.inHeader = inHeader;
340       }
341
342       public boolean isOutHeader()
343       {
344          return outHeader;
345       }
346
347       public void setOutHeader(boolean outHeader)
348       {
349          this.outHeader = outHeader;
350       }
351
352       public boolean equals(Object JavaDoc obj)
353       {
354          if (obj == null) return false;
355          return toString().equals(obj.toString());
356       }
357
358       public int hashCode()
359       {
360          return toString().hashCode();
361       }
362
363       public String JavaDoc toString()
364       {
365          return "[name=" + name + "qname=" + qname + ",mode=" + mode + ",type=" + type + "]";
366       }
367    }
368
369 // Used in wsdd operations
370
public static class Fault
371    {
372       private String JavaDoc name;
373       private QName JavaDoc qname;
374       private QName JavaDoc typeQName;
375       private String JavaDoc javaType;
376
377       public Fault(String JavaDoc name, QName JavaDoc qname, String JavaDoc javaType, QName JavaDoc type)
378       {
379          if (name == null)
380             throw new IllegalArgumentException JavaDoc("Fault name cannot be null");
381          if (javaType == null)
382             throw new IllegalArgumentException JavaDoc("Fault javaType cannot be null");
383          if (type == null)
384             throw new IllegalArgumentException JavaDoc("Fault type cannot be null");
385
386          if (qname == null)
387             qname = new QName JavaDoc(name);
388
389          this.name = name;
390          this.qname = qname;
391          this.javaType = javaType;
392          this.typeQName = type;
393       }
394
395       public String JavaDoc getName()
396       {
397          return name;
398       }
399
400       public QName JavaDoc getQName()
401       {
402          return qname;
403       }
404
405       public QName JavaDoc getTypeQName()
406       {
407          return typeQName;
408       }
409
410       public String JavaDoc getJavaType()
411       {
412          return javaType;
413       }
414
415       public boolean equals(Object JavaDoc obj)
416       {
417          if (obj == null) return false;
418          return toString().equals(obj.toString());
419       }
420
421       public int hashCode()
422       {
423          return toString().hashCode();
424       }
425
426       public String JavaDoc toString()
427       {
428          return "[name=" + name + "qname=" + qname + ",type=" + typeQName + ",java=" + javaType + "]";
429       }
430    }
431 }
Popular Tags