KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > runtime > web > WebBundleRuntimeNode


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.runtime.web;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.xml.sax.Attributes JavaDoc;
34
35 import com.sun.enterprise.deployment.Application;
36 import com.sun.enterprise.deployment.Descriptor;
37 import com.sun.enterprise.deployment.Group;
38 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper;
39 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor;
40 import com.sun.enterprise.deployment.node.runtime.*;
41 import com.sun.enterprise.deployment.node.runtime.common.EjbRefNode;
42 import com.sun.enterprise.deployment.node.runtime.common.ResourceEnvRefNode;
43 import com.sun.enterprise.deployment.node.runtime.common.ResourceRefNode;
44 import com.sun.enterprise.deployment.node.runtime.common.SecurityRoleMappingNode;
45 import com.sun.enterprise.deployment.node.ServiceReferenceNode;
46 import com.sun.enterprise.deployment.node.XMLElement;
47 import com.sun.enterprise.deployment.ResourcePrincipal;
48 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
49 import com.sun.enterprise.deployment.Role;
50 import com.sun.enterprise.deployment.runtime.common.DefaultResourcePrincipal;
51 import com.sun.enterprise.deployment.runtime.common.EjbRef;
52 import com.sun.enterprise.deployment.runtime.common.PrincipalNameDescriptor;
53 import com.sun.enterprise.deployment.runtime.common.ResourceEnvRef;
54 import com.sun.enterprise.deployment.runtime.common.ResourceRef;
55 import com.sun.enterprise.deployment.runtime.common.SecurityRoleMapping;
56 import com.sun.enterprise.deployment.runtime.web.Cache;
57 import com.sun.enterprise.deployment.runtime.web.ClassLoader;
58 import com.sun.enterprise.deployment.runtime.web.IdempotentUrlPattern;
59 import com.sun.enterprise.deployment.runtime.web.Servlet;
60 import com.sun.enterprise.deployment.runtime.web.SunWebApp;
61 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
62 import com.sun.enterprise.deployment.types.EjbReference;
63 import com.sun.enterprise.deployment.util.DOLUtils;
64 import com.sun.enterprise.deployment.WebBundleDescriptor;
65 import com.sun.enterprise.deployment.WebComponentDescriptor;
66 import com.sun.enterprise.deployment.WritableJndiNameEnvironment;
67 import com.sun.enterprise.deployment.xml.ApplicationTagNames;
68 import com.sun.enterprise.deployment.xml.DTDRegistry;
69 import com.sun.enterprise.deployment.xml.RuntimeTagNames;
70 import com.sun.enterprise.deployment.xml.WebServicesTagNames;
71
72
73 /**
74  * This node is responsible for handling all runtime information for
75  * web bundle.
76  *
77  * @author Jerome Dochez
78  * @version
79  */

80 public class WebBundleRuntimeNode extends RuntimeBundleNode {
81
82     WebBundleDescriptor descriptor=null;
83         
84     /** Creates new WebBundleRuntimeNode */
85     public WebBundleRuntimeNode(WebBundleDescriptor descriptor) {
86         super(descriptor);
87         this.descriptor = descriptor;
88     getDescriptor();
89     }
90     
91     /** Creates new WebBundleRuntimeNode */
92     public WebBundleRuntimeNode() {
93         super(null);
94     }
95     
96     /**
97      * Initialize the child handlers
98      */

99     protected void Init() {
100         // we do not care about our standard DDS handles
101
handlers = null;
102         
103         registerElementHandler(new XMLElement(RuntimeTagNames.SECURITY_ROLE_MAPPING),
104                                SecurityRoleMappingNode.class);
105     registerElementHandler(new XMLElement(RuntimeTagNames.SERVLET),
106                    com.sun.enterprise.deployment.node.runtime.ServletNode.class);
107     registerElementHandler(new XMLElement(RuntimeTagNames.IDEMPOTENT_URL_PATTERN), IdempotentUrlPatternNode.class, "addIdempotentUrlPattern");
108     registerElementHandler(new XMLElement(RuntimeTagNames.SESSION_CONFIG),
109                    SessionConfigNode.class, "setSessionConfig");
110     registerElementHandler(new XMLElement(RuntimeTagNames.RESOURCE_ENV_REFERENCE),
111                                ResourceEnvRefNode.class);
112         registerElementHandler(new XMLElement(RuntimeTagNames.MESSAGE_DESTINATION_REFERENCE),
113                                MessageDestinationRefNode.class);
114
115         registerElementHandler(new XMLElement(RuntimeTagNames.RESOURCE_REFERENCE),
116                                ResourceRefNode.class);
117         registerElementHandler(new XMLElement(RuntimeTagNames.EJB_REFERENCE),
118                                EjbRefNode.class);
119         
120     registerElementHandler(new XMLElement(RuntimeTagNames.CACHE),
121                                CacheNode.class, "setCache");
122     
123     registerElementHandler(new XMLElement(RuntimeTagNames.CLASS_LOADER),
124                                ClassLoaderNode.class, "setClassLoader");
125     
126         registerElementHandler(new XMLElement(RuntimeTagNames.JSP_CONFIG),
127                                WebPropertyContainerNode.class, "setJspConfig");
128         
129         registerElementHandler(new XMLElement(RuntimeTagNames.LOCALE_CHARSET_INFO),
130                                LocaleCharsetInfoNode.class, "setLocaleCharsetInfo");
131     
132     registerElementHandler(new XMLElement(RuntimeTagNames.PROPERTY),
133                                WebPropertyContainerNode.class, "addWebProperty");
134                    
135         registerElementHandler(new XMLElement(WebServicesTagNames.SERVICE_REF),
136                                ServiceRefNode.class);
137         registerElementHandler(new XMLElement(RuntimeTagNames.MESSAGE_DESTINATION),
138                     MessageDestinationRuntimeNode.class);
139         registerElementHandler(new XMLElement(WebServicesTagNames.WEB_SERVICE),
140                         WebServiceRuntimeNode.class);
141     }
142     
143     /**
144      * @return the XML tag associated with this XMLNode
145      */

146     protected XMLElement getXMLRootTag() {
147         return new XMLElement(RuntimeTagNames.S1AS_WEB_RUNTIME_TAG);
148     }
149     
150     /**
151      * @return the DOCTYPE that should be written to the XML file
152      */

153     public String JavaDoc getDocType() {
154     return DTDRegistry.SUN_WEBAPP_250_DTD_PUBLIC_ID;
155     }
156     
157     /**
158      * @return the SystemID of the XML file
159      */

160     public String JavaDoc getSystemID() {
161     return DTDRegistry.SUN_WEBAPP_250_DTD_SYSTEM_ID;
162     }
163
164     /**
165      * @return NULL for all runtime nodes.
166      */

167     public List JavaDoc<String JavaDoc> getSystemIDs() {
168         return null;
169     }
170     
171    /**
172     * register this node as a root node capable of loading entire DD files
173     *
174     * @param publicIDToDTD is a mapping between xml Public-ID to DTD
175     * @return the doctype tag name
176     */

177    public static String JavaDoc registerBundle(Map JavaDoc publicIDToDTD) {
178        publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_230_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_230_DTD_SYSTEM_ID);
179        publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_231_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_231_DTD_SYSTEM_ID);
180        publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_240_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_240_DTD_SYSTEM_ID);
181        publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_241_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_241_DTD_SYSTEM_ID);
182        publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_250_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_250_DTD_SYSTEM_ID);
183        if (!restrictDTDDeclarations()) {
184           publicIDToDTD.put(DTDRegistry.SUN_WEBAPP_240beta_DTD_PUBLIC_ID, DTDRegistry.SUN_WEBAPP_240beta_DTD_SYSTEM_ID);
185        }
186        
187        return RuntimeTagNames.S1AS_WEB_RUNTIME_TAG;
188    }
189     
190    /**
191     * @return the descriptor instance to associate with this XMLNode
192     */

193     public Object JavaDoc getDescriptor() {
194         
195     return descriptor.getSunDescriptor();
196     }
197     
198        /**
199     * @return the web bundle descriptor instance to associate with this XMLNode
200     */

201     public WebBundleDescriptor getWebBundleDescriptor() {
202     return descriptor;
203     }
204     
205     /**
206      * Adds a new DOL descriptor instance to the descriptor instance associated with
207      * this XMLNode
208      *
209      * @param descriptor the new descriptor
210      */

211     public void addDescriptor(Object JavaDoc newDescriptor) {
212     if (newDescriptor instanceof EjbRef) {
213         EjbRef ejbRef = (EjbRef) newDescriptor;
214         descriptor.getSunDescriptor().addEjbRef(ejbRef);
215         EjbReference ref = descriptor.getEjbReference(ejbRef.getEjbRefName());
216         ref.setJndiName(ejbRef.getJndiName());
217     } else
218     if (newDescriptor instanceof ResourceRef) {
219         ResourceRef resourceRef = (ResourceRef) newDescriptor;
220         descriptor.getSunDescriptor().addResourceRef(resourceRef);
221         ResourceReferenceDescriptor rrd = descriptor.getResourceReferenceByName(resourceRef.getResRefName());
222         rrd.setJndiName(resourceRef.getJndiName());
223         DefaultResourcePrincipal drp = resourceRef.getDefaultResourcePrincipal();
224         if (drp!=null) {
225         ResourcePrincipal rp = new ResourcePrincipal(drp.getName(), drp.getPassword());
226         rrd.setResourcePrincipal(rp);
227         }
228     } else
229     if (newDescriptor instanceof ResourceEnvRef) {
230         ResourceEnvRef resourceEnvRef = (ResourceEnvRef) newDescriptor;
231         descriptor.getSunDescriptor().addResourceEnvRef(resourceEnvRef);
232         JmsDestinationReferenceDescriptor rrd = descriptor.getJmsDestinationReferenceByName(resourceEnvRef.getResourceEnvRefName());
233         rrd.setJndiName(resourceEnvRef.getJndiName());
234     } else
235     if (newDescriptor instanceof WebComponentDescriptor) {
236         WebComponentDescriptor servlet = (WebComponentDescriptor) newDescriptor;
237             // for backward compatibility with s1as schema2beans generated desc
238
Servlet s1descriptor = new Servlet();
239             s1descriptor.setServletName(servlet.getCanonicalName());
240             if (servlet.getRunAsIdentity()!=null) {
241                 s1descriptor.setPrincipalName(servlet.getRunAsIdentity().getPrincipal());
242             }
243         descriptor.getSunDescriptor().addServlet(s1descriptor);
244     } else
245         if (newDescriptor instanceof ServiceReferenceDescriptor) {
246             descriptor.addServiceReferenceDescriptor((ServiceReferenceDescriptor) newDescriptor);
247         } else
248         if (newDescriptor instanceof SecurityRoleMapping) {
249             SecurityRoleMapping srm = (SecurityRoleMapping) newDescriptor;
250             descriptor.getSunDescriptor().addSecurityRoleMapping(srm);
251             // store it in the application using pure DOL descriptors...
252
Application app = descriptor.getApplication();
253             if (app!=null && app.isVirtual()) {
254                 Role role = new Role(srm.getRoleName());
255                 SecurityRoleMapper rm = app.getRoleMapper();
256                 if (rm != null) {
257                     List JavaDoc<PrincipalNameDescriptor> principals = srm.getPrincipalNames();
258                     for (int i = 0; i < principals.size(); i++) {
259                         rm.assignRole(principals.get(i).getPrincipal(), role);
260                     }
261                     List JavaDoc<String JavaDoc> groups = srm.getGroupNames();
262                     for (int i = 0; i < groups.size(); i++) {
263                         rm.assignRole(new Group(groups.get(i)), role);
264                     }
265                 }
266             }
267         }
268     else super.addDescriptor(descriptor);
269     }
270
271     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
272         if (element.getQName().equals(RuntimeTagNames.PARAMETER_ENCODING)) {
273             SunWebApp sunWebApp = (SunWebApp)getDescriptor();
274             sunWebApp.setParameterEncoding(true);
275             for (int i=0; i<attributes.getLength();i++) {
276                 if (RuntimeTagNames.DEFAULT_CHARSET.equals(
277                     attributes.getQName(i))) {
278                     sunWebApp.setAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.DEFAULT_CHARSET, attributes.getValue(i));
279                 }
280                 if (RuntimeTagNames.FORM_HINT_FIELD.equals(
281                     attributes.getQName(i))) {
282                     sunWebApp.setAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.FORM_HINT_FIELD, attributes.getValue(i));
283                 }
284             }
285         } else super.startElement(element, attributes);
286     }
287
288     /**
289      * parsed an attribute of an element
290      *
291      * @param the element name
292      * @param the attribute name
293      * @param the attribute value
294      * @return true if the attribute was processed
295      */

296     protected boolean setAttributeValue(XMLElement elementName,
297         XMLElement attributeName, String JavaDoc value) {
298         SunWebApp sunWebApp = (SunWebApp)getDescriptor();
299         if (attributeName.getQName().equals(RuntimeTagNames.ERROR_URL)) {
300             sunWebApp.setAttributeValue(sunWebApp.ERROR_URL, value);
301             return true;
302         }
303         if (attributeName.getQName().equals(RuntimeTagNames.HTTPSERVLET_SECURITY_PROVIDER)) {
304             sunWebApp.setAttributeValue(sunWebApp.HTTPSERVLET_SECURITY_PROVIDER, value);
305             return true;
306         }
307
308         return false;
309     }
310
311
312     /**
313      * receives notification of the value for a particular tag
314      *
315      * @param element the xml element
316      * @param value it's associated value
317      */

318     public void setElementValue(XMLElement element, String JavaDoc value) {
319         if (element.getQName().equals(RuntimeTagNames.CONTEXT_ROOT)) {
320             // only set the context root for standalone war;
321
// for embedded war, the context root will be set
322
// using the value in application.xml
323
Application app = descriptor.getApplication();
324             if ( (app == null) || (app!=null && app.isVirtual()) ) {
325                 descriptor.setContextRoot(value);
326             }
327         } else
328     super.setElementValue(element, value);
329     }
330
331     /**
332      * write the descriptor class to a DOM tree and return it
333      *
334      * @param parent node for the DOM tree
335      * @param the descriptor to write
336      * @return the DOM tree top node
337      */

338     public Node JavaDoc writeDescriptor(Node JavaDoc parent, Descriptor descriptor) {
339         if (! (descriptor instanceof WebBundleDescriptor)) {
340             throw new IllegalArgumentException JavaDoc(getClass() + " cannot handles descriptors of type " + descriptor.getClass());
341         }
342         WebBundleDescriptor bundleDescriptor = (WebBundleDescriptor) descriptor;
343         Element JavaDoc web = (Element JavaDoc)super.writeDescriptor(parent, descriptor);
344     
345     SunWebApp sunWebApp = bundleDescriptor.getSunDescriptor();
346         
347         // context-root?
348
appendTextChild(web, RuntimeTagNames.CONTEXT_ROOT, bundleDescriptor.getContextRoot());
349         
350     // security-role-mapping
351
SecurityRoleMapping[] roleMappings = sunWebApp.getSecurityRoleMapping();
352     if (roleMappings!=null && roleMappings.length>0) {
353         SecurityRoleMappingNode srmn = new SecurityRoleMappingNode();
354         for (int i=0;i<roleMappings.length;i++) {
355         srmn.writeDescriptor(web, RuntimeTagNames.SECURITY_ROLE_MAPPING, roleMappings[i]);
356         }
357     }
358     
359     // servlet
360
Set JavaDoc servlets = bundleDescriptor.getServletDescriptors();
361     if (servlets!=null) {
362         com.sun.enterprise.deployment.node.runtime.ServletNode node =
363                 new com.sun.enterprise.deployment.node.runtime.ServletNode();
364         for (Iterator JavaDoc itr=servlets.iterator();itr.hasNext();) {
365                 WebComponentDescriptor servlet = (WebComponentDescriptor) itr.next();
366         node.writeDescriptor(web, RuntimeTagNames.SERVLET, servlet);
367         }
368     }
369     
370         // idempotent-url-pattern
371
IdempotentUrlPattern[] patterns = sunWebApp.getIdempotentUrlPatterns();
372         if (patterns != null && patterns.length > 0) {
373             IdempotentUrlPatternNode node = new IdempotentUrlPatternNode();
374             for (int i = 0;i < patterns.length; i++) {
375                 node.writeDescriptor(web, RuntimeTagNames.IDEMPOTENT_URL_PATTERN, patterns[i]);
376             }
377         }
378
379     // session-config?
380
if (sunWebApp.getSessionConfig()!=null) {
381         SessionConfigNode scn = new SessionConfigNode();
382         scn.writeDescriptor(web, RuntimeTagNames.SESSION_CONFIG, sunWebApp.getSessionConfig());
383     }
384         
385     // ejb-ref*
386
EjbRef[] ejbRefs = sunWebApp.getEjbRef();
387     if (ejbRefs!=null && ejbRefs.length>0) {
388         EjbRefNode node = new EjbRefNode();
389         for (int i=0;i<ejbRefs.length;i++) {
390         node.writeDescriptor(web, RuntimeTagNames.EJB_REF, ejbRefs[i]);
391         }
392     }
393     
394     // resource-ref*
395
ResourceRef[] resourceRefs = sunWebApp.getResourceRef();
396     if (resourceRefs!=null && resourceRefs.length>0) {
397         ResourceRefNode node = new ResourceRefNode();
398         for (int i=0;i<resourceRefs.length;i++) {
399         node.writeDescriptor(web, RuntimeTagNames.RESOURCE_REF, resourceRefs[i]);
400         }
401     }
402         
403     // resource-env-ref*
404
ResourceEnvRef[] resourceEnvRefs = sunWebApp.getResourceEnvRef();
405     if (resourceEnvRefs!=null && resourceEnvRefs.length>0) {
406         ResourceEnvRefNode node = new ResourceEnvRefNode();
407         for (int i=0;i<resourceEnvRefs.length;i++) {
408         node.writeDescriptor(web, RuntimeTagNames.RESOURCE_ENV_REF, resourceEnvRefs[i]);
409         }
410     }
411         
412     // service-ref*
413
if (bundleDescriptor.hasServiceReferenceDescriptors()) {
414         ServiceRefNode serviceNode = new ServiceRefNode();
415         for (Iterator JavaDoc serviceItr=bundleDescriptor.getServiceReferenceDescriptors().iterator();
416                 serviceItr.hasNext();) {
417         ServiceReferenceDescriptor next = (ServiceReferenceDescriptor) serviceItr.next();
418         serviceNode.writeDescriptor(web, WebServicesTagNames.SERVICE_REF, next);
419         }
420     }
421     
422     // cache?
423
Cache cache = sunWebApp.getCache();
424     if (cache!=null) {
425         CacheNode cn = new CacheNode();
426         cn.writeDescriptor(web, RuntimeTagNames.CACHE, cache);
427     }
428     
429     // class-loader?
430
ClassLoader JavaDoc classLoader = sunWebApp.getClassLoader();
431         if (classLoader!=null) {
432             ClassLoaderNode cln = new ClassLoaderNode();
433             cln.writeDescriptor(web, RuntimeTagNames.CLASS_LOADER, classLoader);
434         }
435
436     // jsp-config?
437
if (sunWebApp.getJspConfig()!=null) {
438         WebPropertyNode propertyNode = new WebPropertyNode();
439         Node JavaDoc jspConfig = appendChild(web, RuntimeTagNames.JSP_CONFIG);
440         propertyNode.writeDescriptor(jspConfig, RuntimeTagNames.PROPERTY, sunWebApp.getJspConfig().getWebProperty());
441     }
442     
443     // locale-charset-info?
444
if (sunWebApp.getLocaleCharsetInfo()!=null) {
445         LocaleCharsetInfoNode localeNode = new LocaleCharsetInfoNode();
446         localeNode.writeDescriptor(web, RuntimeTagNames.LOCALE_CHARSET_INFO,
447             sunWebApp.getLocaleCharsetInfo());
448     }
449     
450         // parameter-encoding?
451
if (sunWebApp.isParameterEncoding()) {
452             Element JavaDoc parameter = (Element JavaDoc) appendChild(web, RuntimeTagNames.PARAMETER_ENCODING);
453
454             if (sunWebApp.getAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.FORM_HINT_FIELD)!=null) {
455                 setAttribute(parameter, RuntimeTagNames.FORM_HINT_FIELD,
456                 (String JavaDoc) sunWebApp.getAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.FORM_HINT_FIELD));
457             }
458
459             if (sunWebApp.getAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.DEFAULT_CHARSET)!=null) {
460                 setAttribute(parameter, RuntimeTagNames.DEFAULT_CHARSET,
461                 (String JavaDoc) sunWebApp.getAttributeValue(SunWebApp.PARAMETER_ENCODING, SunWebApp.DEFAULT_CHARSET));
462             }
463         }
464
465     // property*
466
if (sunWebApp.getWebProperty()!=null) {
467         WebPropertyNode props = new WebPropertyNode();
468         props.writeDescriptor(web, RuntimeTagNames.PROPERTY, sunWebApp.getWebProperty());
469     }
470         
471     // message-destination*
472
RuntimeDescriptorNode.writeMessageDestinationInfo(web, bundleDescriptor);
473
474     // webservice-description*
475
WebServiceRuntimeNode webServiceNode = new WebServiceRuntimeNode();
476         webServiceNode.writeWebServiceRuntimeInfo(web, bundleDescriptor);
477
478         // error-url
479
if (sunWebApp.getAttributeValue(sunWebApp.ERROR_URL) != null) {
480             setAttribute(web, RuntimeTagNames.ERROR_URL, sunWebApp.getAttributeValue(sunWebApp.ERROR_URL));
481         }
482
483         // httpservlet-security-provider
484
if (sunWebApp.getAttributeValue(sunWebApp.HTTPSERVLET_SECURITY_PROVIDER) != null) {
485             setAttribute(web, RuntimeTagNames.HTTPSERVLET_SECURITY_PROVIDER,
486                          sunWebApp.getAttributeValue(sunWebApp.HTTPSERVLET_SECURITY_PROVIDER));
487         }
488
489         return web;
490     }
491 }
492
Popular Tags