1 17 18 package org.apache.catalina.util; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.NoSuchElementException ; 27 import java.util.StringTokenizer ; 28 import java.util.jar.JarInputStream ; 29 import java.util.jar.Manifest ; 30 31 import javax.naming.Binding ; 32 import javax.naming.NamingEnumeration ; 33 import javax.naming.NamingException ; 34 import javax.naming.directory.DirContext ; 35 36 import org.apache.catalina.core.StandardContext; 37 import org.apache.naming.resources.Resource; 38 39 40 53 public final class ExtensionValidator { 54 55 private static org.apache.commons.logging.Log log= 56 org.apache.commons.logging.LogFactory.getLog(ExtensionValidator.class); 57 58 61 private static StringManager sm = 62 StringManager.getManager("org.apache.catalina.util"); 63 64 private static ArrayList containerAvailableExtensions = null; 65 private static ArrayList containerManifestResources = new ArrayList (); 66 67 68 70 71 79 static { 80 81 String systemClasspath = System.getProperty("java.class.path"); 83 84 StringTokenizer strTok = new StringTokenizer (systemClasspath, 85 File.pathSeparator); 86 87 while (strTok.hasMoreTokens()) { 89 String classpathItem = strTok.nextToken(); 90 if (classpathItem.toLowerCase().endsWith(".jar")) { 91 File item = new File (classpathItem); 92 if (item.exists()) { 93 try { 94 addSystemResource(item); 95 } catch (IOException e) { 96 log.error(sm.getString 97 ("extensionValidator.failload", item), e); 98 } 99 } 100 } 101 } 102 103 addFolderList("java.ext.dirs"); 105 addFolderList("catalina.ext.dirs"); 106 107 } 108 109 110 112 113 130 public static synchronized boolean validateApplication( 131 DirContext dirContext, 132 StandardContext context) 133 throws IOException { 134 135 String appName = context.getPath(); 136 ArrayList appManifestResources = new ArrayList (); 137 if (dirContext == null) return false; 140 InputStream inputStream = null; 142 try { 143 NamingEnumeration wne = dirContext.listBindings("/META-INF/"); 144 Binding binding = (Binding ) wne.nextElement(); 145 if (binding.getName().toUpperCase().equals("MANIFEST.MF")) { 146 Resource resource = (Resource)dirContext.lookup 147 ("/META-INF/" + binding.getName()); 148 inputStream = resource.streamContent(); 149 Manifest manifest = new Manifest (inputStream); 150 inputStream.close(); 151 inputStream = null; 152 ManifestResource mre = new ManifestResource 153 (sm.getString("extensionValidator.web-application-manifest"), 154 manifest, ManifestResource.WAR); 155 appManifestResources.add(mre); 156 } 157 } catch (NamingException nex) { 158 } catch (NoSuchElementException nse) { 160 } finally { 162 if (inputStream != null) { 163 try { 164 inputStream.close(); 165 } catch (Throwable t) { 166 } 168 } 169 } 170 171 NamingEnumeration ne = null; 173 try { 174 if (dirContext != null) { 175 ne = dirContext.listBindings("WEB-INF/lib/"); 176 } 177 while ((ne != null) && ne.hasMoreElements()) { 178 Binding binding = (Binding )ne.nextElement(); 179 if (!binding.getName().toLowerCase().endsWith(".jar")) { 180 continue; 181 } 182 Resource resource = (Resource)dirContext.lookup 183 ("/WEB-INF/lib/" + binding.getName()); 184 Manifest jmanifest = getManifest(resource.streamContent()); 185 if (jmanifest != null) { 186 ManifestResource mre = new ManifestResource( 187 binding.getName(), 188 jmanifest, 189 ManifestResource.APPLICATION); 190 appManifestResources.add(mre); 191 } 192 } 193 } catch (NamingException nex) { 194 } 197 198 return validateManifestResources(appName, appManifestResources); 199 } 200 201 202 208 public static void addSystemResource(File jarFile) throws IOException { 209 Manifest manifest = getManifest(new FileInputStream (jarFile)); 210 if (manifest != null) { 211 ManifestResource mre 212 = new ManifestResource(jarFile.getAbsolutePath(), 213 manifest, 214 ManifestResource.SYSTEM); 215 containerManifestResources.add(mre); 216 } 217 } 218 219 220 222 223 242 private static boolean validateManifestResources(String appName, 243 ArrayList resources) { 244 boolean passes = true; 245 int failureCount = 0; 246 ArrayList availableExtensions = null; 247 248 Iterator it = resources.iterator(); 249 while (it.hasNext()) { 250 ManifestResource mre = (ManifestResource)it.next(); 251 ArrayList requiredList = mre.getRequiredExtensions(); 252 if (requiredList == null) { 253 continue; 254 } 255 256 if (availableExtensions == null) { 258 availableExtensions = buildAvailableExtensionsList(resources); 259 } 260 261 if (containerAvailableExtensions == null) { 264 containerAvailableExtensions 265 = buildAvailableExtensionsList(containerManifestResources); 266 } 267 268 Iterator rit = requiredList.iterator(); 270 while (rit.hasNext()) { 271 boolean found = false; 272 Extension requiredExt = (Extension)rit.next(); 273 if (availableExtensions != null) { 275 Iterator ait = availableExtensions.iterator(); 276 while (ait.hasNext()) { 277 Extension targetExt = (Extension) ait.next(); 278 if (targetExt.isCompatibleWith(requiredExt)) { 279 requiredExt.setFulfilled(true); 280 found = true; 281 break; 282 } 283 } 284 } 285 if (!found && containerAvailableExtensions != null) { 287 Iterator cit = containerAvailableExtensions.iterator(); 288 while (cit.hasNext()) { 289 Extension targetExt = (Extension) cit.next(); 290 if (targetExt.isCompatibleWith(requiredExt)) { 291 requiredExt.setFulfilled(true); 292 found = true; 293 break; 294 } 295 } 296 } 297 if (!found) { 298 log.info(sm.getString( 300 "extensionValidator.extension-not-found-error", 301 appName, mre.getResourceName(), 302 requiredExt.getExtensionName())); 303 passes = false; 304 failureCount++; 305 } 306 } 307 } 308 309 if (!passes) { 310 log.info(sm.getString( 311 "extensionValidator.extension-validation-error", appName, 312 failureCount + "")); 313 } 314 315 return passes; 316 } 317 318 335 private static ArrayList buildAvailableExtensionsList(ArrayList resources) { 336 337 ArrayList availableList = null; 338 339 Iterator it = resources.iterator(); 340 while (it.hasNext()) { 341 ManifestResource mre = (ManifestResource)it.next(); 342 ArrayList list = mre.getAvailableExtensions(); 343 if (list != null) { 344 Iterator values = list.iterator(); 345 while (values.hasNext()) { 346 Extension ext = (Extension) values.next(); 347 if (availableList == null) { 348 availableList = new ArrayList (); 349 availableList.add(ext); 350 } else { 351 availableList.add(ext); 352 } 353 } 354 } 355 } 356 357 return availableList; 358 } 359 360 366 private static Manifest getManifest(InputStream inStream) 367 throws IOException { 368 369 Manifest manifest = null; 370 JarInputStream jin = null; 371 372 try { 373 jin = new JarInputStream (inStream); 374 manifest = jin.getManifest(); 375 jin.close(); 376 jin = null; 377 } finally { 378 if (jin != null) { 379 try { 380 jin.close(); 381 } catch (Throwable t) { 382 } 384 } 385 } 386 387 return manifest; 388 } 389 390 391 394 private static void addFolderList(String property) { 395 396 String extensionsDir = System.getProperty(property); 398 if (extensionsDir != null) { 399 StringTokenizer extensionsTok 400 = new StringTokenizer (extensionsDir, File.pathSeparator); 401 while (extensionsTok.hasMoreTokens()) { 402 File targetDir = new File (extensionsTok.nextToken()); 403 if (!targetDir.exists() || !targetDir.isDirectory()) { 404 continue; 405 } 406 File [] files = targetDir.listFiles(); 407 for (int i = 0; i < files.length; i++) { 408 if (files[i].getName().toLowerCase().endsWith(".jar")) { 409 try { 410 addSystemResource(files[i]); 411 } catch (IOException e) { 412 log.error 413 (sm.getString 414 ("extensionValidator.failload", files[i]), e); 415 } 416 } 417 } 418 } 419 } 420 421 } 422 423 424 } 425 | Popular Tags |