KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb;
25
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 import com.sun.enterprise.deployment.node.XMLElement;
32 import com.sun.enterprise.deployment.node.DescriptorFactory;
33
34 import com.sun.enterprise.deployment.Descriptor;
35 import com.sun.enterprise.deployment.EjbDescriptor;
36 import com.sun.enterprise.deployment.EjbEntityDescriptor;
37 import com.sun.enterprise.deployment.EjbBundleDescriptor;
38 import com.sun.enterprise.deployment.EjbCMPEntityDescriptor;
39 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
40 import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor;
41 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor;
42 import com.sun.enterprise.deployment.FieldDescriptor;
43 import com.sun.enterprise.deployment.MethodDescriptor;
44 import com.sun.enterprise.deployment.QueryDescriptor;
45 import com.sun.enterprise.deployment.xml.EjbTagNames;
46
47 /**
48  * This class handles all information pertinent to CMP and BMP entity beans
49  *
50  * @author Jerome Dochez
51  * @version
52  */

53 public class EjbEntityNode extends InterfaceBasedEjbNode {
54     
55     private EjbEntityDescriptor descriptor;
56     
57     /** Creates new EjbSessionNode */
58     public EjbEntityNode() {
59         super();
60         registerElementHandler(new XMLElement(EjbTagNames.CMP_FIELD), CmpFieldNode.class);
61         registerElementHandler(new XMLElement(EjbTagNames.QUERY), QueryNode.class);
62     }
63     
64    /**
65     * @return the descriptor instance to associate with this XMLNode
66     */

67     public EjbDescriptor getEjbDescriptor() {
68         
69         if (descriptor==null) {
70             descriptor = (EjbEntityDescriptor) DescriptorFactory.getDescriptor(getXMLPath());
71             descriptor.setEjbBundleDescriptor((EjbBundleDescriptor) getParentNode().getDescriptor());
72         }
73         return descriptor;
74     }
75
76     /**
77      * @return an instance of an EjbCMPEntityDescriptor initialized with all the
78      * fields already parsed.
79      */

80     private EjbCMPEntityDescriptor getCMPEntityDescriptor() {
81         EjbDescriptor current = getEjbDescriptor();
82         if (!(current instanceof EjbCMPEntityDescriptor)) {
83             descriptor = new IASEjbCMPEntityDescriptor(current);
84         }
85         return (EjbCMPEntityDescriptor) descriptor;
86     }
87     
88     /**
89      * Adds a new DOL descriptor instance to the descriptor instance associated with
90      * this XMLNode
91      *
92      * @param descriptor the new descriptor
93      */

94     public void addDescriptor(Object JavaDoc newDescriptor) {
95         if (newDescriptor instanceof FieldDescriptor) {
96            getCMPEntityDescriptor().getPersistenceDescriptor().addCMPField((FieldDescriptor) newDescriptor);
97         } else if (newDescriptor instanceof QueryDescriptor) {
98             QueryDescriptor newQuery = (QueryDescriptor) newDescriptor;
99            getCMPEntityDescriptor().getPersistenceDescriptor().setQueryFor(
100                         newQuery.getQueryMethodDescriptor(), newQuery);
101         } else {
102             super.addDescriptor(newDescriptor);
103         }
104     }
105     
106     
107     /**
108      * all sub-implementation of this class can use a dispatch table to map xml element to
109      * method name on the descriptor class for setting the element value.
110      *
111      * @return the map with the element name as a key, the setter method as a value
112      */

113     protected Map JavaDoc getDispatchTable() {
114         // no need to be synchronized for now
115
Map JavaDoc table = super.getDispatchTable();
116         table.put(EjbTagNames.PERSISTENCE_TYPE, "setPersistenceType");
117         table.put(EjbTagNames.PRIMARY_KEY_CLASS, "setPrimaryKeyClassName");
118         table.put(EjbTagNames.REENTRANT, "setReentrant");
119         return table;
120     }
121     
122     
123     /**
124      * receives notiification of the value for a particular tag
125      *
126      * @param element the xml element
127      * @param value it's associated value
128      */

129     public void setElementValue(XMLElement element, String JavaDoc value) {
130         if (EjbTagNames.CMP_VERSION.equals(element.getQName())) {
131             if (EjbTagNames.CMP_1_VERSION.equals(value)) {
132                 getCMPEntityDescriptor().setCMPVersion(EjbCMPEntityDescriptor.CMP_1_1);
133             } else if (EjbTagNames.CMP_2_VERSION.equals(value)) {
134                 getCMPEntityDescriptor().setCMPVersion(EjbCMPEntityDescriptor.CMP_2_x);
135             }
136         } else if (EjbTagNames.ABSTRACT_SCHEMA_NAME.equals(element.getQName())) {
137             getCMPEntityDescriptor().setAbstractSchemaName(value);
138         } else if (EjbTagNames.PRIMARY_KEY_FIELD.equals(element.getQName())) {
139             getCMPEntityDescriptor().setPrimaryKeyFieldDesc(new FieldDescriptor(value));
140         } else {
141             super.setElementValue(element, value);
142         }
143     }
144     
145     /**
146      * write the descriptor class to a DOM tree and return it
147      *
148      * @param parent node for the DOM tree
149      * @param node name for the root element of this xml fragment
150      * @param the descriptor to write
151      * @return the DOM tree top node
152      */

153     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, Descriptor descriptor) {
154         if (! (descriptor instanceof EjbEntityDescriptor)) {
155             throw new IllegalArgumentException JavaDoc(getClass() + " cannot handles descriptors of type " + descriptor.getClass());
156         }
157         EjbEntityDescriptor ejbDesc = (EjbEntityDescriptor) descriptor;
158         
159         Node JavaDoc ejbNode = super.writeDescriptor(parent, nodeName, descriptor);
160         writeDisplayableComponentInfo(ejbNode, descriptor);
161         writeCommonHeaderEjbDescriptor(ejbNode, ejbDesc);
162         appendTextChild(ejbNode, EjbTagNames.PERSISTENCE_TYPE, ejbDesc.getPersistenceType());
163         appendTextChild(ejbNode, EjbTagNames.PRIMARY_KEY_CLASS, ejbDesc.getPrimaryKeyClassName());
164         appendTextChild(ejbNode, EjbTagNames.REENTRANT, ejbDesc.getReentrant());
165         
166         // cmp entity beans related tags
167
if (ejbDesc instanceof EjbCMPEntityDescriptor) {
168             EjbCMPEntityDescriptor cmpDesc = (EjbCMPEntityDescriptor) ejbDesc;
169             if (cmpDesc.getCMPVersion()==cmpDesc.CMP_1_1) {
170                 appendTextChild(ejbNode, EjbTagNames.CMP_VERSION, EjbTagNames.CMP_1_VERSION);
171             } else {
172                 appendTextChild(ejbNode, EjbTagNames.CMP_VERSION, EjbTagNames.CMP_2_VERSION);
173             }
174             
175             appendTextChild(ejbNode, EjbTagNames.ABSTRACT_SCHEMA_NAME, cmpDesc.getAbstractSchemaName());
176             // cmp-field*
177
CmpFieldNode cmpNode = new CmpFieldNode();
178             for (Iterator JavaDoc fields = cmpDesc.getPersistenceDescriptor().getCMPFields().iterator();fields.hasNext();) {
179                 FieldDescriptor aField = (FieldDescriptor) fields.next();
180                 cmpNode.writeDescriptor(ejbNode, EjbTagNames.CMP_FIELD, aField);
181             }
182             if ( cmpDesc.getPrimaryKeyFieldDesc()!=null) {
183                 appendTextChild(ejbNode, EjbTagNames.PRIMARY_KEY_FIELD, cmpDesc.getPrimaryKeyFieldDesc().getName());
184             }
185         }
186         
187         // env-entry*
188
writeEnvEntryDescriptors(ejbNode, ejbDesc.getEnvironmentProperties().iterator());
189         
190         // ejb-ref * and ejb-local-ref*
191
writeEjbReferenceDescriptors(ejbNode, ejbDesc.getEjbReferenceDescriptors().iterator());
192
193         // service-ref*
194
writeServiceReferenceDescriptors(ejbNode, ejbDesc.getServiceReferenceDescriptors().iterator());
195         
196         // resource-ref*
197
writeResourceRefDescriptors(ejbNode, ejbDesc.getResourceReferenceDescriptors().iterator());
198         
199         // resource-env-ref*
200
writeResourceEnvRefDescriptors(ejbNode, ejbDesc.getJmsDestinationReferenceDescriptors().iterator());
201         
202         // message-destination-ref*
203
writeMessageDestinationRefDescriptors(ejbNode, ejbDesc.getMessageDestinationReferenceDescriptors().iterator());
204
205         // persistence-context-ref*
206
writeEntityManagerReferenceDescriptors(ejbNode, ejbDesc.getEntityManagerReferenceDescriptors().iterator());
207         
208         // persistence-unit-ref*
209
writeEntityManagerFactoryReferenceDescriptors(ejbNode, ejbDesc.getEntityManagerFactoryReferenceDescriptors().iterator());
210         
211         // post-construct
212
writePostConstructDescriptors(ejbNode, ejbDesc.getPostConstructDescriptors().iterator());
213
214         // pre-destroy
215
writePreDestroyDescriptors(ejbNode, ejbDesc.getPreDestroyDescriptors().iterator());
216
217         // security-role-ref*
218
writeRoleReferenceDescriptors(ejbNode, ejbDesc.getRoleReferences().iterator());
219         
220         // security-identity
221
writeSecurityIdentityDescriptor(ejbNode, ejbDesc);
222
223         // query
224
if (ejbDesc instanceof EjbCMPEntityDescriptor) {
225             EjbCMPEntityDescriptor cmpDesc = (EjbCMPEntityDescriptor) ejbDesc;
226             Set JavaDoc queriedMethods = cmpDesc.getPersistenceDescriptor().getQueriedMethods();
227             if (queriedMethods.size()>0) {
228                 QueryNode queryNode = new QueryNode();
229                 for (Iterator JavaDoc e=queriedMethods.iterator();e.hasNext();) {
230                     queryNode.writeDescriptor(ejbNode, EjbTagNames.QUERY,
231                         cmpDesc.getPersistenceDescriptor().getQueryFor((MethodDescriptor) e.next()));
232                 }
233             }
234         }
235         return ejbNode;
236     }
237 }
238
Popular Tags