KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 import com.sun.enterprise.deployment.runtime.RuntimeDescriptor;
32 import com.sun.enterprise.deployment.runtime.web.CacheMapping;
33 import com.sun.enterprise.deployment.runtime.web.ConstraintField;
34 import com.sun.enterprise.deployment.xml.RuntimeTagNames;
35 import com.sun.enterprise.deployment.node.XMLElement;
36
37 /**
38 * node for cache-mapping tag
39 *
40 * @author Jerome Dochez
41 */

42 public class CacheMappingNode extends WebRuntimeNode {
43     
44     public CacheMappingNode() {
45     
46         registerElementHandler(new XMLElement(RuntimeTagNames.CONSTRAINT_FIELD),
47                                ConstraintFieldNode.class, "addNewConstraintField");
48     }
49         
50     /**
51      * all sub-implementation of this class can use a dispatch table to map xml element to
52      * method name on the descriptor class for setting the element value.
53      *
54      * @return the map with the element name as a key, the setter method as a value
55      */

56     protected Map JavaDoc getDispatchTable() {
57     Map JavaDoc dispatchTable = super.getDispatchTable();
58     dispatchTable.put(RuntimeTagNames.SERVLET_NAME, "setServletName");
59     dispatchTable.put(RuntimeTagNames.URL_PATTERN, "setUrlPattern");
60     dispatchTable.put(RuntimeTagNames.CACHE_HELPER_REF, "setCacheHelperRef");
61     dispatchTable.put(RuntimeTagNames.TIMEOUT, "setTimeout");
62     dispatchTable.put(RuntimeTagNames.HTTP_METHOD, "addNewHttpMethod");
63     dispatchTable.put(RuntimeTagNames.DISPATCHER, "addNewDispatcher");
64     return dispatchTable;
65     }
66
67     public void startElement(XMLElement element, Attributes JavaDoc attributes) {
68         CacheMapping descriptor = (CacheMapping) getRuntimeDescriptor();
69     if (descriptor==null) {
70         throw new RuntimeException JavaDoc("Trying to set values on a null descriptor");
71     }
72     if (element.getQName().equals(RuntimeTagNames.TIMEOUT)) {
73             for (int i=0; i<attributes.getLength();i++) {
74                 if (RuntimeTagNames.NAME.equals(attributes.getQName(i))) {
75             descriptor.setAttributeValue(CacheMapping.TIMEOUT, CacheMapping.NAME, attributes.getValue(i));
76                 } else
77                 if (RuntimeTagNames.SCOPE.equals(attributes.getQName(i))) {
78                     int index=0;
79                     while (descriptor.getAttributeValue(CacheMapping.TIMEOUT, index, CacheMapping.NAME)!=null) {
80                         index++;
81                     }
82             descriptor.setAttributeValue(CacheMapping.TIMEOUT, index-1, CacheMapping.SCOPE, attributes.getValue(i));
83             }
84             }
85     } else
86     if (element.getQName().equals(RuntimeTagNames.REFRESH_FIELD)) {
87         descriptor.setRefreshField(true);
88             for (int i=0; i<attributes.getLength();i++) {
89             if (RuntimeTagNames.NAME.equals(attributes.getQName(i))) {
90             descriptor.setAttributeValue(CacheMapping.REFRESH_FIELD, 0, CacheMapping.NAME, attributes.getValue(i));
91             } else
92             if (RuntimeTagNames.SCOPE.equals(attributes.getQName(i))) {
93                     descriptor.setAttributeValue(CacheMapping.REFRESH_FIELD, 0, CacheMapping.SCOPE, attributes.getValue(i));
94                 }
95         }
96     } else
97     if (element.getQName().equals(RuntimeTagNames.KEY_FIELD)) {
98         descriptor.addKeyField(true);
99             for (int i=0; i<attributes.getLength();i++) {
100                 if (RuntimeTagNames.NAME.equals(attributes.getQName(i))) {
101             descriptor.setAttributeValue(CacheMapping.KEY_FIELD, CacheMapping.NAME, attributes.getValue(i));
102             } else
103                 if (RuntimeTagNames.SCOPE.equals(attributes.getQName(i))) {
104                     int index = descriptor.sizeKeyField();
105                 descriptor.setAttributeValue(CacheMapping.KEY_FIELD, index-1, CacheMapping.SCOPE, attributes.getValue(i));
106             }
107             }
108     } else super.startElement(element, attributes);
109     }
110     
111     /**
112      * write the descriptor class to a DOM tree and return it
113      *
114      * @param parent node for the DOM tree
115      * @param node name
116      * @param the descriptor to write
117      * @return the DOM tree top node
118      */

119     public Node JavaDoc writeDescriptor(Node JavaDoc parent, String JavaDoc nodeName, CacheMapping descriptor) {
120     Node JavaDoc cacheMapping = super.writeDescriptor(parent, nodeName, descriptor);
121     if (descriptor.getServletName()!=null) {
122         appendTextChild(cacheMapping, RuntimeTagNames.SERVLET_NAME, descriptor.getServletName());
123     } else {
124         appendTextChild(cacheMapping, RuntimeTagNames.URL_PATTERN, descriptor.getUrlPattern());
125     }
126     
127     // cache-helper-ref
128
appendTextChild(cacheMapping, RuntimeTagNames.CACHE_HELPER_REF,
129             (String JavaDoc) descriptor.getValue(CacheMapping.CACHE_HELPER_REF));
130     
131     //dispatcher*
132
String JavaDoc[] dispatchers = descriptor.getDispatcher();
133     if (dispatchers!=null) {
134         for (int i=0;i<dispatchers.length;i++) {
135         appendTextChild(cacheMapping, RuntimeTagNames.DISPATCHER, dispatchers[i]);
136         }
137     }
138
139     // timeout?
140
Element JavaDoc timeout = (Element JavaDoc) forceAppendTextChild(cacheMapping, RuntimeTagNames.TIMEOUT,
141             (String JavaDoc) descriptor.getValue(CacheMapping.TIMEOUT));
142         // timeout attributes
143
String JavaDoc name = descriptor.getAttributeValue(CacheMapping.TIMEOUT, CacheMapping.NAME);
144         if (name!=null) {
145             setAttribute(timeout, RuntimeTagNames.NAME, name);
146         }
147         String JavaDoc scope = descriptor.getAttributeValue(CacheMapping.TIMEOUT, CacheMapping.SCOPE);
148         if (scope!=null) {
149             setAttribute(timeout, RuntimeTagNames.SCOPE, scope);
150         }
151     
152         //refresh-field?,
153
if (descriptor.isRefreshField()) {
154         Element JavaDoc refreshField = (Element JavaDoc) appendChild(cacheMapping, RuntimeTagNames.REFRESH_FIELD);
155         setAttribute(refreshField, RuntimeTagNames.NAME,
156             (String JavaDoc) descriptor.getAttributeValue(CacheMapping.REFRESH_FIELD, CacheMapping.NAME));
157         setAttribute(refreshField, RuntimeTagNames.SCOPE,
158             (String JavaDoc) descriptor.getAttributeValue(CacheMapping.REFRESH_FIELD, CacheMapping.SCOPE));
159     }
160     
161     //http-method*
162
String JavaDoc[] httpMethods = descriptor.getHttpMethod();
163     if (httpMethods!=null) {
164         for (int i=0;i<httpMethods.length;i++) {
165         appendTextChild(cacheMapping, RuntimeTagNames.HTTP_METHOD, httpMethods[i]);
166         }
167     }
168     
169     //key-field*
170
if (descriptor.sizeKeyField()>0) {
171         for (int i=0;i<descriptor.sizeKeyField();i++) {
172         
173         if (descriptor.isKeyField(i)) {
174             Element JavaDoc keyField = (Element JavaDoc) appendChild(cacheMapping, RuntimeTagNames.KEY_FIELD);
175             setAttribute(keyField, RuntimeTagNames.NAME,
176             (String JavaDoc) descriptor.getAttributeValue(CacheMapping.KEY_FIELD, i, CacheMapping.NAME));
177             setAttribute(keyField, RuntimeTagNames.SCOPE,
178             (String JavaDoc) descriptor.getAttributeValue(CacheMapping.KEY_FIELD, i, CacheMapping.SCOPE));
179         }
180         }
181     }
182     
183     //constraint-field*
184
if (descriptor.sizeConstraintField()>0) {
185             ConstraintField[] constraintFields = descriptor.getConstraintField();
186             ConstraintFieldNode cfn = new ConstraintFieldNode();
187         cfn.writeDescriptor(cacheMapping, RuntimeTagNames.CONSTRAINT_FIELD, constraintFields);
188     }
189     
190     return cacheMapping;
191     }
192 }
193
Popular Tags