KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > web > ServletNode


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.web;
25
26 import java.util.Map JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 import com.sun.enterprise.deployment.xml.WebTagNames;
32 import com.sun.enterprise.deployment.node.DisplayableComponentNode;
33 import com.sun.enterprise.deployment.node.DescriptorFactory;
34 import com.sun.enterprise.deployment.node.XMLElement;
35 import com.sun.enterprise.deployment.node.SecurityRoleRefNode;
36 import com.sun.enterprise.deployment.node.RunAsNode;
37
38 import com.sun.enterprise.deployment.Descriptor;
39 import com.sun.enterprise.deployment.RoleReference;
40 import com.sun.enterprise.deployment.RunAsIdentityDescriptor;
41 import com.sun.enterprise.deployment.web.EnvironmentEntry;
42 import com.sun.enterprise.deployment.web.InitializationParameter;
43 import com.sun.enterprise.deployment.WebComponentDescriptor;
44
45 import com.sun.enterprise.deployment.util.DOLUtils;
46
47 /**
48  * This node is responsible for handling the servlet xml sub tree
49  *
50  * @author Jerome Dochez
51  * @version
52  */

53 public class ServletNode extends DisplayableComponentNode {
54
55     private final static XMLElement tag =
56         new XMLElement(WebTagNames.SERVLET);
57     
58     private WebComponentDescriptor descriptor;
59     
60     /** Creates new ServletNode */
61     public ServletNode() {
62         super();
63         registerElementHandler(new XMLElement(WebTagNames.ROLE_REFERENCE), SecurityRoleRefNode.class);
64         registerElementHandler(new XMLElement(WebTagNames.INIT_PARAM), InitParamNode.class);
65         registerElementHandler(new XMLElement(WebTagNames.RUNAS_SPECIFIED_IDENTITY),
66                                                              RunAsNode.class, "setRunAsIdentity");
67         
68     }
69     
70     /**
71      * @return the XML tag associated with this XMLNode
72      */

73     protected XMLElement getXMLRootTag() {
74         return tag;
75     }
76     
77    /**
78     * @return the descriptor instance to associate with this XMLNode
79     */

80     public Object JavaDoc getDescriptor() {
81         
82         if (descriptor==null) {
83             descriptor = (WebComponentDescriptor) DescriptorFactory.getDescriptor(getXMLPath());
84         }
85         return 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 RoleReference) {
96             if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
97                 DOLUtils.getDefaultLogger().fine("Adding security role ref " + newDescriptor);
98             }
99             descriptor.addSecurityRoleReference(
100                         (RoleReference) newDescriptor);
101         } else if (newDescriptor instanceof EnvironmentEntry) {
102             if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
103                 DOLUtils.getDefaultLogger().fine("Adding init-param " + newDescriptor);
104             }
105             descriptor.addInitializationParameter(
106                         (InitializationParameter) newDescriptor);
107         } else super.addDescriptor(newDescriptor);
108     }
109     
110     /**
111      * all sub-implementation of this class can use a dispatch table to map xml element to
112      * method name on the descriptor class for setting the element value.
113      *
114      * @return the map with the element name as a key, the setter method as a value
115      */

116     protected Map JavaDoc getDispatchTable() {
117         // no need to be synchronized for now
118
Map JavaDoc table = super.getDispatchTable();
119         table.put(WebTagNames.NAME, "setName");
120         table.put(WebTagNames.SERVLET_NAME, "setCanonicalName");
121         table.put(WebTagNames.LOAD_ON_STARTUP, "setLoadOnStartUp");
122         return table;
123     }
124
125     /**
126      * receives notiification of the value for a particular tag
127      *
128      * @param element the xml element
129      * @param value it's associated value
130      */

131     public void setElementValue(XMLElement element, String JavaDoc value) {
132         if (WebTagNames.SERVLET_CLASS.equals(element.getQName())) {
133             descriptor.setServlet(true);
134             descriptor.setWebComponentImplementation(value);
135         } else if (WebTagNames.JSP_FILENAME.equals(element.getQName())) {
136             descriptor.setServlet(false);
137             descriptor.setWebComponentImplementation(value);
138         } else {
139             super.setElementValue(element, value);
140         }
141     }
142     
143     /**
144      * write the descriptor class to a DOM tree and return it
145      *
146      * @param parent node in the DOM tree
147      * @param node name for the root element of this xml fragment
148      * @param the descriptor to write
149      * @return the DOM tree top node
150      */

151     public Node JavaDoc writeDescriptor(Node JavaDoc parent, WebComponentDescriptor descriptor) {
152
153         Node JavaDoc myNode = super.writeDescriptor(parent, descriptor);
154         appendTextChild(myNode, WebTagNames.SERVLET_NAME, descriptor.getCanonicalName());
155         if (descriptor.isServlet()) {
156             appendTextChild(myNode, WebTagNames.SERVLET_CLASS, descriptor.getWebComponentImplementation());
157         } else {
158             appendTextChild(myNode, WebTagNames.JSP_FILENAME, descriptor.getWebComponentImplementation());
159         }
160         
161         // init-param*
162
WebBundleNode.addInitParam(myNode, WebTagNames.INIT_PARAM, descriptor.getInitializationParameters());
163         
164         if (descriptor.getLoadOnStartUp()!=-1) {
165             appendTextChild(myNode, WebTagNames.LOAD_ON_STARTUP, String.valueOf(descriptor.getLoadOnStartUp()));
166         }
167         
168         // run-as
169
RunAsIdentityDescriptor runAs = descriptor.getRunAsIdentity();
170         if (runAs!=null) {
171             RunAsNode runAsNode = new RunAsNode();
172             runAsNode.writeDescriptor(myNode, WebTagNames.RUNAS_SPECIFIED_IDENTITY, runAs);
173         }
174         
175         // sercurity-role-ref*
176
Enumeration JavaDoc roleRefs = descriptor.getSecurityRoleReferences();
177         SecurityRoleRefNode roleRefNode = new SecurityRoleRefNode();
178         while (roleRefs.hasMoreElements()) {
179             roleRefNode.writeDescriptor(myNode, WebTagNames.ROLE_REFERENCE,
180                             (RoleReference) roleRefs.nextElement());
181         }
182         
183         return myNode;
184     }
185 }
186
Popular Tags