Exemplo n.º 1
0
  private Set<File> getDependencyFiles() {
    List<Resource> overlays = new ArrayList<Resource>();

    Set<File> dependencies = getClasspath().getFiles();
    logger.debug("Adding dependencies {} for WEB-INF/lib ", dependencies);

    // todo incorporate overlays when our resolved dependencies provide type information
    //            if (artifact.getType().equals("war")) {
    //                try {
    //                    Resource r = Resource.newResource("jar:" +
    // artifact.getFile().toURL().toString() + "!/");
    //                    overlays.add(r);
    //                    getExtraScanTargets().add(artifact.getFile());
    //                }
    //                catch (Exception e) {
    //                    throw new RuntimeException(e);
    //                }
    //                continue;
    //            }
    if (!overlays.isEmpty()) {
      try {
        Resource resource = getWebAppConfig().getBaseResource();
        ResourceCollection rc = new ResourceCollection();
        if (resource == null) {
          // nothing configured, so we automagically enable the overlays
          int size = overlays.size() + 1;
          Resource[] resources = new Resource[size];
          resources[0] = Resource.newResource(getWebAppSourceDirectory().toURI().toURL());
          for (int i = 1; i < size; i++) {
            resources[i] = overlays.get(i - 1);
            logger.info("Adding overlay: " + resources[i]);
          }
          rc.setResources(resources);
        } else {
          if (resource instanceof ResourceCollection) {
            // there was a preconfigured ResourceCollection ... append the artifact wars
            Resource[] old = ((ResourceCollection) resource).getResources();
            int size = old.length + overlays.size();
            Resource[] resources = new Resource[size];
            System.arraycopy(old, 0, resources, 0, old.length);
            for (int i = old.length; i < size; i++) {
              resources[i] = overlays.get(i - old.length);
              logger.info("Adding overlay: " + resources[i]);
            }
            rc.setResources(resources);
          } else {
            // baseResource was already configured w/c could be src/main/webapp
            if (!resource.isDirectory() && String.valueOf(resource.getFile()).endsWith(".war")) {
              // its a war
              resource = Resource.newResource("jar:" + resource.getURL().toString() + "!/");
            }
            int size = overlays.size() + 1;
            Resource[] resources = new Resource[size];
            resources[0] = resource;
            for (int i = 1; i < size; i++) {
              resources[i] = overlays.get(i - 1);
              logger.info("Adding overlay: " + resources[i]);
            }
            rc.setResources(resources);
          }
        }
        getWebAppConfig().setBaseResource(rc);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return dependencies;
  }
Exemplo n.º 2
0
  /* ------------------------------------------------------------ */
  public static void extract(Resource resource, File directory, boolean deleteOnExit)
      throws IOException {
    if (Log.isDebugEnabled()) Log.debug("Extract " + resource + " to " + directory);

    String urlString = resource.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);

    if (endOfJarUrl < 0) throw new IOException("Not a valid jar url: " + urlString);

    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    String subEntryName =
        (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
    boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);

    if (Log.isDebugEnabled())
      Log.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);

    InputStream is = jarFileURL.openConnection().getInputStream();
    JarInputStream jin = new JarInputStream(is);
    JarEntry entry = null;
    boolean shouldExtract = true;
    while ((entry = jin.getNextJarEntry()) != null) {
      String entryName = entry.getName();

      if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
        // if there is a particular subEntry that we are looking for, only
        // extract it.
        if (subEntryIsDir) {
          // if it is a subdirectory we are looking for, then we
          // are looking to extract its contents into the target
          // directory. Remove the name of the subdirectory so
          // that we don't wind up creating it too.
          entryName = entryName.substring(subEntryName.length());
          if (!entryName.equals("")) {
            // the entry is
            shouldExtract = true;
          } else shouldExtract = false;
        } else shouldExtract = true;
      } else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
        // there is a particular entry we are looking for, and this one
        // isn't it
        shouldExtract = false;
      } else {
        // we are extracting everything
        shouldExtract = true;
      }

      if (!shouldExtract) {
        if (Log.isDebugEnabled()) Log.debug("Skipping entry: " + entryName);
        continue;
      }

      File file = new File(directory, entryName);
      if (entry.isDirectory()) {
        // Make directory
        if (!file.exists()) file.mkdirs();
      } else {
        // make directory (some jars don't list dirs)
        File dir = new File(file.getParent());
        if (!dir.exists()) dir.mkdirs();

        // Make file
        FileOutputStream fout = null;
        try {
          fout = new FileOutputStream(file);
          IO.copy(jin, fout);
        } finally {
          IO.close(fout);
        }

        // touch the file.
        if (entry.getTime() >= 0) file.setLastModified(entry.getTime());
      }
      if (deleteOnExit) file.deleteOnExit();
    }
  }