Example usage for java.io File getCanonicalFile

List of usage examples for java.io File getCanonicalFile

Introduction

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

Prototype

public File getCanonicalFile() throws IOException 

Source Link

Document

Returns the canonical form of this abstract pathname.

Usage

From source file:hudson.Util.java

/**
 * Checks if the given file represents a symlink.
 *//*from   w w w .ja  v a 2 s. c  om*/
public static boolean isSymlink(File file) throws IOException {
    String name = file.getName();
    if (name.equals(".") || name.equals("..")) {
        return false;
    }

    File fileInCanonicalParent;
    File parentDir = file.getParentFile();
    if (parentDir == null) {
        fileInCanonicalParent = file;
    } else {
        fileInCanonicalParent = new File(parentDir.getCanonicalPath(), name);
    }
    return !fileInCanonicalParent.getCanonicalFile().equals(fileInCanonicalParent.getAbsoluteFile());
}

From source file:org.dcm4che2.tool.dcmdir.DcmDir.java

public int addFile(File f) throws IOException {
    f = f.getCanonicalFile();
    // skip adding DICOMDIR
    if (f.equals(file))
        return 0;
    int n = 0;/*from  w w w. ja  v a  2s  .  c o  m*/
    if (f.isDirectory()) {
        File[] fs = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            n += addFile(fs[i]);
        }
        return n;
    }
    DicomInputStream in = new DicomInputStream(f);
    in.setHandler(new StopTagInputHandler(Tag.PixelData));
    DicomObject dcmobj = in.readDicomObject();
    DicomObject patrec = ap.makePatientDirectoryRecord(dcmobj);
    DicomObject styrec = ap.makeStudyDirectoryRecord(dcmobj);
    DicomObject serrec = ap.makeSeriesDirectoryRecord(dcmobj);
    DicomObject instrec = ap.makeInstanceDirectoryRecord(dcmobj, dicomdir.toFileID(f));

    DicomObject rec = writer().addPatientRecord(patrec);
    if (rec == patrec) {
        ++n;
    }
    rec = writer().addStudyRecord(rec, styrec);
    if (rec == styrec) {
        ++n;
    }
    rec = writer().addSeriesRecord(rec, serrec);
    if (rec == serrec) {
        ++n;
    }
    if (n == 0 && checkDuplicate) {
        String iuid = dcmobj.getString(Tag.MediaStorageSOPInstanceUID);
        if (dicomdir.findInstanceRecord(rec, iuid) != null) {
            System.out.print('D');
            return 0;
        }
    }
    writer().addChildRecord(rec, instrec);
    System.out.print('.');
    return n + 1;
}

From source file:uk.org.raje.maven.plugin.msbuild.CppCheckMojo.java

/**
 * Adjust the list of include paths to be relative to the projectFile directory 
 *//*from  w  ww  . j a v  a2  s  .co  m*/
private List<File> getRelativeIncludeDirectories(VCProject vcProject) throws MojoExecutionException {
    final List<File> relativeIncludeDirectories = new ArrayList<File>();

    for (File includeDir : vcProject.getIncludeDirectories()) {
        if (includeDir.isAbsolute()) {
            relativeIncludeDirectories.add(includeDir);
        } else {
            try {
                File absoluteIncludeDir = new File(vcProject.getFile().getParentFile(), includeDir.getPath());
                relativeIncludeDirectories.add(
                        getRelativeFile(vcProject.getBaseDirectory(), absoluteIncludeDir.getCanonicalFile()));
            } catch (IOException ioe) {
                throw new MojoExecutionException("Failed to compute relative path for directroy " + includeDir,
                        ioe);
            }
        }
    }

    return relativeIncludeDirectories;
}

From source file:com.btoddb.fastpersitentqueue.MemorySegmentSerializer.java

public void removePagingFile(MemorySegment segment) {
    File theFile = new File(directory, segment.getId().toString());
    try {/*from   w  w  w.ja  v  a2 s .c o m*/
        FileUtils.forceDelete(theFile);
        logger.debug("removed memory paging file {}", theFile.toString());
    } catch (FileNotFoundException e) {
        try {
            logger.debug("File not found (this is normal) while removing memory paging file, {}",
                    theFile.getCanonicalFile());
        } catch (IOException e1) {
            // ignore
        }
    } catch (IOException e) {
        try {
            logger.error("exception while removing memory paging file, {}", theFile.getCanonicalFile(), e);
        } catch (IOException e1) {
            // ignore
            logger.error("another exception while removing memory paging file", e);
        }
    }
}

From source file:org.geoserver.rest.catalog.StructuredCoverageStoresTest.java

@Test
public void testHarvestSingle() throws Exception {
    File file = movedFiles.get(0);
    File target = new File(mosaic, file.getName());
    assertTrue(file.renameTo(target));//  w  w  w .ja v a  2  s . c o m

    URL url = DataUtilities.fileToURL(target.getCanonicalFile());
    String body = url.toExternalForm();
    MockHttpServletResponse response = postAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/wcs/coveragestores/watertemp/external.imagemosaic",
            body, "text/plain");
    assertEquals(202, response.getStatus());

    Document dom = getAsDOM(RestBaseController.ROOT_PATH
            + "/workspaces/wcs/coveragestores/watertemp/coverages/watertemp/index/granules.xml");
    // print(dom);
    assertXpathEvaluatesTo("3", "count(//gf:watertemp)", dom);
    assertXpathEvaluatesTo("1", "count(//gf:watertemp[gf:location = '" + file.getName() + "'])", dom);
}

From source file:org.mrgeo.utils.DependencyLoader.java

private static void addFileToClasspath(Configuration conf, Set<String> existing, FileSystem fs, Path hdfsBase,
        File file) throws IOException {
    Path hdfsPath = new Path(hdfsBase, file.getName());
    if (!existing.contains(hdfsPath.toString())) {
        if (fs.exists(hdfsPath)) {
            // check the timestamp and exit if the one in hdfs is "newer"
            FileStatus status = fs.getFileStatus(hdfsPath);

            if (file.lastModified() <= status.getModificationTime()) {
                log.debug(file.getPath() + " up to date");
                DistributedCache.addFileToClassPath(hdfsPath, conf, fs);

                existing.add(hdfsPath.toString());
                return;
            }// w  w  w  . ja  v  a  2  s .c o  m
        }

        // copy the file...
        log.debug("Copying " + file.getPath() + " to HDFS for distribution");

        fs.copyFromLocalFile(new Path(file.getCanonicalFile().toURI()), hdfsPath);
        DistributedCache.addFileToClassPath(hdfsPath, conf, fs);
        existing.add(hdfsPath.toString());
    }
}

From source file:org.qedeq.base.io.IoUtility.java

/**
 * Delete directory contents for all files that match the filter. The main directory itself is
 * not deleted.//from   ww  w .  j  a v  a 2  s.c o  m
 *
 * @param   directory   Directory to scan for files to delete.
 * @param   filter      Filter files (and directories) to delete.
 * @return  Was deletion successful?
 */
public static boolean deleteDir(final File directory, final FileFilter filter) {
    // first we check if the file is a symbolic link
    try {
        if (isSymbolicLink(directory)) {
            return false;
        }
    } catch (IOException e) {
        return false;
    }
    final File candir;
    try {
        candir = directory.getCanonicalFile();
    } catch (IOException e) {
        return false;
    }

    // now we go through all of the files and subdirectories in the
    // directory and delete them one by one
    boolean success = true;
    File[] files = candir.listFiles(filter);
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            // in case this directory is actually a symbolic link, or it's
            // empty, we want to try to delete the link before we try
            // anything
            boolean deleted = file.delete();
            if (!deleted) {
                // deleting the file failed, so maybe it's a non-empty
                // directory
                if (file.isDirectory()) {
                    deleted = deleteDir(file, true);
                }

                // otherwise, there's nothing else we can do
            }
            success = success && deleted;
        }
    }

    return success;
}

From source file:com.redhat.acceptance.AbstractStepsBase.java

public File writeFile(File file, String contents) throws IOException {
    log.debug("creating file [" + file.getPath() + "] with content ["
            + (contents.length() > 50 ? contents.substring(0, 50) + "..." : contents) + "]");
    file.getParentFile().mkdirs();//from www  .  j  av a 2  s.c o  m
    IOUtils.write(contents.getBytes(), new FileOutputStream(file));
    return file.getCanonicalFile();
}

From source file:org.qedeq.base.io.IoUtility.java

/**
 * Delete file directory recursive.//w  ww  . j av  a2s  . c om
 *
 * @param   directory   Directory to delete. Must not be a symbolic link.
 * @param   deleteDir   Delete directory itself too?
 * @return  Was deletion successful?
 */
public static boolean deleteDir(final File directory, final boolean deleteDir) {

    // first we check if the file is a symbolic link
    try {
        if (isSymbolicLink(directory)) {
            return false;
        }
    } catch (IOException e) {
        return false;
    }
    final File candir;
    try {
        candir = directory.getCanonicalFile();
    } catch (IOException e) {
        return false;
    }

    // now we go through all of the files and subdirectories in the
    // directory and delete them one by one
    boolean success = true;
    File[] files = candir.listFiles();
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            // in case this directory is actually a symbolic link, or it's
            // empty, we want to try to delete the link before we try
            // anything
            boolean deleted = file.delete();
            if (!deleted) {
                // deleting the file failed, so maybe it's a non-empty
                // directory
                if (file.isDirectory()) {
                    deleted = deleteDir(file, true);
                }

                // otherwise, there's nothing else we can do
            }
            success = success && deleted;
        }
    }

    // now that we tried to clear the directory out, we can try to delete it
    if (deleteDir && directory.exists()) {
        return directory.delete();
    }
    return success;
}

From source file:com.liferay.blade.cli.command.CreateCommand.java

private File _getDefaultExtDir() throws Exception {
    BladeCLI bladeCLI = getBladeCLI();//  ww  w  . j  ava  2s .c  om

    BaseArgs args = bladeCLI.getArgs();

    File base = new File(args.getBase());

    File baseDir = base.getCanonicalFile();

    if (!isWorkspace(baseDir)) {
        return baseDir;
    }

    Properties properties = getWorkspaceProperties();

    String extDirProperty = (String) properties.get(WorkspaceConstants.DEFAULT_EXT_DIR_PROPERTY);

    if (extDirProperty == null) {
        extDirProperty = WorkspaceConstants.DEFAULT_EXT_DIR;
    }

    WorkspaceProvider workspaceProvider = bladeCLI.getWorkspaceProvider(baseDir);

    File projectDir = workspaceProvider.getWorkspaceDir(baseDir);

    File extDir = new File(projectDir, extDirProperty);

    if (_containsDir(baseDir, extDir)) {
        return baseDir;
    }

    return extDir;
}