KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > ejb > EjbRelationshipRoleNode


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 /*
25  * EjbRelationshipRoleNode.java
26  *
27  * Created on February 1, 2002, 3:22 PM
28  */

29
30 package com.sun.enterprise.deployment.node.ejb;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 import com.sun.enterprise.deployment.node.XMLElement;
37 import com.sun.enterprise.deployment.node.XMLNode;
38 import com.sun.enterprise.deployment.node.DeploymentDescriptorNode;
39 import com.sun.enterprise.deployment.node.LocalizedInfoNode;
40 import com.sun.enterprise.deployment.node.ConfigurableNode;
41 import com.sun.enterprise.deployment.node.DescriptorFactory;
42
43 import com.sun.enterprise.deployment.Descriptor;
44 import com.sun.enterprise.deployment.RelationRoleDescriptor;
45 import com.sun.enterprise.deployment.EjbBundleDescriptor;
46 import com.sun.enterprise.deployment.EjbCMPEntityDescriptor;
47 import com.sun.enterprise.deployment.RelationshipDescriptor;
48 import com.sun.enterprise.deployment.xml.EjbTagNames;
49
50 /**
51  * This class is responsible for handling the ejb-relationship-role xml elements
52  *
53  * @author Jerome Dochez
54  * @version
55  */

56 public class EjbRelationshipRoleNode extends DeploymentDescriptorNode {
57
58     RelationRoleDescriptor descriptor;
59
60     public EjbRelationshipRoleNode() {
61        super();
62        registerElementHandler(new XMLElement(EjbTagNames.DESCRIPTION), LocalizedInfoNode.class);
63     }
64     
65     /**
66      * @return the handler registered for the subtag element of the curent XMLNode
67      */

68     public XMLNode getHandlerFor(XMLElement element) {
69         if (EjbTagNames.RELATIONSHIP_ROLE_SOURCE.equals(element.getQName())) {
70             Map JavaDoc dispatchTable = new HashMap JavaDoc();
71             dispatchTable.put(EjbTagNames.DESCRIPTION, "setRoleSourceDescription");
72             ConfigurableNode newNode = new ConfigurableNode(getDescriptor(), dispatchTable, element);
73             return newNode;
74         } if (EjbTagNames.CMR_FIELD.equals(element.getQName())) {
75             Map JavaDoc dispatchTable = new HashMap JavaDoc();
76             dispatchTable.put(EjbTagNames.DESCRIPTION, "setCMRFieldDescription");
77             ConfigurableNode newNode = new ConfigurableNode(getDescriptor(), dispatchTable, element);
78             return newNode;
79         } else {
80             return super.getHandlerFor(element);
81         }
82     }
83     
84     /**
85      * @return true if the element tag can be handled by any registered sub nodes of the
86      * current XMLNode
87      */

88     public boolean handlesElement(XMLElement element) {
89         if (EjbTagNames.RELATIONSHIP_ROLE_SOURCE.equals(element.getQName())) {
90             return true;
91         }
92         if (EjbTagNames.CMR_FIELD.equals(element.getQName())) {
93             return true;
94         }
95         return super.handlesElement(element);
96     }
97     
98    /**
99     * @return the descriptor instance to associate with this XMLNode
100     */

101     public Object JavaDoc getDescriptor() {
102         if (descriptor==null) {
103             descriptor = (RelationRoleDescriptor) DescriptorFactory.getDescriptor(getXMLPath());
104         }
105         return descriptor;
106     }
107     
108     /**
109      * all sub-implementation of this class can use a dispatch table to map xml element to
110      * method name on the descriptor class for setting the element value.
111      *
112      * @return the map with the element name as a key, the setter method as a value
113      */

114     protected Map JavaDoc getDispatchTable() {
115         // no need to be synchronized for now
116
Map JavaDoc table = super.getDispatchTable();
117         table.put(EjbTagNames.EJB_RELATIONSHIP_ROLE_NAME, "setName");
118         table.put(EjbTagNames.CMR_FIELD_NAME, "setCMRField");
119         table.put(EjbTagNames.CMR_FIELD_TYPE, "setCMRFieldType");
120         return table;
121     }
122     
123     /**
124      * receives notification of the end of an XML element by the Parser
125      *
126      * @param element the xml tag identification
127      * @return true if this node is done processing the XML sub tree
128      */

129     public boolean endElement(XMLElement element) {
130         if (EjbTagNames.CASCADE_DELETE.equals(element.getQName())) {
131                 descriptor.setCascadeDelete(true);
132         }
133         return super.endElement(element);
134     }
135     
136     /**
137      * receives notiification of the value for a particular tag
138      *
139      * @param element the xml element
140      * @param value it's associated value
141      */

142     public void setElementValue(XMLElement element, String JavaDoc value) {
143         if (EjbTagNames.MULTIPLICITY.equals(element.getQName())) {
144             if ( value.equals("Many") )
145                 descriptor.setIsMany(true);
146             else if ( value.equals("One") )
147                 descriptor.setIsMany(false);
148             else if ( value.equals("many") ) // for backward compat with 1.3 FCS
149
descriptor.setIsMany(true);
150             else if ( value.equals("one") ) // for backward compat with 1.3 FCS
151
descriptor.setIsMany(false);
152             else
153                 throw new IllegalArgumentException JavaDoc("Error in value of multiplicity element in EJB deployment descriptor XML: the value must be One or Many");
154         } else if (EjbTagNames.EJB_NAME.equals(element.getQName())) {
155             // let's get our bunlde descriptor...
156
EjbBundleDescriptor bundleDesc = getEjbBundleDescriptor();
157                 EjbCMPEntityDescriptor desc = (EjbCMPEntityDescriptor)bundleDesc.getEjbByName(value);
158                 if (desc!=null){
159                     descriptor.setPersistenceDescriptor(desc.getPersistenceDescriptor());
160                 } else {
161                     throw new IllegalArgumentException JavaDoc("Cannot find ejb " + value + " in bundle for relationship " + descriptor.getName());
162                 }
163         } else super.setElementValue(element, value);
164     }
165     
166     private EjbBundleDescriptor getEjbBundleDescriptor() {
167         XMLNode parent = getParentNode();
168         Object JavaDoc parentDesc = parent.getDescriptor();
169         while (parent!=null && !(parentDesc instanceof EjbBundleDescriptor)) {
170             parent = parent.getParentNode();
171             if (parent !=null)
172                 parentDesc = parent.getDescriptor();
173         }
174         if (parent!=null) {
175             return (EjbBundleDescriptor) parentDesc;
176         } else {
177             throw new IllegalArgumentException JavaDoc("Cannot find bundle descriptor");
178         }
179     }
180     
181     /**
182      * write the descriptor class to a DOM tree and return it
183      *
184      * @param parent node in the DOM tree
185      * @param node name for the root element of this xml fragment
186      * @param the descriptor to write
187      * @return the DOM tree top node
188      */

189     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, RelationRoleDescriptor descriptor) {
190         Node JavaDoc roleNode = super.writeDescriptor(parent, nodeName, descriptor);
191         LocalizedInfoNode localizedNode = new LocalizedInfoNode();
192         localizedNode.writeLocalizedMap(roleNode, EjbTagNames.DESCRIPTION, descriptor.getLocalizedDescriptions());
193         if (descriptor.getRelationRoleName() != null) {
194             appendTextChild(roleNode, EjbTagNames.EJB_RELATIONSHIP_ROLE_NAME,
195                 descriptor.getRelationRoleName());
196         }
197         
198         // multiplicity
199
if (descriptor.getIsMany()) {
200             appendTextChild(roleNode, EjbTagNames.MULTIPLICITY, "Many");
201         } else {
202             appendTextChild(roleNode, EjbTagNames.MULTIPLICITY, "One");
203         }
204         
205         // cascade-delete
206
if (descriptor.getCascadeDelete()) {
207             appendChild(roleNode, EjbTagNames.CASCADE_DELETE);
208         }
209         
210         Node JavaDoc roleSourceNode = appendChild(roleNode, EjbTagNames.RELATIONSHIP_ROLE_SOURCE);
211         appendTextChild(roleSourceNode, EjbTagNames.DESCRIPTION, descriptor.getRoleSourceDescription());
212         appendTextChild(roleSourceNode, EjbTagNames.EJB_NAME, descriptor.getOwner().getName());
213         
214     // cmr-field
215
if (descriptor.getCMRField() != null ) {
216             Node JavaDoc cmrFieldNode = appendChild(roleNode, EjbTagNames.CMR_FIELD);
217             
218         // description
219
appendTextChild(cmrFieldNode, EjbTagNames.DESCRIPTION,
220                                         descriptor.getCMRFieldDescription());
221         // cmr-field-name
222
appendTextChild(cmrFieldNode, EjbTagNames.CMR_FIELD_NAME,
223                                         descriptor.getCMRField());
224         // cmr-field-type
225
appendTextChild(cmrFieldNode, EjbTagNames.CMR_FIELD_TYPE,
226                                         descriptor.getCMRFieldType());
227     }
228         return roleNode;
229     }
230 }
231
Popular Tags