KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > description > OperationDesc


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.description;
56
57 import org.jboss.axis.enums.Style;
58 import org.jboss.axis.enums.Use;
59 import org.jboss.logging.Logger;
60
61 import javax.xml.namespace.QName JavaDoc;
62 import java.lang.reflect.Method JavaDoc;
63 import java.util.ArrayList JavaDoc;
64 import java.util.Iterator JavaDoc;
65
66
67 /**
68  * An OperationDesc is an abstract description of an operation on a service.
69  *
70  * @author Glen Daniels (gdaniels@apache.org)
71  */

72 public class OperationDesc
73 {
74    // Constants for "message style" operation patterns. If this OperationDesc
75
// is message style, the Java method will have one of these signatures:
76

77    // public SOAPBodyElement [] method(SOAPBodyElement [])
78
public static final int MSG_METHOD_BODYARRAY = 1;
79    // public SOAPEnvelope method(SOAPEnvelope)
80
public static final int MSG_METHOD_SOAPENVELOPE = 2;
81    // public Element [] method(Element [])
82
public static final int MSG_METHOD_ELEMENTARRAY = 3;
83    // public Document method(Document)
84
public static final int MSG_METHOD_DOCUMENT = 4;
85
86    public static final int MSG_METHOD_NONCONFORMING = -4;
87
88    private static Logger log = Logger.getLogger(OperationDesc.class.getName());
89
90    /**
91     * The service we're a part of
92     */

93    private ServiceDesc parent;
94
95    /**
96     * Parameter list
97     */

98    private ArrayList JavaDoc parameters = new ArrayList JavaDoc();
99
100    /**
101     * The operation name (String, or QName?)
102     */

103    private String JavaDoc name;
104
105    /**
106     * An XML QName which should dispatch to this method
107     */

108    private QName JavaDoc elementQName;
109
110    /**
111     * The actual Java method associated with this operation, if known
112     */

113    private Method JavaDoc method;
114
115    /**
116     * This operation's style/use. If null, we default to our parent's
117     */

118    private Style style = null;
119    private Use use = null;
120
121    /**
122     * The number of "in" params (i.e. IN or INOUT) for this operation
123     */

124    private int numInParams = 0;
125    /**
126     * The number of "out" params (i.e. OUT or INOUT) for this operation
127     */

128    private int numOutParams = 0;
129
130    /**
131     * A unique SOAPAction value for this operation
132     */

133    private String JavaDoc soapAction = null;
134
135    /**
136     * Faults for this operation
137     */

138    private ArrayList JavaDoc faults = null;
139
140    private ParameterDesc returnDesc = new ParameterDesc();
141
142    /**
143     * If we're a message-style operation, what's our signature?
144     */

145    private int messageOperationStyle = -1;
146
147    /**
148     * One-Way request semantics
149     * [TDI] 18-Aug-2004
150     */

151    private boolean oneWay = false;
152
153    /**
154     * Default constructor.
155     */

156    public OperationDesc()
157    {
158       returnDesc.setMode(ParameterDesc.OUT);
159       returnDesc.setIsReturn(true);
160    }
161
162    /**
163     * "Complete" constructor
164     */

165    public OperationDesc(String JavaDoc name, ParameterDesc[] parameters, QName JavaDoc returnQName)
166    {
167       this.name = name;
168       returnDesc.setQName(returnQName);
169       returnDesc.setMode(ParameterDesc.OUT);
170       returnDesc.setIsReturn(true);
171       for (int i = 0; i < parameters.length; i++)
172       {
173          addParameter(parameters[i]);
174       }
175    }
176
177    /**
178     * Return the operation's name
179     */

180    public String JavaDoc getName()
181    {
182       return name;
183    }
184
185    /**
186     * Set the operation's name
187     */

188    public void setName(String JavaDoc name)
189    {
190       this.name = name;
191    }
192
193    public QName JavaDoc getReturnQName()
194    {
195       return returnDesc.getQName();
196    }
197
198    public void setReturnQName(QName JavaDoc returnQName)
199    {
200       returnDesc.setQName(returnQName);
201    }
202
203    public QName JavaDoc getReturnType()
204    {
205       return returnDesc.getTypeQName();
206    }
207
208    public void setReturnType(QName JavaDoc returnType)
209    {
210       returnDesc.setTypeQName(returnType);
211    }
212
213    public Class JavaDoc getReturnClass()
214    {
215       return returnDesc.getJavaType();
216    }
217
218    public void setReturnClass(Class JavaDoc returnClass)
219    {
220       returnDesc.setJavaType(returnClass);
221    }
222
223    public QName JavaDoc getElementQName()
224    {
225       return elementQName;
226    }
227
228    public void setElementQName(QName JavaDoc elementQName)
229    {
230       this.elementQName = elementQName;
231    }
232
233    public boolean isOneWay()
234    {
235       return oneWay;
236    }
237
238    public void setOneWay(boolean oneWay)
239    {
240       this.oneWay = oneWay;
241    }
242
243    public ServiceDesc getParent()
244    {
245       return parent;
246    }
247
248    public void setParent(ServiceDesc parent)
249    {
250       this.parent = parent;
251    }
252
253    public String JavaDoc getSoapAction()
254    {
255       return soapAction;
256    }
257
258    public void setSoapAction(String JavaDoc soapAction)
259    {
260       this.soapAction = soapAction;
261    }
262
263    public void setStyle(Style style)
264    {
265       this.style = style;
266    }
267
268    /**
269     * Return the style of the operation, defaulting to the parent
270     * ServiceDesc's style if we don't have one explicitly set.
271     */

272    public Style getStyle()
273    {
274       if (style == null)
275       {
276          if (parent != null)
277          {
278             return parent.getStyle();
279          }
280          return Style.DEFAULT; // Default
281
}
282
283       return style;
284    }
285
286    public void setUse(Use use)
287    {
288       this.use = use;
289    }
290
291    /**
292     * Return the use of the operation, defaulting to the parent
293     * ServiceDesc's use if we don't have one explicitly set.
294     */

295    public Use getUse()
296    {
297       if (use == null)
298       {
299          if (parent != null)
300          {
301             return parent.getUse();
302          }
303          return Use.DEFAULT; // Default
304
}
305
306       return use;
307    }
308
309    public void addParameter(ParameterDesc param)
310    {
311       // Should we enforce adding INs then INOUTs then OUTs?
312

313       parameters.add(param);
314       if ((param.getMode() == ParameterDesc.IN) ||
315               (param.getMode() == ParameterDesc.INOUT))
316       {
317          param.setOrder(numInParams++);
318       }
319       if ((param.getMode() == ParameterDesc.OUT) ||
320               (param.getMode() == ParameterDesc.INOUT))
321       {
322          numOutParams++;
323       }
324    }
325
326    public void addParameter(QName JavaDoc paramName,
327                             QName JavaDoc xmlType,
328                             Class JavaDoc javaType,
329                             byte parameterMode,
330                             boolean inHeader,
331                             boolean outHeader)
332    {
333       ParameterDesc param =
334               new ParameterDesc(paramName, parameterMode, xmlType,
335                       javaType, inHeader, outHeader);
336       addParameter(param);
337    }
338
339    public ParameterDesc getParameter(int i)
340    {
341       if (parameters.size() <= i)
342          return null;
343
344       return (ParameterDesc)parameters.get(i);
345    }
346
347    public ArrayList JavaDoc getParameters()
348    {
349       return parameters;
350    }
351
352    /**
353     * Set the parameters wholesale.
354     *
355     * @param newParameters an ArrayList of ParameterDescs
356     */

357    public void setParameters(ArrayList JavaDoc newParameters)
358    {
359       parameters = new ArrayList JavaDoc(); //Keep numInParams correct.
360
numInParams = 0;
361       numOutParams = 0;
362
363       for (java.util.ListIterator JavaDoc li = newParameters.listIterator();
364            li.hasNext();)
365       {
366          addParameter((ParameterDesc)li.next());
367       }
368    }
369
370    public int getNumInParams()
371    {
372       return numInParams;
373    }
374
375    public int getNumOutParams()
376    {
377       return numOutParams;
378    }
379
380    public int getNumParams()
381    {
382       return parameters.size();
383    }
384
385    public Method JavaDoc getMethod()
386    {
387       return method;
388    }
389
390    public void setMethod(Method JavaDoc method)
391    {
392       this.method = method;
393    }
394
395    /**
396     * Is the return value in the header of the response message?
397     */

398    public boolean isReturnHeader()
399    {
400       return returnDesc.isOutHeader();
401    }
402
403    /**
404     * Set whether the return value is in the response message.
405     */

406    public void setReturnHeader(boolean value)
407    {
408       returnDesc.setOutHeader(value);
409    }
410
411    public ParameterDesc getParamByQName(QName JavaDoc qname)
412    {
413       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
414       {
415          ParameterDesc param = (ParameterDesc)i.next();
416          if (param.getQName().equals(qname))
417             return param;
418       }
419
420       return null;
421    }
422
423    public ParameterDesc getInputParamByQName(QName JavaDoc qname)
424    {
425       ParameterDesc param = null;
426
427       param = getParamByQName(qname);
428
429       if ((param == null) || (param.getMode() == ParameterDesc.OUT))
430       {
431          param = null;
432       }
433
434       return param;
435    }
436
437    public ParameterDesc getOutputParamByQName(QName JavaDoc qname)
438    {
439       ParameterDesc param = null;
440
441       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
442       {
443          ParameterDesc pnext = (ParameterDesc)i.next();
444          if (pnext.getQName().equals(qname) &&
445                  pnext.getMode() != ParameterDesc.IN)
446          {
447             param = pnext;
448             break;
449          }
450       }
451
452       if (param == null)
453       {
454          if (null == returnDesc.getQName())
455          {
456             param = new ParameterDesc(returnDesc); //Create copy
457
param.setQName(qname);
458          }
459          else if (qname.equals(returnDesc.getQName()))
460          {
461             param = returnDesc;
462          }
463       }
464
465       return param;
466    }
467
468    /**
469     * Return a list of ALL "in" params (including INOUTs)
470     * <p/>
471     * Note: if we were sure the order went IN->INOUT->OUT, we could optimize
472     * this.
473     *
474     * @return
475     */

476    public ArrayList JavaDoc getAllInParams()
477    {
478       ArrayList JavaDoc result = new ArrayList JavaDoc();
479       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
480       {
481          ParameterDesc desc = (ParameterDesc)i.next();
482          if (desc.getMode() != ParameterDesc.OUT)
483          {
484             result.add(desc);
485          }
486       }
487       return result;
488    }
489
490    /**
491     * Return a list of ALL "out" params (including INOUTs)
492     * <p/>
493     * Note: if we were sure the order went IN->INOUT->OUT, we could optimize
494     * this.
495     *
496     * @return
497     */

498    public ArrayList JavaDoc getAllOutParams()
499    {
500       ArrayList JavaDoc result = new ArrayList JavaDoc();
501       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
502       {
503          ParameterDesc desc = (ParameterDesc)i.next();
504          if (desc.getMode() != ParameterDesc.IN)
505          {
506             result.add(desc);
507          }
508       }
509       return result;
510    }
511
512    /**
513     * Returns an ordered list of IN params (not INOUT)
514     */

515    public ArrayList JavaDoc getInParams()
516    {
517       ArrayList JavaDoc result = new ArrayList JavaDoc();
518       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
519       {
520          ParameterDesc desc = (ParameterDesc)i.next();
521          if (desc.getMode() == ParameterDesc.IN)
522          {
523             result.add(desc);
524          }
525       }
526       return result;
527    }
528
529    /**
530     * Returns an ordered list of OUT params (not INOUT)
531     */

532    public ArrayList JavaDoc getOutParams()
533    {
534       ArrayList JavaDoc result = new ArrayList JavaDoc();
535       for (Iterator JavaDoc i = parameters.iterator(); i.hasNext();)
536       {
537          ParameterDesc desc = (ParameterDesc)i.next();
538          if (desc.getMode() == ParameterDesc.OUT)
539          {
540             result.add(desc);
541          }
542       }
543       return result;
544    }
545
546    public void addFault(FaultDesc fault)
547    {
548       if (faults == null)
549          faults = new ArrayList JavaDoc();
550       faults.add(fault);
551    }
552
553    public ArrayList JavaDoc getFaults()
554    {
555       return faults;
556    }
557
558    /**
559     * Returns the FaultDesc for the fault class given.
560     * Returns null if not found.
561     */

562    public FaultDesc getFaultByClass(Class JavaDoc cls)
563    {
564       if (faults == null || cls == null)
565       {
566          return null;
567       }
568
569       while (cls != null)
570       {
571          // Check each class in the inheritance hierarchy, stopping at
572
// java.* or javax.* classes.
573

574          for (Iterator JavaDoc iterator = faults.iterator(); iterator.hasNext();)
575          {
576             FaultDesc desc = (FaultDesc)iterator.next();
577             if (cls.getName().equals(desc.getClassName()))
578             {
579                return desc;
580             }
581          }
582
583          cls = cls.getSuperclass();
584          if (cls != null && (cls.getName().startsWith("java.") ||
585                  cls.getName().startsWith("javax.")))
586          {
587             cls = null;
588          }
589       }
590
591       return null;
592    }
593
594    /**
595     * Returns the FaultDesc for a QName (which is typically found
596     * in the details element of a SOAP fault).
597     * Returns null if not found.
598     */

599    public FaultDesc getFaultByQName(QName JavaDoc qname)
600    {
601       if (faults != null)
602       {
603          for (Iterator JavaDoc iterator = faults.iterator(); iterator.hasNext();)
604          {
605             FaultDesc desc = (FaultDesc)iterator.next();
606             if (qname.equals(desc.getQName()))
607             {
608                return desc;
609             }
610          }
611       }
612       return null;
613    }
614
615    /**
616     * Returns the FaultDesc for an XMLType.
617     * Returns null if not found.
618     */

619    public FaultDesc getFaultByXmlType(QName JavaDoc xmlType)
620    {
621       if (faults != null)
622       {
623          for (Iterator JavaDoc iterator = faults.iterator(); iterator.hasNext();)
624          {
625             FaultDesc desc = (FaultDesc)iterator.next();
626             if (xmlType.equals(desc.getXmlType()))
627             {
628                return desc;
629             }
630          }
631       }
632       return null;
633    }
634
635    public ParameterDesc getReturnParamDesc()
636    {
637       return returnDesc;
638    }
639
640    public String JavaDoc toString()
641    {
642       return toString("");
643    }
644
645    public String JavaDoc toString(String JavaDoc indent)
646    {
647       String JavaDoc text = "\nOperationDesc\n";
648       text += indent + "name: " + getName() + "\n";
649       text += indent + "returnQName: " + getReturnQName() + "\n";
650       text += indent + "returnType: " + getReturnType() + "\n";
651       text += indent + "returnClass: " + getReturnClass() + "\n";
652       text += indent + "elementQName:" + getElementQName() + "\n";
653       text += indent + "soapAction: " + getSoapAction() + "\n";
654       text += indent + "style: " + getStyle().getName() + "\n";
655       text += indent + "use: " + getUse().getName() + "\n";
656       text += indent + "numInParams: " + getNumInParams() + "\n";
657       text += indent + "method:" + getMethod() + "\n";
658       for (int i = 0; i < parameters.size(); i++)
659       {
660          text += indent + " ParameterDesc[" + i + "]:\n";
661          text += indent + ((ParameterDesc)parameters.get(i)).toString(" ") + "\n";
662       }
663       if (faults != null)
664       {
665          for (int i = 0; i < faults.size(); i++)
666          {
667             text += indent + " FaultDesc[" + i + "]:\n";
668             text += indent + ((FaultDesc)faults.get(i)).toString(" ") + "\n";
669          }
670       }
671       return text;
672    }
673
674    public int getMessageOperationStyle()
675    {
676       return messageOperationStyle;
677    }
678
679    public void setMessageOperationStyle(int messageOperationStyle)
680    {
681       this.messageOperationStyle = messageOperationStyle;
682    }
683 }
684
685
Popular Tags