Ejemplo n.º 1
0
    public void process(File f) throws Exception {
      if (f.isDirectory()) {
        File[] files =
            f.listFiles(
                new FilenameFilter() {

                  @Override
                  public boolean accept(File dir, String name) {
                    return name.matches(FILE_PATTERN);
                  }
                });
        for (File ff : files) {
          byte[] bindingBytes = Files.toByteArray(ff);
          this.addCurrentBinding(bindingBytes, ff.getName(), "file:" + ff.getAbsolutePath());
        }
      } else {
        String digest = new BigInteger(Files.getDigest(f, Digest.MD5.get())).abs().toString(16);
        CURRENT_PROPS.put(BINDING_CACHE_JAR_PREFIX + f.getName(), digest);
        final JarFile jar = new JarFile(f);
        final List<JarEntry> jarList = Collections.list(jar.entries());
        for (final JarEntry j : jarList) {
          try {
            if (j.getName().matches(FILE_PATTERN)) {
              byte[] bindingBytes = ByteStreams.toByteArray(jar.getInputStream(j));
              String bindingName = j.getName();
              String bindingFullPath = "jar:file:" + f.getAbsolutePath() + "!/" + bindingName;
              this.addCurrentBinding(bindingBytes, bindingName, bindingFullPath);
            } else if (j.getName().matches(".*\\.class.{0,1}")) {
              final String classGuess =
                  j.getName().replaceAll("/", ".").replaceAll("\\.class.{0,1}", "");
              final Class candidate = ClassLoader.getSystemClassLoader().loadClass(classGuess);
              if (MSG_BASE_CLASS.isAssignableFrom(candidate)
                  || MSG_DATA_CLASS.isAssignableFrom(candidate)) {
                InputSupplier<InputStream> classSupplier =
                    Resources.newInputStreamSupplier(ClassLoader.getSystemResource(j.getName()));
                File destClassFile = SubDirectory.CLASSCACHE.getChildFile(j.getName());
                if (!destClassFile.exists()) {
                  Files.createParentDirs(destClassFile);
                  Files.copy(classSupplier, destClassFile);
                  Logs.extreme()
                      .debug("Caching: " + j.getName() + " => " + destClassFile.getAbsolutePath());
                }
                BINDING_CLASS_MAP.putIfAbsent(classGuess, candidate);
              }
            }
          } catch (RuntimeException ex) {
            LOG.error(ex, ex);
            jar.close();
            throw ex;
          }
        }
        jar.close();
      }
    }
  public void find(String binFile, List<SourceFileResult> results) {
    File bin = new File(binFile);
    String url = null;
    String fileDownloaded = null;

    try {
      if (canceled) return;
      InputStream is2 = null;
      String md5;
      try {
        md5 = new String(Hex.encodeHex(Files.getDigest(bin, MessageDigest.getInstance("MD5"))));
        String serviceUrl = SERVICE + "/rest/libraries?md5=" + md5;
        is2 = new URL(serviceUrl).openStream();
        String str = IOUtils.toString(is2);
        JSONArray json = JSONArray.fromObject(str);

        for (int i = 0; i < json.size(); i++) {
          if (canceled) return;
          JSONObject obj = (JSONObject) json.get(i);
          JSONObject source = obj.getJSONObject("source");
          if (source != null && !source.isNullObject()) {
            JSONArray ar = source.getJSONArray("urls");
            if (ar != null && !ar.isEmpty()) {
              String url1 = ar.getString(0);
              String tmpFile = new UrlDownloader().download(url1);
              if (tmpFile != null && isSourceCodeFor(tmpFile, bin.getAbsolutePath())) {
                fileDownloaded = tmpFile;
                url = url1;
                break;
              }
            }
          }
        }

        if (url != null && fileDownloaded != null) {
          String name = url.substring(url.lastIndexOf('/') + 1);

          SourceFileResult object = new SourceFileResult(binFile, fileDownloaded, name, 90);
          Logger.debug(this.toString() + " FOUND: " + object, null);
          results.add(object);
        }

      } finally {
        IOUtils.closeQuietly(is2);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }