/* ------------------------------------------------------------ */
  @Test
  public void testJarFile() throws Exception {
    String s = "jar:" + __userURL + "TestData/test.zip!/subdir/";
    Resource r = Resource.newResource(s);

    Set<String> entries = new HashSet<>(Arrays.asList(r.list()));
    assertEquals(3, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    assertTrue(entries.contains("subsubdir/"));

    File extract = File.createTempFile("extract", null);
    if (extract.exists()) extract.delete();
    extract.mkdir();
    extract.deleteOnExit();

    r.copyTo(extract);

    Resource e = Resource.newResource(extract.getAbsolutePath());

    entries = new HashSet<>(Arrays.asList(e.list()));
    assertEquals(3, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    assertTrue(entries.contains("subsubdir/"));
    IO.delete(extract);

    s = "jar:" + __userURL + "TestData/test.zip!/subdir/subsubdir/";
    r = Resource.newResource(s);

    entries = new HashSet<>(Arrays.asList(r.list()));
    assertEquals(2, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));

    extract = File.createTempFile("extract", null);
    if (extract.exists()) extract.delete();
    extract.mkdir();
    extract.deleteOnExit();

    r.copyTo(extract);

    e = Resource.newResource(extract.getAbsolutePath());

    entries = new HashSet<>(Arrays.asList(e.list()));
    assertEquals(2, entries.size());
    assertTrue(entries.contains("alphabet"));
    assertTrue(entries.contains("numbers"));
    IO.delete(extract);
  }
  /**
   * Look for jars in WEB-INF/lib
   *
   * @param context
   * @return the list of jar resources found within context
   * @throws Exception
   */
  protected List<Resource> findJars(WebAppContext context) throws Exception {
    List<Resource> jarResources = new ArrayList<Resource>();

    Resource web_inf = context.getWebInf();
    if (web_inf == null || !web_inf.exists()) return null;

    Resource web_inf_lib = web_inf.addPath("/lib");

    if (web_inf_lib.exists() && web_inf_lib.isDirectory()) {
      String[] files = web_inf_lib.list();
      for (int f = 0; files != null && f < files.length; f++) {
        try {
          Resource file = web_inf_lib.addPath(files[f]);
          String fnlc = file.getName().toLowerCase(Locale.ENGLISH);
          int dot = fnlc.lastIndexOf('.');
          String extension = (dot < 0 ? null : fnlc.substring(dot));
          if (extension != null && (extension.equals(".jar") || extension.equals(".zip"))) {
            jarResources.add(file);
          }
        } catch (Exception ex) {
          LOG.warn(Log.EXCEPTION, ex);
        }
      }
    }
    return jarResources;
  }
Exemple #3
0
    public void addJars(Resource lib) throws IOException {
      if (lib == null || !lib.exists()) throw new IllegalStateException("No such lib: " + lib);

      String[] list = lib.list();
      if (list == null) return;

      for (String path : list) {
        if (".".equals(path) || "..".equals(path)) continue;

        try (Resource item = lib.addPath(path)) {
          if (item.isDirectory()) addJars(item);
          else {
            if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) {
              URL url = item.getURL();
              _classpath.add(url);
            }
          }
        }
      }
    }
  /**
   * Parse all classes in a directory
   *
   * @param dir
   * @param resolver
   * @throws Exception
   */
  public void parseDir(Resource dir, ClassNameResolver resolver) throws Exception {
    // skip dirs whose name start with . (ie hidden)
    if (!dir.isDirectory() || !dir.exists() || dir.getName().startsWith(".")) return;

    if (LOG.isDebugEnabled()) {
      LOG.debug("Scanning dir {}", dir);
    }
    ;

    String[] files = dir.list();
    for (int f = 0; files != null && f < files.length; f++) {
      try {
        Resource res = dir.addPath(files[f]);
        if (res.isDirectory()) parseDir(res, resolver);
        else {
          // we've already verified the directories, so just verify the class file name
          String filename = res.getFile().getName();
          if (isValidClassFileName(filename)) {
            String name = res.getName();
            if ((resolver == null)
                || (!resolver.isExcluded(name)
                    && (!isParsed(name) || resolver.shouldOverride(name)))) {
              Resource r = Resource.newResource(res.getURL());
              if (LOG.isDebugEnabled()) {
                LOG.debug("Scanning class {}", r);
              }
              ;
              scanClass(r.getInputStream());
            }
          }
        }
      } catch (Exception ex) {
        LOG.warn(Log.EXCEPTION, ex);
      }
    }
  }