KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > runtime > ASCacheMapping


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.tools.verifier.tests.web.runtime;
25
26 import com.sun.enterprise.tools.verifier.tests.web.WebCheck;
27 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
28 import com.sun.enterprise.deployment.WebBundleDescriptor;
29 import com.sun.enterprise.deployment.WebComponentDescriptor;
30 import com.sun.enterprise.deployment.runtime.web.Cache;
31 import com.sun.enterprise.deployment.runtime.web.CacheMapping;
32 import com.sun.enterprise.deployment.runtime.web.CacheHelper;
33 import com.sun.enterprise.tools.verifier.Result;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37
38 //<addition author="irfan@sun.com" [bug/rfe]-id="4711198" >
39
/* Changed the result messages to reflect consistency between the result messages generated
40  * for the EJB test cases for SunONE specific deployment descriptors*/

41 //</addition>
42

43 public class ASCacheMapping extends ASCache implements WebCheck {
44
45
46     boolean oneWarning = false;
47     boolean oneFailed = false;
48     public Result check(WebBundleDescriptor descriptor) {
49
50     Result result = getInitializedResult();
51     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
52
53         boolean notApp = false;
54         //Cache cache = getCache(descriptor);
55
try{
56             Cache cache = (descriptor.getSunDescriptor()).getCache();
57             CacheMapping[] cacheMapp=null;
58             String JavaDoc servletName=null;
59             String JavaDoc urlPattern=null;
60             String JavaDoc timeout=null;
61             String JavaDoc[] httpMethods;
62             //boolean[] keyFields;
63
String JavaDoc cacheHelperRef;
64             if (cache != null ){
65                 cacheMapp=cache.getCacheMapping();
66             }
67             if (cache != null && cacheMapp !=null && cacheMapp.length != 0 ){
68                 for(int rep=0;rep < cacheMapp.length;rep++){
69                     servletName = cacheMapp[rep].getServletName();
70
71                     urlPattern = cacheMapp[rep].getUrlPattern();
72
73                     timeout = cacheMapp[rep].getTimeout();
74                     httpMethods = cacheMapp[rep].getHttpMethod();
75                     cacheHelperRef = cacheMapp[rep].getCacheHelperRef();
76                     if(servletName != null){
77                         if(validServletName(servletName,descriptor)){
78                             addGoodDetails(result, compName);
79                             result.passed(smh.getLocalString
80                                     (getClass().getName() + ".passed",
81                                             "PASSED [AS-WEB cache-mapping] servlet-name [ {0} ] properly defined.",
82                                             new Object JavaDoc[] {servletName}));
83                             }
84                         else{
85                             addErrorDetails(result, compName);
86                             result.failed(smh.getLocalString
87                                     (getClass().getName() + ".failed",
88                                             "FAILED [AS-WEB cache-mapping] servlet-name [ {0} ], does not exist in the web application.",
89                                             new Object JavaDoc[] {servletName}));
90                             oneFailed = true;
91                             }
92                     }
93                     else if(urlPattern !=null){
94                         if(validURL(urlPattern)){
95                             addGoodDetails(result, compName);
96                             result.passed(smh.getLocalString
97                                     (getClass().getName() + ".passed1",
98                                             "PASSED [AS-WEB cache-mapping] url-pattern [ {0} ] properly defined.",
99                                             new Object JavaDoc[] {urlPattern}));
100                             }else{
101                             addErrorDetails(result, compName);
102                             result.failed(smh.getLocalString
103                                     (getClass().getName() + ".failed1",
104                                             "FAILED [AS-WEB cache-mapping] url-pattern [ {0} ], does not exist in the web application.",
105                                             new Object JavaDoc[] {urlPattern}));
106                             oneFailed = true;
107                         }
108                     }
109                     if(cacheHelperRef !=null){
110                         //test cache-helper-ref
111
if(validCacheHelperRef(cacheHelperRef,cache)){
112                             addGoodDetails(result, compName);
113                             result.passed(smh.getLocalString
114                                     (getClass().getName() + ".passed2",
115                                             "PASSED [AS-WEB cache-mapping] cache-helper-ref element [ {0} ] defined properly.",
116                                             new Object JavaDoc[] {cacheHelperRef}));
117                         }
118                         else{
119                             oneFailed = true;
120                             addErrorDetails(result, compName);
121                             result.failed(smh.getLocalString
122                                     (getClass().getName() + ".failed2",
123                                             "FAILED [AS-WEB cache-mapping] cache-helper-ref [ {0} ] not valid, either empty or cache-helper not defined for it.",
124                                             new Object JavaDoc[] {cacheHelperRef}));
125                             }
126                     }else{
127                         if(timeout != null){
128                             int i = rep +1;
129                             String JavaDoc timeoutName = getXPathValue("sun-web-app/cache/cache-mapping["+i+"]/timeout/@name");
130                             if(validTimeout(timeout,timeoutName)){
131                                 addGoodDetails(result, compName);
132                                 result.passed(smh.getLocalString
133                                         (getClass().getName() + ".passed3",
134                                                 "PASSED [AS-WEB cache-mapping] timeout element [ {0} ] properly defined.",
135                                                 new Object JavaDoc[] {new Integer JavaDoc(timeout)}));
136                             }else{
137                                 oneFailed = true;
138                                 addErrorDetails(result, compName);
139                                 result.failed(smh.getLocalString
140                                       (getClass().getName() + ".failed3",
141                                       "FAILED [AS-WEB cache-mapping] timeout element [{0}] must be a Long ( Not less than -1 and not more that MAX_LONG) and its name attribute [{1}] can not be empty/null.",
142                                       new Object JavaDoc[] {timeout,timeoutName}));
143                             }
144                         }
145                         //<addition author="irfan@sun.com" [bug/rfe]-id="4706026" >
146
int j = rep+1;
147                         int count = getCountNodeSet("sun-web-app/cache/cache-mapping["+j+"]/refresh-field");
148                         if(count>0) // refresh field element present
149
{
150                             String JavaDoc cacheMapName = null;
151                             if(cacheMapp[rep].getServletName()!=null)
152                                 cacheMapName = cacheMapp[rep].getServletName();
153                             else
154                                 cacheMapName = cacheMapp[rep].getUrlPattern();
155                             String JavaDoc name = getXPathValue("sun-web-app/cache/cache-mapping["+j+"]/refresh-field/@name");
156                             if(name!=null && name.length()!=0)
157                             {
158                                 addGoodDetails(result, compName);
159                                 result.passed(smh.getLocalString(getClass().getName()+".passed3a",
160                                         "PASSED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] has been furnished",
161                                         new Object JavaDoc[]{cacheMapName,name}));
162                             }else
163                             {
164                                 addErrorDetails(result, compName);
165                                 result.failed(smh.getLocalString(getClass().getName()+".failed3a",
166                                         "FAILED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] cannot be empty/null string",
167                                         new Object JavaDoc[]{cacheMapName,name}));
168                                         oneFailed = true;
169                             }
170                         }
171                         //</addition>
172
if(checkHTTPMethodList(httpMethods,result,compName,descriptor)){
173
174                         }
175                         else{
176                             oneFailed = true;
177                             addErrorDetails(result, compName);
178                             result.failed(smh.getLocalString
179                             (getClass().getName() + ".failed4",
180                             "FAILED [AS-WEB cache-mapping] http-method - List of HTTP methods is not proper, "+
181                             " atleast one of the method name in the list is empty/null "));
182
183                         }
184                        //<addition author="irfan@sun.com" [bug/rfe]-id="4706026" >
185
if ((getCountNodeSet("sun-web-app/cache/cache-mapping["+j+"]/key-field"))>0)
186                             testForKeyFields(cache, j, result, compName);
187                         //</addition>
188
}
189                 }
190             }else{
191                 notApp = true;
192                 addNaDetails(result, compName);
193                 result.notApplicable(smh.getLocalString
194                                  (getClass().getName() + ".notApplicable",
195                                   "NOT APPLICABLE [AS-WEB cache-mapping] element not defined",
196                                   new Object JavaDoc[] {descriptor.getName()}));
197             }
198             if (oneFailed){
199                 result.setStatus(Result.FAILED);
200             }else if(oneWarning){
201                 result.setStatus(Result.WARNING);
202             }
203             else if(notApp){
204                 result.setStatus(Result.NOT_APPLICABLE);
205             }else{
206                 result.setStatus(Result.PASSED);
207             }
208         }catch(Exception JavaDoc ex){
209             oneFailed = true;
210                 addErrorDetails(result, compName);
211                 result.failed(smh.getLocalString
212                 (getClass().getName() + ".failed7",
213                     "FAILED [AS-WEB cache-mapping] could not create the cache object"));
214         }
215     return result;
216     }
217
218     boolean validURL(String JavaDoc URL){
219           boolean valid=false;
220           if (URL != null) {
221               if ((URL.startsWith("/")) ||
222                 ((URL.startsWith("/")) && (URL.endsWith("/*"))) ||
223                 (URL.startsWith("*."))) {
224                             valid = true;
225               }
226           }
227           return valid;
228     }
229
230     boolean validServletName(String JavaDoc servletName, WebBundleDescriptor descriptor){
231         boolean valid=false;
232           if (servletName != null && servletName.length() != 0){
233               Set JavaDoc servlets = descriptor.getServletDescriptors();
234               Iterator JavaDoc itr = servlets.iterator();
235                     // test the servlets in this .war
236
while (itr.hasNext()){
237                         String JavaDoc thisServletName = ((WebComponentDescriptor)itr.next()).getCanonicalName();
238                         if (servletName.equals(thisServletName)){
239                             valid = true;
240                             break;
241                         }
242                     }
243           }
244           return valid;
245     }
246
247     boolean validTimeout(String JavaDoc timeout,String JavaDoc timeoutName){
248           boolean valid=false;
249           if (timeout != null) {
250               try{
251                   long timeoutValue = Long.parseLong(timeout);
252                   if(timeoutValue >= -1 && timeoutValue <= Long.MAX_VALUE){
253                     //if(Integer.parseInt(timeout) >= -1) { //4705932
254
//check the name is non-empty
255
if(timeoutName !=null && timeoutName.length() != 0)
256                                 valid = true;
257                     }
258               } catch(NumberFormatException JavaDoc exception){
259                   //nothing required
260
}
261              
262           } else {//since optional field
263
valid = true;
264           }
265           return valid;
266     }
267
268     boolean checkHTTPMethodList(String JavaDoc[] httpMethods, Result result, ComponentNameConstructor compName,WebBundleDescriptor descriptor ){
269
270           boolean valid=true;
271           if (httpMethods != null) {
272              for(int rep=0;rep < httpMethods.length;rep++){
273                 if(httpMethods[rep]!=null && !(httpMethods[rep].trim().equals("")))
274                  {
275                     if((httpMethods[rep].equalsIgnoreCase("GET")
276                       || httpMethods[rep].equalsIgnoreCase("POST") || httpMethods[rep].equalsIgnoreCase("HEAD")))
277                     {
278                         addGoodDetails(result, compName);
279                         result.passed(smh.getLocalString
280                       (getClass().getName() + ".passed4",
281                        "PASSED [AS-WEB cache-mapping ] http-method [ {0} ] properly defined in the WEB DD.",
282                        new Object JavaDoc[] {httpMethods[rep]}));
283                     }else{
284                         oneWarning = true;
285                         addWarningDetails(result, compName);
286                         result.warning(smh.getLocalString
287                        (getClass().getName() + ".warning",
288                         "WARNING [AS-WEB cache-mapping] http-method name [ {0} ] present, suggested to be GET | POST | HEAD.",
289                         new Object JavaDoc[] {httpMethods[rep]}));
290                     }
291
292                 }
293                 else{
294                     addErrorDetails(result, compName);
295                     result.failed(smh.getLocalString
296                        (getClass().getName() + ".failed5",
297                         "FAILED [AS-WEB cache-mapping] http-method name [ {0} ] is invalid, it should be GET | POST | HEAD.",
298                         new Object JavaDoc[] {httpMethods[rep]}));
299
300                     valid=false;
301                 }
302
303              }
304
305           }
306           return valid;
307     }
308     
309     boolean validCacheHelperRef(String JavaDoc helperRef, Cache cache){
310           boolean valid=false;
311           if (helperRef.length() != 0){
312             CacheHelper[] helperClasses=null;
313             CacheHelper helperClass=null;
314             String JavaDoc name=null;
315             if (cache != null ){
316                helperClasses=cache.getCacheHelper();
317             }
318             if (cache != null && helperClasses !=null){
319                 for(int rep=0;rep < helperClasses.length;rep++){
320                     helperClass=helperClasses[rep];
321                     if(helperClass==null)
322                         continue;
323                     int i = rep +1;
324                     name = getXPathValue("sun-web-app/cache/cache-helper["+i+"]/@name");
325                     if(helperRef.equals(name)){
326                         valid=true;
327                         break;
328                     }
329                 }
330             }
331           }
332           return valid;
333     }
334
335     public void testForKeyFields(Cache cache, int mapCount, Result result, ComponentNameConstructor compName)
336     {
337         int keyCount = getCountNodeSet("sun-web-app/cache/cache-mapping["+mapCount+"]/key-field");
338         String JavaDoc cacheMapName = getXPathValue("sun-web-app/cache/cache-mapping["+mapCount+"]/servlet-name");
339         if (cacheMapName == null)
340         cacheMapName = getXPathValue("sun-web-app/cache/cache-mapping["+mapCount+"]/url-pattern");
341         if (keyCount>0){
342             for (int k=1;k<=keyCount;k++){
343                 String JavaDoc name = getXPathValue("sun-web-app/cache/cache-mapping["+mapCount+"]/key-field["+k+"]/@name");
344                 if(name!=null && name.length()==0)
345                 {
346                     addErrorDetails(result, compName);
347                     result.failed(smh.getLocalString(getClass().getName()+".failed6",
348                             "FAILED [AS-WEB cache-mapping] for {0}, key-field #{1}, name cannot be an empty string",
349                             new Object JavaDoc[]{cacheMapName,new Integer JavaDoc(k)}));
350                     oneFailed = true;
351                 }
352                 else
353                 {
354                     addGoodDetails(result, compName);
355                     result.passed(smh.getLocalString(getClass().getName()+".passed5",
356                             "PASSED [AS-WEB cache-mapping] for {0}, key-field #{1} name value furnished",
357                             new Object JavaDoc[]{cacheMapName,new Integer JavaDoc(k)}));
358                 }
359             }
360         }
361     }
362 }
363
Popular Tags