Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 14th, 2011  |  syntax: None  |  size: 2.56 KB  |  views: 92  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public static Set<File> findOnCP(String namePattern) {
  2.         ClassLoader loader = TestUtility.class.getClassLoader();
  3.         File f;
  4.         Pattern p = Pattern.compile(namePattern);
  5.         Set<File> selected = new HashSet<File>();
  6.  
  7.         Set<String> stuffOnClassPath = new HashSet<String>();
  8.  
  9.         //take all URLs on the CP
  10.         if (loader instanceof URLClassLoader) {
  11.             URLClassLoader uLoader = (URLClassLoader) loader;
  12.             for (URL url : uLoader.getURLs()) {
  13.                 if ("file".equals(url.getProtocol())) {
  14.                     //this is a file somewhere on the CP
  15.                     try {
  16.                         stuffOnClassPath.add(new File(url.toURI()).getCanonicalPath());
  17.                     } catch (Exception e) {
  18.                         throw new IllegalStateException("something went horribly wrong",e);
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.         //another approach
  24.         String[] elements = System.getProperty("java.class.path").split(File.pathSeparator);
  25.         stuffOnClassPath.addAll(Arrays.asList(elements));
  26.         if (elements.length==1) {
  27.             //on windows this is a workaround for long paths
  28.             File jarFile = new File(elements[0]);
  29.             if (jarFile.exists() && jarFile.canRead()) {
  30.                 try {
  31.                     JarInputStream is = new JarInputStream(new FileInputStream(jarFile));
  32.                     stuffOnClassPath.addAll(Arrays.asList(is.getManifest().getMainAttributes().getValue("Class-Path").split("[\\s]+")));
  33.                 } catch (Exception e) {
  34.                     throw new IllegalStateException(e);
  35.                 }
  36.             }
  37.         }
  38.  
  39.         for (String fileName : stuffOnClassPath) {
  40.             try {
  41.                 f = new File(fileName);
  42.                 if (!f.exists()) {
  43.                     try {
  44.                         f = new File(new URI(fileName));
  45.                     } catch (URISyntaxException e) {
  46.                         continue;
  47.                     }
  48.                 }
  49.                 if (!f.exists()) {
  50.                     continue;
  51.                 }
  52.                 if (p.matcher(f.getName()).matches()) {
  53.                     selected.add(f);
  54.                 }
  55.             } catch (Exception e) {
  56.                 throw new IllegalStateException(e);
  57.             }
  58.         }
  59.  
  60.         System.out.println("search for "+namePattern+" produced "+selected.size()+" results");
  61.  
  62.         if (selected.isEmpty()) {
  63.             return Collections.emptySet();
  64.         }
  65.         return selected;
  66.     }
clone this paste RAW Paste Data