1 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 ; 35 import java.util.Iterator ; 36 37 38 41 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 try{ 56 Cache cache = (descriptor.getSunDescriptor()).getCache(); 57 CacheMapping[] cacheMapp=null; 58 String servletName=null; 59 String urlPattern=null; 60 String timeout=null; 61 String [] httpMethods; 62 String 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 [] {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 [] {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 [] {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 [] {urlPattern})); 106 oneFailed = true; 107 } 108 } 109 if(cacheHelperRef !=null){ 110 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 [] {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 [] {cacheHelperRef})); 125 } 126 }else{ 127 if(timeout != null){ 128 int i = rep +1; 129 String 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 [] {new Integer (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 [] {timeout,timeoutName})); 143 } 144 } 145 int j = rep+1; 147 int count = getCountNodeSet("sun-web-app/cache/cache-mapping["+j+"]/refresh-field"); 148 if(count>0) { 150 String cacheMapName = null; 151 if(cacheMapp[rep].getServletName()!=null) 152 cacheMapName = cacheMapp[rep].getServletName(); 153 else 154 cacheMapName = cacheMapp[rep].getUrlPattern(); 155 String 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 []{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 []{cacheMapName,name})); 168 oneFailed = true; 169 } 170 } 171 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 if ((getCountNodeSet("sun-web-app/cache/cache-mapping["+j+"]/key-field"))>0) 186 testForKeyFields(cache, j, result, compName); 187 } 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 [] {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 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 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 servletName, WebBundleDescriptor descriptor){ 231 boolean valid=false; 232 if (servletName != null && servletName.length() != 0){ 233 Set servlets = descriptor.getServletDescriptors(); 234 Iterator itr = servlets.iterator(); 235 while (itr.hasNext()){ 237 String 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 timeout,String 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(timeoutName !=null && timeoutName.length() != 0) 256 valid = true; 257 } 258 } catch(NumberFormatException exception){ 259 } 261 262 } else { valid = true; 264 } 265 return valid; 266 } 267 268 boolean checkHTTPMethodList(String [] 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 [] {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 [] {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 [] {httpMethods[rep]})); 299 300 valid=false; 301 } 302 303 } 304 305 } 306 return valid; 307 } 308 309 boolean validCacheHelperRef(String helperRef, Cache cache){ 310 boolean valid=false; 311 if (helperRef.length() != 0){ 312 CacheHelper[] helperClasses=null; 313 CacheHelper helperClass=null; 314 String 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 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 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 []{cacheMapName,new Integer (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 []{cacheMapName,new Integer (k)})); 358 } 359 } 360 } 361 } 362 } 363 | Popular Tags |