Example usage for java.nio.file Files isSymbolicLink

List of usage examples for java.nio.file Files isSymbolicLink

Introduction

In this page you can find the example usage for java.nio.file Files isSymbolicLink.

Prototype

public static boolean isSymbolicLink(Path path) 

Source Link

Document

Tests whether a file is a symbolic link.

Usage

From source file:org.wrml.runtime.service.file.FileSystemService.java

private File getDataFile(final Keys keys) {

    final Context context = getContext();
    final Set<URI> keyedSchemaUris = keys.getKeyedSchemaUris();
    final URI filedSchemaUri = getFiledSchemaUri(context);
    if (keyedSchemaUris.contains(filedSchemaUri)) {
        final File dataFile = keys.getValue(filedSchemaUri);
        if (dataFile != null) {
            return dataFile;
        }/*  w  w  w .  j ava  2  s .  c om*/
    }

    final Path keyLinkPath = findExistingKeyLinkPath(keys);
    if (keyLinkPath == null) {
        LOG.debug("A key link was NOT found for keys:\n{}", keys);
        return null;
    }

    LOG.debug("A key link \"{}\" was found for keys:\n{}", keyLinkPath, keys);

    if (!Files.isSymbolicLink(keyLinkPath)) {
        final File keyLinkPathFile = keyLinkPath.toFile();
        return keyLinkPathFile;
    }

    if (Files.isSymbolicLink(keyLinkPath)) {
        // Resolve the key symlink to the model's data file.
        try {
            final Path dataFilePath = keyLinkPath.toRealPath();
            return dataFilePath.toFile();
        } catch (final IOException e) {

            LOG.error(e.getMessage(), e);
            throw new ServiceException(
                    "Unable to dereference the key symlink (I/O problem: " + e.getMessage() + ").", e, this);

        }
    }

    return null;

}

From source file:org.panbox.desktop.common.vfs.backend.generic.GenericVirtualFileImpl.java

@Override
public boolean isSymbolic() {
    return Files.isSymbolicLink(getFile().toPath());
}

From source file:org.apache.storm.localizer.AsyncLocalizerTest.java

public void testArchives(File archiveFile, boolean supportSymlinks, int size) throws Exception {
    if (Utils.isOnWindows()) {
        // Windows should set this to false cause symlink in compressed file doesn't work properly.
        supportSymlinks = false;/*  www  .  ja v a 2 s . c o m*/
    }

    Map<String, Object> conf = new HashMap();
    // set clean time really high so doesn't kick in
    conf.put(DaemonConfig.SUPERVISOR_LOCALIZER_CACHE_CLEANUP_INTERVAL_MS, 60 * 60 * 1000);

    String key1 = archiveFile.getName();
    String topo1 = "topo1";
    AsyncLocalizer localizer = new TestLocalizer(conf, baseDir.toString());
    // set really small so will do cleanup
    localizer.setTargetCacheSize(1);

    ReadableBlobMeta rbm = new ReadableBlobMeta();
    rbm.set_settable(new SettableBlobMeta(WORLD_EVERYTHING));
    when(mockblobstore.getBlobMeta(key1)).thenReturn(rbm);

    when(mockblobstore.getBlob(key1))
            .thenReturn(new TestInputStreamWithMeta(new FileInputStream(archiveFile.getAbsolutePath())));

    long timeBefore = System.nanoTime();
    File user1Dir = localizer.getLocalUserFileCacheDir(user1);
    assertTrue("failed to create user dir", user1Dir.mkdirs());
    LocalizedResource lrsrc = localizer.getBlob(new LocalResource(key1, true), user1, topo1, user1Dir);
    long timeAfter = System.nanoTime();

    String expectedUserDir = joinPath(baseDir.toString(), USERCACHE, user1);
    String expectedFileDir = joinPath(expectedUserDir, AsyncLocalizer.FILECACHE, AsyncLocalizer.ARCHIVESDIR);
    assertTrue("user filecache dir not created", new File(expectedFileDir).exists());
    File keyFile = new File(expectedFileDir, key1 + ".0");
    assertTrue("blob not created", keyFile.exists());
    assertTrue("blob is not uncompressed", keyFile.isDirectory());
    File symlinkFile = new File(keyFile, "tmptestsymlink");

    if (supportSymlinks) {
        assertTrue("blob uncompressed doesn't contain symlink", Files.isSymbolicLink(symlinkFile.toPath()));
    } else {
        assertTrue("blob symlink file doesn't exist", symlinkFile.exists());
    }

    LocalizedResourceSet lrsrcSet = localizer.getUserResources().get(user1);
    assertEquals("local resource set size wrong", 1, lrsrcSet.getSize());
    assertEquals("user doesn't match", user1, lrsrcSet.getUser());
    LocalizedResource key1rsrc = lrsrcSet.get(key1, true);
    assertNotNull("Local resource doesn't exist but should", key1rsrc);
    assertEquals("key doesn't match", key1, key1rsrc.getKey());
    assertEquals("refcount doesn't match", 1, key1rsrc.getRefCount());
    assertEquals("file path doesn't match", keyFile.toString(), key1rsrc.getFilePathWithVersion());
    assertEquals("size doesn't match", size, key1rsrc.getSize());
    assertTrue("timestamp not within range",
            (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter));

    timeBefore = System.nanoTime();
    localizer.removeBlobReference(lrsrc.getKey(), user1, topo1, true);
    timeAfter = System.nanoTime();

    lrsrcSet = localizer.getUserResources().get(user1);
    assertEquals("local resource set size wrong", 1, lrsrcSet.getSize());
    key1rsrc = lrsrcSet.get(key1, true);
    assertNotNull("Local resource doesn't exist but should", key1rsrc);
    assertEquals("refcount doesn't match", 0, key1rsrc.getRefCount());
    assertTrue("timestamp not within range",
            (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter));

    // should remove the blob since cache size set really small
    localizer.cleanup();

    lrsrcSet = localizer.getUserResources().get(user1);
    assertFalse("blob contents not deleted", symlinkFile.exists());
    assertFalse("blob not deleted", keyFile.exists());
    assertFalse("blob file dir not deleted", new File(expectedFileDir).exists());
    assertFalse("blob dir not deleted", new File(expectedUserDir).exists());
    assertNull("user set should be null", lrsrcSet);

}

From source file:com.android.repository.util.InstallerUtilTest.java

public void testUnzip() throws Exception {
    if (new MockFileOp().isWindows()) {
        // can't run on windows.
        return;// w  ww . java 2 s .  c o  m
    }
    // zip needs a real file, so no MockFileOp for us.
    FileOp fop = FileOpUtils.create();

    Path root = Files.createTempDirectory("InstallerUtilTest");
    Path outRoot = Files.createTempDirectory("InstallerUtilTest");
    try {
        Path file1 = root.resolve("foo");
        Files.write(file1, "content".getBytes());
        Path dir1 = root.resolve("bar");
        Files.createDirectories(dir1);
        Path file2 = dir1.resolve("baz");
        Files.write(file2, "content2".getBytes());
        Files.createSymbolicLink(root.resolve("link1"), dir1);
        Files.createSymbolicLink(root.resolve("link2"), file2);

        Path outZip = outRoot.resolve("out.zip");
        try (ZipArchiveOutputStream out = new ZipArchiveOutputStream(outZip.toFile());
                Stream<Path> listing = Files.walk(root)) {
            listing.forEach(path -> {
                try {
                    ZipArchiveEntry archiveEntry = (ZipArchiveEntry) out.createArchiveEntry(path.toFile(),
                            root.relativize(path).toString());
                    out.putArchiveEntry(archiveEntry);
                    if (Files.isSymbolicLink(path)) {
                        archiveEntry.setUnixMode(UnixStat.LINK_FLAG | archiveEntry.getUnixMode());
                        out.write(path.getParent().relativize(Files.readSymbolicLink(path)).toString()
                                .getBytes());
                    } else if (!Files.isDirectory(path)) {
                        out.write(Files.readAllBytes(path));
                    }
                    out.closeArchiveEntry();
                } catch (Exception e) {
                    fail();
                }
            });
        }
        Path unzipped = outRoot.resolve("unzipped");
        Files.createDirectories(unzipped);
        InstallerUtil.unzip(outZip.toFile(), unzipped.toFile(), fop, 1, new FakeProgressIndicator());
        assertEquals("content", new String(Files.readAllBytes(unzipped.resolve("foo"))));
        Path resultDir = unzipped.resolve("bar");
        Path resultFile2 = resultDir.resolve("baz");
        assertEquals("content2", new String(Files.readAllBytes(resultFile2)));
        Path resultLink = unzipped.resolve("link1");
        assertTrue(Files.isDirectory(resultLink));
        assertTrue(Files.isSymbolicLink(resultLink));
        assertTrue(Files.isSameFile(resultLink, resultDir));
        Path resultLink2 = unzipped.resolve("link2");
        assertEquals("content2", new String(Files.readAllBytes(resultLink2)));
        assertTrue(Files.isSymbolicLink(resultLink2));
        assertTrue(Files.isSameFile(resultLink2, resultFile2));
    } finally {
        fop.deleteFileOrFolder(root.toFile());
        fop.deleteFileOrFolder(outRoot.toFile());
    }
}

From source file:org.orbisgis.framework.BundleTools.java

private static void parseDirectory(File rootPath, File path, Set<String> packages) throws SecurityException {
    File[] files = path.listFiles();
    for (File file : files) {
        if (!file.isDirectory()) {
            if (file.getName().endsWith(".class")
                    && file.getParent().length() > rootPath.getAbsolutePath().length()) {
                String parentPath = file.getParent().substring(rootPath.getAbsolutePath().length() + 1);
                packages.add(parentPath.replace(File.separator, "."));
            }/*from w  ww  .  j ava 2  s  .c  o  m*/
        } else {
            if (!Files.isSymbolicLink(file.toPath())) {
                parseDirectory(rootPath, file, packages);
            }
        }
    }
}

From source file:org.csstudio.diirt.util.core.preferences.DIIRTPreferences.java

/**
 * This method is copied from {@link IOUtils} because when it is
 * used (inside the runtime shutdown hook) all OSGI modules are
 * already disposed.//ww w.  j  a va2 s. c o m
 *
 * @param directory
 * @throws IOException
 */
private static void deleteDirectory(final File directory) throws IOException {

    if (!directory.exists()) {
        return;
    }

    if (!Files.isSymbolicLink(directory.toPath())) {
        cleanDirectory(directory);
    }

    if (!directory.delete()) {
        throw new IOException("Unable to delete directory " + directory + ".");
    }

}