KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > runtime > application > ApplicationRuntimeNode


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 package com.sun.enterprise.deployment.node.runtime.application;
26
27 import com.sun.enterprise.deployment.Application;
28 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
29 import com.sun.enterprise.deployment.Descriptor;
30 import com.sun.enterprise.deployment.Group;
31 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper;
32 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile;
33 import com.sun.enterprise.deployment.node.runtime.common.SecurityRoleMappingNode;
34 import com.sun.enterprise.deployment.node.runtime.RuntimeBundleNode;
35 import com.sun.enterprise.deployment.node.XMLElement;
36 import com.sun.enterprise.deployment.node.XMLNode;
37 import com.sun.enterprise.deployment.Role;
38 import com.sun.enterprise.deployment.runtime.common.PrincipalNameDescriptor;
39 import com.sun.enterprise.deployment.runtime.common.SecurityRoleMapping;
40 import com.sun.enterprise.deployment.util.ModuleDescriptor;
41 import com.sun.enterprise.deployment.xml.DTDRegistry;
42 import com.sun.enterprise.deployment.xml.RuntimeTagNames;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Vector JavaDoc;
47 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50
51 /**
52  * This node handles all runtime-information pertinent to applications
53  * The reading needs to be backward compatible with J2EE 1.2 and 1.3
54  * where all runtime information was saved at the .ear file level in an
55  * unique sun-ri.xml file. In J2EE 1.4, each sub archivist is responsible
56  * for saving its runtime-info at his level.
57  *
58  * @author Jerome Dochez
59  * @version
60  */

61 public class ApplicationRuntimeNode extends RuntimeBundleNode {
62     
63     private Application descriptor=null;
64     private String JavaDoc currentWebUri=null;
65     
66     public ApplicationRuntimeNode(Application descriptor) {
67         super(descriptor);
68     this.descriptor=descriptor;
69     }
70     
71     /**
72      * Initialize the child handlers
73      */

74     protected void Init() {
75         super.Init();
76         registerElementHandler(new XMLElement(RuntimeTagNames.SECURITY_ROLE_MAPPING),
77                                SecurityRoleMappingNode.class);
78     }
79         
80    /**
81     * register this node as a root node capable of loading entire DD files
82     *
83     * @param publicIDToDTD is a mapping between xml Public-ID to DTD
84     * @return the doctype tag name
85     */

86    public static String JavaDoc registerBundle(Map JavaDoc publicIDToDTD) {
87        publicIDToDTD.put(DTDRegistry.SUN_APPLICATION_130_DTD_PUBLIC_ID, DTDRegistry.SUN_APPLICATION_130_DTD_SYSTEM_ID);
88        publicIDToDTD.put(DTDRegistry.SUN_APPLICATION_140_DTD_PUBLIC_ID, DTDRegistry.SUN_APPLICATION_140_DTD_SYSTEM_ID);
89        publicIDToDTD.put(DTDRegistry.SUN_APPLICATION_141_DTD_PUBLIC_ID, DTDRegistry.SUN_APPLICATION_141_DTD_SYSTEM_ID);
90        publicIDToDTD.put(DTDRegistry.SUN_APPLICATION_500_DTD_PUBLIC_ID, DTDRegistry.SUN_APPLICATION_500_DTD_SYSTEM_ID);
91        if (!restrictDTDDeclarations()) {
92            publicIDToDTD.put(DTDRegistry.SUN_APPLICATION_140beta_DTD_PUBLIC_ID, DTDRegistry.SUN_APPLICATION_140beta_DTD_SYSTEM_ID);
93        }
94        return RuntimeTagNames.S1AS_APPLICATION_RUNTIME_TAG;
95    }
96     
97     /**
98      * @return the XML tag associated with this XMLNode
99      */

100     protected XMLElement getXMLRootTag() {
101         return new XMLElement(RuntimeTagNames.S1AS_APPLICATION_RUNTIME_TAG);
102     }
103     
104     /**
105      * @return the DOCTYPE that should be written to the XML file
106      */

107     public String JavaDoc getDocType() {
108     return DTDRegistry.SUN_APPLICATION_500_DTD_PUBLIC_ID;
109     }
110     
111     /**
112      * @return the SystemID of the XML file
113      */

114     public String JavaDoc getSystemID() {
115     return DTDRegistry.SUN_APPLICATION_500_DTD_SYSTEM_ID;
116     }
117
118     /**
119      * @return NULL for all runtime nodes.
120      */

121     public List JavaDoc<String JavaDoc> getSystemIDs() {
122         return null;
123     }
124     
125     /**
126      * all sub-implementation of this class can use a dispatch table to map xml element to
127      * method name on the descriptor class for setting the element value.
128      *
129      * @return the map with the element name as a key, the setter method as a value
130      */

131     protected Map JavaDoc getDispatchTable() {
132         Map JavaDoc table = super.getDispatchTable();
133         table.put(RuntimeTagNames.REALM, "setRealm");
134         return table;
135     }
136     
137     /**
138      * receives notification of the value for a particular tag
139      *
140      * @param element the xml element
141      * @param value it's associated value
142      */

143     public void setElementValue(XMLElement element, String JavaDoc value) {
144     if (element.getQName().equals(RuntimeTagNames.PASS_BY_REFERENCE)) {
145         descriptor.setPassByReference("true".equalsIgnoreCase(value));
146     } else
147     if (element.getQName().equals(RuntimeTagNames.UNIQUE_ID)) {
148         descriptor.setUniqueId(Long.parseLong(value));
149     } else
150     if (element.getQName().equals(RuntimeTagNames.WEB_URI)) {
151         currentWebUri=value;
152     } else
153     if (element.getQName().equals(RuntimeTagNames.CONTEXT_ROOT)) {
154         if (currentWebUri!=null) {
155         ModuleDescriptor md = descriptor.getModuleDescriptorByUri(currentWebUri);
156                 if (md==null) {
157                     throw new RuntimeException JavaDoc("No bundle in application with uri " + currentWebUri);
158                 }
159         currentWebUri=null;
160         if (md.getModuleType().equals(ModuleType.WAR)) {
161             md.setContextRoot(value);
162         } else {
163             throw new RuntimeException JavaDoc(currentWebUri + " uri does not point to a web bundle");
164         }
165         } else {
166         throw new RuntimeException JavaDoc("No uri provided for this context-root " + value);
167         }
168     } else super.setElementValue(element, value);
169     }
170
171     /**
172      * Adds a new DOL descriptor instance to the descriptor instance associated with
173      * this XMLNode
174      *
175      * @param descriptor the new descriptor
176      */

177     public void addDescriptor(Object JavaDoc newDescriptor) {
178         if (newDescriptor instanceof SecurityRoleMapping) {
179             SecurityRoleMapping roleMap = (SecurityRoleMapping)newDescriptor;
180             descriptor.addSecurityRoleMapping(roleMap);
181             if (descriptor!=null && !descriptor.isVirtual()) {
182                 Role role = new Role(roleMap.getRoleName());
183                 SecurityRoleMapper rm = descriptor.getRoleMapper();
184                 if (rm != null) {
185                     List JavaDoc<PrincipalNameDescriptor> principals = roleMap.getPrincipalNames();
186                     for (int i = 0; i < principals.size(); i++) {
187                         rm.assignRole(principals.get(i).getPrincipal(), role);
188                     }
189                     List JavaDoc<String JavaDoc> groups = roleMap.getGroupNames();
190                     for (int i = 0; i < groups.size(); i++) {
191                         rm.assignRole(new Group(groups.get(i)), role);
192                     }
193                 }
194             }
195         }
196     }
197     
198     
199     /**
200      * write the descriptor class to a DOM tree and return it
201      *
202      * @param parent node for the DOM tree
203      * @param the descriptor to write
204      * @return the DOM tree top node
205      */

206     public Node writeDescriptor(Node parent, String JavaDoc nodeName, Descriptor descriptor) {
207         if (! (descriptor instanceof Application)) {
208             throw new IllegalArgumentException JavaDoc(getClass() + " cannot handles descriptors of type " + descriptor.getClass());
209         }
210         Application application = (Application) descriptor;
211         Node appNode = super.writeDescriptor(parent, nodeName, descriptor);
212     
213         // web*
214
for (Iterator JavaDoc modules=application.getModules();modules.hasNext();) {
215         ModuleDescriptor module = (ModuleDescriptor) modules.next();
216         if (module.getModuleType().equals(ModuleType.WAR)) {
217         Node web = appendChild(appNode, RuntimeTagNames.WEB);
218         appendTextChild(web, RuntimeTagNames.WEB_URI, module.getArchiveUri());
219         appendTextChild(web, RuntimeTagNames.CONTEXT_ROOT, module.getContextRoot());
220         }
221     }
222     
223     // pass-by-reference ?
224
if (application.isPassByReferenceDefined()) {
225         appendTextChild(appNode, RuntimeTagNames.PASS_BY_REFERENCE, String.valueOf(application.getPassByReference()));
226     }
227     
228     // unique-id
229
appendTextChild(appNode, RuntimeTagNames.UNIQUE_ID, String.valueOf(application.getUniqueId()));
230     
231         // security-role-mapping*
232
List JavaDoc<SecurityRoleMapping> roleMappings = application.getSecurityRoleMappings();
233         for (int i = 0; i < roleMappings.size(); i++) {
234             SecurityRoleMappingNode srmn = new SecurityRoleMappingNode();
235             srmn.writeDescriptor(appNode, RuntimeTagNames.SECURITY_ROLE_MAPPING, roleMappings.get(i));
236         }
237         
238         // realm?
239
appendTextChild(appNode, RuntimeTagNames.REALM, application.getRealm());
240         
241         return appNode;
242     }
243 }
244
Popular Tags