Example usage for java.util.jar JarFile getEntry

List of usage examples for java.util.jar JarFile getEntry

Introduction

In this page you can find the example usage for java.util.jar JarFile getEntry.

Prototype

public ZipEntry getEntry(String name) 

Source Link

Document

Returns the ZipEntry for the given base entry name or null if not found.

Usage

From source file:org.openmrs.util.OpenmrsUtil.java

/**
 * Opens input stream for given resource. This method behaves differently for different URL
 * types:/*from  www .  j a  v  a 2s  . com*/
 * <ul>
 * <li>for <b>local files</b> it returns buffered file input stream;</li>
 * <li>for <b>local JAR files</b> it reads resource content into memory buffer and returns byte
 * array input stream that wraps those buffer (this prevents locking JAR file);</li>
 * <li>for <b>common URL's</b> this method simply opens stream to that URL using standard URL
 * API.</li>
 * </ul>
 * It is not recommended to use this method for big resources within JAR files.
 * 
 * @param url resource URL
 * @return input stream for given resource
 * @throws IOException if any I/O error has occurred
 */
public static InputStream getResourceInputStream(final URL url) throws IOException {
    File file = url2file(url);
    if (file != null) {
        return new BufferedInputStream(new FileInputStream(file));
    }
    if (!"jar".equalsIgnoreCase(url.getProtocol())) {
        return url.openStream();
    }
    String urlStr = url.toExternalForm();
    if (urlStr.endsWith("!/")) {
        // JAR URL points to a root entry
        throw new FileNotFoundException(url.toExternalForm());
    }
    int p = urlStr.indexOf("!/");
    if (p == -1) {
        throw new MalformedURLException(url.toExternalForm());
    }
    String path = urlStr.substring(p + 2);
    file = url2file(new URL(urlStr.substring(4, p)));
    if (file == null) {// non-local JAR file URL
        return url.openStream();
    }
    JarFile jarFile = new JarFile(file);
    try {
        ZipEntry entry = jarFile.getEntry(path);
        if (entry == null) {
            throw new FileNotFoundException(url.toExternalForm());
        }
        InputStream in = jarFile.getInputStream(entry);
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            copyFile(in, out);
            return new ByteArrayInputStream(out.toByteArray());
        } finally {
            in.close();
        }
    } finally {
        jarFile.close();
    }
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlModuleDefaultJavaFile() throws IOException {
    compile("modules/def/JavaClass.java");

    File carFile = getModuleArchive("default", null);
    assertTrue(carFile.exists());/*www  . j a v a2 s  .  c  o m*/

    JarFile car = new JarFile(carFile);

    ZipEntry moduleClass = car.getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/def/JavaClass.class");
    assertNotNull(moduleClass);
    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlModuleDefault() throws IOException {
    compile("modules/def/CeylonClass.ceylon");

    File carFile = getModuleArchive("default", null);
    assertTrue(carFile.exists());//from   w ww  .jav  a 2 s  .co  m

    JarFile car = new JarFile(carFile);

    ZipEntry moduleClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/def/CeylonClass.class");
    assertNotNull(moduleClass);
    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlOsgiManifestDisabled() throws IOException {
    ErrorCollector c = new ErrorCollector();
    List<String> options = new ArrayList<String>(defaultOptions.size() + 1);
    options.addAll(defaultOptions);//from w w  w . j a  va 2  s  .c  o m
    options.add("-noosgi");
    assertCompilesOk(c, getCompilerTask(options, c, "modules/osgi/a/module.ceylon",
            "modules/osgi/a/package.ceylon", "modules/osgi/a/A.ceylon").call2());

    final String moduleName = "com.redhat.ceylon.compiler.java.test.cmr.modules.osgi.a";
    final String moduleVersion = "1.1.0";

    File carFile = getModuleArchive(moduleName, moduleVersion);
    JarFile car = new JarFile(carFile);

    ZipEntry manifest = car.getEntry(OsgiUtil.OsgiManifest.MANIFEST_FILE_NAME);
    assertNull(manifest);

    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlNoPomManifest() throws IOException {
    ErrorCollector c = new ErrorCollector();
    assertCompilesOk(c, getCompilerTask(Arrays.asList("-nopom", "-out", destDir), c,
            "modules/pom/a/module.ceylon", "modules/pom/b/module.ceylon").call2());

    final String moduleName = "com.redhat.ceylon.compiler.java.test.cmr.modules.pom.b";
    final String moduleVersion = "1";

    File carFile = getModuleArchive(moduleName, moduleVersion);
    assertTrue(carFile.exists());//from   w  w w . ja v  a2 s . c  o  m
    JarFile car = new JarFile(carFile);

    ZipEntry pomFile = car
            .getEntry("META-INF/maven/com.redhat.ceylon.compiler.java.test.cmr.modules.pom/b/pom.xml");
    assertNull(pomFile);

    ZipEntry propertiesFile = car
            .getEntry("META-INF/maven/com.redhat.ceylon.compiler.java.test.cmr.modules.pom/b/pom.properties");
    assertNull(propertiesFile);
    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlCompilerGeneratesModuleForValidUnits() throws IOException {
    CeyloncTaskImpl compilerTask = getCompilerTask("modules/single/module.ceylon",
            "modules/single/Correct.ceylon", "modules/single/Invalid.ceylon");
    Boolean success = compilerTask.call();
    assertFalse(success);//from w ww . ja  v  a  2 s. c o  m

    File carFile = getModuleArchive("com.redhat.ceylon.compiler.java.test.cmr.modules.single", "6.6.6");
    assertTrue(carFile.exists());

    JarFile car = new JarFile(carFile);

    ZipEntry moduleClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/$module_.class");
    assertNotNull(moduleClass);

    ZipEntry correctClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/Correct.class");
    assertNotNull(correctClass);

    ZipEntry invalidClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/Invalid.class");
    assertNull(invalidClass);

    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlPomManifest() throws IOException {
    compile("modules/pom/a/module.ceylon", "modules/pom/b/module.ceylon");

    final String moduleName = "com.redhat.ceylon.compiler.java.test.cmr.modules.pom.b";
    final String moduleVersion = "1";

    File carFile = getModuleArchive(moduleName, moduleVersion);
    assertTrue(carFile.exists());/*from  w  ww  .  j a v a  2  s . c  om*/
    JarFile car = new JarFile(carFile);

    ZipEntry pomFile = car
            .getEntry("META-INF/maven/com.redhat.ceylon.compiler.java.test.cmr.modules.pom/b/pom.xml");
    assertNotNull(pomFile);
    String pomContents = read(car, pomFile);
    assertEquals("<?xml version=\"1.0\" ?>\n"
            + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n"
            + " <modelVersion>4.0.0</modelVersion>\n"
            + " <groupId>com.redhat.ceylon.compiler.java.test.cmr.modules.pom</groupId>\n"
            + " <artifactId>b</artifactId>\n" + " <version>1</version>\n"
            + " <name>com.redhat.ceylon.compiler.java.test.cmr.modules.pom.b</name>\n" + " <dependencies>\n"
            + "  <dependency>\n"
            + "    <groupId>com.redhat.ceylon.compiler.java.test.cmr.modules.pom</groupId>\n"
            + "    <artifactId>a</artifactId>\n" + "    <version>1</version>\n" + "  </dependency>\n"
            + " </dependencies>\n" + "</project>\n", pomContents);

    ZipEntry propertiesFile = car
            .getEntry("META-INF/maven/com.redhat.ceylon.compiler.java.test.cmr.modules.pom/b/pom.properties");
    assertNotNull(propertiesFile);
    String propertiesContents = read(car, propertiesFile);
    // remove the date comment
    propertiesContents = propertiesContents.replaceFirst("^(#Generated by Ceylon\n)#[^\n]+\n", "$1");
    assertEquals(
            "#Generated by Ceylon\n" + "version=1\n"
                    + "groupId=com.redhat.ceylon.compiler.java.test.cmr.modules.pom\n" + "artifactId=b\n",
            propertiesContents);
    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlByName() throws IOException {
    List<String> options = new LinkedList<String>();
    options.add("-src");
    options.add(getPackagePath() + "/modules/byName");
    options.addAll(defaultOptions);/* ww  w . j  av a  2 s.c  om*/
    CeyloncTaskImpl task = getCompilerTask(options, null, Arrays.asList("default", "mod"));
    Boolean ret = task.call();
    assertTrue(ret);

    File carFile = getModuleArchive("default", null);
    assertTrue(carFile.exists());

    JarFile car = new JarFile(carFile);

    ZipEntry moduleClass = car.getEntry("def/Foo.class");
    assertNotNull(moduleClass);
    ZipEntry moduleClassDir = car.getEntry("def/");
    assertNotNull(moduleClassDir);
    assertTrue(moduleClassDir.isDirectory());

    car.close();

    carFile = getModuleArchive("mod", "1");
    assertTrue(carFile.exists());

    car = new JarFile(carFile);

    moduleClass = car.getEntry("mod/$module_.class");
    assertNotNull(moduleClass);
    moduleClassDir = car.getEntry("mod/");
    assertNotNull(moduleClassDir);
    assertTrue(moduleClassDir.isDirectory());

    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Test
public void testMdlModuleFromCompiledModule() throws IOException {
    compile("modules/single/module.ceylon");

    File carFile = getModuleArchive("com.redhat.ceylon.compiler.java.test.cmr.modules.single", "6.6.6");
    assertTrue(carFile.exists());//from   w  w w. j a  v a 2  s .c  o  m

    JarFile car = new JarFile(carFile);
    // just to be sure
    ZipEntry bogusEntry = car.getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/BOGUS");
    assertNull(bogusEntry);

    ZipEntry moduleClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/$module_.class");
    assertNotNull(moduleClass);
    car.close();

    compile("modules/single/subpackage/Subpackage.ceylon");

    // MUST reopen it
    car = new JarFile(carFile);

    ZipEntry subpackageClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/subpackage/Subpackage.class");
    assertNotNull(subpackageClass);

    car.close();
}

From source file:com.redhat.ceylon.compiler.java.test.cmr.CMRTests.java

@Ignore("See https://github.com/ceylon/ceylon/issues/6027")
@Test//www  . ja va2s  .c  o m
public void testMdlCarWithInvalidSHA1() throws IOException {
    compile("modules/single/module.ceylon");

    File carFile = getModuleArchive("com.redhat.ceylon.compiler.java.test.cmr.modules.single", "6.6.6");
    assertTrue(carFile.exists());

    JarFile car = new JarFile(carFile);
    // just to be sure
    ZipEntry moduleClass = car
            .getEntry("com/redhat/ceylon/compiler/java/test/cmr/modules/single/$module_.class");
    assertNotNull(moduleClass);
    car.close();

    // now let's break the SHA1
    File shaFile = getArchiveName("com.redhat.ceylon.compiler.java.test.cmr.modules.single", "6.6.6", destDir,
            "car.sha1");
    Writer w = new FileWriter(shaFile);
    w.write("fubar");
    w.flush();
    w.close();

    // now try to compile the subpackage with a broken SHA1
    String carName = "/com/redhat/ceylon/compiler/java/test/cmr/modules/single/6.6.6/com.redhat.ceylon.compiler.java.test.cmr.modules.single-6.6.6.car";
    carName = carName.replace('/', File.separatorChar);
    assertErrors("modules/single/subpackage/Subpackage", new CompilerError(-1, "Module car " + carName
            + " obtained from repository " + (new File(destDir).getAbsolutePath())
            + " has an invalid SHA1 signature: you need to remove it and rebuild the archive, since it may be corrupted."));
}