KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FilterMappingNode.java
26  *
27  * Created on February 26, 2002, 9:21 PM
28  */

29
30 package com.sun.enterprise.deployment.node.web;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 import com.sun.enterprise.deployment.ServletFilterMappingDescriptor;
38 import com.sun.enterprise.deployment.xml.WebTagNames;
39 import com.sun.enterprise.deployment.node.DeploymentDescriptorNode;
40 import com.sun.enterprise.deployment.node.XMLElement;
41 import com.sun.enterprise.deployment.WebBundleDescriptor;
42 import com.sun.enterprise.util.web.URLPattern;
43 import com.sun.enterprise.util.LocalStringManagerImpl;
44
45
46 /**
47  * This node handles all information relative to servlet-mapping xml tag
48  *
49  * @author Jerome Dochez
50  * @version
51  */

52 public class FilterMappingNode extends DeploymentDescriptorNode {
53
54     private ServletFilterMappingDescriptor descriptor;
55     private static LocalStringManagerImpl localStrings =
56             new LocalStringManagerImpl(ServletMappingNode.class);
57     
58    /**
59     * @return the descriptor instance to associate with this XMLNode
60     */

61     public Object JavaDoc getDescriptor() {
62         
63        if (descriptor==null) {
64             descriptor = (ServletFilterMappingDescriptor) super.getDescriptor();
65         }
66         return descriptor;
67     }
68     
69     /**
70      * all sub-implementation of this class can use a dispatch table to map xml element to
71      * method name on the descriptor class for setting the element value.
72      *
73      * @return the map with the element name as a key, the setter method as a value
74      */

75     protected Map JavaDoc getDispatchTable() {
76         Map JavaDoc table = super.getDispatchTable();
77         table.put(WebTagNames.FILTER_NAME, "setName");
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.SERVLET_NAME.equals(element.getQName())) {
89             descriptor.addServletName(value);
90         } else if (WebTagNames.URL_PATTERN.equals(element.getQName())) {
91             // If URL Pattern does not start with "/" then
92
// prepend it (for Servlet2.2 Web apps)
93
Object JavaDoc parent = getParentNode().getDescriptor();
94             if (parent instanceof WebBundleDescriptor &&
95                 ((WebBundleDescriptor) parent).getSpecVersion().equals("2.2"))
96             {
97                 if(!value.startsWith("/") && !value.startsWith("*.")) {
98                     value = "/" + value;
99                 }
100             }
101
102             if (!URLPattern.isValid(value)) {
103                 throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
104                 "enterprise.deployment.invalidurlpattern",
105                 "Invalid URL Pattern: [{0}]",
106                 new Object JavaDoc[] {value}));
107             }
108             descriptor.addURLPattern(value);
109         } else if (WebTagNames.DISPATCHER.equals(element.getQName())) {
110             descriptor.addDispatcher(value);
111         } else super.setElementValue(element, value);
112     }
113     
114     /**
115      * write the descriptor class to a DOM tree and return it
116      *
117      * @param parent node in the DOM tree
118      * @param node name for the root element of this xml fragment
119      * @param the descriptor to write
120      * @return the DOM tree top node
121      */

122     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, ServletFilterMappingDescriptor descriptor) {
123         Node JavaDoc myNode = appendChild(parent, nodeName);
124         appendTextChild(myNode, WebTagNames.FILTER_NAME, descriptor.getName());
125         for (String JavaDoc servletName : descriptor.getServletNames()) {
126             appendTextChild(myNode, WebTagNames.SERVLET_NAME, servletName);
127         }
128
129         for (String JavaDoc urlPattern : descriptor.getURLPatterns()) {
130             appendTextChild(myNode, WebTagNames.URL_PATTERN, urlPattern);
131         }
132
133         Set JavaDoc dispatchers = descriptor.getDispatchers();
134         for (Iterator JavaDoc i = dispatchers.iterator(); i.hasNext();) {
135             String JavaDoc dispatcher = (String JavaDoc) i.next();
136             appendTextChild(myNode, WebTagNames.DISPATCHER, dispatcher);
137         }
138         return myNode;
139     }
140 }
141
Popular Tags