KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
27 import javax.persistence.PersistenceContextType;
28 import org.w3c.dom.Node JavaDoc;
29
30 import com.sun.enterprise.deployment.Descriptor;
31 import com.sun.enterprise.deployment.InjectionTarget;
32 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor;
33 import com.sun.enterprise.deployment.xml.EjbTagNames;
34 import com.sun.enterprise.deployment.xml.TagNames;
35
36 /**
37  * This node handles all persistence-context-ref xml tag elements
38  *
39  * @author Shing Wai Chan
40  * @version
41  */

42 public class EntityManagerReferenceNode extends DeploymentDescriptorNode {
43     private static final String JavaDoc TRANSACTION = "Transaction";
44     private static final String JavaDoc EXTENDED = "Extended";
45
46     // Holds property name during name/value processing.
47
private String JavaDoc propertyName = null;
48
49     public EntityManagerReferenceNode() {
50         super();
51         registerElementHandler(new XMLElement(TagNames.INJECTION_TARGET),
52                                 InjectionTargetNode.class, "addInjectionTarget");
53     }
54
55     
56     /**
57      * all sub-implementation of this class can use a dispatch table to map
58      * xml element to method name on the descriptor class for setting
59      * the element value.
60      *
61      * @return the map with the element name as a key, the setter method as
62      * a value
63      */

64     protected Map JavaDoc getDispatchTable() {
65         // no need to be synchronized for now
66
Map JavaDoc table = super.getDispatchTable();
67         table.put(TagNames.PERSISTENCE_CONTEXT_REF_NAME, "setName");
68         table.put(TagNames.PERSISTENCE_UNIT_NAME, "setUnitName");
69         return table;
70     }
71     
72     /**
73      * receives notiification of the value for a particular tag
74      *
75      * @param element the xml element
76      * @param value it's associated value
77      */

78     public void setElementValue(XMLElement element, String JavaDoc value) {
79         if (TagNames.PERSISTENCE_CONTEXT_TYPE.equals(element.getQName())) {
80             EntityManagerReferenceDescriptor entityMgrReferenceDescriptor =
81                 (EntityManagerReferenceDescriptor)getDescriptor();
82             PersistenceContextType contextType = null;
83             if (EXTENDED.equals(value)) {
84                 contextType = PersistenceContextType.EXTENDED;
85             } else if (TRANSACTION.equals(value)) {
86                 contextType = PersistenceContextType.TRANSACTION;
87             } else {
88                 throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
89                 "enterprise.deployment.node.invalidvalue",
90                 "Invalid value for a tag under {0} : {1}",
91                 new Object JavaDoc[] {TagNames.PERSISTENCE_CONTEXT_TYPE, value}));
92             }
93
94             entityMgrReferenceDescriptor.setPersistenceContextType(contextType);
95
96         } else if (TagNames.NAME_VALUE_PAIR_NAME.equals(element.getQName())) {
97             propertyName = value;
98         } else if (TagNames.NAME_VALUE_PAIR_VALUE.equals(element.getQName())) {
99             EntityManagerReferenceDescriptor entityMgrReferenceDescriptor =
100                 (EntityManagerReferenceDescriptor)getDescriptor();
101             entityMgrReferenceDescriptor.addProperty(propertyName, value);
102             propertyName = null;
103         } else {
104             super.setElementValue(element, value);
105         }
106     }
107
108     /**
109      * write the descriptor class to a DOM tree and return it
110      *
111      * @param parent node in the DOM tree
112      * @param node name for the root element of this xml fragment
113      * @param the descriptor to write
114      * @return the DOM tree top node
115      */

116     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, EntityManagerReferenceDescriptor descriptor) {
117         Node JavaDoc entityMgrRefNode = appendChild(parent, nodeName);
118         writeLocalizedDescriptions(entityMgrRefNode, descriptor);
119         
120         appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_REF_NAME, descriptor.getName());
121         appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_UNIT_NAME, descriptor.getUnitName());
122         PersistenceContextType contextType = descriptor.getPersistenceContextType();
123         String JavaDoc contextTypeString = (contextType != null &&
124             PersistenceContextType.EXTENDED.equals(contextType)) ?
125             EXTENDED : TRANSACTION;
126         appendTextChild(entityMgrRefNode, TagNames.PERSISTENCE_CONTEXT_TYPE,
127             contextTypeString);
128
129         for(Map.Entry JavaDoc<String JavaDoc, String JavaDoc> property :
130                 descriptor.getProperties().entrySet()) {
131             Node JavaDoc propertyNode = appendChild(entityMgrRefNode,
132                                             TagNames.PERSISTENCE_PROPERTY);
133             appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_NAME,
134                             property.getKey());
135             appendTextChild(propertyNode, TagNames.NAME_VALUE_PAIR_VALUE,
136                             property.getValue());
137         }
138
139         if( descriptor.isInjectable() ) {
140             InjectionTargetNode ijNode = new InjectionTargetNode();
141             for (InjectionTarget target : descriptor.getInjectionTargets()) {
142                 ijNode.writeDescriptor(entityMgrRefNode, TagNames.INJECTION_TARGET, target);
143             }
144         }
145             
146         return entityMgrRefNode;
147     }
148 }
149
Popular Tags