KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > deployment > DeploymentParser


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
17 package org.apache.axis2.deployment;
18
19 import org.apache.axis2.deployment.util.DeploymentData;
20 import org.apache.axis2.description.*;
21 import org.apache.axis2.engine.AxisConfigurationImpl;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.engine.MessageReceiver;
24 import org.apache.axis2.transport.TransportListener;
25 import org.apache.axis2.transport.TransportSender;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import javax.xml.namespace.QName JavaDoc;
30 import javax.xml.stream.XMLInputFactory;
31 import javax.xml.stream.XMLStreamConstants;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamReader;
34 import java.io.InputStream JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * This class is used to parse the following xml douments
39  * 1 axis2.xml
40  * 2 service.xml
41  * 3 module.xml
42  * <p/>
43  * this class implements DeployCons to get some constant values need to
44  * parse a given document
45  */

46 public class DeploymentParser implements DeploymentConstants {
47
48     private Log log = LogFactory.getLog(getClass());
49     //module.xml strating tag
50
private static final String JavaDoc MODULEXMLST = "module";
51     // service.xml strating tag
52
private static final String JavaDoc SERVICEXMLST = "service";
53
54     private XMLStreamReader pullparser;
55
56     /**
57      * referebce to the deployment engine
58      */

59     private DeploymentEngine dpengine;
60
61     /**
62      * constructor to parce service.xml
63      *
64      * @param inputStream
65      * @param engine
66      */

67     public DeploymentParser(InputStream JavaDoc inputStream, DeploymentEngine engine)
68             throws XMLStreamException {
69         this.dpengine = engine;
70         pullparser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
71     }
72
73     public void parseServiceXML(ServiceDescription axisService) throws DeploymentException {
74         //To check whether document end tag has encountered
75
boolean END_DOCUMENT = false;
76         // ServiceMetaData service = null;
77
try {
78             while (!END_DOCUMENT) {
79                 int eventType = pullparser.next();
80                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
81                     END_DOCUMENT = true;
82                 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
83                     String JavaDoc ST = pullparser.getLocalName();
84                     if (ST.equals(SERVICEXMLST)) {
85                         procesServiceXML(axisService);
86                     }
87                     break;
88                 }
89             }
90         } catch (XMLStreamException e) {
91             throw new DeploymentException("parser Exception", e);
92         }
93     }
94
95     /**
96      * To process axis2.xml
97      */

98     public void processGlobalConfig(AxisConfigurationImpl axisGlobal, String JavaDoc starttag)
99             throws DeploymentException {
100         String JavaDoc START_TAG = starttag;
101         try {
102             boolean END_DOCUMENT = false;
103             while (!END_DOCUMENT) {
104                 int eventType = pullparser.next();
105                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
106                     // document end tag met , break the loop
107
END_DOCUMENT = true;
108                     break;
109                 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
110                     String JavaDoc ST = pullparser.getLocalName(); //Staring tag name
111
if (START_TAG.equals(ST)) {
112                         //todo complete this to fill the names
113
} else if (PARAMETERST.equals(ST)) {
114                         Parameter parameter = processParameter();
115                         axisGlobal.addParameter(parameter);
116                     } else if (TRANSPORTSENDER.equals(ST)) {
117                         TransportOutDescription transportout = proccessTrasnsportOUT();
118                         dpengine.getAxisConfig().addTransportOut(transportout);
119                     } else if (TRANSPORTRECEIVER.equals(ST)) {
120                         TransportInDescription transportin = proccessTrasnsportIN();
121                         dpengine.getAxisConfig().addTransportIn(transportin);
122                     } else if (TYPEMAPPINGST.equals(ST)) {
123                         throw new UnsupportedOperationException JavaDoc("Type Mappings are not allowed in axis2.xml");
124                     } else if (MESSAGERECEIVER.equals(ST)) {
125                         int attribCount = pullparser.getAttributeCount();
126                         if (attribCount == 2) {
127                             String JavaDoc attname = pullparser.getAttributeLocalName(0);
128                             String JavaDoc attvalue = pullparser.getAttributeValue(0);
129                             if (MEP.equals(attname)) {
130                                 String JavaDoc name = attvalue;
131                                 attname = pullparser.getAttributeLocalName(1);
132                                 attvalue = pullparser.getAttributeValue(1);
133                                 if (CLASSNAME.equals(attname)) {
134                                     try {
135                                         Class JavaDoc messageReceiver = null;
136                                         ClassLoader JavaDoc loader1 =
137                                                 Thread.currentThread().getContextClassLoader();
138                                         if (attvalue != null && !"".equals(attvalue)) {
139                                             messageReceiver =
140                                                     Class.forName(attvalue, true, loader1);
141                                             axisGlobal.addMessageReceiver(
142                                                     name,
143                                                     (MessageReceiver) messageReceiver.newInstance());
144                                         }
145                                     } catch (ClassNotFoundException JavaDoc e) {
146                                         throw new DeploymentException(
147                                                 "Error in loading messageRecivers " , e);
148                                     } catch (IllegalAccessException JavaDoc e) {
149                                         throw new DeploymentException(
150                                                 "Error in loading messageRecivers " , e);
151                                     } catch (InstantiationException JavaDoc e) {
152                                         throw new DeploymentException(
153                                                 "Error in loading messageRecivers " , e);
154                                     }
155                                 } else
156                                     throw new UnsupportedOperationException JavaDoc(
157                                             "invalid attributes in axis2.xml (messageReceiver elemet) "
158                                             + attname);
159                             } else
160                                 throw new UnsupportedOperationException JavaDoc(
161                                         "invalid attributes in axis2.xml (messageReceiver elemet) "
162                                         + attname);
163                         } else
164                             throw new UnsupportedOperationException JavaDoc("invalid attributes in axis2.xml (messageReceiver elemet)");
165
166                     } else if (MODULEST.equals(ST)) {
167                         int attribCount = pullparser.getAttributeCount();
168                         if (attribCount > 0) {
169                             for (int i = 0; i < attribCount; i++) {
170                                 String JavaDoc attname = pullparser.getAttributeLocalName(i);
171                                 String JavaDoc attvalue = pullparser.getAttributeValue(i);
172                                 if (REF.equals(attname)) {
173                                     DeploymentData.getInstance().addModule(new QName JavaDoc(attvalue));
174                                 }
175                             }
176                         }
177                     } else if (PHASE_ORDER.equals(ST)) {
178                         int attribCount = pullparser.getAttributeCount();
179                         DeploymentData tempdata = DeploymentData.getInstance();
180                         if (attribCount > 0) {
181                             for (int i = 0; i < attribCount; i++) {
182                                 String JavaDoc attname = pullparser.getAttributeLocalName(i);
183                                 String JavaDoc attvalue = pullparser.getAttributeValue(i);
184                                 if (TYPE.equals(attname)) {
185                                     if (INFLOWST.equals(attvalue)) {
186                                         tempdata.setINPhases(processPhaseOrder());
187                                     } else if (OUTFLOWST.equals(attvalue)) {
188                                         tempdata.setOUTPhases(processPhaseOrder());
189                                     } else if (IN_FAILTFLOW.equals(attvalue)) {
190                                         tempdata.setIN_FaultPhases(processPhaseOrder());
191                                     } else if (OUT_FAILTFLOW.equals(attvalue)) {
192                                         tempdata.setOUT_FaultPhases(processPhaseOrder());
193                                     } else {
194                                         throw new DeploymentException(
195                                                 "un defined flow type " + ST);
196                                     }
197                                 }
198                             }
199                         } else {
200                             throw new DeploymentException(
201                                     "Flow type is a required attribute in " + ST);
202                         }
203                     } else {
204                         throw new UnsupportedOperationException JavaDoc(
205                                 ST + " element is not allowed in the axis2.xml");
206                     }
207                 } else if (eventType == XMLStreamConstants.END_ELEMENT) {
208                     String JavaDoc endtagname = pullparser.getLocalName();
209                     if (START_TAG.equals(endtagname)) {
210                         END_DOCUMENT = true;
211                         break;
212                     }
213                 }
214             }
215         } catch (XMLStreamException e) {
216             throw new DeploymentException("parser Exception", e);
217         } catch (AxisFault e) {
218             throw new DeploymentException(e);
219         }
220     }
221
222     public TransportInDescription proccessTrasnsportIN() throws DeploymentException {
223         TransportInDescription transportin = null;
224         String JavaDoc attname = pullparser.getAttributeLocalName(0);
225         String JavaDoc attvalue = pullparser.getAttributeValue(0);
226
227         int attribCount = pullparser.getAttributeCount();
228         for (int i = 0; i < attribCount; i++) {
229             attname = pullparser.getAttributeLocalName(i);
230             attvalue = pullparser.getAttributeValue(i);
231             if (ATTNAME.equals(attname)) {
232                 transportin = new TransportInDescription(new QName JavaDoc(attvalue));
233             } else if (transportin != null && CLASSNAME.equals(attname)) {
234                 Class JavaDoc reciever = null;
235                 try {
236                     reciever =
237                             Class.forName(
238                                     attvalue,
239                                     true,
240                                     Thread.currentThread().getContextClassLoader());
241                     TransportListener trnsrecievr = (TransportListener) reciever.newInstance();
242                     transportin.setReciver(trnsrecievr);
243                 } catch (ClassNotFoundException JavaDoc e) {
244                     throw new DeploymentException(e);
245                 } catch (IllegalAccessException JavaDoc e) {
246                     throw new DeploymentException(e);
247                 } catch (InstantiationException JavaDoc e) {
248                     throw new DeploymentException(e);
249                 }
250             }
251         }
252         boolean END_TRANSPORTS = false;
253         try {
254             while (!END_TRANSPORTS) {
255                 int eventType = pullparser.next();
256                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
257                     END_TRANSPORTS = true;
258                 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
259                     String JavaDoc tagnae = pullparser.getLocalName();
260                     if (transportin != null && PARAMETERST.equals(tagnae)) {
261                         Parameter parameter = processParameter();
262                         transportin.addParameter(parameter);
263                     } else if (transportin != null && INFLOWST.equals(tagnae)) {
264                         Flow inFlow = processInFlow();
265                         transportin.setInFlow(inFlow);
266                     } else if (transportin != null && OUTFLOWST.equals(tagnae)) {
267                         throw new DeploymentException(
268                                 "OUTFlow dose not support in AxisTransportIN " + tagnae);
269                     } else if (transportin != null && IN_FAILTFLOW.equals(tagnae)) {
270                         Flow faultFlow = processInFaultFlow();
271                         transportin.setFaultFlow(faultFlow);
272                     } else {
273                         throw new DeploymentException("Unknown element " + tagnae);
274                     }
275                 } else if (eventType == XMLStreamConstants.END_ELEMENT) {
276                     String JavaDoc endtagname = pullparser.getLocalName();
277                     if (TRANSPORTRECEIVER.equals(endtagname)) {
278                         END_TRANSPORTS = true;
279                         break;
280                     }
281                 }
282             }
283         } catch (XMLStreamException e) {
284             throw new DeploymentException("parser Exception", e);
285         } catch (Exception JavaDoc e) {
286             throw new DeploymentException(e);
287         }
288         return transportin;
289     }
290
291     public TransportOutDescription proccessTrasnsportOUT() throws DeploymentException {
292         TransportOutDescription transportout = null;
293         String JavaDoc attname;
294         String JavaDoc attvalue;
295         int attribCount = pullparser.getAttributeCount();
296         for (int i = 0; i < attribCount; i++) {
297             attname = pullparser.getAttributeLocalName(i);
298             attvalue = pullparser.getAttributeValue(i);
299             if (ATTNAME.equals(attname)) {
300                 transportout = new TransportOutDescription(new QName JavaDoc(attvalue));
301             } else if (transportout != null && CLASSNAME.equals(attname)) {
302                 Class JavaDoc sender = null;
303                 try {
304                     sender =
305                             Class.forName(
306                                     attvalue,
307                                     true,
308                                     Thread.currentThread().getContextClassLoader());
309                     TransportSender transportSender = (TransportSender) sender.newInstance();
310                     transportout.setSender(transportSender);
311                 } catch (ClassNotFoundException JavaDoc e) {
312                     throw new DeploymentException(e);
313                 } catch (IllegalAccessException JavaDoc e) {
314                     throw new DeploymentException(e);
315                 } catch (InstantiationException JavaDoc e) {
316                     throw new DeploymentException(e);
317                 }
318             }
319         }
320         boolean END_TRANSPORTS = false;
321         try {
322             while (!END_TRANSPORTS) {
323                 int eventType = pullparser.next();
324                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
325                     END_TRANSPORTS = true;
326                 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
327                     String JavaDoc tagnae = pullparser.getLocalName();
328                     if (transportout != null && PARAMETERST.equals(tagnae)) {
329                         Parameter parameter = processParameter();
330                         transportout.addParameter(parameter);
331                     } else if (transportout != null && INFLOWST.equals(tagnae)) {
332                         throw new DeploymentException(
333                                 "InFlow dose not support in TransportOutDescription " + tagnae);
334                     } else if (transportout != null && OUTFLOWST.equals(tagnae)) {
335                         Flow outFlow = processOutFlow();
336                         transportout.setOutFlow(outFlow);
337                     } else if (transportout != null && OUT_FAILTFLOW.equals(tagnae)) {
338                         Flow faultFlow = processOutFaultFlow();
339                         transportout.setFaultFlow(faultFlow);
340                     } else {
341                         throw new DeploymentException("Unknown element " + tagnae);
342                     }
343                 } else if (eventType == XMLStreamConstants.END_ELEMENT) {
344                     String JavaDoc endtagname = pullparser.getLocalName();
345                     if (TRANSPORTSENDER.equals(endtagname)) {
346                         END_TRANSPORTS = true;
347                         break;
348                     }
349                 }
350             }
351         } catch (XMLStreamException e) {
352             throw new DeploymentException("parser Exception", e);
353         } catch (Exception JavaDoc e) {
354             throw new DeploymentException(e);
355         }
356         return transportout;
357     }
358
359     /**
360      * to process service.xml
361      */

362     private void procesServiceXML(ServiceDescription axisService) throws DeploymentException {
363         int attribCount = pullparser.getAttributeCount();
364         if (attribCount >= 1) {
365             for (int i = 0; i < attribCount; i++) {
366                 String JavaDoc attname = pullparser.getAttributeLocalName(i);
367                 String JavaDoc attvalue = pullparser.getAttributeValue(i);
368                 if (ATQNAME.equals(attname)) {
369                     if (attvalue == null || attvalue.trim().equals("")) {
370                         axisService.setName(
371                                 new QName JavaDoc(
372                                         getAxisServiceName(
373                                                 dpengine.getCurrentFileItem().getServiceName())));
374                     } else {
375                         axisService.setName(new QName JavaDoc(attvalue));
376                     }
377                 } else {
378                     throw new DeploymentException(
379                             attname
380                             + " Bad arguments for the service"
381                             + getAxisServiceName(dpengine.getCurrentFileItem().getServiceName()));
382                 }
383             }
384         } else {
385             //if user dose not specify the service name then the default name will be the archive name
386
axisService.setName(
387                     new QName JavaDoc(getAxisServiceName(dpengine.getCurrentFileItem().getServiceName())));
388         }
389         boolean END_DOCUMENT = false;
390         try {
391             while (!END_DOCUMENT) {
392                 int eventType = pullparser.next();
393                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
394                     // document end tag met , break the loop
395
END_DOCUMENT = true;
396                     break;
397                 } else if (eventType == XMLStreamConstants.START_ELEMENT) {
398                     String JavaDoc ST = pullparser.getLocalName(); //Staring tag name
399
if (PARAMETERST.equals(ST)) {
400                         Parameter parameter = processParameter();
401                         axisService.addParameter(parameter);
402                         //axisService. .appParameter(parameter);
403
} else if (DESCRIPTION.equals(ST)) {
404                         String JavaDoc desc = processDescription();
405                         axisService.setServiceDescription(desc);
406                     } else if (TYPEMAPPINGST.equals(ST)) {
407                         throw new UnsupportedOperationException JavaDoc("Type mapping dose not implemented yet ");
408                         // processTypeMapping();
409
} else if (BEANMAPPINGST.equals(ST)) {
410                         throw new UnsupportedOperationException JavaDoc("Bean mapping dose not implemented yet ");
411                         // processBeanMapping();
412
} else if (OPRATIONST.equals(ST)) {
413                         OperationDescription operation = processOperation(axisService);
414                         DeploymentData.getInstance().setOperationPhases(operation);
415                         if (operation.getMessageReciever() == null) {
416                             try {
417                                 /**
418                                  * Setting default Message Recive as Message Reciever
419                                  */

420                                 ClassLoader JavaDoc loader1 =
421                                         Thread.currentThread().getContextClassLoader();
422                                 Class JavaDoc messageReceiver =
423                                         Class.forName(
424                                                 "org.apache.axis2.receivers.RawXMLINOutMessageReceiver",
425                                                 true,
426                                                 loader1);
427                                 operation.setMessageReciever(
428                                         (MessageReceiver) messageReceiver.newInstance());
429                             } catch (ClassNotFoundException JavaDoc e) {
430                                 throw new DeploymentException(
431                                         "Error in loading messageRecivers " + e.getMessage());
432                             } catch (IllegalAccessException JavaDoc e) {
433                                 throw new DeploymentException(
434                                         "Error in loading messageRecivers " + e.getMessage());
435                             } catch (InstantiationException JavaDoc e) {
436                                 throw new DeploymentException(
437                                         "Error in loading messageRecivers " + e.getMessage());
438                             }
439                         }
440                         axisService.addOperation(operation);
441                     } else if (INFLOWST.equals(ST)) {
442                         Flow inFlow = processInFlow();
443                         axisService.setInFlow(inFlow);
444                     } else if (OUTFLOWST.equals(ST)) {
445                         Flow outFlow = processOutFlow();
446                         axisService.setOutFlow(outFlow);
447                     } else if (IN_FAILTFLOW.equals(ST)) {
448                         Flow faultFlow = processInFaultFlow();
449                         axisService.setFaultInFlow(faultFlow);
450                     } else if (OUT_FAILTFLOW.equals(ST)) {
451                         Flow faultFlow = processOutFaultFlow();
452                         axisService.setFaultOutFlow(faultFlow);
453                     } else if (MODULEST.equals(ST)) {
454                         attribCount = pullparser.getAttributeCount();
455                         if (attribCount > 0) {
456                             for (int i = 0; i < attribCount; i++) {
457                                 String JavaDoc attname = pullparser.getAttributeLocalName(i);
458                                 String JavaDoc attvalue = pullparser.getAttributeValue(i);
459                                 if (REF.equals(attname)) {
460                                     if (dpengine.getModule(new QName JavaDoc(attvalue)) == null) {
461                                         throw new DeploymentException(
462                                                 ST
463                                                 + " module is invalid or dose not have bean deployed");
464                                     } else {
465                                         dpengine.getCurrentFileItem().addModule(
466                                                 new QName JavaDoc(attvalue));
467                                     }
468                                 }
469                             }
470                         }
471
472                     } else {
473                         throw new DeploymentException(
474                                 "parser Exception : un supported element" + ST);
475                     }
476                 }
477             }
478         } catch (XMLStreamException e) {
479             throw new DeploymentException("parser Exception", e);
480         } catch (AxisFault axisFault) {
481             throw new DeploymentException(axisFault);
482         }
483     }
484
485     private String JavaDoc processDescription() throws DeploymentException {
486         String JavaDoc desc = "";
487         boolean END_DESC = false;
488         try {
489             while (!END_DESC) {
490                 int eventType = pullparser.next();
491                 if (eventType == XMLStreamConstants.END_DOCUMENT) {
492                     END_DESC = true;
493                 } else if (eventType == XMLStreamConstants.END_ELEMENT) {
494                     String JavaDoc endtagname = pullparser.getLocalName();
495                     if (DESCRIPTION.equals(endtagname)) {
496                         END_DESC = true;
497                         break;
498                     }
499
500                 } else if (eventType == XMLStreamConstants.CHARACTERS) {
501                     desc += pullparser.getText();
502                 }
503             }
504         } catch (XMLStreamException e) {
505             throw new DeploymentException("parser Exception", e);
506         } catch (Exception JavaDoc e) {
507             throw new DeploymentException(e);
508         }
509         return desc;
510     }
511
512     private Parameter processParameter() throws DeploymentException {
513         Parameter parameter = new ParameterImpl();
514         int attribCount = pullparser.getAttributeCount();
515         if (attribCount == 2) { // there should be two attributes
516
for (int i = 0; i < attribCount; i++) {
517                 String JavaDoc attname = pullparser.getAttributeLocalName(i);
518                 String JavaDoc attvalue = pullparser.getAttributeValue(i);
519                 if (ATTNAME.equals(attname)) {
520                     parameter.setName(attvalue);
521                 } else if (ATTLOCKED.equals(attname)) {
522                     String JavaDoc boolval = getValue(attvalue);
523                     if (boolval.equals("true")) {
524                         parameter.setLocked(true);
525           &nb