Example usage for java.io File setLastModified

List of usage examples for java.io File setLastModified

Introduction

In this page you can find the example usage for java.io File setLastModified.

Prototype

public boolean setLastModified(long time) 

Source Link

Document

Sets the last-modified time of the file or directory named by this abstract pathname.

Usage

From source file:org.apache.jackrabbit.core.data.LocalCache.java

public synchronized File getFileIfStored(String fileName) throws IOException {
    fileName = fileName.replace("\\", "/");
    File f = getFile(fileName);
    // return file in purge mode = true and file present in asyncUploadCache
    // as asyncUploadCache's files will be not be deleted in cache purge.
    if (!f.exists() || (isInPurgeMode() && !asyncUploadCache.hasEntry(fileName, false))) {
        LOG.debug("getFileIfStored returned: purgeMode=[{}], file=[{}] exists=[{}]",
                new Object[] { isInPurgeMode(), fileName, f.exists() });
        return null;
    } else {/*from  w  w w  .  j  a  va  2  s  .  c  om*/
        // touch entry in LRU caches
        cache.put(fileName, f.length());
        f.setLastModified(System.currentTimeMillis());
        return f;
    }
}

From source file:org.appcelerator.titanium.util.TiResponseCache.java

@Override
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
    if (uri == null || cacheDir == null)
        return null;

    // Get our key, which is a hash of the URI
    String hash = DigestUtils.shaHex(uri.toString());

    // Make our cache files
    File hFile = new File(cacheDir, hash + HEADER_SUFFIX);
    File bFile = new File(cacheDir, hash + BODY_SUFFIX);

    if (!bFile.exists() || !hFile.exists()) {
        return null;
    }//from w  w w . j a  va2 s.  c o  m

    // Read in the headers
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    BufferedReader rdr = new BufferedReader(new FileReader(hFile), 1024);
    for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
        String keyval[] = line.split("=", 2);
        if (!headers.containsKey(keyval[0])) {
            headers.put(keyval[0], new ArrayList<String>());
        }
        headers.get(keyval[0]).add(keyval[1]);
    }
    rdr.close();

    // Update the access log
    hFile.setLastModified(System.currentTimeMillis());

    // Respond with the cache
    return new TiCacheResponse(headers, new FileInputStream(bFile));
}

From source file:com.googlecode.onevre.utils.ServerClassLoader.java

private URL getResourceURL(String name) throws IOException {
    URL url = cachedFiles.get(name);

    // If the cached jar is not found, find the class in a remote jar file
    if (url == null) {
        return getRemoteResource(name);
    }/*from   w  ww  .  j a v a2s. c  o  m*/

    // If the cached jar is found, check if it is updated remotely
    File jar = cachedJars.get(url);
    if (System.currentTimeMillis() - jar.lastModified() > CACHE_TIMEOUT) {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        addSslConnection(connection);
        connection.setRequestMethod("HEAD");
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

            long time = connection.getHeaderFieldDate("Last-Modified", System.currentTimeMillis());

            // If the remote jar has been updated,
            // redownload the jar and load it
            if (jar.lastModified() < time) {
                downloadJar(url);
            } else {
                jar.setLastModified(System.currentTimeMillis());
            }
        } else if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            return getRemoteResource(name);
        } else {
            throw new IOException("Connection Error: " + connection.getResponseCode() + " "
                    + connection.getResponseMessage());
        }
    }

    return url;
}

From source file:org.isatools.isatab.export.isatab.filesdispatching.ISATabExportDataFilesDispatcher.java

/**
 * Dispatch the files about a given assay group
 *
 * @param outPath where the files go, no dispatch done if this is null.
 *//*ww w. jav a  2 s  . co  m*/
private void dispatchAssayGroup(final AssayGroup ag) throws IOException {
    if (ag == null) {
        return;
    }

    SectionInstance assaySectionInstance = ag.getAssaySectionInstance();
    if (assaySectionInstance == null) {
        log.warn("I cannot find any record in the DB for the file: " + ag.getFilePath() + ", hope it's fine");
        return;
    }

    log.debug("Dispatching the files referred by " + ag.getFilePath());

    Study study = ag.getStudy();

    for (Record record : assaySectionInstance.getRecords()) {
        int recSize = record.size();
        for (int fieldIndex = 0; fieldIndex < recSize; fieldIndex++) {
            // TODO: change this so that we get: field name, file-type, file-path
            String[] filePathPair = FormatExporter.getExternalFileValue(record, fieldIndex);

            if (filePathPair == null) {
                continue;
            }
            String fieldHeader = filePathPair[0], srcFileRelPath = filePathPair[1],
                    srcFileType = filePathPair[2];

            if (srcFileRelPath == null) {
                continue;
            }

            AnnotationTypes targetType;
            if ("raw".equals(srcFileType)) {
                targetType = AnnotationTypes.RAW_DATA_FILE_PATH;
            } else if ("processed".equals(srcFileType)) {
                targetType = AnnotationTypes.PROCESSED_DATA_FILE_PATH;
            } else {
                targetType = AnnotationTypes.GENERIC_DATA_FILE_PATH;
            }

            String srcFilePath = dataLocationMgr.getDataLocation(study, ag.getMeasurement(), ag.getTechnology(),
                    targetType);
            if (srcFilePath == null) {
                log.trace("source path not available for the file " + srcFileRelPath);
                continue;
            }

            srcFilePath += "/" + srcFileRelPath;
            File srcFile = new File(srcFilePath);
            srcFilePath = srcFile.getCanonicalPath();
            File targetDir = new File(exportPath);
            String targetFilePath = exportPath + "/" + srcFileRelPath;
            File targetFile = new File(targetFilePath);
            targetFilePath = targetFile.getCanonicalPath();

            if (!srcFile.exists()) {
                log.info("WARNING: Source file '" + srcFilePath + "' / '" + fieldHeader + "' not found");
            } else {
                if (targetFile.exists() && targetFile.lastModified() == srcFile.lastModified()) {
                    log.debug("Not copying " + srcFilePath + "' to '" + targetFilePath
                            + "': they seem to be the same.");
                } else {
                    log.trace("Copying data file '" + fieldHeader + "' / '" + srcFilePath
                            + "' to data repository...");
                    FileUtils.copyFileToDirectory(srcFile, targetDir, true);
                    // Needed cause of a bug in the previous function
                    targetFile.setLastModified(srcFile.lastModified());
                    log.trace("...done");
                    log.info("Data file '" + fieldHeader + "' / '" + srcFilePath + "' copied to '"
                            + targetFilePath + "'");
                }
            } // if
        } // for ( field )
    } // for ( record )
}

From source file:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method when using file content.
 *///  w  w  w  .  j a va 2s  .  co  m
public void test_calculateContent() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(content.getBytes())));

        etag = new FileETag();
        etag.setFlags(FileETag.FLAG_CONTENT);
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method using the default characteristics: file last
 * modified and file size./*from w w  w.  j  av  a  2  s  . com*/
 */
public void test_calculateDefaultLastModifiedAndSize() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        StringBuffer buffer = new StringBuffer();
        buffer.append(lastModified);
        buffer.append(content.length());
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes())));

        etag = new FileETag();
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method when using file last modified.
 */// w ww .  j a v a  2 s . com
public void test_calculateLastModified() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        StringBuffer buffer = new StringBuffer();
        buffer.append(lastModified);
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes())));

        etag = new FileETag();
        etag.setFlags(FileETag.FLAG_MTIME);
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method when using file size.
 *//* w  w  w . j ava 2s.c om*/
public void test_calculateSize() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        StringBuffer buffer = new StringBuffer();
        buffer.append(content.length());
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes())));

        etag = new FileETag();
        etag.setFlags(FileETag.FLAG_SIZE);
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:com.jpeterson.util.etag.FileETagTest.java

/**
 * Test the calculate method when using both file last modified and file
 * size values.//from  w  w w  . ja v a 2 s. c  o  m
 */
public void test_calculateLastModifiedAndSize() {
    File file;
    String content = "Hello, world!";
    String expected;
    FileETag etag;

    try {
        // create temporary file
        file = File.createTempFile("temp", "txt");

        // make sure that it gets cleaned up
        file.deleteOnExit();

        // put some date in the file
        FileOutputStream out = new FileOutputStream(file);
        out.write(content.getBytes());
        out.flush();
        out.close();

        // manipulate the last modified value to a "known" value
        SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        long lastModified = date.parse("06/21/2007 11:19:36").getTime();
        file.setLastModified(lastModified);

        // determined expected
        StringBuffer buffer = new StringBuffer();
        buffer.append(lastModified);
        buffer.append(content.length());
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        expected = new String(Hex.encodeHex(messageDigest.digest(buffer.toString().getBytes())));

        etag = new FileETag();
        etag.setFlags(FileETag.FLAG_MTIME | FileETag.FLAG_SIZE);
        String value = etag.calculate(file);

        assertEquals("Unexpected value", expected, value);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (IOException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
}