KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbicommon > descriptor > JBIDescriptorBuilder


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
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  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JBIDescriptorBuilder.java 565 2006-06-08 10:03:27Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.tools.jbicommon.descriptor; // NOPMD by ofabre
24

25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.URI JavaDoc;
32 import java.net.URISyntaxException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 import javax.xml.XMLConstants JavaDoc;
42 import javax.xml.parsers.DocumentBuilder JavaDoc;
43 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
44 import javax.xml.parsers.ParserConfigurationException JavaDoc;
45 import javax.xml.transform.stream.StreamSource JavaDoc;
46 import javax.xml.validation.Schema JavaDoc;
47 import javax.xml.validation.SchemaFactory JavaDoc;
48 import javax.xml.validation.Validator JavaDoc;
49
50 import org.objectweb.petals.tools.jbicommon.util.FileUtil;
51 import org.objectweb.petals.tools.jbicommon.util.XMLUtil;
52 import org.w3c.dom.Document JavaDoc;
53 import org.w3c.dom.DocumentFragment JavaDoc;
54 import org.w3c.dom.Element JavaDoc;
55 import org.w3c.dom.Node JavaDoc;
56 import org.xml.sax.ErrorHandler JavaDoc;
57 import org.xml.sax.SAXException JavaDoc;
58 import org.xml.sax.SAXParseException JavaDoc;
59
60 /**
61  * The JBIDescriptorBuilder helps to populate a <code>JBIDescriptor</code>
62  * instance with values loaded from a specified <i>jbi.xml</i>.
63  *
64  * @version $Rev: 565 $ $Date: 2006-06-08 10:03:27Z $
65  * @since Petals 1.0
66  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
67  */

68 public class JBIDescriptorBuilder {
69     /**
70      * This class is used to handle errors during creating XML objects, parsing
71      * XML and validating a XML.
72      */

73     private class JbiXmlErrorHandler implements ErrorHandler JavaDoc {
74
75         /**
76          *
77          */

78         public JbiXmlErrorHandler() { // NOPMD by ofabre
79
super();
80         }
81
82         /**
83          * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
84          */

85         public void error(final SAXParseException JavaDoc se) throws SAXException JavaDoc {
86             log.log(Level.SEVERE, se.getMessage(), se);
87             throw se;
88         }
89
90         /**
91          * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
92          */

93         public void fatalError(final SAXParseException JavaDoc se) throws SAXException JavaDoc {
94             log.log(Level.SEVERE, se.getMessage(), se);
95             throw se;
96         }
97
98         /**
99          * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
100          */

101         public void warning(final SAXParseException JavaDoc e) throws SAXException JavaDoc {
102             log.log(Level.WARNING, e.getMessage(), e);
103         }
104
105     }
106
107     public static final String JavaDoc JBI_XSD = "jbi.xsd";
108
109     /**
110      * Standard location to the JBI Descriptor resource.
111      */

112     public static final String JavaDoc JBI_DESCRIPTOR_RESOURCE = "META-INF"
113             + File.separator + "jbi.xml";
114
115     /**
116      * The JBI Descriptor XML Schema Resource Location ("schema/jbi.xsd").
117      */

118     public static final String JavaDoc JBI_SCHEMA_RESOURCE = "schema" + File.separator
119             + JBI_XSD;
120
121     private static final String JavaDoc BOOTSTRAP_CLASS_NAME_ELEM_NAME = "bootstrap-class-name";
122
123     private static final String JavaDoc BOOTSTRAP_CLASS_PATH_ELEM_NAME = "bootstrap-class-path";
124
125     private static final String JavaDoc COMPONENT_CLASS_NAME_ELEM_NAME = "component-class-name";
126
127     private static final String JavaDoc COMPONENT_CLASS_PATH_ELEM_NAME = "component-class-path";
128
129     private static final String JavaDoc ENDPOINT_ATTR_NAME = "endpoint-name";
130
131     private static final String JavaDoc IDENTIFICATION_ELEM_NAME = "identification";
132
133     private static final String JavaDoc INTERFACE_ATTR_NAME = "interface-name";
134
135     private static final String JavaDoc INVALID_EXTENSION_NAMESPACE = "Invalid namespace for jbi extensions ";
136
137     /**
138      * Schema file
139      */

140     private static Schema JavaDoc schema = null;
141
142     private static final String JavaDoc SERVICE_ATTR_NAME = "service-name";
143
144     private static final String JavaDoc SHARED_LIBRARY_ELEM_NAME = "shared-library";
145
146     // private static final String SHARED_LIBRARY_LIST_ELEM_NAME =
147
// "shared-library-list";
148

149     private static final String JavaDoc VERSION_ATTR_NAME = "version";
150
151     /**
152      * URI to the jbi.xml file.
153      */

154     private URI JavaDoc jbiXmlUri;
155
156     /**
157      * Logger wrapper
158      */

159     private Logger JavaDoc log; // NOPMD by ofabre
160

161     /**
162      * The source XML document file.
163      */

164     private Document JavaDoc source;
165
166     private final JbiXmlErrorHandler xmlErrorHandler = new JbiXmlErrorHandler();
167
168     /**
169      * JBI Descriptor Builder constructor using the source xml file URI from
170      * which the document will be loaded.
171      */

172     public JBIDescriptorBuilder(final URI JavaDoc sourceUri, final Logger JavaDoc logger) {
173         if (sourceUri == null) {
174             throw new IllegalArgumentException JavaDoc(
175                     "DescriptorBuilder: invalid source URI argument.");
176         }
177
178         this.jbiXmlUri = sourceUri;
179         log = logger;
180     }
181
182     /**
183      * Load, Parse and Validate jbi.xml document for building (populating) the
184      * <code>Descriptor</code> object.
185      *
186      * @return a JBIDescriptor object mapping the XML jbi descriptor
187      *
188      * @throws JBIDescriptorException
189      * if an error occured during JBIDescriptor creation
190      */

191     public JBIDescriptor build() throws JBIDescriptorException {
192
193         // do not repeat load, parse and validation steps
194
if (source == null) {
195
196             // validate the jbi.xml file against the schema
197
if (!validateJbiXml(jbiXmlUri)) {
198                 throw new JBIDescriptorException(
199                         "Can't validate jbi descriptor :"
200                                 + jbiXmlUri.toString());
201             }
202
203             // parse XML after validation
204
source = parseJbiXml(jbiXmlUri);
205
206         }
207         return buildDescriptor(source);
208     }
209
210     /**
211      * Returns a String representation of a JBI descriptor file
212      *
213      * @param descriptorFile
214      * the JBI Descriptor <code>File</code>
215      * @return a <code>String</code> representation of the JBI Descriptor
216      */

217     public String JavaDoc getJbiDescriptorAsString() {
218         File JavaDoc descriptorFile = new File JavaDoc(jbiXmlUri);
219         String JavaDoc ret = null;
220         if (descriptorFile.isFile()) {
221             ByteArrayOutputStream JavaDoc os = null;
222             InputStream JavaDoc is = null;
223             try {
224                 os = new ByteArrayOutputStream JavaDoc();
225                 is = new FileInputStream JavaDoc(descriptorFile);
226                 FileUtil.copyInputStream(is, os);
227                 ret = os.toString();
228             } catch (IOException JavaDoc e) {
229                 log.log(Level.SEVERE, "Error reading jbi descritor: "
230                         + descriptorFile, e);
231             }
232         }
233         return ret;
234     }
235
236     /**
237      * @return Returns the source.
238      */

239     public Document JavaDoc getSource() {
240         return source;
241     }
242
243     /**
244      * @return Returns the jbiXmlUri.
245      */

246     public URI JavaDoc getURI() {
247         return jbiXmlUri;
248     }
249
250     /**
251      *
252      * @param sourceURI
253      * @return
254      */

255     public Document JavaDoc parseJbiXml(final URI JavaDoc sourceURI)
256             throws JBIDescriptorException {
257
258         DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory
259                 .newInstance();
260         docBuilderFactory.setNamespaceAware(true);
261
262         DocumentBuilder JavaDoc docBuilder = null;
263
264         try {
265             docBuilder = docBuilderFactory.newDocumentBuilder();
266         } catch (ParserConfigurationException JavaDoc pce) {
267             throw new JBIDescriptorException("Bad XML parser configuration",
268                     pce);
269         }
270
271         // set up xml parsing error handler
272
docBuilder.setErrorHandler(xmlErrorHandler);
273
274         // parse the xml document URI to return
275
String JavaDoc jbiXmlURI = sourceURI.toString();
276
277         Document JavaDoc document = null;
278
279         try {
280             document = docBuilder.parse(jbiXmlURI);
281         } catch (SAXException JavaDoc saxe) {
282             throw new JBIDescriptorException(
283                     "JBI descriptor is not well formed XML", saxe);
284         } catch (IOException JavaDoc ioe) {
285             throw new JBIDescriptorException("Can't read JBI descriptor", ioe);
286         }
287         return document;
288     }
289
290     /**
291      *
292      * @param sourceURI
293      * @return
294      * @throws SAXException
295      * @throws IOException
296      */

297     public boolean validateJbiXml(final URI JavaDoc sourceURI) {
298         boolean res = true;
299         // instance for xml error handling
300

301         Schema JavaDoc jbiSchema = getJBISchema();
302         // create a Validator instance which is be used to validate a
303
// document
304
if (jbiSchema == null) {
305             log.log(Level.WARNING, "Can't check jbi descriptor validity "
306                     + "because jbi schema is unavailable");
307         } else {
308             Validator JavaDoc validator = schema.newValidator();
309             validator.setErrorHandler(xmlErrorHandler);
310
311             try {
312                 // validate the DOM tree
313
validator.validate(new StreamSource JavaDoc(new File JavaDoc(sourceURI)));
314             } catch (SAXException JavaDoc e) {
315                 log.log(Level.SEVERE, "Bad JBI descriptor for " + sourceURI, e);
316                 res = false;
317             } catch (IOException JavaDoc e) {
318                 log.log(Level.SEVERE, "JBI descriptor not found " + sourceURI,
319                         e);
320                 res = false;
321             }
322         }
323         return res;
324     }
325
326     protected URI JavaDoc getJbiXmlUri() {
327         return jbiXmlUri;
328     }
329
330     protected void setJbiXmlUri(final URI JavaDoc jbiXmlUri) {
331         this.jbiXmlUri = jbiXmlUri;
332     }
333
334     /**
335      * FIXME : Needed by unit tests. Unit tests should initialize it by a
336      * constructor.
337      *
338      * @param log
339      */

340     protected void setLog(final Logger JavaDoc log) {
341         this.log = log;
342     }
343
344     /**
345      * Parse the XML document and load values to the Descriptor object
346      * representation.
347      *
348      * @param document
349      * @return a populated Descriptor instance with values loaded from the XML
350      * file. May return <code>null</code> if an error occur while
351      * navigating on XML tree.
352      * @throws JBIDescriptorException
353      */

354     private JBIDescriptor buildDescriptor(final Document JavaDoc document)
355             throws JBIDescriptorException {
356
357         JBIDescriptor descriptor = new JBIDescriptor();
358
359         // get root element
360
Element JavaDoc jbi = document.getDocumentElement();
361
362         // extract JBI version attribute from the root element
363
String JavaDoc versionAttr = XMLUtil.getRequiredAttributeValue(jbi,
364                 VERSION_ATTR_NAME);
365         descriptor.setVersion(Double.parseDouble(versionAttr));
366
367         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(jbi);
368         for (Node JavaDoc child : children) {
369             if ("component".equals(child.getNodeName())) {
370                 descriptor.setComponent(loadComponentXml(child));
371             } else if (SHARED_LIBRARY_ELEM_NAME.equals(child.getNodeName())) {
372                 descriptor.setSharedLibrary(loadSharedLibraryXml(child));
373             } else if ("service-assembly".equals(child.getNodeName())) {
374                 descriptor.setServiceAssembly(loadServiceAssemblyXml(child));
375             } else if ("services".equals(child.getNodeName())) {
376                 descriptor.setServices(loadServicesXml(child));
377             }
378         }
379         return descriptor;
380     }
381
382     private synchronized Schema JavaDoc getJBISchema() { // NOPMD by gblondelle
383
if (schema == null) {
384
385             // create a SchemaFactory capable of understanding WXS schemas
386
SchemaFactory JavaDoc factory = SchemaFactory
387                     .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
388
389             factory.setErrorHandler(xmlErrorHandler);
390
391             // load a WXS schema, represented by a Schema instance
392
try {
393                 schema = factory
394                         .newSchema(new StreamSource JavaDoc(openJbiSchemaFile()));
395             } catch (SAXException JavaDoc saxe) {
396                 log.log(Level.SEVERE, saxe.getMessage(), saxe);
397             }
398         }
399         return schema;
400     }
401
402     /**
403      * @param element
404      * @throws JBIDescriptorException
405      */

406     private ComponentDescription loadComponentXml(final Node JavaDoc production)
407             throws JBIDescriptorException {
408         ComponentDescription component = new ComponentDescription();
409
410         // component attributes
411
component
412                 .setType(XMLUtil.getRequiredAttributeValue(production, "type"));
413         component.setComponentClassLoaderDelegation(XMLUtil.getAttributeValue(
414                 production, "component-class-loader-delegation"));
415         component.setBootstrapClassLoaderDelegation(XMLUtil.getAttributeValue(
416                 production, "bootstrap-class-loader-delegation"));
417
418         Extensions extensions = new Extensions();
419         List JavaDoc<SharedLibraryList> slList = new ArrayList JavaDoc<SharedLibraryList>();
420         // component sub nodes
421
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(production);
422         for (Node JavaDoc child : children) {
423             if (IDENTIFICATION_ELEM_NAME.equals(child.getNodeName())) {
424                 component.setIdentification(loadIdentificationXml(child));
425             } else if (COMPONENT_CLASS_NAME_ELEM_NAME.equals(child
426                     .getNodeName())) {
427                 component.setComponentClassName(XMLUtil.getTextContent(child));
428             } else if (COMPONENT_CLASS_PATH_ELEM_NAME.equals(child
429                     .getNodeName())) {
430                 component.setComponentClassPath(XMLUtil.getTextContents(child
431                         .getChildNodes()));
432             } else if (BOOTSTRAP_CLASS_NAME_ELEM_NAME.equals(child
433                     .getNodeName())) {
434                 component.setBootstrapClassName(XMLUtil.getTextContent(child));
435             } else if (BOOTSTRAP_CLASS_PATH_ELEM_NAME.equals(child
436                     .getNodeName())) {
437                 component.setBootstrapClassPath(XMLUtil.getTextContents(child
438                         .getChildNodes()));
439             } else if (SHARED_LIBRARY_ELEM_NAME.equals(child.getNodeName())) {
440                 loadSharedLibraryListXml(slList, child);
441             } else {
442                 loadExtensions(extensions, child);
443             }
444         }
445         // set extensions
446
component.setExtensions(extensions);
447         // set shared libs
448
component.setSharedLibraryList(slList);
449
450         return component;
451     }
452
453     /**
454      * @param connectionsTree
455      * @return
456      * @throws JBIDescriptorException
457      */

458     private List JavaDoc<Connection> loadConnectionsXml(final Node JavaDoc connectionsTree)
459             throws JBIDescriptorException {
460
461         List JavaDoc<Connection> connections = new ArrayList JavaDoc<Connection>();
462
463         if (connectionsTree != null) {
464             // get "connections/connection" Node List
465
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(connectionsTree);
466             for (Node JavaDoc child : children) {
467                 if ("connection".equals(child.getNodeName())) {
468                     connections.add(loadConnectionXml(child));
469                 }
470             }
471         }
472         return connections;
473     }
474
475     private Connection loadConnectionXml(final Node JavaDoc node)
476             throws JBIDescriptorException {
477
478         // connection object
479
Connection newItem = new Connection();
480
481         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
482         for (Node JavaDoc child : children) {
483             if ("consumer".equals(child.getNodeName())) {
484                 /*
485                  * TODO deal with cases where there is only the interface name
486                  * and no endpoint name and service name
487                  * newItem.setConsumerInterfaceName(extractXmlAttributeQName(consumer,
488                  * "interface-name"));
489                  */

490
491                 newItem.setConsumerServiceName(XMLUtil
492                         .extractXmlAttributeQName(child, SERVICE_ATTR_NAME));
493
494                 newItem.setConsumerEndpointName(XMLUtil.getAttributeValue(
495                         child, ENDPOINT_ATTR_NAME));
496             } else if ("provider".equals(child.getNodeName())) {
497                 newItem.setProviderServiceName(XMLUtil
498                         .extractRequiredXmlAttributeQName(child,
499                                 SERVICE_ATTR_NAME));
500
501                 newItem.setProviderEndpointName(XMLUtil
502                         .getRequiredAttributeValue(child, ENDPOINT_ATTR_NAME));
503             }
504         }
505         return newItem;
506     }
507
508     /**
509      * @param list
510      * @return
511      * @throws JBIDescriptorException
512      */

513     private Consumes loadConsumesXml(final Node JavaDoc node)
514             throws JBIDescriptorException {
515         Consumes newItem = new Consumes();
516
517         // consumes attributes
518
newItem.setInterfaceName(XMLUtil.extractRequiredXmlAttributeQName(node,
519                 INTERFACE_ATTR_NAME));
520
521         newItem.setServiceName(XMLUtil.extractXmlAttributeQName(node,
522                 SERVICE_ATTR_NAME));
523
524         newItem.setEndpointName(XMLUtil.getAttributeValue(node,
525                 ENDPOINT_ATTR_NAME));
526
527         newItem.setLinkType(XMLUtil.getAttributeValue(node, "link-type"));
528
529         // consumes extensions
530
Extensions extensions = new Extensions();
531         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
532         for (Node JavaDoc child : children) {
533             loadExtensions(extensions, child);
534         }
535         newItem.setExtensions(extensions);
536
537         return newItem;
538     }
539
540     /**
541      * Extension nodes are nodes which names are different from those specified
542      * by JBI. This method load these extensions.
543      *
544      * @param node
545      * an extension xml node
546      */

547     private void loadExtensions(final Extensions extensions, final Node JavaDoc node) {
548
549         // Add the extension node to the extensions documentfragment containing
550
// all extensions
551
DocumentFragment JavaDoc fragment = extensions.getDocumentFragment();
552         if (fragment == null) {
553             fragment = node.getOwnerDocument().createDocumentFragment();
554             extensions.setDocumentFragment(fragment);
555         }
556         fragment.appendChild(node);
557
558         // Load extension information
559
String JavaDoc uriString = node.lookupNamespaceURI(node.getPrefix());
560         if (uriString != null) {
561             URI JavaDoc uri = null;
562             try {
563                 uri = new URI JavaDoc(uriString);
564             } catch (URISyntaxException JavaDoc e) {
565                 log.warning(INVALID_EXTENSION_NAMESPACE + node.getNodeName());
566             }
567
568             // Check if the node uri is a PEtALS uri
569
if (PetalsExtensionsUris.PETALS_EXTENSIONS
570                     .equals(PetalsExtensionsUris.valueOf(uri))) {
571                 loadPetalsExtensions(extensions, node);
572             } else {
573                 loadUnknownExtension(extensions, node);
574             }
575         }
576     }
577
578     private Identification loadIdentificationXml(final Node JavaDoc identificationNode) {
579         Identification identification = new Identification();
580
581         Extensions extensions = new Extensions();
582         // identification sub nodes
583
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(identificationNode);
584         for (Node JavaDoc child : children) {
585             if ("name".equals(child.getNodeName())) {
586                 identification.setName(XMLUtil.getTextContent(child));
587             } else if ("description".equals(child.getNodeName())) {
588                 identification.setDescription(XMLUtil.getTextContent(child));
589             } else {
590                 loadExtensions(extensions, child);
591             }
592         }
593         // set extensions
594
identification.setExtensions(extensions);
595
596         return identification;
597     }
598
599     private void loadPetalsExtensions(final Extensions extensions,
600             final Node JavaDoc node) {
601         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
602         for (Node JavaDoc child : children) {
603             String JavaDoc childURIString = child.lookupNamespaceURI(child.getPrefix());
604             if (childURIString != null) {
605                 URI JavaDoc childURI = null;
606                 try {
607                     childURI = new URI JavaDoc(childURIString); // NOPMD by gblondelle
608
} catch (URISyntaxException JavaDoc e) {
609                     log.warning(INVALID_EXTENSION_NAMESPACE
610                             + child.getNodeName());
611                 }
612
613                 if (PetalsExtensionsUris.PETALS_EXTENSION_KEY_VALUE
614                         .equals(PetalsExtensionsUris.valueOf(childURI))) {
615                     loadPetalsKeyValueExtension(extensions, child);
616                 } else {
617                     loadUnknownExtension(extensions, child);
618                 }
619             }
620         }
621     }
622
623     private void loadPetalsKeyValueExtension(final Extensions extensions,
624             final Node JavaDoc child) {
625         PetalsExtension petalsExtension = new PetalsExtension();
626         petalsExtension
627                 .setExtensionURI(PetalsExtensionsUris.PETALS_EXTENSION_KEY_VALUE
628                         .value());
629         // treat key value nodes
630
Map JavaDoc<String JavaDoc, String JavaDoc> keyValues = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
631         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(child);
632         for (Node JavaDoc extChild : children) {
633             keyValues.put(extChild.getNodeName(), XMLUtil
634                     .getTextContent(extChild));
635         }
636         petalsExtension.setExtensionObject(keyValues);
637         extensions.addPetalsExtension(
638                 PetalsExtensionsUris.PETALS_EXTENSION_KEY_VALUE.value(),
639                 petalsExtension);
640     }
641
642     /**
643      * @param list
644      * @return
645      * @throws JBIDescriptorException
646      */

647     private Provides loadProvidesXml(final Node JavaDoc node)
648             throws JBIDescriptorException {
649         Provides newItem = new Provides();
650
651         // Provides attributes
652
newItem.setInterfaceName(XMLUtil.extractRequiredXmlAttributeQName(node,
653                 INTERFACE_ATTR_NAME));
654
655         newItem.setServiceName(XMLUtil.extractRequiredXmlAttributeQName(node,
656                 SERVICE_ATTR_NAME));
657
658         newItem.setEndpointName(XMLUtil.getRequiredAttributeValue(node,
659                 ENDPOINT_ATTR_NAME));
660
661         // provides extensions
662
Extensions extensions = new Extensions();
663         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
664         for (Node JavaDoc child : children) {
665             loadExtensions(extensions, child);
666         }
667         newItem.setExtensions(extensions);
668
669         return newItem;
670     }
671
672     /**
673      * @param element
674      * @throws JBIDescriptorException
675      */

676     private ServiceAssembly loadServiceAssemblyXml(final Node JavaDoc node)
677             throws JBIDescriptorException {
678         ServiceAssembly serviceAssembly = new ServiceAssembly();
679
680         Extensions extensions = new Extensions();
681         // Services assembly sub nodes
682
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
683         for (Node JavaDoc child : children) {
684             if (IDENTIFICATION_ELEM_NAME.equals(child.getNodeName())) {
685                 serviceAssembly.setIdentification(loadIdentificationXml(child));
686             } else if ("service-unit".equals(child.getNodeName())) {
687                 serviceAssembly.addServiceUnit(loadServiceUnitXml(child));
688             } else if ("connections".equals(child.getNodeName())) {
689                 serviceAssembly.setConnections(loadConnectionsXml(child));
690             } else {
691                 loadExtensions(extensions, child);
692             }
693         }
694         // set extensions
695
serviceAssembly.setExtensions(extensions);
696
697         return serviceAssembly;
698     }
699
700     /**
701      * @param element
702      * @throws JBIDescriptorException
703      */

704     private Services loadServicesXml(final Node JavaDoc production)
705             throws JBIDescriptorException {
706
707         Services services = new Services();
708
709         // services attributes
710
// is binding component (relative to service unit)
711
String JavaDoc isBindingComponent = XMLUtil.getRequiredAttributeValue(
712                 production, "binding-component");
713         services.setBindingComponent(Boolean.parseBoolean(isBindingComponent));
714
715         Extensions extensions = new Extensions();
716         // Services sub nodes
717
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(production);
718         for (Node JavaDoc child : children) {
719             if ("provides".equals(child.getNodeName())) {
720                 services.addProvides(loadProvidesXml(child));
721             } else if ("consumes".equals(child.getNodeName())) {
722                 services.addConsumes(loadConsumesXml(child));
723             } else {
724                 loadExtensions(extensions, child);
725             }
726         }
727         // set extensions
728
services.setExtensions(extensions);
729
730         return services;
731     }
732
733     /**
734      * @param list
735      * @return
736      */

737     private ServiceUnit loadServiceUnitXml(final Node JavaDoc node) {
738
739         ServiceUnit result = new ServiceUnit();
740
741         Extensions extensions = new Extensions();
742         // Service unit sub nodes
743
List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(node);
744         for (Node JavaDoc child : children) {
745             if (IDENTIFICATION_ELEM_NAME.equals(child.getNodeName())) {
746                 result.setIdentification(loadIdentificationXml(child));
747             } else if ("target".equals(child.getNodeName())) {
748                 List JavaDoc<Node JavaDoc> childChildren = XMLUtil.getNodeChildren(child);
749                 for (Node JavaDoc targetChild : childChildren) {
750                     if ("artifacts-zip".equals(targetChild.getNodeName())) {
751                         result.setTargetArtifactsZip(XMLUtil
752                                 .getTextContent(targetChild));
753                     } else if ("component-name".equals(targetChild
754                             .getNodeName())) {
755                         result.setTargetComponentName(XMLUtil
756                                 .getTextContent(targetChild));
757                     }
758                 }
759             } else {
760                 loadExtensions(extensions, child);
761             }
762         }
763         // set extensions
764
result.setExtensions(extensions);
765
766         return result;
767     }
768
769     /**
770      * @param list
771      * @return
772      */

773     private void loadSharedLibraryListXml(final List JavaDoc<SharedLibraryList> slList,
774             final Node JavaDoc node) {
775         SharedLibraryList sharedLibrary = new SharedLibraryList();
776         sharedLibrary.setName(XMLUtil.getTextContent(node));
777         sharedLibrary.setVersion(XMLUtil.getAttributeValue(node,
778                 VERSION_ATTR_NAME));
779         slList.add(sharedLibrary);
780     }
781
782     /**
783      * @param element
784      */

785     private SharedLibrary loadSharedLibraryXml(final Node JavaDoc production) {
786         SharedLibrary sharedLibrary = new SharedLibrary();
787
788         // shared library attributes
789
sharedLibrary.setClassLoaderDelegation(XMLUtil.getAttributeValue(
790                 production, "class-loader-delegation"));
791         sharedLibrary.setVersion(XMLUtil.getAttributeValue(production,
792                 VERSION_ATTR_NAME));
793
794         List JavaDoc<Node JavaDoc> children = XMLUtil.getNodeChildren(production);
795         for (Node JavaDoc child : children) {
796             if (IDENTIFICATION_ELEM_NAME.equals(child.getNodeName())) {
797                 sharedLibrary.setIdentification(loadIdentificationXml(child));
798             } else if ("shared-library-class-path".equals(child.getNodeName())) {
799                 sharedLibrary.setSharedLibraryClassPath(XMLUtil
800                         .getTextContents(child.getChildNodes()));
801             }
802         }
803         return sharedLibrary;
804     }
805
806     private void loadUnknownExtension(final Extensions extensions,
807             final Node JavaDoc node) {
808         String JavaDoc namespaceURI = node.lookupNamespaceURI(node.getPrefix());
809         UnknownExtension unknownExtension = new UnknownExtension();
810         URI JavaDoc unknownURI = null;
811         try {
812             unknownURI = new URI JavaDoc(namespaceURI);
813         } catch (URISyntaxException JavaDoc e) {
814             log.warning(INVALID_EXTENSION_NAMESPACE + node.getNodeName());
815         }
816         if (unknownURI != null) {
817             unknownExtension.setExtensionURI(unknownURI);
818             unknownExtension.setNode(node);
819             extensions.addUnknownExtension(unknownURI, unknownExtension);
820         }
821     }
822
823     /**
824      * NOTE: The first location to search for JBI schema files is within the
825      * Petals installation root directory. If a schema file is not found within
826      * root directory, the alternative is use the packaged schema files flatten
827      * somewhere which can be found in class path. It won't work if the schema
828      * are compressed in a Zip or Jar archive.
829      *
830      * @return
831      */

832     private InputStream JavaDoc openJbiSchemaFile() {
833         InputStream JavaDoc theJbiSchemaResource = null;
834
835         // find through class path
836
// obtain class loader
837
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
838         // FIXME : is this piece of code necessary?
839
// if (cl == null) {
840
// cl = JBIDescriptorBuilder.class.getClassLoader();
841
// }
842

843         // load jbi.xsd schema resource file
844
URL JavaDoc schemaURL = cl.getResource(JBI_XSD);
845         if (schemaURL != null) {
846             try {
847                 theJbiSchemaResource = schemaURL.openStream();
848             } catch (IOException JavaDoc e) {
849                 log.log(Level.SEVERE, e.getMessage(), e);
850             }
851         }
852
853         if (theJbiSchemaResource == null) {
854             // find in petals installation root
855
String JavaDoc petalsRoot = System.getProperty("petals.home");
856             if (petalsRoot != null) {
857                 try {
858                     theJbiSchemaResource = new FileInputStream JavaDoc(new File JavaDoc(
859                             petalsRoot + JBI_SCHEMA_RESOURCE));
860                 } catch (FileNotFoundException JavaDoc e) {
861                     log.log(Level.SEVERE, e.getMessage(), e);
862                 }
863             }
864         }
865
866         return theJbiSchemaResource;
867     }
868
869 }
870
Popular Tags