KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > ias > 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.ias;
25
26 import java.util.*;
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.deployment.web.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
32 import com.sun.enterprise.deployment.WebBundleDescriptor;
33 import com.sun.enterprise.tools.verifier.tests.web.*;
34 import com.sun.enterprise.tools.common.dd.webapp.*;
35
36 //<addition author="irfan@sun.com" [bug/rfe]-id="4711198" >
37
/* Changed the result messages to reflect consistency between the result messages generated
38  * for the EJB test cases for SunONE specific deployment descriptors*/

39 //</addition>
40

41 public class ASCacheMapping extends ASCache implements WebCheck {
42
43
44     boolean oneWarning = false;
45     public Result check(WebBundleDescriptor descriptor) {
46
47     Result result = getInitializedResult();
48     WebComponentNameConstructor compName = new WebComponentNameConstructor(descriptor);
49
50         boolean oneFailed = false;
51         boolean notApp = false;
52         Cache cache = getCache(descriptor);
53
54         CacheMapping[] cacheMapp=null;
55
56         String JavaDoc servletName=null;
57         String JavaDoc urlPattern=null;
58         String JavaDoc timeout=null;
59         String JavaDoc[] httpMethods;
60         //boolean[] keyFields;
61
String JavaDoc cacheHelperRef;
62
63
64
65         if (cache != null ){
66           cacheMapp=cache.getCacheMapping();
67
68           }
69                   
70
71          if (cache != null && cacheMapp !=null && cacheMapp.length !=0 ) {
72             for(int rep=0;rep < cacheMapp.length;rep++){
73         servletName = cacheMapp[rep].getServletName();
74                 urlPattern = cacheMapp[rep].getUrlPattern();
75                 timeout = cacheMapp[rep].getTimeout();
76                 httpMethods = cacheMapp[rep].getHttpMethod();
77                 cacheHelperRef = cacheMapp[rep].getCacheHelperRef();
78
79                 if(servletName !=null){
80                       if(validServletName(servletName,descriptor)){
81
82                       result.passed(smh.getLocalString
83                       (getClass().getName() + ".passed",
84                        "PASSED [AS-WEB cache-mapping] servlet-name [ {0} ] properly defined.",
85                        new Object JavaDoc[] {servletName}));
86
87                       }else{
88
89                 result.failed(smh.getLocalString
90                        (getClass().getName() + ".failed",
91                         "FAILED [AS-WEB cache-mapping] servlet-name [ {0} ], does not exist in the web application.",
92                                             new Object JavaDoc[] {servletName}));
93                 oneFailed = true;
94
95                       }
96
97                 }
98                 else if(urlPattern !=null)
99                 {
100                       if(validURL(urlPattern)){
101
102                       result.passed(smh.getLocalString
103                       (getClass().getName() + ".passed1",
104                        "PASSED [AS-WEB cache-mapping] url-pattern [ {0} ] properly defined.",
105                        new Object JavaDoc[] {urlPattern}));
106
107                       }else{
108
109               result.failed(smh.getLocalString
110                        (getClass().getName() + ".failed1",
111                         "FAILED [AS-WEB cache-mapping] url-pattern [ {0} ], does not exist in the web application.",
112                                             new Object JavaDoc[] {urlPattern}));
113               oneFailed = true;
114                       }
115
116                 }
117                
118                 if(cacheHelperRef !=null){
119                     //test cache-helper-ref
120
if(validCacheHelperRef(cacheHelperRef,cache)){
121                         
122                     result.passed(smh.getLocalString
123                       (getClass().getName() + ".passed2",
124                        "PASSED [AS-WEB cache-mapping] cache-helper-ref element [ {0} ] defined properly.",
125                        new Object JavaDoc[] {cacheHelperRef}));
126
127                     }
128                     else{
129                     oneFailed = true;
130                     result.failed(smh.getLocalString
131                                       (getClass().getName() + ".failed2",
132                                       "FAILED [AS-WEB cache-mapping] cache-helper-ref [ {0} ] not valid, either empty or cache-helper not defined for it.",
133                                       new Object JavaDoc[] {cacheHelperRef}));
134
135                     }
136                     
137                 } else {
138                    
139                     if(timeout != null){
140                     String JavaDoc timeoutName=cacheMapp[rep].getAttributeValue("Timeout","Name");
141                     
142                     if(validTimeout(timeout,timeoutName)){
143                     
144             result.passed(smh.getLocalString
145                        (getClass().getName() + ".passed3",
146                         "PASSED [AS-WEB cache-mapping] timeout element [ {0} ] properly defined.",
147                                             new Object JavaDoc[] {new Integer JavaDoc(timeout)}));
148
149                     }
150                     else{
151                     oneFailed = true;
152                     result.failed(smh.getLocalString
153                                       (getClass().getName() + ".failed3",
154                                       "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.",
155                                       new Object JavaDoc[] {timeout,timeoutName}));
156
157
158
159                     }
160                     }
161                     //<addition author="irfan@sun.com" [bug/rfe]-id="4706026" >
162
if(cacheMapp[rep].isRefreshField())
163                     {
164                         String JavaDoc cacheMapName = null;
165                         if(cacheMapp[rep].getServletName()!=null)
166                             cacheMapName = cacheMapp[rep].getServletName();
167                         else
168                             cacheMapName = cacheMapp[rep].getUrlPattern();
169                         String JavaDoc name = cacheMapp[rep].getAttributeValue(cacheMapp[rep].REFRESH_FIELD,"name");
170                         if(name!=null && name.length()==0)
171                         {
172                             result.failed(smh.getLocalString(getClass().getName()+".failed3a",
173                                 "FAILED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] cannot be empty/null string",
174                                 new Object JavaDoc[]{cacheMapName,name}));
175                             oneFailed = true;
176                         }
177                         else
178                         {
179                             result.passed(smh.getLocalString(getClass().getName()+".passed3a",
180                                 "PASSED [AS-WEB cache-mapping] for {0}, refresh-field name [{1}] has been furnished",
181                                 new Object JavaDoc[]{cacheMapName,name}));
182                         }
183                     }
184                     //</addition>
185

186                     if(checkHTTPMethodList(httpMethods,result,compName,descriptor)){
187
188                     }
189                     else{
190                     oneFailed = true;
191                     result.failed(smh.getLocalString
192                         (getClass().getName() + ".failed4",
193                         "FAILED [AS-WEB cache-mapping] http-method - List of HTTP methods is not proper, "+
194                         " atleast one of the method name in the list is empty/null "));
195
196                     }
197
198                 //<addition author="irfan@sun.com" [bug/rfe]-id="4706026" >
199
int numKeyFields = cacheMapp[rep].sizeKeyField();
200                 if(numKeyFields>0)
201                     testKeyFields(cacheMapp[rep],result,oneFailed);
202                 //</addition>
203

204                               
205                 }
206
207             }
208
209         } else {
210             notApp = true;
211         result.notApplicable(smh.getLocalString
212                                  (getClass().getName() + ".notApplicable",
213                                   "NOT APPLICABLE [AS-WEB cache-mapping] element not defined",
214                                   new Object JavaDoc[] {descriptor.getName()}));
215         }
216         if (oneFailed) {
217             result.setStatus(Result.FAILED);
218         }else if(oneWarning) {
219             result.setStatus(Result.WARNING);
220         } else if(notApp) {
221             result.setStatus(Result.NOT_APPLICABLE);
222         }else {
223             result.setStatus(Result.PASSED);
224         }
225     return result;
226     }
227
228
229     boolean validURL(String JavaDoc URL){
230           boolean valid=false;
231           if (URL != null) {
232               if ((URL.startsWith("/")) ||
233                 ((URL.startsWith("/")) && (URL.endsWith("/*"))) ||
234                 (URL.startsWith("*."))) {
235                             valid = true;
236               }
237           }
238           return valid;
239     }
240
241     boolean validServletName(String JavaDoc servletName, WebBundleDescriptor descriptor){
242           boolean valid=false;
243           if (servletName != null && servletName.length() != 0) {
244               Set servlets = descriptor.getServletDescriptors();
245                     Iterator itr = servlets.iterator();
246                     // test the servlets in this .war
247
while (itr.hasNext()) {
248                         ServletDescriptor servlet = (ServletDescriptor) itr.next();
249                         String JavaDoc thisServletName = servlet.getCanonicalName();
250             if (servletName.equals(thisServletName)) {
251                             valid = true;
252                             break;
253                         }
254                     }
255
256           }
257           return valid;
258     }
259
260     boolean validTimeout(String JavaDoc timeout,String JavaDoc timeoutName){
261           boolean valid=false;
262           if (timeout != null) {
263               try{
264                   long timeoutValue = Long.parseLong(timeout);
265                   if(timeoutValue >= -1 && timeoutValue <= Long.MAX_VALUE){
266                     //if(Integer.parseInt(timeout) >= -1) { //4705932
267
//check the name is non-empty
268
if(timeoutName !=null && timeoutName.length() != 0)
269                                 valid = true;
270                     }
271               } catch(NumberFormatException JavaDoc exception){
272                   //nothing required
273
}
274              
275           } else {//since optional field
276
valid = true;
277           }
278           return valid;
279     }
280
281     boolean checkHTTPMethodList(String JavaDoc[] httpMethods, Result result, WebComponentNameConstructor compName,WebBundleDescriptor descriptor ){
282
283           boolean valid=true;
284           if (httpMethods != null) {
285              for(int rep=0;rep < httpMethods.length;rep++){
286                 if(httpMethods[rep]!=null && !(httpMethods[rep].trim().equals("")))
287                  {
288                     if((httpMethods[rep].equalsIgnoreCase("GET")
289                       || httpMethods[rep].equalsIgnoreCase("POST") || httpMethods[rep].equalsIgnoreCase("HEAD")))
290                     {
291
292                       result.passed(smh.getLocalString
293                       (getClass().getName() + ".passed4",
294                        "PASSED [AS-WEB cache-mapping ] http-method [ {0} ] properly defined in the WEB DD.",
295                        new Object JavaDoc[] {httpMethods[rep]}));
296                     }else{
297
298                     oneWarning = true;
299                     result.warning(smh.getLocalString
300                        (getClass().getName() + ".warning",
301                         "WARNING [AS-WEB cache-mapping] http-method name [ {0} ] present, suggested to be GET | POST | HEAD.",
302                         new Object JavaDoc[] {httpMethods[rep]}));
303
304
305
306                     }
307
308                  }
309                 else{
310
311                 result.failed(smh.getLocalString
312                        (getClass().getName() + ".failed5",
313                         "FAILED [AS-WEB cache-mapping] http-method name [ {0} ] is invalid, it should be GET | POST | HEAD.",
314                         new Object JavaDoc[] {httpMethods[rep]}));
315
316                 valid=false;
317                 }
318
319              }
320
321           }
322
323           return valid;
324     }
325     
326     boolean validCacheHelperRef(String JavaDoc helperRef, Cache cache){
327           boolean valid=false;
328           if (helperRef.length() != 0) {
329             
330             CacheHelper[] helperClasses=null;
331             CacheHelper helperClass=null;
332             String JavaDoc name=null;
333         
334             if (cache != null )
335             helperClasses=cache.getCacheHelper();
336           
337             if (cache != null && helperClasses !=null) {
338        
339                 for(int rep=0;rep < helperClasses.length;rep++){
340                     helperClass=helperClasses[rep];
341                     if(helperClass==null)
342                         continue;
343             name = helperClass.getAttributeValue("name");
344                     //System.out.println("checking ref for : " +name);
345
if(helperRef.equals(name)){
346                         //System.out.println("Valid ref found : " +name);
347
valid=true;
348                         break;
349                     }
350                 }
351             }
352
353           }
354           return valid;
355     }
356     
357     //<addition author="irfan@sun.com" [bug/rfe]-id="4706026" >
358
public void testKeyFields(CacheMapping cacheMap, Result result, boolean oneFailed)
359     {
360         String JavaDoc cacheMapName = null;
361         if(cacheMap.getServletName()!=null && cacheMap.getServletName().length()!=0)
362             cacheMapName = cacheMap.getServletName();
363         else
364             cacheMapName = cacheMap.getUrlPattern();
365             
366         for(int i=0;i<cacheMap.sizeKeyField();i++)
367         {
368             if(cacheMap.isKeyField(i))
369             {
370                 String JavaDoc name = cacheMap.getAttributeValue(cacheMap.KEY_FIELD,i,"name");
371                 //testing name attribute
372
if(name!=null && name.length()==0)
373                 {
374                     result.failed(smh.getLocalString(getClass().getName()+".failed6",
375                        "FAILED [AS-WEB cache-mapping] for {0}, key-field #{1}, name cannot be an empty string",
376                         new Object JavaDoc[]{cacheMapName,new Integer JavaDoc(i)}));
377                     oneFailed = true;
378                 }
379                 else
380                 {
381                     result.passed(smh.getLocalString(getClass().getName()+".passed5",
382                         "PASSED [AS-WEB cache-mapping] for {0}, key-field #{1} name value furnished",
383                         new Object JavaDoc[]{cacheMapName,new Integer JavaDoc(i)}));
384                 }
385             }
386         }
387     }
388     //</addition>
389
}
390
391
Popular Tags