/**
   * Load the policies from the specified file. Also checks that the policies are correctly signed.
   */
  private static void loadPolicies(
      File jarPathName, CryptoPermissions defaultPolicy, CryptoPermissions exemptPolicy)
      throws Exception {

    JarFile jf = new JarFile(jarPathName);

    Enumeration<JarEntry> entries = jf.entries();
    while (entries.hasMoreElements()) {
      JarEntry je = entries.nextElement();
      InputStream is = null;
      try {
        if (je.getName().startsWith("default_")) {
          is = jf.getInputStream(je);
          defaultPolicy.load(is);
        } else if (je.getName().startsWith("exempt_")) {
          is = jf.getInputStream(je);
          exemptPolicy.load(is);
        } else {
          continue;
        }
      } finally {
        if (is != null) {
          is.close();
        }
      }

      // Enforce the signer restraint, i.e. signer of JCE framework
      // jar should also be the signer of the two jurisdiction policy
      // jar files.
      JarVerifier.verifyPolicySigned(je.getCertificates());
    }
    // Close and nullify the JarFile reference to help GC.
    jf.close();
    jf = null;
  }
  public static List getClasseNamesInPackage(String jarName, String packageName) {
    ArrayList classes = new ArrayList();

    packageName = packageName.replaceAll("\\.", "/");
    if (debug) System.out.println("Jar " + jarName + " looking for " + packageName);
    try {
      JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
      JarEntry jarEntry;

      while (true) {
        jarEntry = jarFile.getNextJarEntry();
        if (jarEntry == null) {
          break;
        }
        if ((jarEntry.getName().startsWith(packageName))
            && (jarEntry.getName().endsWith(".class"))) {
          if (debug) System.out.println("Found " + jarEntry.getName().replaceAll("/", "\\."));
          classes.add(jarEntry.getName().replaceAll("/", "\\."));
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return classes;
  }
Example #3
0
    public String hasSameContent(JarFile2 file, JarEntry entry) throws IOException {

      String thisName = null;

      Long crcL = new Long(entry.getCrc());

      // check if this jar contains files with the passed in entry's crc
      if (_crcToEntryMap.containsKey(crcL)) {
        // get the Linked List with files with the crc
        LinkedList ll = (LinkedList) _crcToEntryMap.get(crcL);
        // go through the list and check for content match
        ListIterator li = ll.listIterator(0);
        if (li != null) {
          while (li.hasNext()) {
            JarEntry thisEntry = (JarEntry) li.next();

            // check for content match
            InputStream oldIS = getJarFile().getInputStream(thisEntry);
            InputStream newIS = file.getJarFile().getInputStream(entry);

            if (!differs(oldIS, newIS)) {
              thisName = thisEntry.getName();
              return thisName;
            }
          }
        }
      }

      return thisName;
    }
  /**
   * Load classes from given file. File could be either directory or JAR file.
   *
   * @param clsLdr Class loader to load files.
   * @param file Either directory or JAR file which contains classes or references to them.
   * @return Set of found and loaded classes or empty set if file does not exist.
   * @throws GridSpiException Thrown if given JAR file references to none existed class or
   *     IOException occurred during processing.
   */
  static Set<Class<? extends GridTask<?, ?>>> getClasses(ClassLoader clsLdr, File file)
      throws GridSpiException {
    Set<Class<? extends GridTask<?, ?>>> rsrcs = new HashSet<Class<? extends GridTask<?, ?>>>();

    if (file.exists() == false) {
      return rsrcs;
    }

    GridUriDeploymentFileResourceLoader fileRsrcLdr =
        new GridUriDeploymentFileResourceLoader(clsLdr, file);

    if (file.isDirectory()) {
      findResourcesInDirectory(fileRsrcLdr, file, rsrcs);
    } else {
      try {
        for (JarEntry entry : U.asIterable(new JarFile(file.getAbsolutePath()).entries())) {
          Class<? extends GridTask<?, ?>> rsrc = fileRsrcLdr.createResource(entry.getName(), false);

          if (rsrc != null) {
            rsrcs.add(rsrc);
          }
        }
      } catch (IOException e) {
        throw new GridSpiException(
            "Failed to discover classes in file: " + file.getAbsolutePath(), e);
      }
    }

    return rsrcs;
  }
Example #5
0
  /**
   * load from the specified jar filled with help files in the [language] directory in the jar
   *
   * @param file the jar file
   */
  private void loadFromJar(File file) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        int counter = 0;
        JarInputStream jis;
        JarEntry je;
        counter = 0;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          String mnemo = trimEntryName(je);
          if (je.getName().toLowerCase().matches(helproot + "/" + language + "/.*.htm")
              && !exists(mnemo)) {
            addToCache(jis, mnemo);
            counter++;
          }
          je = jis.getNextJarEntry();
        }
        jis.close();

        System.out.println(
            "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + file.getCanonicalPath());
      } catch (IOException ignored) {
      }
    }
  }
Example #6
0
 /**
  * Return the {@code JarEntry} specified by name or {@code null} if no such entry exists.
  *
  * @param name the name of the entry in the JAR file.
  * @return the ZIP entry extracted.
  */
 @Override
 public ZipEntry getEntry(String name) {
   ZipEntry ze = super.getEntry(name);
   if (ze == null) {
     return ze;
   }
   JarEntry je = new JarEntry(ze);
   je.parentJar = this;
   return je;
 }
 private Certificate[] getCertificates(final File container, final String entry)
     throws IOException {
   if (container.isDirectory()) {
     return null;
   }
   final JarFile jarFile = this.jarFiles.get(container);
   if (jarFile == null) {
     return null;
   }
   final JarEntry ent = jarFile.getJarEntry(entry);
   return (Certificate[]) ((ent == null) ? null : ent.getCertificates());
 }
Example #8
0
    private void index() throws IOException {
      Enumeration entries = _jar.entries();

      _nameToEntryMap = new HashMap();
      _crcToEntryMap = new HashMap();

      _entries = new ArrayList();
      if (_debug) {
        System.out.println("indexing: " + _jar.getName());
      }
      if (entries != null) {
        while (entries.hasMoreElements()) {
          JarEntry entry = (JarEntry) entries.nextElement();

          long crc = entry.getCrc();

          Long crcL = new Long(crc);

          if (_debug) {
            System.out.println("\t" + entry.getName() + " CRC " + crc);
          }

          _nameToEntryMap.put(entry.getName(), entry);
          _entries.add(entry);

          // generate the CRC to entries map
          if (_crcToEntryMap.containsKey(crcL)) {
            // key exist, add the entry to the correcponding
            // linked list

            // get the linked list
            LinkedList ll = (LinkedList) _crcToEntryMap.get(crcL);

            // put in the new entry
            ll.add(entry);

            // put it back in the hash map
            _crcToEntryMap.put(crcL, ll);
          } else {
            // create a new entry in the hashmap for the new key

            // first create the linked list and put in the new
            // entry
            LinkedList ll = new LinkedList();
            ll.add(entry);

            // create the new entry in the hashmap
            _crcToEntryMap.put(crcL, ll);
          }
        }
      }
    }
Example #9
0
 void expand(File jar, File dir) throws IOException {
   JarFile jarFile = new JarFile(jar);
   try {
     Enumeration<JarEntry> entries = jarFile.entries();
     while (entries.hasMoreElements()) {
       JarEntry je = entries.nextElement();
       if (!je.isDirectory()) {
         copy(jarFile.getInputStream(je), new File(dir, je.getName()));
       }
     }
   } finally {
     jarFile.close();
   }
 }
 private static Set<String> findStandardMBeansFromJar(URL codeBase) throws Exception {
   InputStream is = codeBase.openStream();
   JarInputStream jis = new JarInputStream(is);
   Set<String> names = new TreeSet<String>();
   JarEntry entry;
   while ((entry = jis.getNextJarEntry()) != null) {
     String name = entry.getName();
     if (!name.endsWith(".class")) continue;
     name = name.substring(0, name.length() - 6);
     name = name.replace('/', '.');
     names.add(name);
   }
   return names;
 }
Example #11
0
  public static void unzipInteralZip(
      ClassLoader classLoader, String resourcePath, File libDir, boolean debug) {
    if (debug) System.out.println("Extracting " + resourcePath);
    libDir.mkdir();
    URL resource = classLoader.getResource(resourcePath);
    if (resource == null) {
      System.err.println("Could not find the " + resourcePath + " on classpath!");
      System.exit(1);
    }
    class PrintDot extends TimerTask {
      public void run() {
        System.out.print(".");
      }
    }
    Timer timer = new Timer();
    PrintDot task = new PrintDot();
    timer.schedule(task, 0, 2000);

    try {

      BufferedInputStream bis = new BufferedInputStream(resource.openStream());
      JarInputStream jis = new JarInputStream(bis);
      JarEntry je = null;
      while ((je = jis.getNextJarEntry()) != null) {
        java.io.File f =
            new java.io.File(libDir.toString() + java.io.File.separator + je.getName());
        if (je.isDirectory()) {
          f.mkdir();
          continue;
        }
        File parentDir = new File(f.getParent());
        if (!parentDir.exists()) {
          parentDir.mkdir();
        }
        FileOutputStream fileOutStream = new FileOutputStream(f);
        writeStreamTo(jis, fileOutStream, 8 * KB);
        if (f.getPath().endsWith("pack.gz")) {
          unpack(f);
          fileOutStream.close();
          f.delete();
        }
      }

    } catch (Exception exc) {
      task.cancel();
      exc.printStackTrace();
    }
    task.cancel();
  }
Example #12
0
  /**
   * Enumerates the resouces in a give package name. This works even if the resources are loaded
   * from a jar file!
   *
   * <p>Adapted from code by mikewse on the java.sun.com message boards.
   * http://forum.java.sun.com/thread.jsp?forum=22&thread=30984
   *
   * @param packageName The package to enumerate
   * @return A Set of Strings for each resouce in the package.
   */
  public static Set getResoucesInPackage(String packageName) throws IOException {
    String localPackageName;
    if (packageName.endsWith("/")) {
      localPackageName = packageName;
    } else {
      localPackageName = packageName + '/';
    }

    Enumeration dirEnum = ClassLoader.getSystemResources(localPackageName);

    Set names = new HashSet();

    // Loop CLASSPATH directories
    while (dirEnum.hasMoreElements()) {
      URL resUrl = (URL) dirEnum.nextElement();

      // Pointing to filesystem directory
      if (resUrl.getProtocol().equals("file")) {
        File dir = new File(resUrl.getFile());
        File[] files = dir.listFiles();
        if (files != null) {
          for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) continue;
            names.add(localPackageName + file.getName());
          }
        }

        // Pointing to Jar file
      } else if (resUrl.getProtocol().equals("jar")) {
        JarURLConnection jconn = (JarURLConnection) resUrl.openConnection();
        JarFile jfile = jconn.getJarFile();
        Enumeration entryEnum = jfile.entries();
        while (entryEnum.hasMoreElements()) {
          JarEntry entry = (JarEntry) entryEnum.nextElement();
          String entryName = entry.getName();
          // Exclude our own directory
          if (entryName.equals(localPackageName)) continue;
          String parentDirName = entryName.substring(0, entryName.lastIndexOf('/') + 1);
          if (!parentDirName.equals(localPackageName)) continue;
          names.add(entryName);
        }
      } else {
        // Invalid classpath entry
      }
    }

    return names;
  }
Example #13
0
 private static void completePackage(Set seenClasses, JarFile jar, String packageName) {
   int len = packageName.length();
   Enumeration entries = jar.entries();
   while (entries.hasMoreElements()) {
     JarEntry entry = (JarEntry) entries.nextElement();
     String name = entry.getName();
     if (name.startsWith(packageName) && name.endsWith(".class") && name.lastIndexOf('/') == len) {
       // Trim ".class" from end
       name = name.substring(0, name.length() - 6);
       if (seenClasses.add(name)) {
         System.out.println(name);
       }
     }
   }
 }
 private static void writeRuntimeToJar(final JarOutputStream stream) throws IOException {
   final File unpackedRuntimePath = getUnpackedRuntimePath();
   if (unpackedRuntimePath != null) {
     FileUtil.processFilesRecursively(
         unpackedRuntimePath,
         new Processor<File>() {
           @Override
           public boolean process(File file) {
             if (file.isDirectory()) return true;
             final String relativePath = FileUtil.getRelativePath(unpackedRuntimePath, file);
             try {
               stream.putNextEntry(new JarEntry(FileUtil.toSystemIndependentName(relativePath)));
               FileInputStream fis = new FileInputStream(file);
               try {
                 FileUtil.copy(fis, stream);
               } finally {
                 fis.close();
               }
             } catch (IOException e) {
               throw new RuntimeException(e);
             }
             return true;
           }
         });
   } else {
     File runtimeJarPath = getRuntimeJarPath();
     if (runtimeJarPath != null) {
       JarInputStream jis = new JarInputStream(new FileInputStream(runtimeJarPath));
       try {
         while (true) {
           JarEntry e = jis.getNextJarEntry();
           if (e == null) {
             break;
           }
           if (FileUtil.getExtension(e.getName()).equals("class")) {
             stream.putNextEntry(e);
             FileUtil.copy(jis, stream);
           }
         }
       } finally {
         jis.close();
       }
     } else {
       throw new CompileEnvironmentException("Couldn't find runtime library");
     }
   }
 }
 VerifierStream(Manifest man, JarEntry je, InputStream is, JarVerifier jv) throws IOException {
   this.is = is;
   this.jv = jv;
   this.mev = new ManifestEntryVerifier(man);
   this.jv.beginEntry(je, mev);
   this.numLeft = je.getSize();
   if (this.numLeft == 0) this.jv.update(-1, this.mev);
 }
Example #16
0
    public boolean contains(JarFile2 f, JarEntry e) throws IOException {

      JarEntry thisEntry = getEntryByName(e.getName());

      // Look up name in 'this' Jar2File - if not exist return false
      if (thisEntry == null) return false;

      // Check CRC - if no match - return false
      if (thisEntry.getCrc() != e.getCrc()) return false;

      // Check contents - if no match - return false
      InputStream oldIS = getJarFile().getInputStream(thisEntry);
      InputStream newIS = f.getJarFile().getInputStream(e);
      boolean retValue = differs(oldIS, newIS);

      return !retValue;
    }
Example #17
0
    public String getBestMatch(JarFile2 file, JarEntry entry) throws IOException {
      // check for same name and same content, return name if found
      if (contains(file, entry)) {
        return (entry.getName());
      }

      // return name of same content file or null
      return (hasSameContent(file, entry));
    }
  public String verify(JarFile jar, String... algorithms) throws IOException {
    if (algorithms == null || algorithms.length == 0) algorithms = new String[] {"MD5", "SHA"};
    else if (algorithms.length == 1 && algorithms[0].equals("-")) return null;

    try {
      Manifest m = jar.getManifest();
      if (m.getEntries().isEmpty()) return "No name sections";

      for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
        JarEntry je = e.nextElement();
        if (MANIFEST_ENTRY.matcher(je.getName()).matches()) continue;

        Attributes nameSection = m.getAttributes(je.getName());
        if (nameSection == null) return "No name section for " + je.getName();

        for (String algorithm : algorithms) {
          try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            String expected = nameSection.getValue(algorithm + "-Digest");
            if (expected != null) {
              byte digest[] = Base64.decodeBase64(expected);
              copy(jar.getInputStream(je), md);
              if (!Arrays.equals(digest, md.digest()))
                return "Invalid digest for "
                    + je.getName()
                    + ", "
                    + expected
                    + " != "
                    + Base64.encodeBase64(md.digest());
            } else reporter.error("could not find digest for " + algorithm + "-Digest");
          } catch (NoSuchAlgorithmException nsae) {
            return "Missing digest algorithm " + algorithm;
          }
        }
      }
    } catch (Exception e) {
      return "Failed to verify due to exception: " + e.getMessage();
    }
    return null;
  }
Example #19
0
  /**
   * scans inside a jar for possible language directories
   *
   * @param file the jar file
   * @param paths the list to add to
   * @param helproot the root directory of all help directories
   */
  private void searchJarPath(File file, List<String> paths) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        JarInputStream jis;
        JarEntry je;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          if (je.getName().startsWith(helproot)) {
            String[] name = je.getName().split("/");
            if (name.length > 1) {
              addToList(paths, name[1]);
            }
          }
          je = jis.getNextJarEntry();
        }
        jis.close();
      } catch (IOException ignored) {
      }
    }
  }
Example #20
0
 void setupEntryCertificates() {
   // Fallback to JarInputStream to obtain certificates, not fast but hopefully not
   // happening that often.
   try {
     JarInputStream inputStream =
         new JarInputStream(getData().getInputStream(ResourceAccess.ONCE));
     try {
       java.util.jar.JarEntry entry = inputStream.getNextJarEntry();
       while (entry != null) {
         inputStream.closeEntry();
         JarEntry jarEntry = getJarEntry(entry.getName());
         if (jarEntry != null) {
           jarEntry.setupCertificates(entry);
         }
         entry = inputStream.getNextJarEntry();
       }
     } finally {
       inputStream.close();
     }
   } catch (IOException ex) {
     throw new IllegalStateException(ex);
   }
 }
Example #21
0
  public GLBootstrap() throws Exception {

    System.setProperty("jogamp.gluegen.UseTempJarCache", "false");

    log.info(
        "Initializing native JOGL jar dependencies for platform [{}]. Temp jar cache disabled.",
        PlatformPropsImpl.os_and_arch);

    String nativeJarName = String.format("%s-%s", NATIVES, PlatformPropsImpl.os_and_arch);
    String[] classpathEntries =
        System.getProperty(JAVA_CLASSPATH).split(System.getProperty(JAVA_SEPARATOR));

    for (String jarPath : classpathEntries) {

      if (jarPath.contains(nativeJarName)) {

        if (log.isDebugEnabled()) {
          log.debug("Applicable platform jar: [{}]", jarPath);
        }

        JarFile jf = new JarFile(jarPath);

        try {
          Enumeration<JarEntry> jarEntries = jf.entries();

          while (jarEntries.hasMoreElements()) {

            JarEntry je = jarEntries.nextElement();

            if (!je.isDirectory() && !je.getName().contains(JAVA_META_INF)) {
              if (log.isDebugEnabled()) {
                log.debug("Mapping jar entry [{}] -> [{}]", je.getName(), jarPath);
              }
              if (log.isDebugEnabled() && platformNativeIndex.containsKey(je.getName())) {
                log.debug("Duplicate jar entry: [{}]", je.getName());
                log.debug("Mapped at: [{}]", platformNativeIndex.get(je.getName()));
                log.debug("Also at: [{}]", jarPath);
              }
              platformNativeIndex.put(je.getName(), jarPath);
            }
          }
        } finally {
          closeJar(jf);
        }
      }
    }
  }
 public CodeSigner[] getCodeSigners(JarFile jar, JarEntry entry) {
   String name = entry.getName();
   if (eagerValidation && sigFileSigners.get(name) != null) {
     /*
      * Force a read of the entry data to generate the
      * verification hash.
      */
     try {
       InputStream s = jar.getInputStream(entry);
       byte[] buffer = new byte[1024];
       int n = buffer.length;
       while (n != -1) {
         n = s.read(buffer, 0, buffer.length);
       }
       s.close();
     } catch (IOException e) {
     }
   }
   return getCodeSigners(name);
 }
Example #23
0
 /**
  * Parses single JAR entry with the specified name.
  *
  * @param jarEntry JAR entry
  * @param entryName JAR entry name
  * @param zipEntry ZIP entry
  */
 private static void parseElement(
     final JarEntry jarEntry, final String entryName, final ZipEntry zipEntry) {
   final String[] path = entryName.split("/");
   JarEntry currentLevel = jarEntry;
   for (int i = 0; i < path.length; i++) {
     if (i < path.length - 1) {
       // We are getting deeper into packages
       JarEntry child = currentLevel.getChildByName(path[i]);
       if (child == null) {
         child = new JarEntry(JarEntryType.packageEntry, path[i], currentLevel);
         child.setZipEntry(zipEntry);
         currentLevel.addChild(child);
       }
       currentLevel = child;
     } else {
       // We reached last element
       final JarEntry newEntry = new JarEntry(getJarEntryType(path[i]), path[i], currentLevel);
       newEntry.setZipEntry(zipEntry);
       currentLevel.addChild(newEntry);
     }
   }
 }
Example #24
0
  /**
   * Enables to get the name of an object in the spoken language thanks to the jar file
   *
   * @param jarName the file that contains the object classes
   * @return the object name in the spoken language
   */
  private String getLangName(File jarName) {
    String name = null;

    try {
      URL url = jarName.toURI().toURL();
      JarInputStream jarFile = new JarInputStream(url.openStream());
      JarEntry jarEntry = jarFile.getNextJarEntry();
      while (jarEntry != null) {
        if (!jarEntry.isDirectory()
            && jarEntry.getName().contains(Configuration.instance().getLanguage())) {
          int lang_index = jarEntry.getName().lastIndexOf(Configuration.instance().getLanguage());
          name = jarEntry.getName().substring(lang_index + 3, jarEntry.getName().length() - 6);
        }
        jarEntry = jarFile.getNextJarEntry();
      }
    } catch (Exception e) {
      LOG.error("Error getLangName " + jarName + " " + e);
    }
    return name;
  }
Example #25
0
 private static String trimEntryName(JarEntry je) {
   String[] s = je.getName().toLowerCase().replace(".htm", "").split("/");
   return s[s.length - 1];
 }
Example #26
0
 public JarEntry nextElement() {
   JarEntry je = new JarEntry(ze.nextElement());
   je.parentJar = jf;
   return je;
 }
Example #27
0
    private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "0");
      // Process the output directory or jar output.
      new PackageReader(pkg, in).read();

      if (_props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
      if (_props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "50");
      pkg.ensureAllClassFiles();
      // Now write out the files.
      HashSet classesToWrite = new HashSet(pkg.getClasses());
      for (Iterator i = pkg.getFiles().iterator(); i.hasNext(); ) {
        Package.File file = (Package.File) i.next();
        String name = file.nameString;
        JarEntry je = new JarEntry(Utils.getJarEntryName(name));
        boolean deflate;

        deflate =
            (keepDeflateHint)
                ? (((file.options & Constants.FO_DEFLATE_HINT) != 0)
                    || ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
                : deflateHint;

        boolean needCRC = !deflate; // STORE mode requires CRC

        if (needCRC) crc.reset();
        bufOut.reset();
        if (file.isClassStub()) {
          Package.Class cls = file.getStubClass();
          assert (cls != null);
          new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
          classesToWrite.remove(cls); // for an error check
        } else {
          // collect data & maybe CRC
          file.writeTo(needCRC ? crcOut : bufOut);
        }
        je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
        if (needCRC) {
          if (verbose > 0)
            Utils.log.info("stored size=" + bufOut.size() + " and crc=" + crc.getValue());

          je.setMethod(JarEntry.STORED);
          je.setSize(bufOut.size());
          je.setCrc(crc.getValue());
        }
        if (keepModtime) {
          je.setTime(file.modtime);
          // Convert back to milliseconds
          je.setTime((long) file.modtime * 1000);
        } else {
          je.setTime((long) modtime * 1000);
        }
        out.putNextEntry(je);
        bufOut.writeTo(out);
        out.closeEntry();
        if (verbose > 0) Utils.log.info("Writing " + Utils.zeString((ZipEntry) je));
      }
      assert (classesToWrite.isEmpty());
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "100");
      pkg.reset(); // reset for the next segment, if any
    }
 public java.security.cert.Certificate[] getCertificates() {
   Certificate[] certs = je.getCertificates();
   return certs == null ? null : (Certificate[]) certs.clone();
 }
Example #29
0
  /** Creates a patch from the two passed in files, writing the result to <code>os</code>. */
  public static void createPatch(String oldPath, String newPath, OutputStream os, boolean minimal)
      throws IOException {
    JarFile2 oldJar = new JarFile2(oldPath);
    JarFile2 newJar = new JarFile2(newPath);

    try {
      Iterator entries;
      HashMap moved = new HashMap();
      HashSet visited = new HashSet();
      HashSet implicit = new HashSet();
      HashSet moveSrc = new HashSet();
      HashSet newEntries = new HashSet();

      // FIRST PASS
      // Go through the entries in new jar and
      // determine which files are candidates for implicit moves
      // ( files that has the same filename and same content in old.jar
      // and new.jar )
      // and for files that cannot be implicitly moved, we will either
      // find out whether it is moved or new (modified)
      entries = newJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry newEntry = (JarEntry) entries.next();
          String newname = newEntry.getName();

          // Return best match of contents, will return a name match if possible
          String oldname = oldJar.getBestMatch(newJar, newEntry);
          if (oldname == null) {
            // New or modified entry
            if (_debug) {
              System.out.println("NEW: " + newname);
            }
            newEntries.add(newname);
          } else {
            // Content already exist - need to do a move

            // Should do implicit move? Yes, if names are the same, and
            // no move command already exist from oldJar
            if (oldname.equals(newname) && !moveSrc.contains(oldname)) {
              if (_debug) {
                System.out.println(newname + " added to implicit set!");
              }
              implicit.add(newname);
            } else {
              // The 1.0.1/1.0 JarDiffPatcher cannot handle
              // multiple MOVE command with same src.
              // The work around here is if we are going to generate
              // a MOVE command with duplicate src, we will
              // instead add the target as a new file.  This way
              // the jardiff can be applied by 1.0.1/1.0
              // JarDiffPatcher also.
              if (!minimal && (implicit.contains(oldname) || moveSrc.contains(oldname))) {

                // generate non-minimal jardiff
                // for backward compatibility

                if (_debug) {

                  System.out.println("NEW: " + newname);
                }
                newEntries.add(newname);
              } else {
                // Use newname as key, since they are unique
                if (_debug) {
                  System.err.println("moved.put " + newname + " " + oldname);
                }
                moved.put(newname, oldname);
                moveSrc.add(oldname);
              }
              // Check if this disables an implicit 'move <oldname> <oldname>'
              if (implicit.contains(oldname) && minimal) {

                if (_debug) {
                  System.err.println("implicit.remove " + oldname);

                  System.err.println("moved.put " + oldname + " " + oldname);
                }
                implicit.remove(oldname);
                moved.put(oldname, oldname);
                moveSrc.add(oldname);
              }
            }
          }
        }
      } // if (entries != null)

      // SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
      // <source of move commands> - <new or modified entries>
      ArrayList deleted = new ArrayList();
      entries = oldJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry oldEntry = (JarEntry) entries.next();
          String oldName = oldEntry.getName();
          if (!implicit.contains(oldName)
              && !moveSrc.contains(oldName)
              && !newEntries.contains(oldName)) {
            if (_debug) {
              System.err.println("deleted.add " + oldName);
            }
            deleted.add(oldName);
          }
        }
      }

      // DEBUG
      if (_debug) {
        // DEBUG:  print out moved map
        entries = moved.keySet().iterator();
        if (entries != null) {
          System.out.println("MOVED MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            String oldName = (String) moved.get(newName);
            System.out.println("key is " + newName + " value is " + oldName);
          }
        }

        // DEBUG:  print out IMOVE map
        entries = implicit.iterator();
        if (entries != null) {
          System.out.println("IMOVE MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            System.out.println("key is " + newName);
          }
        }
      }

      JarOutputStream jos = new JarOutputStream(os);

      // Write out all the MOVEs and REMOVEs
      createIndex(jos, deleted, moved);

      // Put in New and Modified entries
      entries = newEntries.iterator();
      if (entries != null) {

        while (entries.hasNext()) {
          String newName = (String) entries.next();
          if (_debug) {
            System.out.println("New File: " + newName);
          }
          writeEntry(jos, newJar.getEntryByName(newName), newJar);
        }
      }

      jos.finish();
      jos.close();

    } catch (IOException ioE) {
      throw ioE;
    } finally {
      try {
        oldJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
      try {
        newJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
    } // finally
  }
  /** called when we reach the end of entry in one of the read() methods. */
  private void processEntry(ManifestEntryVerifier mev) throws IOException {
    if (!parsingBlockOrSF) {
      JarEntry je = mev.getEntry();
      if ((je != null) && (je.signers == null)) {
        je.signers = mev.verify(verifiedSigners, sigFileSigners);
        je.certs = mapSignersToCertArray(je.signers);
      }
    } else {

      try {
        parsingBlockOrSF = false;

        if (debug != null) {
          debug.println("processEntry: processing block");
        }

        String uname = mev.getEntry().getName().toUpperCase(Locale.ENGLISH);

        if (uname.endsWith(".SF")) {
          String key = uname.substring(0, uname.length() - 3);
          byte bytes[] = baos.toByteArray();
          // add to sigFileData in case future blocks need it
          sigFileData.put(key, bytes);
          // check pending blocks, we can now process
          // anyone waiting for this .SF file
          Iterator it = pendingBlocks.iterator();
          while (it.hasNext()) {
            SignatureFileVerifier sfv = (SignatureFileVerifier) it.next();
            if (sfv.needSignatureFile(key)) {
              if (debug != null) {
                debug.println("processEntry: processing pending block");
              }

              sfv.setSignatureFile(bytes);
              sfv.process(sigFileSigners, manifestDigests);
            }
          }
          return;
        }

        // now we are parsing a signature block file

        String key = uname.substring(0, uname.lastIndexOf("."));

        if (signerCache == null) signerCache = new ArrayList();

        if (manDig == null) {
          synchronized (manifestRawBytes) {
            if (manDig == null) {
              manDig = new ManifestDigester(manifestRawBytes);
              manifestRawBytes = null;
            }
          }
        }

        SignatureFileVerifier sfv =
            new SignatureFileVerifier(signerCache, manDig, uname, baos.toByteArray());

        if (sfv.needSignatureFileBytes()) {
          // see if we have already parsed an external .SF file
          byte[] bytes = (byte[]) sigFileData.get(key);

          if (bytes == null) {
            // put this block on queue for later processing
            // since we don't have the .SF bytes yet
            // (uname, block);
            if (debug != null) {
              debug.println("adding pending block");
            }
            pendingBlocks.add(sfv);
            return;
          } else {
            sfv.setSignatureFile(bytes);
          }
        }
        sfv.process(sigFileSigners, manifestDigests);

      } catch (IOException ioe) {
        // e.g. sun.security.pkcs.ParsingException
        if (debug != null) debug.println("processEntry caught: " + ioe);
        // ignore and treat as unsigned
      } catch (SignatureException se) {
        if (debug != null) debug.println("processEntry caught: " + se);
        // ignore and treat as unsigned
      } catch (NoSuchAlgorithmException nsae) {
        if (debug != null) debug.println("processEntry caught: " + nsae);
        // ignore and treat as unsigned
      } catch (CertificateException ce) {
        if (debug != null) debug.println("processEntry caught: " + ce);
        // ignore and treat as unsigned
      }
    }
  }