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:com.redhat.rcm.version.mgr.VersionManager.java

public Set<File> modifyVersions(final File dir, final String pomNamePattern, final String pomExcludePattern,
        final List<String> boms, final String toolchain, final VersionManagerSession session)
        throws VManException {
    final String[] includedSubpaths = getIncludedSubpaths(dir, pomNamePattern, pomExcludePattern, session);
    final List<File> pomFiles = new ArrayList<File>();

    for (final String subpath : includedSubpaths) {
        File pom = new File(dir, subpath);
        try {//from ww  w  . ja  va  2s  .c o  m
            pom = pom.getCanonicalFile();
        } catch (final IOException e) {
            pom = pom.getAbsoluteFile();
        }

        if (!pomFiles.contains(pom)) {
            logger.info("Loading POM: '" + pom + "'");
            pomFiles.add(pom);
        }
    }

    final File[] pomFileArray = pomFiles.toArray(new File[] {});

    configureSession(boms, toolchain, session, pomFileArray);

    final Set<File> outFiles = modVersions(dir, session, session.isPreserveFiles(), pomFileArray);

    logger.info("Modified " + outFiles.size() + " POM versions in directory.\n\n\tDirectory: " + dir
            + "\n\tBOMs:\t" + StringUtils.join(boms.iterator(), "\n\t\t") + "\n\tPOM Backups: "
            + session.getBackups() + "\n\n");

    return outFiles;
}

From source file:org.fuin.esmp.AbstractEventStoreMojo.java

private File canonicalFile(final File file) throws MojoExecutionException {
    try {/*from  w w w .  j  a  v a 2s  .  c  o  m*/
        return file.getCanonicalFile();
    } catch (final IOException ex) {
        throw new MojoExecutionException("Error creating canonical file: " + file, ex);
    }
}

From source file:org.sipfoundry.sipxconfig.admin.CertificateManagerImpl.java

public Set<CertificateDecorator> listCertificates() {
    Set<CertificateDecorator> certificates = new TreeSet<CertificateDecorator>();
    Collection<File> files = FileUtils.listFiles(new File(m_sslAuthDirectory), SSL_CA_EXTENSIONS, false);
    CertificateDecorator certificateDecorator = null;

    for (File file : files) {
        try {/*from   www  . j a va 2  s  .c o m*/
            file = file.getCanonicalFile();
        } catch (IOException ex) {
            continue;
        }
        certificateDecorator = new CertificateDecorator();
        certificateDecorator.setFileName(file.getName());
        certificateDecorator.setDirName(file.getParentFile().getPath());
        certificateDecorator.setSystemGenerated(isSystemGenerated(file.getName()));
        certificates.add(certificateDecorator);
    }
    return certificates;
}

From source file:gr.omadak.leviathan.asp.AspParser.java

private Writer getWriter(File file) throws IOException {
    String basePath = baseDir.getCanonicalFile().getAbsolutePath();
    String absFile = file.getCanonicalFile().getAbsolutePath();
    absFile = absFile.substring(basePath.length());
    if (absFile.charAt(0) == '/' || absFile.charAt(0) == '\\') {
        absFile = absFile.substring(1);/*w  ww.j  av  a 2  s  .c om*/
    }
    StringTokenizer st = new StringTokenizer(absFile, File.separator);
    File out = null;
    while (st.hasMoreTokens()) {
        String pElem = st.nextToken();
        boolean hasMore = st.hasMoreTokens();
        if (!hasMore) {
            if (pElem.endsWith(".asp")) {
                pElem = pElem.substring(0, pElem.lastIndexOf('.')) + ".php";
            }
        }
        out = new File(out == null ? baseOutDir : out, pElem);
        if (hasMore && !out.exists()) {
            out.mkdir();
        }
    }
    if (out == null) {
        throw new IOException("Unable to determine path from paths:" + baseDir.getAbsolutePath() + " : "
                + file.getAbsolutePath());
    } else {
        return new FileWriter(out);
    }
}

From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java

/**
 * Path to code file path./*from  w  w  w.  ja  v a  2 s . c o m*/
 *
 * @param file the file
 * @return the path
 * @throws IOException the io exception
 */
public Path pathToCodeFile(@javax.annotation.Nonnull File file) throws IOException {
    return fileName.getCanonicalFile().toPath().relativize(file.getCanonicalFile().toPath());
}

From source file:org.sipfoundry.sipxconfig.admin.CertificateManagerImpl.java

public void deleteCA(CertificateDecorator cert) {
    if (isSystemGenerated(cert.getFileName())) {
        throw new UserException("&error.delete.default.ca", cert.getFileName());
    }//from   w w w  . ja v  a 2s. com

    Collection<File> files = FileUtils.listFiles(new File(m_sslAuthDirectory), SSL_CA_EXTENSIONS, false);
    Collection<File> filesToDelete = new ArrayList<File>();
    try {
        for (File file : files) {
            if (StringUtils.equals(file.getCanonicalFile().getName(), cert.getFileName())) {
                filesToDelete.add(getCAFile(file.getName()));
            }
        }
        HttpClient httpClient = getNewHttpClient();
        Location[] locations = m_locationsManager.getLocations();
        for (Location curLocation : locations) {
            for (File file : filesToDelete) {
                String remoteFileName = join(
                        new Object[] { curLocation.getHttpsServerUrl(), file.getAbsolutePath() });
                DeleteMethod httpDelete = new DeleteMethod(remoteFileName);
                try {
                    int statusCode = httpClient.executeMethod(httpDelete);
                    if (statusCode != 200) {
                        throw new UserException("&error.https.server.status.code", curLocation.getFqdn(),
                                String.valueOf(statusCode));
                    }
                } catch (HttpException ex) {
                    throw new UserException("&error.https.server", curLocation.getFqdn(), ex.getMessage());
                } finally {
                    httpDelete.releaseConnection();
                }
            }
        }
    } catch (IOException ex) {
        throw new UserException("&error.delete.cert");
    }
}

From source file:org.commonjava.maven.ext.io.PomIO.java

private List<PomPeek> peekAtPomHierarchy(final File topPom) throws ManipulationException {
    final List<PomPeek> peeked = new ArrayList<>();

    try {//from   www .  j  av a  2  s  . com
        final LinkedList<File> pendingPoms = new LinkedList<>();
        pendingPoms.add(topPom.getCanonicalFile());

        final String topDir = topPom.getAbsoluteFile().getParentFile().getCanonicalPath();

        final Set<File> seen = new HashSet<>();

        File topLevelParent = topPom;

        while (!pendingPoms.isEmpty()) {
            final File pom = pendingPoms.removeFirst();
            seen.add(pom);

            logger.debug("PEEK: " + pom);

            final PomPeek peek = new PomPeek(pom);
            final ProjectVersionRef key = peek.getKey();
            if (key != null) {
                peeked.add(peek);

                final File dir = pom.getParentFile();

                final String relPath = peek.getParentRelativePath();
                if (relPath != null) {
                    logger.debug("Found parent relativePath: " + relPath + " in pom: " + pom);
                    File parent = new File(dir, relPath);
                    if (parent.isDirectory()) {
                        parent = new File(parent, "pom.xml");
                    }

                    parent = parent.getCanonicalFile();
                    if (parent.getParentFile().getCanonicalPath().startsWith(topDir) && parent.exists()
                            && !seen.contains(parent) && !pendingPoms.contains(parent)) {
                        topLevelParent = parent;
                        logger.debug("Possible top level parent " + parent);
                        pendingPoms.add(parent);
                    } else {
                        logger.debug("Skipping reference to non-existent parent relativePath: '" + relPath
                                + "' in: " + pom);
                    }
                }

                final Set<String> modules = peek.getModules();
                if (modules != null && !modules.isEmpty()) {
                    for (final String module : modules) {
                        logger.debug("Found module: " + module + " in pom: " + pom);

                        File modPom = new File(dir, module);
                        if (modPom.isDirectory()) {
                            modPom = new File(modPom, "pom.xml");
                        }

                        if (modPom.exists() && !seen.contains(modPom) && !pendingPoms.contains(modPom)) {
                            pendingPoms.addLast(modPom);
                        } else {
                            logger.debug(
                                    "Skipping reference to non-existent module: '" + module + "' in: " + pom);
                        }
                    }
                }
            } else {
                logger.debug("Skipping " + pom + " as its a template file.");
            }
        }

        final HashSet<ProjectVersionRef> projectrefs = new HashSet<>();

        for (final PomPeek p : peeked) {
            projectrefs.add(p.getKey());

            if (p.getPom().equals(topLevelParent)) {
                logger.debug("Setting top level parent to " + p.getPom() + " :: " + p.getKey());
                p.setInheritanceRoot(true);
            }
        }

        for (final PomPeek p : peeked) {
            if (p.getParentKey() == null || !seenThisParent(projectrefs, p.getParentKey())) {
                logger.debug("Found a standalone pom " + p.getPom() + " :: " + p.getKey());
                p.setInheritanceRoot(true);
            }
        }
    } catch (final IOException e) {
        throw new ManipulationException("Problem peeking at POMs.", e);
    }

    return peeked;
}

From source file:org.apache.catalina.ant.ValidatorTask.java

/**
 * Execute the specified command.  This logic only performs the common
 * attribute validation required by all subclasses; it does not perform
 * any functional logic directly./*w  w  w. j  a  v a 2  s  .  c  o m*/
 *
 * @exception BuildException if a validation error occurs
 */
public void execute() throws BuildException {

    if (path == null) {
        throw new BuildException("Must specify 'path'");
    }

    File file = new File(path, Constants.ApplicationWebXml);
    if ((!file.exists()) || (!file.canRead())) {
        throw new BuildException("Cannot find web.xml");
    }

    // Commons-logging likes having the context classloader set
    ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(ValidatorTask.class.getClassLoader());

    Digester digester = DigesterFactory.newDigester(true, true, null);
    try {
        file = file.getCanonicalFile();
        InputStream stream = new BufferedInputStream(new FileInputStream(file));
        InputSource is = new InputSource(file.toURL().toExternalForm());
        is.setByteStream(stream);
        digester.parse(is);
        System.out.println("web.xml validated");
    } catch (Throwable t) {
        throw new BuildException("Validation failure", t);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCL);
    }

}

From source file:org.echocat.jomon.process.local.daemon.LocalProcessDaemon.java

@Nonnull
protected File getBaseDirectoryOfEnvironmentVariable(@Nonnull String name) throws IOException {
    final String directoryPath = System.getenv(name);
    if (directoryPath == null) {
        throw new IllegalStateException(
                "In the current environment is no " + name + " environment variable set.");
    }//w ww .  j  a  va 2s.c  om
    final File directory = new File(directoryPath);
    if (!directory.exists()) {
        throw new IllegalStateException("In the current environment points the " + name
                + " environment variable to " + directory + " but it does not exists.");
    }
    if (!directory.isDirectory()) {
        throw new IllegalStateException("In the current environment points the " + name
                + " environment variable to " + directory + " but it is not a directory.");
    }
    return directory.getCanonicalFile();
}

From source file:org.qedeq.kernel.se.common.DefaultModuleAddressTest.java

public void testDefaultModuleAddressFile() throws Exception {
    final File file = new File("unknown/hulouo.xml");
    dflt = new DefaultModuleAddress(file);
    assertEquals("hulouo.xml", dflt.getFileName());
    assertEquals("file://", dflt.getHeader());
    assertEquals("hulouo", dflt.getName());
    assertEquals(//from   w  w  w.java  2  s.c o m
            (SystemUtils.IS_OS_WINDOWS ? "/" : "")
                    + FilenameUtils.separatorsToUnix(file.getCanonicalFile().getParentFile().getPath()) + "/",
            dflt.getPath());
    assertEquals("file://" + (SystemUtils.IS_OS_WINDOWS ? "/" : "")
            + FilenameUtils.separatorsToUnix(file.getCanonicalPath()), dflt.getUrl());
    assertEquals(true, dflt.isFileAddress());
    assertEquals(false, dflt.isRelativeAddress());
    assertEquals(dflt, dflt.createModuleContext().getModuleLocation());
}