KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > description > ServiceDescription


1 /*
2 * Copyright 2004,2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 package org.apache.axis2.description;
17
18 import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
19 import com.ibm.wsdl.extensions.soap.SOAPConstants;
20 import org.apache.axis2.context.MessageContext;
21 import org.apache.axis2.context.ServiceContext;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.phaseresolver.PhaseResolver;
24 import org.apache.wsdl.WSDLBindingOperation;
25 import org.apache.wsdl.WSDLEndpoint;
26 import org.apache.wsdl.WSDLExtensibilityElement;
27 import org.apache.wsdl.WSDLService;
28 import org.apache.wsdl.extensions.ExtensionConstants;
29 import org.apache.wsdl.extensions.SOAPOperation;
30 import org.apache.wsdl.impl.WSDLInterfaceImpl;
31 import org.apache.wsdl.impl.WSDLServiceImpl;
32
33 import javax.wsdl.*;
34 import javax.wsdl.extensions.ExtensibilityElement;
35 import javax.wsdl.extensions.soap.SOAPAddress;
36 import javax.wsdl.factory.WSDLFactory;
37 import javax.xml.namespace.QName JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.Writer JavaDoc;
40 import java.util.*;
41
42 /**
43  * Class ServiceDescription
44  */

45 public class ServiceDescription
46         extends WSDLServiceImpl
47         implements WSDLService, ParameterInclude, FlowInclude, DescriptionConstants {
48
49     private Definition difDefinition = null; //to store the wsdl definition , which is build at the deployment time
50

51
52     /**
53      * TODO this should be in the WSDLInterface, yet we want it to have in the
54      * the Services, so we put this here for M1 until we foud better way to do
55      * that
56      */

57 // protected final HashMap operationsMap = new HashMap();
58

59     private String JavaDoc serviceDescription = "Not Specified";
60
61     /**
62      * Constructor ServiceDescription
63      */

64     public ServiceDescription() {
65         this.setComponentProperty(MODULEREF_KEY, new ArrayList());
66         this.setComponentProperty(PARAMETER_KEY, new ParameterIncludeImpl());
67         this.setServiceInterface( new WSDLInterfaceImpl());
68     }
69
70     /**
71      * Constructor ServiceDescription
72      *
73      * @param qName
74      */

75     public ServiceDescription(QName JavaDoc qName) {
76         this();
77         this.setName(qName);
78     }
79
80     /*
81     * (non-Javadoc)
82     *
83     * @see org.apache.axis2.description.ServiceDescription#addModule(javax.xml.namespace.QName)
84     */

85
86     /**
87      * To ebgage a module it is reuired to use this method
88      * @param moduleref
89      * @throws AxisFault
90      */

91     public void engageModule(ModuleDescription moduleref) throws AxisFault {
92         if (moduleref == null) {
93             return;
94         }
95         if (moduleref != null) {
96             Collection collectionModule = (Collection) this.getComponentProperty(MODULEREF_KEY);
97             for (Iterator iterator = collectionModule.iterator(); iterator.hasNext();) {
98                 ModuleDescription modu = (ModuleDescription) iterator.next();
99                 if(modu.getName().equals(moduleref.getName())){
100                     throw new AxisFault(moduleref.getName().getLocalPart()+ " module has alredy engaged to the seevice" +
101                             " operation terminated !!!");
102                 }
103
104             }
105         }
106         new PhaseResolver().engageModuleToService(this,moduleref);
107         Collection collectionModule = (Collection) this.getComponentProperty(MODULEREF_KEY);
108         collectionModule.add(moduleref);
109     }
110
111     /**
112      * To add a opeartion to a service if a module requird to do so
113      * @param module
114      */

115     public void addModuleOperations(ModuleDescription module){
116         HashMap map = module.getOperations();
117         Collection col = map.values();
118         for (Iterator iterator = col.iterator(); iterator.hasNext();) {
119             OperationDescription operation = (OperationDescription) iterator.next();
120             this.addOperation(operation);
121         }
122     }
123
124     public void addToEngagModuleList(ModuleDescription moduleName){
125         Collection collectionModule = (Collection) this.getComponentProperty(MODULEREF_KEY);
126         for (Iterator iterator = collectionModule.iterator(); iterator.hasNext();) {
127             ModuleDescription moduleDescription = (ModuleDescription) iterator.next();
128             if(moduleName.getName().equals(moduleDescription.getName())){
129                 return;
130             }
131         }
132         collectionModule.add(moduleName);
133     }
134
135     /*
136     * (non-Javadoc)
137     *
138     * @see org.apache.axis2.description.ServiceDescription#getEngadgedModules()
139     */

140
141     /**
142      * Method getEngadgedModules
143      *
144      * @return
145      */

146     public Collection getEngagedModules() {
147         return (Collection) this.getComponentProperty(MODULEREF_KEY);
148     }
149
150     /**
151      * Method getOperation
152      *
153      * @param operationName
154      * @return
155      */

156     public OperationDescription getOperation(QName JavaDoc operationName) {
157         String JavaDoc opStr = operationName.getLocalPart();
158         
159         HashMap allOperations = this.getServiceInterface().getAllOperations();
160         return (OperationDescription) allOperations.get(opStr);
161     }
162
163     /*
164     * (non-Javadoc)
165     *
166     * @see org.apache.axis2.description.ServiceDescription#addOperation(org.apache.axis2.description.OperationDescription)
167     */

168
169     /**
170      * Method addOperation
171      *
172      * @param operation
173      */

174     public void addOperation(OperationDescription operation) {
175
176         this.getServiceInterface().setOperation(operation);
177     }
178
179     /*
180     * (non-Javadoc)
181     *
182     * @see org.apache.axis2.description.ServiceDescription#setClassLoader(java.lang.ClassLoader)
183     */

184
185     /**
186      * Method setClassLoader
187      *
188      * @param classLoader
189      */

190     public void setClassLoader(ClassLoader JavaDoc classLoader) {
191         if (classLoader != null) {
192             this.setComponentProperty(CLASSLOADER_KEY, classLoader);
193         }
194     }
195
196     /*
197     * (non-Javadoc)
198     *
199     * @see org.apache.axis2.description.ServiceDescription#getClassLoader()
200     */

201
202     /**
203      * Method getClassLoader
204      *
205      * @return
206      */

207     public ClassLoader JavaDoc getClassLoader() {
208         return (ClassLoader JavaDoc) this.getComponentProperty(CLASSLOADER_KEY);
209     }
210
211     /*
212     * (non-Javadoc)
213     *
214     * @see org.apache.axis2.description.ServiceDescription#setContextPath(java.lang.String)
215     */

216
217     /**
218      * Method setContextPath
219      *
220      * @param contextPath
221      */

222     public void setContextPath(String JavaDoc contextPath) {
223         if (contextPath != null) {
224             this.setComponentProperty(CONTEXTPATH_KEY, contextPath);
225         }
226     }
227
228     /*
229     * (non-Javadoc)
230     *
231     * @see org.apache.axis2.description.ServiceDescription#getContextPath()
232     */

233
234     /**
235      * Method getContextPath
236      *
237      * @return
238      */

239     public String JavaDoc getContextPath() {
240         return (String JavaDoc) this.getComponentProperty(CONTEXTPATH_KEY);
241     }
242
243     /*
244     * (non-Javadoc)
245     *
246     * @see org.apache.axis2.description.ServiceDescription#setStyle(javax.swing.text.Style)
247     */

248
249     /**
250      * Method setStyle
251      *
252      * @param style
253      */

254     public void setStyle(String JavaDoc style) {
255         if (style != null) {
256             this.setComponentProperty(STYLE_KEY, style);
257         }
258     }
259
260     /*
261     * (non-Javadoc)
262     *
263     * @see org.apache.axis2.description.ServiceDescription#getStyle()
264     */

265
266     /**
267      * Method getStyle
268      *
269      * @return
270      */

271     public String JavaDoc getStyle() {
272         return (String JavaDoc) this.getComponentProperty(STYLE_KEY);
273     }
274
275     /*
276     * (non-Javadoc)
277     *
278     * @see org.apache.axis2.description.PhasesInclude#getPhases(java.util.ArrayList,
279     * int)
280     */

281
282     /*
283     * (non-Javadoc)
284     *
285     * @see org.apache.axis2.description.ParameterInclude#addParameter(org.apache.axis2.description.Parameter)
286     */

287
288     /**
289      * Method addParameter
290      *
291      * @param param
292      */

293     public void addParameter(Parameter param) {
294         if (param == null) {
295             return;
296         }
297         ParameterIncludeImpl paramInclude =
298                 (ParameterIncludeImpl) this.getComponentProperty(PARAMETER_KEY);
299         paramInclude.addParameter(param);
300     }
301
302     /*
303     * (non-Javadoc)
304     *
305     * @see org.apache.axis2.description.ParameterInclude#getParameter(java.lang.String)
306     */

307
308     /**
309      * Method getParameter
310      *
311      * @param name
312      * @return
313      */

314     public Parameter getParameter(String JavaDoc name) {
315         ParameterIncludeImpl paramInclude =
316                 (ParameterIncludeImpl) this.getComponentProperty(PARAMETER_KEY);
317         return (Parameter) paramInclude.getParameter(name);
318     }
319
320     /*
321     * (non-Javadoc)
322     *
323     * @see org.apache.axis2.description.FlowInclude#getInFlow()
324     */

325
326     /**
327      * Method getInFlow
328      *
329      * @return
330      */

331     public Flow getInFlow() {
332         return (Flow) this.getComponentProperty(INFLOW_KEY);
333     }
334
335     /*
336     * (non-Javadoc)
337     *
338     * @see org.apache.axis2.description.FlowInclude#setInFlow(org.apache.axis2.description.Flow)
339     */

340
341     /**
342      * Method setInFlow
343      *
344      * @param inFlow
345      */

346     public void setInFlow(Flow inFlow) {
347         if (inFlow != null) {
348             this.setComponentProperty(INFLOW_KEY, inFlow);
349         }
350     }
351
352     /*
353     * (non-Javadoc)
354     *
355     * @see org.apache.axis2.description.FlowInclude#getOutFlow()
356     */

357
358     /**
359      * Method getOutFlow
360      *
361      * @return
362      */

363     public Flow getOutFlow() {
364         return (Flow) this.getComponentProperty(OUTFLOW_KEY);
365     }
366
367     /*
368     * (non-Javadoc)
369     *
370     * @see org.apache.axis2.description.FlowInclude#setOutFlow(org.apache.axis2.description.Flow)
371     */

372
373     /**
374      * Method setOutFlow
375      *
376      * @param outFlow
377      */

378     public void setOutFlow(Flow outFlow) {
379         if (outFlow != null) {
380             this.setComponentProperty(OUTFLOW_KEY, outFlow);
381         }
382     }
383
384     /*
385     * (non-Javadoc)
386     *
387     * @see org.apache.axis2.description.FlowInclude#getFaultInFlow()
388     */

389
390     /**
391      * Method getFaultInFlow
392      *
393      * @return
394      */

395     public Flow getFaultInFlow() {
396         return (Flow) this.getComponentProperty(IN_FAULTFLOW_KEY);
397     }
398
399     /*
400     * (non-Javadoc)
401     *
402     * @see org.apache.axis2.description.FlowInclude#setFaultInFlow(org.apache.axis2.description.Flow)
403     */

404
405     /**
406      * Method setFaultInFlow
407      *
408      * @param faultFlow
409      */

410     public void setFaultInFlow(Flow faultFlow) {
411         if (faultFlow != null) {
412             this.setComponentProperty(IN_FAULTFLOW_KEY, faultFlow);
413         }
414     }
415
416     public Flow getFaultOutFlow() {
417         return (Flow) this.getComponentProperty(OUT_FAULTFLOW_KEY);
418     }
419
420     public void setFaultOutFlow(Flow faultFlow) {
421         if (faultFlow != null) {
422             this.setComponentProperty(OUT_FAULTFLOW_KEY, faultFlow);
423         }
424     }
425
426     /**
427      * Method getOperations
428      *
429      * @return
430      */

431     public HashMap getOperations() {
432         return this.getServiceInterface().getOperations();
433     }
434
435     public OperationDescription getOperation(String JavaDoc ncName){
436         return (OperationDescription)this.getServiceInterface().getOperations().get(ncName);
437     }
438
439     /**
440      * This method will return the operation given particular SOAP Action.
441      * This method should only be called if there is only one Endpoint is defined
442      * for this Service. If more than one Endpoint exists one of them will be picked.
443      * If more than one Operation is found with the given
444      * SOAP Action; null will be ruturned. If no particular Operation is found with
445      * the given SOAP Action; null will be returned.
446      * @param soapAction SOAP Action defined for the particular Operation
447      * @return A OperationDescription if a unque Operation can be found with the given SOAP Action
448      * otherwise will return null.
449      */

450     public OperationDescription getOperationBySOAPAction(String JavaDoc soapAction){
451         Iterator iterator = this.getEndpoints().keySet().iterator();
452         if(iterator.hasNext()){
453             WSDLEndpoint endpoint = (WSDLEndpoint)this.getEndpoints().get(iterator.next());
454             return this.getOperationBySOAPAction(soapAction, endpoint.getName());
455         }
456
457         return null;
458
459
460     }
461
462
463     /**
464      * This method will return the operation given the particular endpoing and the
465      * particular SOAP Action. If more than one Operation is found with the given
466      * SOAP Action; null will be ruturned. If no particular Operation is found with
467      * the given SOAP Action; null will be returned
468      * @param endpoint Particular Enpoint in which the bining is defined with the particular SOAP
469      * Action.
470      * @param soapAction SOAP Action defined for the particular Operation
471      * @return A OperationDescription if a unque Operation can be found with the given SOAP Action
472      * otherwise will return null.
473      */

474     public OperationDescription getOperationBySOAPAction(String JavaDoc soapAction, QName JavaDoc endpoint){
475         HashMap bindingOperations = this.getEndpoint(endpoint).getBinding().getBindingOperations();
476         Iterator operationKeySetIterator = bindingOperations.keySet().iterator();
477         OperationDescription operation = null;
478         int count = 0;
479         while(operationKeySetIterator.hasNext()){
480             WSDLBindingOperation bindingOperation = (WSDLBindingOperation)bindingOperations.get(operationKeySetIterator.next());
481             Iterator extIterator = bindingOperation.getExtensibilityElements().iterator();
482             while(extIterator.hasNext()){
483                 WSDLExtensibilityElement element = (WSDLExtensibilityElement)extIterator.next();
484                 if(element.getType().equals(ExtensionConstants.SOAP_OPERATION)){
485                     if(((SOAPOperation)element).getSoapAction().equals(soapAction)){
486                         operation = (OperationDescription)bindingOperation.getOperation();
487                         count++;
488                     }
489                 }
490             }
491         }
492         if(1 == count){
493             return operation;
494         }
495         return null;
496     }
497
498
499     /**
500      * This finds the ServiceContext provided that the incomming message that
501      * has have some serviceInstanceID. Currently this will not be added to the
502      * EngineContext's ServiceContextMap.
503      *
504      * @param msgContext
505      * @return
506      */

507     public ServiceContext findServiceContext(MessageContext msgContext) {
508         ServiceContext serviceContext = null;
509         if (null == msgContext.getServiceInstanceID()) {
510             serviceContext = new ServiceContext(this, msgContext.getSystemContext());
511             //TODO Once the ServiceContext is bound to an incomming serviceContext ID(like a cookie,reference Property) FIX this
512
// msgContext.getSystemContext().registerServiceContext(serviceContext.getServiceInstanceID(),
513
// serviceContext);
514
} else {
515             serviceContext =
516                     (ServiceContext) msgContext.getSystemContext().getServiceContext(
517                             msgContext.getServiceInstanceID());
518         }
519
520         return serviceContext;
521
522     }
523
524     /**
525      * To get the description about the service
526      * @return
527      */

528     public String JavaDoc getServiceDescription() {
529         return serviceDescription;
530     }
531
532     /**
533      * Set the description about the service
534      * @param serviceDescription
535      */

536     public void setServiceDescription(String JavaDoc serviceDescription) {
537         this.serviceDescription = serviceDescription;
538     }
539
540     public Definition getWSDLDefinition() {
541         return difDefinition;
542     }
543
544     public void setWSDLDefinition(Definition difDefinition) {
545         this.difDefinition = difDefinition;
546     }
547
548     public void printWSDL(Writer JavaDoc out, String JavaDoc PortURL)throws AxisFault{
549         try {
550             Definition wsdlDefinition = this.getWSDLDefinition();
551             if(wsdlDefinition !=null){
552                 Iterator sreviceitr = wsdlDefinition.getServices().keySet().iterator();
553                 while (sreviceitr.hasNext()) {
554                      wsdlDefinition.removeService((QName JavaDoc)sreviceitr.next());
555                 }
556
557               // wsdlDefinition.removeService(this.getName());
558

559                 Service service = wsdlDefinition.createService();
560                 service.setQName(this.getName());
561
562                 Port port = wsdlDefinition.createPort();
563                 SOAPAddress soapAddress = new SOAPAddressImpl();
564                 soapAddress.setElementType(SOAPConstants.Q_ELEM_SOAP_ADDRESS);
565                 soapAddress.setLocationURI(PortURL);
566                 port.addExtensibilityElement(soapAddress);
567                 port.setName(this.getName().getLocalPart() + "Port");
568
569                 Map bindingsMap = wsdlDefinition.getBindings();
570                 Collection bind_col = bindingsMap.values();
571                 for (Iterator iterator = bind_col.iterator(); iterator.hasNext();) {
572                     Binding binding = (Binding) iterator.next();
573                     port.setBinding(binding);
574                     break;
575                 }
576                 service.addPort(port);
577
578                 wsdlDefinition.addService(service);
579                 WSDLFactory.newInstance().newWSDLWriter().writeWSDL(wsdlDefinition,out);
580                 out.flush();
581             } else {
582                 WSDLFactory.newInstance().newWSDLWriter().writeWSDL(wsdlDefinition,out);
583                 out.write("<wsdl>WSDL is NOT found</wsdl>");
584                 out.flush();
585             }
586
587
588
589         } catch (WSDLException e) {
590             throw new AxisFault(e);
591         } catch (IOException JavaDoc e) {
592             throw new AxisFault(e);
593         }
594     }
595
596 }
Popular Tags