Search in sources :

Example 11 with FileTime

use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.

the class Basic method neq.

static void neq(Instant ins, long v2, TimeUnit u2) {
    FileTime t1 = FileTime.from(ins);
    FileTime t2 = FileTime.from(v2, u2);
    if (t1.equals(t2))
        throw new RuntimeException("should not be equal");
}
Also used : FileTime(java.nio.file.attribute.FileTime)

Example 12 with FileTime

use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.

the class TestExtraTime method main.

public static void main(String[] args) throws Throwable {
    File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
    if (src.exists()) {
        long time = src.lastModified();
        FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
        FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
        FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        for (byte[] extra : new byte[][] { null, new byte[] { 1, 2, 3 } }) {
            test(mtime, null, null, null, extra);
            // ms-dos 1980 epoch problem
            test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
            // non-default tz
            test(mtime, null, null, tz, extra);
            test(mtime, atime, null, null, extra);
            test(mtime, null, ctime, null, extra);
            test(mtime, atime, ctime, null, extra);
            test(mtime, atime, null, tz, extra);
            test(mtime, null, ctime, tz, extra);
            test(mtime, atime, ctime, tz, extra);
        }
    }
    testNullHandling();
    testTagOnlyHandling();
    testTimeConversions();
}
Also used : TimeZone(java.util.TimeZone) FileTime(java.nio.file.attribute.FileTime) ZipFile(java.util.zip.ZipFile)

Example 13 with FileTime

use of java.nio.file.attribute.FileTime in project graylog2-server by Graylog2.

the class WebInterfaceAssetsResource method getResponse.

private Response getResponse(Request request, String filename, URL resourceUrl, boolean fromPlugin) throws IOException, URISyntaxException {
    final Date lastModified;
    final InputStream stream;
    final HashCode hashCode;
    switch(resourceUrl.getProtocol()) {
        case "file":
            final File file = new File(resourceUrl.toURI());
            lastModified = new Date(file.lastModified());
            stream = new FileInputStream(file);
            hashCode = Files.hash(file, Hashing.sha256());
            break;
        case "jar":
            final URI uri = resourceUrl.toURI();
            final FileSystem fileSystem = fileSystemCache.getUnchecked(uri);
            final java.nio.file.Path path = fileSystem.getPath(pluginPrefixFilename(fromPlugin, filename));
            final FileTime lastModifiedTime = java.nio.file.Files.getLastModifiedTime(path);
            lastModified = new Date(lastModifiedTime.toMillis());
            stream = resourceUrl.openStream();
            hashCode = Resources.asByteSource(resourceUrl).hash(Hashing.sha256());
            break;
        default:
            throw new IllegalArgumentException("Not a JAR or local file: " + resourceUrl);
    }
    final EntityTag entityTag = new EntityTag(hashCode.toString());
    final Response.ResponseBuilder response = request.evaluatePreconditions(lastModified, entityTag);
    if (response != null) {
        return response.build();
    }
    final String contentType = firstNonNull(mimeTypes.getContentType(filename), MediaType.APPLICATION_OCTET_STREAM);
    final CacheControl cacheControl = new CacheControl();
    cacheControl.setMaxAge((int) TimeUnit.DAYS.toSeconds(365));
    cacheControl.setNoCache(false);
    cacheControl.setPrivate(false);
    return Response.ok(stream).header(HttpHeaders.CONTENT_TYPE, contentType).tag(entityTag).cacheControl(cacheControl).lastModified(lastModified).build();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileTime(java.nio.file.attribute.FileTime) URI(java.net.URI) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) HashCode(com.google.common.hash.HashCode) FileSystem(java.nio.file.FileSystem) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) File(java.io.File)

Example 14 with FileTime

use of java.nio.file.attribute.FileTime in project lucene-solr by apache.

the class SimpleFSLockFactory method obtainFSLock.

@Override
protected Lock obtainFSLock(FSDirectory dir, String lockName) throws IOException {
    Path lockDir = dir.getDirectory();
    // Ensure that lockDir exists and is a directory.
    // note: this will fail if lockDir is a symlink
    Files.createDirectories(lockDir);
    Path lockFile = lockDir.resolve(lockName);
    // create the file: this will fail if it already exists
    try {
        Files.createFile(lockFile);
    } catch (FileAlreadyExistsException | AccessDeniedException e) {
        // convert optional specific exception to our optional specific exception
        throw new LockObtainFailedException("Lock held elsewhere: " + lockFile, e);
    }
    // used as a best-effort check, to see if the underlying file has changed
    final FileTime creationTime = Files.readAttributes(lockFile, BasicFileAttributes.class).creationTime();
    return new SimpleFSLock(lockFile, creationTime);
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 15 with FileTime

use of java.nio.file.attribute.FileTime in project sakuli by ConSol.

the class LogCleanUpResultServiceImplTest method testTriggerAction.

@Test
public void testTriggerAction() throws Exception {
    Files.createDirectories(tempLog);
    Path folder1 = Files.createDirectories(tempLog.resolve("folder1"));
    Path today = Files.createFile(folder1.resolve("today.log"));
    FileTime todayMinus15 = FileTime.from(Instant.now().minus(15, ChronoUnit.DAYS));
    Path toOld1 = createFileWithDate(folder1.resolve("today-15.log"), todayMinus15);
    Path toOld2 = createFileWithDate(tempLog.resolve("root-today-15.log"), todayMinus15);
    FileTime todayMinus13 = FileTime.from(Instant.now().minus(13, ChronoUnit.DAYS));
    Path notToOld = createFileWithDate(folder1.resolve("today-13.log"), todayMinus13);
    Path notToOld2 = createFileWithDate(tempLog.resolve("today-13.log"), todayMinus13);
    when(sakuliProperties.getLogFolder()).thenReturn(tempLog);
    testling.triggerAction();
    verify(testling, times(2)).cleanUpDirectory(any());
    assertTrue(Files.exists(folder1));
    assertTrue(Files.exists(today));
    assertTrue(Files.exists(notToOld));
    assertTrue(Files.exists(notToOld2));
    assertFalse(Files.exists(toOld1));
    assertFalse(Files.exists(toOld2));
}
Also used : Path(java.nio.file.Path) FileTime(java.nio.file.attribute.FileTime) BaseTest(org.sakuli.BaseTest) Test(org.testng.annotations.Test)

Aggregations

FileTime (java.nio.file.attribute.FileTime)40 Path (java.nio.file.Path)11 File (java.io.File)7 Test (org.junit.Test)7 Test (org.testng.annotations.Test)7 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 IOException (java.io.IOException)5 SegmentMetadataImpl (com.linkedin.pinot.core.segment.index.SegmentMetadataImpl)4 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 ProcessResult (com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult)2 IndexSegment (com.linkedin.pinot.core.indexsegment.IndexSegment)2 SegmentV1V2ToV3FormatConverter (com.linkedin.pinot.core.segment.index.converter.SegmentV1V2ToV3FormatConverter)2 FileNotFoundException (java.io.FileNotFoundException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Date (java.util.Date)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1