Example usage for java.util.jar JarEntry getTime

List of usage examples for java.util.jar JarEntry getTime

Introduction

In this page you can find the example usage for java.util.jar JarEntry getTime.

Prototype

public long getTime() 

Source Link

Document

Returns the last modification time of the entry.

Usage

From source file:org.apache.sling.osgi.obr.Repository.java

File spoolModified(InputStream ins) throws IOException {
    JarInputStream jis = new JarInputStream(ins);

    // immediately handle the manifest
    JarOutputStream jos;/* w  w w .  j  av  a  2 s.c  om*/
    Manifest manifest = jis.getManifest();
    if (manifest == null) {
        throw new IOException("Missing Manifest !");
    }

    String symbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName");
    if (symbolicName == null || symbolicName.length() == 0) {
        throw new IOException("Missing Symbolic Name in Manifest !");
    }

    String version = manifest.getMainAttributes().getValue("Bundle-Version");
    Version v = Version.parseVersion(version);
    if (v.getQualifier().indexOf("SNAPSHOT") >= 0) {
        String tStamp;
        synchronized (DATE_FORMAT) {
            tStamp = DATE_FORMAT.format(new Date());
        }
        version = v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + "."
                + v.getQualifier().replaceAll("SNAPSHOT", tStamp);
        manifest.getMainAttributes().putValue("Bundle-Version", version);
    }

    File bundle = new File(this.repoLocation, symbolicName + "-" + v + ".jar");
    OutputStream out = null;
    try {
        out = new FileOutputStream(bundle);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                spool(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();

    } finally {
        IOUtils.closeQuietly(out);
    }

    return bundle;
}

From source file:org.dbmaintain.script.repository.impl.ArchiveScriptLocation.java

protected SortedSet<Script> loadScriptsFromJar(final JarFile jarFile, String subPath) {
    SortedSet<Script> scripts = new TreeSet<Script>();
    for (Enumeration<JarEntry> jarEntries = jarFile.entries(); jarEntries.hasMoreElements();) {
        final JarEntry jarEntry = jarEntries.nextElement();
        String fileName = jarEntry.getName();
        if (LOCATION_PROPERTIES_FILENAME.equals(fileName) || !isScriptFileName(fileName)) {
            continue;
        }/*from  w  w  w .  ja  va 2  s .  c  o  m*/

        String relativeScriptName = jarEntry.getName();
        if (subPath != null) {
            if (!fileName.startsWith(subPath)) {
                continue;
            }
            relativeScriptName = relativeScriptName.substring(subPath.length());
        }
        ScriptContentHandle scriptContentHandle = new ScriptContentHandle(scriptEncoding,
                ignoreCarriageReturnsWhenCalculatingCheckSum) {
            @Override
            protected InputStream getScriptInputStream() {
                try {
                    return jarFile.getInputStream(jarEntry);
                } catch (IOException e) {
                    throw new DbMaintainException("Error while reading jar entry " + jarEntry, e);
                }
            }
        };
        Long fileLastModifiedAt = jarEntry.getTime();
        Script script = scriptFactory.createScriptWithContent(relativeScriptName, fileLastModifiedAt,
                scriptContentHandle);
        scripts.add(script);
    }
    return scripts;
}

From source file:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

static JarEntry cloneEntry(JarEntry originalJarEntry) {
    final JarEntry newJarEntry = new JarEntry(originalJarEntry.getName());
    newJarEntry.setComment(originalJarEntry.getComment());
    newJarEntry.setExtra(originalJarEntry.getExtra());
    newJarEntry.setMethod(originalJarEntry.getMethod());
    newJarEntry.setTime(originalJarEntry.getTime());

    // Must set size and CRC for STORED entries
    if (newJarEntry.getMethod() == ZipEntry.STORED) {
        newJarEntry.setSize(originalJarEntry.getSize());
        newJarEntry.setCrc(originalJarEntry.getCrc());
    }/*from   w w  w .  ja  va  2  s . co  m*/

    return newJarEntry;
}

From source file:org.springframework.boot.gradle.tasks.bundling.AbstractBootArchiveTests.java

@Test
public void fileTimestampPreservationCanBeDisabled() throws IOException {
    this.task.setMainClassName("com.example.Main");
    this.task.setPreserveFileTimestamps(false);
    this.task.execute();
    assertThat(this.task.getArchivePath()).exists();
    try (JarFile jarFile = new JarFile(this.task.getArchivePath())) {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            assertThat(entry.getTime()).isEqualTo(BootZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES);
        }// w  w w . j a va  2  s .  c  o m
    }
}

From source file:org.springframework.boot.loader.tools.JarWriter.java

private long getNestedLibraryTime(File file) {
    try {//w  w  w  .j  a  v  a2  s.  co m
        try (JarFile jarFile = new JarFile(file)) {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    return entry.getTime();
                }
            }
        }
    } catch (Exception ex) {
        // Ignore and just use the source file timestamp
    }
    return file.lastModified();
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void libraries() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class, JAN_1_1985);
    File libJarFile = libJar.getFile();
    File libJarFileToUnpack = libJar.getFile();
    File libNonJarFile = this.temporaryFolder.newFile();
    FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    this.testJarFile.addFile("BOOT-INF/lib/" + libJarFileToUnpack.getName(), libJarFileToUnpack);
    File file = this.testJarFile.getFile();
    libJarFile.setLastModified(JAN_1_1980);
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> {
        callback.library(new Library(libJarFile, LibraryScope.COMPILE));
        callback.library(new Library(libJarFileToUnpack, LibraryScope.COMPILE, true));
        callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
    });/* www  .jav  a2 s . c o  m*/
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFile.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libNonJarFile.getName())).isFalse();
    JarEntry entry = getEntry(file, "BOOT-INF/lib/" + libJarFile.getName());
    assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
    entry = getEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName());
    assertThat(entry.getComment()).startsWith("UNPACK:");
    assertThat(entry.getComment().length()).isEqualTo(47);
}

From source file:org.tobarsegais.webapp.ContentServlet.java

@Override
protected long getLastModified(HttpServletRequest req) {
    String path = req.getPathInfo();
    if (path == null) {
        path = req.getServletPath();//from www  .  j  a  v  a 2  s.com
    }
    int index = path.indexOf(PLUGINS_ROOT);
    if (index != -1) {
        path = path.substring(index + PLUGINS_ROOT.length() - 1);
    }
    Map<String, String> bundles = (Map<String, String>) getServletContext().getAttribute("bundles");
    try {
        for (index = path.indexOf('/'); index != -1; index = path.indexOf('/', index + 1)) {
            String key = path.substring(0, index);
            if (key.startsWith("/")) {
                key = key.substring(1);
            }
            if (bundles.containsKey(key)) {
                key = bundles.get(key);
            }
            URL resource = getServletContext()
                    .getResource(ServletContextListenerImpl.BUNDLE_PATH + "/" + key + ".jar");
            if (resource == null) {
                continue;
            }
            URL jarResource = new URL("jar:" + resource + "!/");
            URLConnection connection = jarResource.openConnection();
            if (!(connection instanceof JarURLConnection)) {
                continue;
            }
            JarURLConnection jarConnection = (JarURLConnection) connection;
            JarFile jarFile = jarConnection.getJarFile();
            try {
                int endOfFileName = path.indexOf('#', index);
                endOfFileName = endOfFileName == -1 ? path.length() : endOfFileName;
                String fileName = path.substring(index + 1, endOfFileName);
                JarEntry jarEntry = jarFile.getJarEntry(fileName);
                if (jarEntry == null) {
                    continue;
                }
                return jarEntry.getTime();
            } finally {
                //jarFile.close();
            }
        }
    } catch (IOException e) {
        // ignore
    }
    return -1;
}