KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.Node JavaDoc;
29
30 import com.sun.enterprise.deployment.WebResourceCollectionImpl;
31 import com.sun.enterprise.deployment.WebBundleDescriptor;
32 import com.sun.enterprise.deployment.node.DeploymentDescriptorNode;
33 import com.sun.enterprise.deployment.node.DescriptorFactory;
34 import com.sun.enterprise.deployment.node.XMLElement;
35 import com.sun.enterprise.deployment.xml.WebTagNames;
36 import com.sun.enterprise.util.web.URLPattern;
37 import com.sun.enterprise.util.LocalStringManagerImpl;
38
39 /**
40  * This nodes handles the web-collection xml tag element
41  *
42  * @author Jerome Dochez
43  * @version
44  */

45 public class WebResourceCollectionNode extends DeploymentDescriptorNode {
46
47     private WebResourceCollectionImpl descriptor;
48     private static LocalStringManagerImpl localStrings =
49             new LocalStringManagerImpl(ServletMappingNode.class);
50
51     /**
52     * @return the descriptor instance to associate with this XMLNode
53     */

54     public Object JavaDoc getDescriptor() {
55         if (descriptor==null) {
56             descriptor = (WebResourceCollectionImpl) DescriptorFactory.getDescriptor(getXMLPath());
57         }
58         return descriptor;
59     }
60
61     /**
62      * @return the XML tag associated with this XMLNode
63      */

64     protected XMLElement getXMLRootTag() {
65         return new XMLElement(WebTagNames.WEB_RESOURCE_COLLECTION);
66     }
67
68     /**
69      * all sub-implementation of this class can use a dispatch table to map xml element to
70      * method name on the descriptor class for setting the element value.
71      *
72      * @return the map with the element name as a key, the setter method as a value
73      */

74     protected Map JavaDoc getDispatchTable() {
75         Map JavaDoc table = super.getDispatchTable();
76         table.put(WebTagNames.WEB_RESOURCE_NAME, "setName");
77         table.put(WebTagNames.HTTP_METHOD, "addHttpMethod");
78         return table;
79     }
80
81     /**
82      * receives notiification of the value for a particular tag
83      *
84      * @param element the xml element
85      * @param value it's associated value
86      */

87     public void setElementValue(XMLElement element, String JavaDoc value) {
88         if (WebTagNames.URL_PATTERN.equals(element.getQName())) {
89             // If URL Pattern does not start with "/" then
90
// prepend it (for Servlet2.2 Web apps)
91
Object JavaDoc parent = getParentNode().getParentNode().getDescriptor();
92             if (parent instanceof WebBundleDescriptor &&
93                 ((WebBundleDescriptor) parent).getSpecVersion().equals("2.2"))
94             {
95                 if(!value.startsWith("/") && !value.startsWith("*.")) {
96                     value = "/" + value;
97                 }
98             }
99             if (!URLPattern.isValid(value)) {
100                 throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
101                 "enterprise.deployment.invalidurlpattern",
102                 "Invalid URL Pattern: [{0}]",
103                 new Object JavaDoc[] {value}));
104             }
105             descriptor.addUrlPattern(value);
106         } else super.setElementValue(element, value);
107     }
108     
109     /**
110      * write the descriptor class to a DOM tree and return it
111      *
112      * @param parent node in the DOM tree
113      * @param node name for the root element of this xml fragment
114      * @param the descriptor to write
115      * @return the DOM tree top node
116      */

117     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, WebResourceCollectionImpl descriptor) {
118         Node JavaDoc myNode = appendChild(parent, nodeName);
119         appendTextChild(myNode, WebTagNames.WEB_RESOURCE_NAME, descriptor.getName());
120         writeLocalizedDescriptions(myNode, descriptor);
121         
122         // url-pattern*
123
for (Enumeration JavaDoc urlPatterns = descriptor.getUrlPatterns();urlPatterns.hasMoreElements();) {
124             appendTextChild(myNode, WebTagNames.URL_PATTERN, (String JavaDoc) urlPatterns.nextElement());
125         }
126                 
127         // http-method*
128
for (Enumeration JavaDoc httpMethods = descriptor.getHttpMethods();httpMethods.hasMoreElements();) {
129             appendTextChild(myNode, WebTagNames.HTTP_METHOD, (String JavaDoc) httpMethods.nextElement());
130         }
131         return myNode;
132     }
133 }
134
Popular Tags