KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > CoadunationParser


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationException.java
20  *
21  * CoadunationParser.java
22  *
23  * This object is responsible for parsing the contents of the of the Coadunation
24  * xml file.
25  */

26
27 package com.rift.coad.lib.deployment;
28
29 // java imports
30
import javax.xml.parsers.SAXParserFactory JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.Attributes JavaDoc;
37
38
39 /**
40  * The parser is responsible for parsing the Coadunation xml file so that it can
41  * be deployed within the coadunation server.
42  *
43  * @author Brett Chaldecott
44  */

45 public class CoadunationParser {
46     
47     /**
48      * The inner class responsible for handling the contents of the Coadunation
49      * xml source document.
50      */

51     public class CoadunationHandler extends DefaultHandler JavaDoc {
52         
53         // the class static member variables
54
private static final String JavaDoc COADUNATION = "coadunation";
55         private static final String JavaDoc COADUNATION_VERSION = "version";
56         private static final String JavaDoc COADUNATION_NAME = "name";
57         private static final String JavaDoc DESCRIPTION = "description";
58         private static final String JavaDoc WEB_SERVICES = "webservices";
59         private static final String JavaDoc WEB_SERVICE = "webservice";
60         private static final String JavaDoc PATH = "path";
61         private static final String JavaDoc CLASSES = "classes";
62         private static final String JavaDoc CACHE = "cache_by_key";
63         private static final String JavaDoc CACHE_TIMEOUT = "cache_timeout";
64         private static final String JavaDoc CLASS_NAME = "class";
65         private static final String JavaDoc WSDL_PATH = "wsdl";
66         private static final String JavaDoc ROLE = "role";
67         private static final String JavaDoc USERNAME = "username";
68         private static final String JavaDoc JMX_BEANS = "jmxbeans";
69         private static final String JavaDoc BEAN = "bean";
70         private static final String JavaDoc OBJECT_NAME = "objectName";
71         private static final String JavaDoc BEANS = "beans";
72         private static final String JavaDoc INTERFACE_NAME = "interface";
73         private static final String JavaDoc BIND_NAME = "bindName";
74         private static final String JavaDoc THREAD = "thread";
75         private static final String JavaDoc THREAD_NUMBER = "number";
76         private static final String JavaDoc TRANSACTION = "transaction";
77
78         // the xpdl information object
79
private DeploymentInfo deploymentInfo = null;
80         
81         // the processing variables
82
private boolean inCoadunation = false;
83         private boolean inDescription = false;
84         private boolean inWebServices = false;
85         private boolean inWebService = false;
86         private boolean inPath = false;
87         private boolean inInterface = false;
88         private boolean inClassName = false;
89         private boolean inClasses = false;
90         private boolean inCache = false;
91         private boolean inCacheTimeout = false;
92         private boolean inWSDL = false;
93         private boolean inRole = false;
94         private boolean inUsername = false;
95         private boolean inJMXBeans = false;
96         private boolean inBean = false;
97         private boolean inObjectName = false;
98         private boolean inBeans = false;
99         private boolean inBindName = false;
100         private boolean inThread = false;
101         private boolean inThreadNumber = false;
102         private boolean inTransaction = false;
103         private String JavaDoc inData = null;
104         
105         // the temporary objects
106
private WebServiceInfo webServiceInfo = null;
107         private JMXBeanInfo jmxBeanInfo = null;
108         private BeanInfo beanInfo = null;
109         private DeploymentThreadInfo threadInfo = null;
110         
111         /**
112          * The constructor of the XPDL Handler
113          */

114         public CoadunationHandler(DeploymentInfo deploymentInfo) {
115             this.deploymentInfo = deploymentInfo;
116         }
117         
118         
119         /**
120          * Parse the start of an element
121          */

122         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
123                          Attributes JavaDoc attributes) throws SAXException JavaDoc {
124             // handle a package and retrieve the value information
125
if (qName.compareToIgnoreCase(COADUNATION) == 0) {
126                 String JavaDoc version = (String JavaDoc)attributes.getValue(COADUNATION_VERSION);
127                 String JavaDoc name = (String JavaDoc)attributes.getValue(COADUNATION_NAME);
128                 if ((version == null) || (name == null)) {
129                     throw new SAXException JavaDoc(
130                             "Failed to retrieve either the name or the version");
131                 }
132                 deploymentInfo.setVersion(version);
133                 deploymentInfo.setName(name);
134                 inCoadunation = true;
135             } else if (inCoadunation &&
136                     qName.compareToIgnoreCase(DESCRIPTION) == 0) {
137                 inDescription = true;
138                 inData = "";
139             }
140             // the web services section
141
else if (inCoadunation &&
142                     qName.compareToIgnoreCase(WEB_SERVICES) == 0) {
143                 inWebServices = true;
144             } else if (inWebServices &&
145                     qName.compareToIgnoreCase(WEB_SERVICE) == 0) {
146                 inWebService = true;
147                 webServiceInfo = new WebServiceInfo();
148             } else if (inWebService &&
149                     qName.compareToIgnoreCase(PATH) == 0) {
150                 inPath = true;
151                 inData = new String JavaDoc();
152             } else if (inWebService &&
153                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
154                 inClassName = true;
155                 inData = new String JavaDoc();
156             } else if (inWebService &&
157                     qName.compareToIgnoreCase(WSDL_PATH) == 0) {
158                 inWSDL = true;
159                 inData = new String JavaDoc();
160             } else if (inWebService &&
161                     qName.compareToIgnoreCase(ROLE) == 0) {
162                 inRole = true;
163                 inData = new String JavaDoc();
164             } else if (inWebService &&
165                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
166                 inTransaction = true;
167                 inData = new String JavaDoc();
168             }
169             // the jmx section
170
else if (inCoadunation &&
171                     qName.compareToIgnoreCase(JMX_BEANS) == 0) {
172                 inJMXBeans = true;
173             } else if (inJMXBeans &&
174                     qName.compareToIgnoreCase(BEAN) == 0) {
175                 jmxBeanInfo = new JMXBeanInfo();
176                 inBean = true;
177             } else if (inJMXBeans && inBean &&
178                     qName.compareToIgnoreCase(INTERFACE_NAME) == 0) {
179                 inInterface = true;
180                 inData = new String JavaDoc();
181             } else if (inJMXBeans && inBean &&
182                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
183                 inClassName = true;
184                 inData = new String JavaDoc();
185             } else if (inJMXBeans && inBean &&
186                     qName.compareToIgnoreCase(CLASSES) == 0) {
187                 inClasses = true;
188                 inData = new String JavaDoc();
189             } else if (inJMXBeans && inBean &&
190                     qName.compareToIgnoreCase(CACHE) == 0) {
191                 inCache = true;
192                 inData = new String JavaDoc();
193             } else if (inJMXBeans && inBean &&
194                     qName.compareToIgnoreCase(CACHE_TIMEOUT) == 0) {
195                 inCacheTimeout = true;
196                 inData = new String JavaDoc();
197             } else if (inJMXBeans && inBean &&
198                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
199                 inTransaction = true;
200                 inData = new String JavaDoc();
201             } else if (inJMXBeans && inBean &&
202                     qName.compareToIgnoreCase(OBJECT_NAME) == 0) {
203                 inObjectName = true;
204                 inData = new String JavaDoc();
205             } else if (inJMXBeans && inBean &&
206                     qName.compareToIgnoreCase(BIND_NAME) == 0) {
207                 inBindName = true;
208                 inData = new String JavaDoc();
209             } else if (inJMXBeans && inBean &&
210                     qName.compareToIgnoreCase(ROLE) == 0) {
211                 inRole = true;
212                 inData = new String JavaDoc();
213             } else if (inJMXBeans && inBean &&
214                     qName.compareToIgnoreCase(USERNAME) == 0) {
215                 inUsername = true;
216                 inData = new String JavaDoc();
217             } else if (inJMXBeans && inBean &&
218                     qName.compareToIgnoreCase(THREAD) == 0) {
219                 inThread = true;
220                 threadInfo = new DeploymentThreadInfo();
221             }
222             // coadunation beans
223
else if (inCoadunation &&
224                     qName.compareToIgnoreCase(BEANS) == 0) {
225                 inBeans = true;
226             } else if (inBeans &&
227                     qName.compareToIgnoreCase(BEAN) == 0) {
228                 beanInfo = new BeanInfo();
229                 inBean = true;
230             } else if (inBeans && inBean &&
231                     qName.compareToIgnoreCase(INTERFACE_NAME) == 0) {
232                 inInterface = true;
233                 inData = new String JavaDoc();
234             } else if (inBeans && inBean &&
235                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
236                 inClassName = true;
237                 inData = new String JavaDoc();
238             } else if (inBeans && inBean &&
239                     qName.compareToIgnoreCase(CLASSES) == 0) {
240                 inClasses = true;
241                 inData = new String JavaDoc();
242             } else if (inBeans && inBean &&
243                     qName.compareToIgnoreCase(CACHE) == 0) {
244                 inCache = true;
245                 inData = new String JavaDoc();
246             } else if (inBeans && inBean &&
247                     qName.compareToIgnoreCase(CACHE_TIMEOUT) == 0) {
248                 inCacheTimeout = true;
249                 inData = new String JavaDoc();
250             } else if (inBeans && inBean &&
251                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
252                 inTransaction = true;
253                 inData = new String JavaDoc();
254             } else if (inBeans && inBean &&
255                     qName.compareToIgnoreCase(BIND_NAME) == 0) {
256                 inBindName = true;
257                 inData = new String JavaDoc();
258             } else if (inBeans && inBean &&
259                     qName.compareToIgnoreCase(ROLE) == 0) {
260                 inRole = true;
261                 inData = new String JavaDoc();
262             } else if (inBeans && inBean &&
263                     qName.compareToIgnoreCase(USERNAME) == 0) {
264                 inUsername = true;
265                 inData = new String JavaDoc();
266             } else if (inBeans && inBean &&
267                     qName.compareToIgnoreCase(THREAD) == 0) {
268                 inThread = true;
269                 threadInfo = new DeploymentThreadInfo();
270             }
271             // setup the thread information
272
else if (((inBeans && inBean && inThread) ||
273                     (inJMXBeans && inBean && inThread)) &&
274                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
275                 inClassName = true;
276                 inData = new String JavaDoc();
277             } else if (((inBeans && inBean && inThread) ||
278                     (inJMXBeans && inBean && inThread)) &&
279                     qName.compareToIgnoreCase(USERNAME) == 0) {
280                 inUsername = true;
281                 inData = new String JavaDoc();
282             } else if (((inBeans && inBean && inThread) ||
283                     (inJMXBeans && inBean && inThread)) &&
284                     qName.compareToIgnoreCase(THREAD_NUMBER) == 0) {
285                 inThreadNumber = true;
286                 inData = new String JavaDoc();
287             }
288             
289         }
290         
291         /**
292          * Read in the characters
293          */

294         public void characters(char[] ch, int start, int length) {
295             if (inDescription || inPath || inClassName || inClasses || inWSDL ||
296                     inRole || inObjectName || inInterface || inBindName ||
297                     inUsername || inThreadNumber || inCache || inCacheTimeout ||
298                     inTransaction) {
299                 inData += new String JavaDoc(ch,start,length);
300             }
301         }
302         
303         /**
304          * Handle the end of an element
305          */

306         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
307                 throws SAXException JavaDoc {
308             // handle a package and retrieve the value information
309
if (qName.compareToIgnoreCase(COADUNATION) == 0) {
310                 inCoadunation = false;
311             } else if (inCoadunation &&
312                     qName.compareToIgnoreCase(DESCRIPTION) == 0) {
313                 inDescription = false;
314                 deploymentInfo.setDescription(inData.trim());
315                 inData = "";
316             }
317             // the web services section
318
else if (inCoadunation &&
319                     qName.compareToIgnoreCase(WEB_SERVICES) == 0) {
320                 inWebServices = false;
321             }
322             else if (inWebServices &&
323                     qName.compareToIgnoreCase(WEB_SERVICE) == 0) {
324                 if (webServiceInfo.isInitialized() == false) {
325                     throw new SAXException JavaDoc(
326                             "The web service information has not been setup " +
327                             "correctly. Must contain [path][class][WSDL]" +
328                             "[role]");
329                 }
330                 deploymentInfo.addWebService(webServiceInfo);
331                 inWebService = false;
332                 inData = new String JavaDoc();
333             } else if (inWebService &&
334                     qName.compareToIgnoreCase(PATH) == 0) {
335                 inPath = false;
336                 webServiceInfo.setPath(inData.trim());
337                 inData = new String JavaDoc();
338             } else if (inWebService &&
339                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
340                 inClassName = false;
341                 webServiceInfo.setClassName(inData.trim());
342                 inData = new String JavaDoc();
343             } else if (inWebService &&
344                     qName.compareToIgnoreCase(WSDL_PATH) == 0) {
345                 inWSDL = false;
346                 webServiceInfo.setWSDLPath(inData.trim());
347                 inData = new String JavaDoc();
348             } else if (inWebService &&
349                     qName.compareToIgnoreCase(ROLE) == 0) {
350                 inRole = false;
351                 webServiceInfo.setRole(inData.trim());
352                 inData = new String JavaDoc();
353             } else if (inWebService &&
354                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
355                 String JavaDoc transactionResult = inData.trim();
356                 if (transactionResult.equalsIgnoreCase("yes") ||
357                         transactionResult.equalsIgnoreCase("true")) {
358                     webServiceInfo.setTransaction(true);
359                 }
360                 inTransaction = false;
361                 inData = new String JavaDoc();
362             }
363             // the jmx section
364
else if (inCoadunation &&
365                     qName.compareToIgnoreCase(JMX_BEANS) == 0) {
366                 inJMXBeans = false;
367             } else if (inJMXBeans &&
368                     qName.compareToIgnoreCase(BEAN) == 0) {
369                 if (jmxBeanInfo.isInitialized() == false) {
370                     throw new SAXException JavaDoc(
371                             "The JMX Bean information has not been setup " +
372                             "correctly. Must contain [class][objectName].");
373                 }
374                 deploymentInfo.addJmxBean(jmxBeanInfo);
375                 inBean = false;
376             } else if (inJMXBeans && inBean && !inThread &&
377                     qName.compareToIgnoreCase(INTERFACE_NAME) == 0) {
378                 jmxBeanInfo.setInterfaceName(inData.trim());
379                 inInterface = false;
380             } else if (inJMXBeans && inBean && !inThread &&
381                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
382                 jmxBeanInfo.setClassName(inData.trim());
383                 inClassName = false;
384             } else if (inJMXBeans && inBean && !inThread &&
385                     qName.compareToIgnoreCase(CLASSES) == 0) {
386                 jmxBeanInfo.addClass(inData.trim());
387                 inClasses = false;
388             } else if (inJMXBeans && inBean && !inThread &&
389                     qName.compareToIgnoreCase(CACHE) == 0) {
390                 String JavaDoc cacheResult = inData.trim();
391                 if (cacheResult.equalsIgnoreCase("yes") ||
392                         cacheResult.equalsIgnoreCase("true")) {
393                     jmxBeanInfo.setCacheResults(true);
394                 }
395                 inCache = false;
396             } else if (inJMXBeans && inBean && !inThread &&
397                     qName.compareToIgnoreCase(CACHE_TIMEOUT) == 0) {
398                 inCacheTimeout = false;
399                 jmxBeanInfo.setCacheTimeout(Long.parseLong(inData.trim()));
400             } else if (inJMXBeans && inBean && !inThread &&
401                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
402                 String JavaDoc transactionResult = inData.trim();
403                 if (transactionResult.equalsIgnoreCase("yes") ||
404                         transactionResult.equalsIgnoreCase("true")) {
405                     jmxBeanInfo.setTransaction(true);
406                 }
407                 inTransaction = false;
408             } else if (inJMXBeans && inBean && !inThread &&
409                     qName.compareToIgnoreCase(OBJECT_NAME) == 0) {
410                 jmxBeanInfo.setObjectName(inData.trim());
411                 inObjectName = false;
412             } else if (inJMXBeans && inBean && !inThread &&
413                     qName.compareToIgnoreCase(BIND_NAME) == 0) {
414                 jmxBeanInfo.setBindName(inData.trim());
415                 inBindName = false;
416             } else if (inJMXBeans && inBean && !inThread &&
417                     qName.compareToIgnoreCase(ROLE) == 0) {
418                 jmxBeanInfo.setRole(inData.trim());
419                 inRole = false;
420             } else if (inJMXBeans && inBean && !inThread &&
421                     qName.compareToIgnoreCase(USERNAME) == 0) {
422                 jmxBeanInfo.setUsername(inData.trim());
423                 inUsername = false;
424             } else if (inJMXBeans && inBean &&
425                     qName.compareToIgnoreCase(THREAD) == 0) {
426                 if (threadInfo.isInitialized() == false) {
427                     throw new SAXException JavaDoc(
428                             "Invalid threading information was supplied [class]"
429                             + "[username] and [number] must be supplied.");
430                 }
431                 jmxBeanInfo.addThreadInfo(threadInfo);
432                 inThread = false;
433             }
434             // coadunation beans
435
else if (inCoadunation &&
436                     qName.compareToIgnoreCase(BEANS) == 0) {
437                 inBeans = false;
438             } else if (inBeans &&
439                     qName.compareToIgnoreCase(BEAN) == 0) {
440                 if (beanInfo.isInitialized() == false) {
441                     throw new SAXException JavaDoc(
442                             "The Coadunation bean information has not been setup " +
443                             "correctly. Must contain [interface][class]" +
444                             "[bindName][role].");
445                 }
446                 deploymentInfo.addBean(beanInfo);
447                 inBean = false;
448             } else if (inBeans && inBean && !inThread &&
449                     qName.compareToIgnoreCase(INTERFACE_NAME) == 0) {
450                 inInterface = false;
451                 beanInfo.setInterfaceName(inData.trim());
452             } else if (inBeans && inBean && !inThread &&
453                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
454                 inClassName = false;
455                 beanInfo.setClassName(inData.trim());
456             } else if (inBeans && inBean && !inThread &&
457                     qName.compareToIgnoreCase(CLASSES) == 0) {
458                 inClasses = false;
459                 beanInfo.addClass(inData.trim());
460             } else if (inBeans && inBean && !inThread &&
461                     qName.compareToIgnoreCase(CACHE) == 0) {
462                 String JavaDoc cacheResult = inData.trim();
463                 if (cacheResult.equalsIgnoreCase("yes") ||
464                         cacheResult.equalsIgnoreCase("true")) {
465                     beanInfo.setCacheResults(true);
466                 }
467                 inCache = false;
468             } else if (inBeans && inBean && !inThread &&
469                     qName.compareToIgnoreCase(CACHE_TIMEOUT) == 0) {
470                 inCacheTimeout = false;
471                 beanInfo.setCacheTimeout(Long.parseLong(inData.trim()));
472             } else if (inBeans && inBean && !inThread &&
473                     qName.compareToIgnoreCase(TRANSACTION) == 0) {
474                 String JavaDoc transactionResult = inData.trim();
475                 if (transactionResult.equalsIgnoreCase("yes") ||
476                         transactionResult.equalsIgnoreCase("true")) {
477                     beanInfo.setTransaction(true);
478                 }
479                 inCache = false;
480             } else if (inBeans && inBean && !inThread &&
481                     qName.compareToIgnoreCase(BIND_NAME) == 0) {
482                 inBindName = false;
483                 beanInfo.setBindName(inData.trim());
484             } else if (inBeans && inBean && !inThread &&
485                     qName.compareToIgnoreCase(ROLE) == 0) {
486                 inRole = false;
487                 beanInfo.setRole(inData.trim());
488             } else if (inBeans && inBean && !inThread &&
489                     qName.compareToIgnoreCase(USERNAME) == 0) {
490                 beanInfo.setUsername(inData.trim());
491                 inUsername = false;
492             } else if (inBeans && inBean &&
493                     qName.compareToIgnoreCase(THREAD) == 0) {
494                 if (threadInfo.isInitialized() == false) {
495                     throw new SAXException JavaDoc(
496                             "Invalid threading information was supplied [class]" +
497                             "[username] and [number] must be supplied.");
498                 }
499                 beanInfo.addThreadInfo(threadInfo);
500                 inThread = false;
501             }
502             // setup the thread information
503
else if (((inBeans && inBean && inThread) ||
504                     (inJMXBeans && inBean && inThread)) &&
505                     qName.compareToIgnoreCase(CLASS_NAME) == 0) {
506                 threadInfo.setClassName(inData.trim());
507                 inClassName = false;
508             } else if (((inBeans && inBean && inThread) ||
509                     (inJMXBeans && inBean && inThread)) &&
510                     qName.compareToIgnoreCase(USERNAME) == 0) {
511                 threadInfo.setUsername(inData.trim());
512                 inUsername = false;
513             } else if (((inBeans && inBean && inThread) ||
514                     (inJMXBeans && inBean && inThread)) &&
515                     qName.compareToIgnoreCase(THREAD_NUMBER) == 0) {
516                 threadInfo.setThreadNumber(Integer.parseInt(inData.trim()));
517                 inThreadNumber = false;
518             }
519         }
520     }
521     
522     
523     // the class member variables
524
private CoadunationHandler handler = null;
525     private DeploymentInfo deploymentInfo = null;
526     
527     /**
528      * Creates a new instance of CoadunationParser
529      *
530      * @param sourceXML The source xml document.
531      *
532      * @throws DeploymentException
533      */

534     public CoadunationParser(String JavaDoc sourceXML) throws DeploymentException {
535         try {
536             deploymentInfo = new DeploymentInfo();
537             handler = new CoadunationHandler(deploymentInfo);
538             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
539             InputSource JavaDoc source = new InputSource JavaDoc(
540                     new StringReader JavaDoc(sourceXML) );
541             parser.parse(source,handler);
542         } catch (Exception JavaDoc ex) {
543             throw new DeploymentException(
544                     "Failed to parse the source xml document :" +
545                     ex.getMessage(),ex);
546         }
547     }
548     
549     
550     /**
551      * The method to retrieve the deployment information.
552      *
553      * @return The object containing the deployment information.
554      */

555     public DeploymentInfo getDeploymentInfo() {
556         return deploymentInfo;
557     }
558 }
559
Popular Tags