KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > DeploymentDescriptorNode


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.node;
25
26 import java.util.*;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 import org.xml.sax.Attributes JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35
36 import com.sun.enterprise.deployment.Descriptor;
37 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
38 import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor;
39 import com.sun.enterprise.deployment.EnvironmentProperty;
40 import com.sun.enterprise.deployment.LifecycleCallbackDescriptor;
41 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
42 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor;
43 import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor;
44 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
45 import com.sun.enterprise.deployment.JndiNameEnvironment;
46 import com.sun.enterprise.deployment.types.EjbReference;
47 import com.sun.enterprise.deployment.util.DOLUtils;
48 import com.sun.enterprise.deployment.xml.TagNames;
49 import com.sun.enterprise.deployment.xml.EjbTagNames;
50 import com.sun.enterprise.deployment.xml.WebServicesTagNames;
51 import com.sun.enterprise.deployment.node.ejb.AroundInvokeNode;
52 import com.sun.enterprise.util.LocalStringManagerImpl;
53
54 /**
55  * Superclass of all Nodes implementation
56  * XMLNode implementation represents all the DOL classes responsible for
57  * handling the XML deployment descriptors. These nodes are called by the
58  * SAX parser when reading and are constructed to build the DOM tree for
59  * saving the XML files.
60  *
61  * XMLNode are orgnalized like a tree with one root XMLNode (which
62  * implement the RootXMLNode interface) and sub XMLNodes responsible
63  * for handling subparts of the XML documents. Sub XMLNodes register
64  * themselfves to their parent XMLNode as handlers for a particular XML
65  * subtag of the tag handled by the parent XMLNode
66  *
67  * Each XMLNode is therefore associated with a xml tag (located anywhere
68  * in the tree of tags as defined by the DTD). It owns the responsibility for
69  * reading and writing the tag, its attributes and all subtags (by using
70  * delegation to sub XMLNode if necessary).
71  *
72  * @author Jerome Dochez
73  * @version
74  */

75 public abstract class DeploymentDescriptorNode extends java.lang.Object JavaDoc
76             implements XMLNode {
77
78     private static final String JavaDoc QNAME_SEPARATOR = ":";
79
80     // handlers is the list of XMLNodes registered for handling sub xml tags of the current
81
// XMLNode
82
protected Hashtable handlers = null;
83     
84     // list of add methods declared on the descriptor class to add sub descriptors extracted
85
// by the handlers registered above. The key for the table is the xml root tag for the
86
// descriptor to be added, the value is the method name to add such descriptor to the
87
// current descriptor.
88
private Hashtable addMethods = null;
89
90     // Each node is associated with a XML tag it is handling
91
private XMLElement xmlTag;
92     
93     // Parent node in the XML Nodes implementation tree we create to map to the XML
94
// tags of the XML document
95
protected XMLNode parentNode = null;
96         
97     // default descriptor associated with this node, some sub nodes which
98
// relies on the dispatch table don't really need to know the actual
99
// type of the descriptor they deal with since they are populated through
100
// reflection method calls
101
protected Object JavaDoc abstractDescriptor;
102     
103
104     // for i18N
105
protected static LocalStringManagerImpl localStrings=
106         new LocalStringManagerImpl(DeploymentDescriptorNode.class);
107     
108     /** Creates new DeploymentDescriptorNode */
109     public DeploymentDescriptorNode() {
110         registerElementHandler(new XMLElement(TagNames.DESCRIPTION), LocalizedInfoNode.class);
111     }
112         
113    /**
114     * @return the descriptor instance to associate with this XMLNode
115     */

116     public Object JavaDoc getDescriptor() {
117         
118         if (abstractDescriptor==null) {
119         abstractDescriptor = DescriptorFactory.getDescriptor(getXMLPath());
120         }
121         return abstractDescriptor;
122     }
123     
124     /**
125      * Adds a new DOL descriptor instance to the descriptor instance associated with
126      * this XMLNode
127      *
128      * @param descriptor the new descriptor
129      */

130     public void addDescriptor(Object JavaDoc descriptor) {
131         if (getParentNode()==null) {
132             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
133                 new Object JavaDoc[] {descriptor , toString()});
134             throw new RuntimeException JavaDoc("Cannot add " + descriptor + " to " + toString());
135         } else {
136             getParentNode().addDescriptor(descriptor);
137         }
138     }
139     
140     /**
141      * Adds a new DOL descriptor instance to the descriptor associated with this
142      * XMLNode
143      *
144      * @param XMLNode the sub-node adding the descriptor;
145      * @param descriptor the new descriptor
146      */

147     protected void addNodeDescriptor(DeploymentDescriptorNode node) {
148         
149         // if there is no descriptor associated with this class, the addDescriptor should implement
150
// the fate of this new descriptor.
151
if (getDescriptor()==null) {
152             addDescriptor(node.getDescriptor());
153             return;
154         }
155         String JavaDoc xmlRootTag = node.getXMLRootTag().getQName();
156         if (addMethods!=null && addMethods.containsKey(xmlRootTag)) {
157             try {
158                 Method JavaDoc toInvoke = getDescriptor().getClass().getMethod(
159                                                                 (String JavaDoc) addMethods.get(xmlRootTag),
160                                                                 new Class JavaDoc[] { node.getDescriptor().getClass() });
161                 toInvoke.invoke(getDescriptor(), new Object JavaDoc[] {node.getDescriptor()});
162             } catch (InvocationTargetException JavaDoc e) {
163             Throwable JavaDoc t = e.getTargetException();
164                     if (t instanceof IllegalArgumentException JavaDoc) {
165                         // We report the error but we continue loading, this will allow the verifier to catch these errors or to register
166
// an error handler for notification
167
DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
168                             new Object JavaDoc[]{node.getDescriptor().getClass() , getDescriptor().getClass()});
169                     } else {
170                         t.printStackTrace();
171                         DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
172                                 new Object JavaDoc[]{t.toString(), null});
173                     }
174             } catch(Throwable JavaDoc t) {
175                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
176                     new Object JavaDoc[] {node.getDescriptor().getClass() , getDescriptor().getClass() });
177                 t.printStackTrace();
178                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure",
179                         new Object JavaDoc[]{t.toString(), null});
180             }
181         } else {
182             addDescriptor(node.getDescriptor());
183         }
184     }
185
186     /**
187      * set the parent node for the current instance.
188      */

189     public void setParentNode(XMLNode parentNode) {
190         this.parentNode = parentNode;
191     }
192     
193     /**
194      * @return the parent node of the current instance
195      */

196     public XMLNode getParentNode() {
197         return parentNode;
198     }
199     
200     /**
201      * register a new XMLNode handler for a particular XML tag.
202      *
203      * @param XMLElement is the XML tag this XMLNode will handle
204      * @param handler the class implemenenting the XMLNode interface
205      */

206     protected void registerElementHandler(XMLElement element, Class JavaDoc handler) {
207         if (handlers==null) {
208             handlers = new Hashtable();
209         }
210         handlers.put(element.getQName(), handler);
211     }
212     
213     /**
214      * register a new XMLNode handler for a particular XML tag.
215      *
216      * @param XMLElement is the XML tag this XMLNode will handle
217      * @param handler the class implemenenting the XMLNode interface
218      * @param addMethodName is the method name for adding the descriptor
219      * extracted by the handler node to the current descriptor
220      */

221     protected void registerElementHandler(XMLElement element, Class JavaDoc handler,
222                                             String JavaDoc addMethodName) {
223                                                                                              
224        registerElementHandler(element, handler);
225        if (addMethods==null) {
226            addMethods = new Hashtable();
227        }
228        addMethods.put(element.getQName(), addMethodName);
229     }
230
231     /**
232      * @return the XML tag associated with this XMLNode
233      */

234     protected XMLElement getXMLRootTag() {
235         return xmlTag;
236     }
237         
238     /**
239      * sets the XML tag associated with this XMLNode
240      */

241     protected void setXMLRootTag(XMLElement element) {
242         xmlTag = element;
243     }
244     
245     /**
246      * @return the handler registered for the subtag element of the curent XMLNode
247      */

248     public XMLNode getHandlerFor(XMLElement element) {
249         if (handlers==null) {
250             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
251                     new Object JavaDoc[] {this , "No handler registered"});
252             return null;
253         } else {
254             Class JavaDoc c = (Class JavaDoc) handlers.get(element.getQName());
255             if (c==null) {
256                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
257                     new Object JavaDoc[] {element.getQName(), "No handler registered"});
258                 return null;
259             }
260             if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) {
261                 DOLUtils.getDefaultLogger().finer("New Handler requested for " + c);
262             }
263             DeploymentDescriptorNode node;
264             try {
265                 node = (DeploymentDescriptorNode) c.newInstance();
266                 node.setParentNode(this);
267         node.setXMLRootTag(element);
268                 node.getDescriptor();
269             } catch(Exception JavaDoc e) {
270                 e.printStackTrace();
271                 return null;
272             }
273         return node;
274         }
275     }
276     
277     /**
278      * SAX Parser API implementation, we don't really care for now.
279      */

280     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
281         //DOLUtils.getDefaultLogger().finer("STARTELEMENT : " + "in " + getXMLRootTag() + " Node, startElement " + element.getQName());
282
if (!this.getXMLRootTag().equals(element))
283             return;
284         
285         if (attributes.getLength()>0) {
286             for (int i=0;i<attributes.getLength();i++) {
287                 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) {
288                     DOLUtils.getDefaultLogger().finer("With attribute " + attributes.getQName(i));
289                     DOLUtils.getDefaultLogger().finer("With value " + attributes.getValue(i));
290                 }
291         // we try the setAttributeValue first, if not processed then the setElement
292
if (!setAttributeValue(element, new XMLElement(attributes.getQName(i)), attributes.getValue(i))) {
293             setElementValue(new XMLElement(attributes.getQName(i)), attributes.getValue(i));
294         }
295                     
296             }
297         }
298     }
299
300     /**
301      * parsed an attribute of an element
302      *
303      * @param the element name
304      * @param the attribute name
305      * @param the attribute value
306      * @return true if the attribute was processed
307      */

308     protected boolean setAttributeValue(XMLElement elementName, XMLElement attributeName, String JavaDoc value) {
309         // we do not support id attribute for the moment
310
if (attributeName.getQName().equals(TagNames.ID)) {
311             return true;
312         }
313
314     return false;
315     }
316     
317     /**
318      * receives notification of the end of an XML element by the Parser
319      *
320      * @param element the xml tag identification
321      * @return true if this node is done processing the XML sub tree
322      */

323     public boolean endElement(XMLElement element) {
324     //DOLUtils.getDefaultLogger().finer("ENDELEMENT : " + "in " + getXMLRootTag() + " Node, endElement " + element.getQName());
325
boolean allDone = element.equals(getXMLRootTag());
326         if (allDone) {
327             postParsing();
328             if (getParentNode()!=null && getDescriptor()!=null) {
329                 ((DeploymentDescriptorNode) getParentNode()).addNodeDescriptor(this);
330             }
331         }
332         return allDone;
333     }
334     
335     /**
336      * notification of the end of XML parsing for this node
337      */

338     public void postParsing() {
339     }
340     
341     /**
342      * @return true if the element tag can be handled by any registered sub nodes of the
343      * current XMLNode
344      */

345     public boolean handlesElement(XMLElement element) {
346         // If we have no handlers for sub-nodes, it means we handle this XML tag
347
if (handlers==null)
348             return true;
349         
350         // Let's iterator over all registered handlers to find one which is responsible
351
// for handling the XML tag.
352
for (Enumeration handlersIterator = handlers.keys();handlersIterator.hasMoreElements();) {
353             String JavaDoc subElement = (String JavaDoc) handlersIterator.nextElement();
354             if (element.getQName().equals(subElement)) {
355                 return false;
356             }
357         }
358         return true;
359     }
360     
361     /**
362      * receives notification of the value for a particular tag
363      *
364      * @param element the xml element
365      * @param value it's associated value
366      */

367     public void setElementValue(XMLElement element, String JavaDoc value) {
368     //DOLUtils.getDefaultLogger().finer("SETELEMENTVALUE : " + "in " + getXMLRootTag() + " Node, startElement " + element.getQName());
369
Map dispatchTable = getDispatchTable();
370         
371         if (dispatchTable != null) {
372             if (dispatchTable.containsKey(element.getQName())) {
373                 if (dispatchTable.get(element.getQName())==null) {
374                     // we just ignore these values from the DDs
375
if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) {
376                         DOLUtils.getDefaultLogger().finer("Deprecated element " + element.getQName() + " with value " + value + " is ignored");
377                     }
378                     return;
379                 }
380                 try {
381                     Object JavaDoc descriptor = getDescriptor();
382                     if (descriptor!=null) {
383                         setDescriptorInfo(descriptor, (String JavaDoc) dispatchTable.get(element.getQName()), (String JavaDoc) value);
384                     } else {
385                         DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
386                             new Object JavaDoc[] {element.getQName() , value });
387                     }
388                     
389                     return;
390                 } catch (InvocationTargetException JavaDoc e) {
391                     DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
392                         new Object JavaDoc[] {dispatchTable.get(element.getQName()) , getDescriptor().getClass()});
393             Throwable JavaDoc t = e.getTargetException();
394                     if (t instanceof IllegalArgumentException JavaDoc) {
395                         // We report the error but we continue loading, this will allow the verifier to catch these errors or to register
396
// an error handler for notification
397
DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
398                             new Object JavaDoc[] {element , value});
399                     } else {
400                         t.printStackTrace();
401                         DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
402                                 new Object JavaDoc[] {t.toString(), null});
403                     }
404                 } catch(Throwable JavaDoc t) {
405                     t.printStackTrace();
406                         DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
407                                 new Object JavaDoc[] {t.toString(), null});
408                 }
409             }
410         }
411         if (value.trim().length()!=0) {
412             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
413                 new Object JavaDoc[] {element.getQName() , value });
414         }
415         return;
416     }
417     
418     /**
419      * all sub-implementation of this class can use a dispatch table to map xml element to
420      * method name on the descriptor class for setting the element value.
421      *
422      * @return the map with the element name as a key, the setter method as a value
423      */

424     protected Map getDispatchTable() {
425         // no need to be synchronized for now
426
Map table = new HashMap();
427         table.put(TagNames.DESCRIPTION, "setDescription");
428         return table;
429     }
430     
431     /**
432      * call a setter method on a descriptor with a new value
433      *
434      * @param target the descriptor to use
435      * @param methodName the setter method to invoke
436      * @param value the new value for the field in the descriptor
437      */

438     protected void setDescriptorInfo(Object JavaDoc target, String JavaDoc methodName, String JavaDoc value)
439             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
440
441         if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
442             DOLUtils.getDefaultLogger().fine("in " + target.getClass() + " method " + methodName + " with " + value );
443         }
444     
445     try {
446         Method JavaDoc toInvoke = target.getClass().getMethod(methodName, new Class JavaDoc[] { String JavaDoc.class });
447         toInvoke.invoke(target, new Object JavaDoc[] {value});
448     } catch(NoSuchMethodException JavaDoc e1) {
449             try {
450                 // try with int as a parameter
451
Method JavaDoc toInvoke = target.getClass().getMethod(methodName, new Class JavaDoc[] { int.class });
452                 toInvoke.invoke(target, new Object JavaDoc[] {Integer.valueOf(value)});
453             } catch (NumberFormatException JavaDoc nfe) {
454                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure",
455             new Object JavaDoc []{ getXMLPath().toString() , nfe.toString()});
456         } catch(NoSuchMethodException JavaDoc e2) {
457                 // try with boolean as a parameter
458
Method JavaDoc toInvoke = target.getClass().getMethod(methodName, new Class JavaDoc[] { boolean.class });
459                 toInvoke.invoke(target, new Object JavaDoc[] {Boolean.valueOf(value)});
460             }
461     }
462     }
463     
464     /**
465      * @return the XPath this XML Node is handling
466      */

467     public String JavaDoc getXMLPath() {
468         if (getParentNode()!=null) {
469             return getParentNode().getXMLPath() + "/" + getXMLRootTag().getQName();
470         } else {
471             return getXMLRootTag().getQName();
472         }
473     }
474
475     /**
476      * write the descriptor class to a DOM tree and return it
477      *
478      * @param parent node in the DOM tree
479      * @param the descriptor to write
480      * @return the DOM tree top node
481      */

482     public Node JavaDoc writeDescriptor(Node JavaDoc parent, Descriptor descriptor) {
483        return writeDescriptor(parent, getXMLRootTag().getQName(), descriptor);
484     }
485     
486     /**
487      * write the descriptor class to a DOM tree and return it
488      *
489      * @param parent node in the DOM tree
490      * @param node name for the root element for this DOM tree fragment
491      * @param the descriptor to write
492      * @return the DOM tree top node
493      */

494     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, Descriptor descriptor) {
495         Node JavaDoc node = appendChild(parent, nodeName);
496         return node;
497     }
498     
499         
500     /**
501      * <p>
502      * @return the Document for the given node
503      * </p>
504      */

505     static protected Document JavaDoc getOwnerDocument(Node JavaDoc node) {
506
507         if (node instanceof Document JavaDoc) {
508             return (Document JavaDoc) node;
509         }
510         return node.getOwnerDocument();
511     }
512     
513     /**
514      * <p>
515      * Append a new element child to the current node
516      * </p>
517      * @param parentNode is the parent node for the new child element
518      * @param elementName is new element tag name
519      * @return the newly created child node
520      */

521     public static Element JavaDoc appendChild(Node JavaDoc parent, String JavaDoc elementName) {
522         Element JavaDoc child = getOwnerDocument(parent).createElement(elementName);
523         parent.appendChild(child);
524         return child;
525     }
526
527     /**
528      * <p>
529      * Append a new text child
530      * </p>
531      * @param parent for the new child element
532      * @param elementName is the new element tag name
533      * @param text the text for the new element
534      * @result the newly create child node
535      */

536     public static Node JavaDoc appendTextChild(Node JavaDoc parent, String JavaDoc elementName, String JavaDoc text) {
537         
538         if (text == null || text.length()==0)
539             return null;
540         
541         Node JavaDoc child = appendChild(parent, elementName);
542         child.appendChild(getOwnerDocument(child).createTextNode(text));
543         return child;
544     }
545     
546     /**
547      * <p>
548      * Append a new text child
549      * </p>
550      * @param parent for the new child element
551      * @param elementName is the new element tag name
552      * @param value the int value for the new element
553      * @result the newly create child node
554      */

555     public static Node JavaDoc appendTextChild(Node JavaDoc parent, String JavaDoc elementName, int value) {
556     return appendTextChild(parent, elementName, String.valueOf(value));
557     }
558     
559     /**
560      * <p>
561      * Append a new text child even if text is empty
562      * </p>
563      * @param parent for the new child element
564      * @param elementName is the new element tag name
565      * @param text the text for the new element
566      * @result the newly create child node
567      */

568     public static Node JavaDoc forceAppendTextChild(Node JavaDoc parent, String JavaDoc elementName, String JavaDoc text) {
569                 
570         Node JavaDoc child = appendChild(parent, elementName);
571         if (text != null && text.length()!=0) {
572             child.appendChild(getOwnerDocument(child).createTextNode(text));
573         }
574         return child;
575     }
576     
577     /**
578      * <p>
579      * Append a new attribute to an element
580      * </p>
581      * @param parent for the new child element
582      * @param elementName is the new element tag name
583      * @param text the text for the new element
584      * @result the newly create child node
585      */

586     public static void setAttribute(Element JavaDoc parent, String JavaDoc elementName, String JavaDoc text) {
587         
588         if (text == null || text.length()==0)
589             return;
590         parent.setAttribute(elementName, text);
591     }
592
593     /**
594      * Set a namespace attribute on an element.
595      * @param element on which to set attribute
596      * @param prefix raw prefix (without "xmlns:")
597      * @param namespaceURI namespace URI to which prefix is mapped.
598      */

599     public static void setAttributeNS(Element JavaDoc element, String JavaDoc prefix,
600                                       String JavaDoc namespaceURI) {
601
602         String JavaDoc nsPrefix = prefix.equals("") ? "xmlns" :
603             "xmlns" + QNAME_SEPARATOR + prefix;
604
605         element.setAttributeNS("http://www.w3.org/2000/xmlns/", nsPrefix,
606                                namespaceURI);
607     }
608
609     /**
610      * write a list of env entry descriptors to a DOM Tree
611      *
612      * @param parent node for the DOM tree
613      * @param the iterator over the descriptors to write
614      */

615     protected void writeEnvEntryDescriptors(Node JavaDoc parentNode, Iterator envEntries) {
616         
617         if (envEntries==null || !envEntries.hasNext())
618             return;
619         
620         EnvEntryNode subNode = new EnvEntryNode();
621         for (;envEntries.hasNext();) {
622             EnvironmentProperty envProp = (EnvironmentProperty) envEntries.next();
623             subNode.writeDescriptor(parentNode, TagNames.ENVIRONMENT_PROPERTY, envProp);
624         }
625     }
626     
627     /**
628      * write the ejb references (local or remote) to the DOM tree
629      *
630      * @param parent node for the DOM tree
631      * @param the set of EjbReferenceDescriptor to write
632      */

633     protected void writeEjbReferenceDescriptors(Node JavaDoc parentNode, Iterator refs) {
634         
635         if (refs==null || !refs.hasNext())
636             return;
637         
638         EjbReferenceNode subNode = new EjbReferenceNode();
639         // ejb-ref*
640
Set localRefDescs = new HashSet();
641         for (;refs.hasNext();) {
642             EjbReference ejbRef = (EjbReference) refs.next();
643             if (ejbRef.isLocal()) {
644                 localRefDescs.add(ejbRef);
645             } else {
646                 subNode.writeDescriptor(parentNode, TagNames.EJB_REFERENCE, ejbRef);
647             }
648         }
649         // ejb-local-ref*
650
for (Iterator e=localRefDescs.iterator(); e.hasNext();) {
651             EjbReference ejbRef = (EjbReference) e.next();
652             subNode.writeDescriptor(parentNode, TagNames.EJB_LOCAL_REFERENCE,ejbRef);
653         }
654     }
655     
656     protected void writeServiceReferenceDescriptors(Node JavaDoc parentNode,
657                                                     Iterator refs) {
658         if( ( refs == null ) || !refs.hasNext() ) {
659             return;
660         }
661
662         ServiceReferenceNode serviceRefNode = new ServiceReferenceNode();
663         while(refs.hasNext()) {
664             ServiceReferenceDescriptor next = (ServiceReferenceDescriptor)
665                 refs.next();
666             serviceRefNode.writeDescriptor
667                 (parentNode, WebServicesTagNames.SERVICE_REF, next);
668         }
669     }
670
671     /**
672      * write a list of resource reference descriptors to a DOM Tree
673      *
674      * @param parent node for the DOM tree
675      * @param the iterator over the descriptors to write
676      */

677     protected void writeResourceRefDescriptors(Node JavaDoc parentNode, Iterator resRefs) {
678         
679         if (resRefs==null || !resRefs.hasNext())
680             return;
681         
682         ResourceRefNode subNode = new ResourceRefNode();
683         for (;resRefs.hasNext();) {
684             ResourceReferenceDescriptor aResRef = (ResourceReferenceDescriptor) resRefs.next();
685             subNode.writeDescriptor(parentNode, TagNames.RESOURCE_REFERENCE, aResRef);
686         }
687     }
688     
689     /**
690      * write a list of resource env reference descriptors to a DOM Tree
691      *
692      * @param parent node for the DOM tree
693      * @param the iterator over the descriptors to write
694      */

695     protected void writeResourceEnvRefDescriptors(Node JavaDoc parentNode, Iterator resRefs) {
696         
697         if (resRefs==null || !resRefs.hasNext())
698             return;
699         
700         ResourceEnvRefNode subNode = new ResourceEnvRefNode();
701         for (;resRefs.hasNext();) {
702             JmsDestinationReferenceDescriptor aResRef = (JmsDestinationReferenceDescriptor) resRefs.next();
703             subNode.writeDescriptor(parentNode, TagNames.RESOURCE_ENV_REFERENCE, aResRef);
704         }
705     }
706     
707     /**
708      * write a list of message destination reference descriptors to a DOM Tree
709      *
710      * @param parent node for the DOM tree
711      * @param the iterator over the descriptors to write
712      */

713     protected void writeMessageDestinationRefDescriptors
714         (Node JavaDoc parentNode, Iterator msgDestRefs) {
715         
716         if (msgDestRefs==null || !msgDestRefs.hasNext())
717             return;
718         
719         MessageDestinationRefNode subNode = new MessageDestinationRefNode();
720         for (;msgDestRefs.hasNext();) {
721             MessageDestinationReferenceDescriptor next =
722                 (MessageDestinationReferenceDescriptor) msgDestRefs.next();
723             subNode.writeDescriptor(parentNode,
724                                     TagNames.MESSAGE_DESTINATION_REFERENCE,
725                                     next);
726         }
727     }
728
729     /**
730      * write a list of entity manager reference descriptors to a DOM Tree
731      *
732      * @param parent node for the DOM tree
733      * @param the iterator over the descriptors to write
734      */

735     protected void writeEntityManagerReferenceDescriptors(Node JavaDoc parentNode, Iterator entityMgrRefs) {
736         
737         if (entityMgrRefs==null || !entityMgrRefs.hasNext())
738             return;
739         
740         EntityManagerReferenceNode subNode = new EntityManagerReferenceNode();
741         for (;entityMgrRefs.hasNext();) {
742             EntityManagerReferenceDescriptor aEntityMgrRef = (EntityManagerReferenceDescriptor)entityMgrRefs.next();
743             subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_CONTEXT_REF, aEntityMgrRef);
744         }
745     }
746
747     /**
748      * write a list of entity manager factory reference descriptors to
749      * a DOM Tree
750      *
751      * @param parent node for the DOM tree
752      * @param the iterator over the descriptors to write
753      */

754     protected void writeEntityManagerFactoryReferenceDescriptors(Node JavaDoc parentNode, Iterator entityMgrFactoryRefs) {
755         
756         if (entityMgrFactoryRefs==null || !entityMgrFactoryRefs.hasNext())
757             return;
758         
759         EntityManagerFactoryReferenceNode subNode =
760                 new EntityManagerFactoryReferenceNode();
761         for (;entityMgrFactoryRefs.hasNext();) {
762             EntityManagerFactoryReferenceDescriptor aEntityMgrFactoryRef =
763                 (EntityManagerFactoryReferenceDescriptor)entityMgrFactoryRefs.next();
764             subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_UNIT_REF, aEntityMgrFactoryRef);
765         }
766     }
767     
768     protected void writeAroundInvokeDescriptors
769         (Node JavaDoc parentNode, Iterator aroundInvokeDescs) {
770         if (aroundInvokeDescs == null || !aroundInvokeDescs.hasNext())
771             return;
772
773         AroundInvokeNode subNode = new AroundInvokeNode();
774         for(; aroundInvokeDescs.hasNext();) {
775             LifecycleCallbackDescriptor next =
776                 (LifecycleCallbackDescriptor) aroundInvokeDescs.next();
777             subNode.writeDescriptor(parentNode,
778                                     EjbTagNames.AROUND_INVOKE_METHOD, next);
779         }
780
781     }
782
783     protected void writePostActivateDescriptors
784         (Node JavaDoc parentNode, Iterator postActivateDescs) {
785         if (postActivateDescs == null || !postActivateDescs.hasNext())
786             return;
787
788         LifecycleCallbackNode subNode = new LifecycleCallbackNode();
789         for(; postActivateDescs.hasNext();) {
790             LifecycleCallbackDescriptor next =
791                 (LifecycleCallbackDescriptor) postActivateDescs.next();
792             subNode.writeDescriptor(parentNode,
793                                     EjbTagNames.POST_ACTIVATE_METHOD, next);
794         }
795
796     }
797
798     protected void writePrePassivateDescriptors
799         (Node JavaDoc parentNode, Iterator prePassivateDescs) {
800         if (prePassivateDescs == null || !prePassivateDescs.hasNext())
801             return;
802
803         LifecycleCallbackNode subNode = new LifecycleCallbackNode();
804         for(; prePassivateDescs.hasNext();) {
805             LifecycleCallbackDescriptor next =
806                 (LifecycleCallbackDescriptor) prePassivateDescs.next();
807             subNode.writeDescriptor(parentNode,
808                                     EjbTagNames.PRE_PASSIVATE_METHOD, next);
809         }
810
811     }
812
813     /**
814      * write a list of post-construct descriptors to a DOM Tree
815      *
816      * @param parent node for the DOM tree
817      * @param the iterator over the descriptors to write
818      */

819     protected void writePostConstructDescriptors
820         (Node JavaDoc parentNode, Iterator<LifecycleCallbackDescriptor> postConstructDescs) {
821         if (postConstructDescs == null || !postConstructDescs.hasNext())
822             return;
823
824         LifecycleCallbackNode subNode = new LifecycleCallbackNode();
825         for (; postConstructDescs.hasNext();) {
826             LifecycleCallbackDescriptor next = postConstructDescs.next();
827             subNode.writeDescriptor(parentNode, TagNames.POST_CONSTRUCT,
828                                     next);
829         }
830     }
831     
832     /**
833      * write a list of pre-destroy descriptors to a DOM Tree
834      *
835      * @param parent node for the DOM tree
836      * @param the iterator over the descriptors to write
837      */

838     protected void writePreDestroyDescriptors
839         (Node JavaDoc parentNode, Iterator<LifecycleCallbackDescriptor> preDestroyDescs) {
840         if (preDestroyDescs == null || !preDestroyDescs.hasNext())
841             return;
842
843         LifecycleCallbackNode subNode = new LifecycleCallbackNode();
844         for (; preDestroyDescs.hasNext();) {
845             LifecycleCallbackDescriptor next = preDestroyDescs.next();
846             subNode.writeDescriptor(parentNode, TagNames.PRE_DESTROY,
847                                     next);
848         }
849     }
850     /**
851      * writes iocalized descriptions (if any) to the DOM node
852      */

853     protected void writeLocalizedDescriptions(Node JavaDoc node, Descriptor desc) {
854         
855         LocalizedInfoNode localizedNode = new LocalizedInfoNode();
856         localizedNode.writeLocalizedMap(node, TagNames.DESCRIPTION, desc.getLocalizedDescriptions());
857     }
858     
859     /**
860      * writes jndi environment references group nodes
861      */

862     protected void writeJNDIEnvironmentRefs(Node JavaDoc node, JndiNameEnvironment descriptor) {
863         
864         /* <xsd:element name="env-entry"
865            type="javaee:env-entryType"
866            minOccurs="0" maxOccurs="unbounded"/>
867          */

868         writeEnvEntryDescriptors(node, descriptor.getEnvironmentProperties().iterator());
869
870         /* <xsd:element name="ejb-ref"
871            type="javaee:ejb-refType"
872            minOccurs="0" maxOccurs="unbounded"/>
873         */

874         /* <xsd:element name="ejb-local-ref"
875            type="javaee:ejb-local-refType"
876            minOccurs="0" maxOccurs="unbounded"/>
877          */

878         writeEjbReferenceDescriptors(node,
879                     descriptor.getEjbReferenceDescriptors().iterator());
880     
881
882         /* <xsd:group ref="javaee:service-refGroup"/>
883          */

884         writeServiceReferenceDescriptors(node, descriptor.getServiceReferenceDescriptors().iterator());
885          
886         /* <xsd:element name="resource-ref"
887            type="javaee:resource-refType"
888            minOccurs="0" maxOccurs="unbounded"/>
889          */

890          writeResourceRefDescriptors(node, descriptor.getResourceReferenceDescriptors().iterator());
891          
892         /* <xsd:element name="resource-env-ref"
893                    type="javaee:resource-env-refType"
894                    minOccurs="0" maxOccurs="unbounded"/>
895          */

896          writeResourceEnvRefDescriptors(node, descriptor.getJmsDestinationReferenceDescriptors().iterator());
897          
898         /* <xsd:element name="message-destination-ref"
899                    type="javaee:message-destination-refType"
900                    minOccurs="0" maxOccurs="unbounded"/>
901          */

902          writeMessageDestinationRefDescriptors(node, descriptor.getMessageDestinationReferenceDescriptors().iterator());
903          
904         /* <xsd:element name="persistence-context-ref"
905            type="javaee:persistence-context-refType"
906            minOccurs="0" maxOccurs="unbounded"/>
907          */

908          writeEntityManagerReferenceDescriptors(node, descriptor.getEntityManagerReferenceDescriptors().iterator());
909          
910         /* <xsd:element name="persistence-unit-ref"
911            type="javaee:persistence-unit-refType"
912            minOccurs="0" maxOccurs="unbounded"/>
913          */

914          writeEntityManagerFactoryReferenceDescriptors(node, descriptor.getEntityManagerFactoryReferenceDescriptors().iterator());
915          
916         /* <xsd:element name="post-construct"
917            type="javaee:lifecycle-callbackType"
918            minOccurs="0"
919            maxOccurs="unbounded"/>
920          */

921          writePostConstructDescriptors(node, descriptor.getPostConstructDescriptors().iterator());
922          
923
924         /* <xsd:element name="pre-destroy"
925            type="javaee:lifecycle-callbackType"
926            minOccurs="0"
927            maxOccurs="unbounded"/>
928          */

929          writePreDestroyDescriptors(node, descriptor.getPreDestroyDescriptors().iterator());
930     }
931     
932     /**
933      * write the deployment extension nodes associated with this node
934      *
935      * @param parent node for the DOM tree
936      * @param the deployment extension descriptor
937      *
938      * @note this was an iterim feature of the J2EE 1.4 platform, I leave it
939      * here for now because it may reappear in a later platform release, I put
940      * it private so that it is not misused in the meantime.
941      */

942     private void writeDeploymentExtensionDescriptor(
943         Node JavaDoc parentNode, Descriptor descriptor) {
944                     
945         Iterator itr = descriptor.getDeploymentExtensions();
946         if (itr==null) {
947             return;
948         }
949         DeploymentExtensionNode subNode = new DeploymentExtensionNode();
950         subNode.writeDescriptor(parentNode, itr);
951     }
952     
953     /**
954      * Any node can now declare its own namespace. this apply to DDs only
955      * when dealing with deployment extensions. Write any declared
956      * namespace declaration
957      *
958      * @param node from which this namespace is declared
959      * @param descriptor containing the namespace declaration if any
960      */

961     protected void addNamespaceDeclaration(Element JavaDoc node, Descriptor descriptor) {
962
963         // declare now all remaining namepace...
964
Map prefixMapping = (descriptor != null ) ?
965             descriptor.getPrefixMapping() : null;
966         if (prefixMapping!=null) {
967             for (Iterator itr =prefixMapping.keySet().iterator();itr.hasNext();) {
968                 String JavaDoc prefix = (String JavaDoc) itr.next();
969                 String JavaDoc namespaceURI = (String JavaDoc) prefixMapping.get(prefix);
970                 setAttributeNS(node, prefix, namespaceURI);
971             }
972         }
973     }
974         
975     /**
976      * notify of a new prefix mapping used in this document
977      */

978     public void addPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
979         Object JavaDoc o = getDescriptor();
980         if (o instanceof Descriptor) {
981             Descriptor descriptor = (Descriptor) o;
982             descriptor.addPrefixMapping(prefix, uri);
983         }
984     }
985
986     /**
987      * Resolve a QName prefix to its corresponding Namespace URI by
988      * searching up node chain starting with child.
989      */

990     public String JavaDoc resolvePrefix(XMLElement element, String JavaDoc prefix) {
991         // If prefix is empty string, returned namespace URI
992
// is the default namespace.
993
return element.getPrefixURIMapping(prefix);
994     }
995
996     /**
997      * @return namespace URI prefix from qname, where qname is
998      * an xsd:QName, or the empty string if there is no prefix.
999      *
1000     * QName ::= (Prefix ':')? LocalPart
1001     */

1002    public String JavaDoc getPrefixFromQName(String JavaDoc qname) {
1003        StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR);
1004        return (tokenizer.countTokens() == 2) ?
1005            tokenizer.nextToken() : "";
1006    }
1007
1008    /**
1009     * Return local part from qname, where qname is an xsd:QName.
1010     *
1011     * QName ::= (Prefix ':')? LocalPart
1012     */

1013    public String JavaDoc getLocalPartFromQName(String JavaDoc qname) {
1014        StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR);
1015        String JavaDoc localPart = qname;
1016        if( tokenizer.countTokens() == 2 ) {
1017            // skip namespace prefix.
1018
tokenizer.nextToken();
1019            localPart = tokenizer.nextToken();
1020        }
1021        return localPart;
1022    }
1023
1024    public String JavaDoc composeQNameValue(String JavaDoc prefix, String JavaDoc localPart) {
1025        return ( (prefix != null) && !(prefix.equals("")) ) ?
1026            prefix + QNAME_SEPARATOR + localPart : localPart;
1027    }
1028
1029    public void appendQNameChild(String JavaDoc elementName, Node JavaDoc parent,
1030                                 String JavaDoc namespaceUri, String JavaDoc localPart,
1031                                 String JavaDoc prefix) {
1032        if( prefix == null ) {
1033            // @@@ make configurable??
1034
prefix = elementName + "_ns__";
1035        }
1036
1037        String JavaDoc elementValue = composeQNameValue(prefix, localPart);
1038        Element JavaDoc element = (Element JavaDoc) appendTextChild
1039            (parent, elementName, elementValue);
1040
1041        // Always set prefix mapping on leaf node. If the DOL was
1042
// populated from an existing deployment descriptor it does
1043
// not preserve the original node structure of the XML document,
1044
// so we can't reliably know what level to place mapping.
1045
// Alternatively, if we're writing out a descriptor that was created
1046
// by the deploytool, there is no prefix->namespace information in
1047
// the first place.
1048
setAttributeNS(element, prefix, namespaceUri);
1049        
1050    }
1051}
1052
Popular Tags