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.github.mjdetullio.jenkins.plugins.multibranch.AbstractMultiBranchProject.java

/**
 * Migrates <code>SyncBranchesTrigger</code> to {@link hudson.triggers.TimerTrigger} and copies the
 * template's {@link hudson.security.AuthorizationMatrixProperty} to the parent as a
 * {@link com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty}.
 *//*from  ww w.  j av  a  2 s . c  om*/
@SuppressWarnings(UNUSED)
@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void migrate() throws IOException {
    final String projectAmpStartTag = "<hudson.security.AuthorizationMatrixProperty>";

    final File projectsDir = new File(Jenkins.getActiveInstance().getRootDir(), "jobs");

    if (!projectsDir.getCanonicalFile().isDirectory()) {
        return;
    }

    Collection<File> configFiles = FileUtils.listFiles(projectsDir, new NameFileFilter("config.xml"),
            TrueFileFilter.TRUE);

    for (final File configFile : configFiles) {
        String xml = FileUtils.readFileToString(configFile);

        // Rename and wrap trigger open tag
        xml = xml.replaceFirst("(?m)^  <syncBranchesTrigger>(\r?\n)    <spec>",
                "  <triggers>\n    <hudson.triggers.TimerTrigger>\n      <spec>");

        // Rename and wrap trigger close tag
        xml = xml.replaceFirst("(?m)^  </syncBranchesTrigger>",
                "    </hudson.triggers.TimerTrigger>\n  </triggers>");

        // Copy AMP from template if parent does not have a properties tag
        if (!xml.matches("(?ms).+(\r?\n)  <properties.+")
                // Long line is long
                && xml.matches(
                        "(?ms).*<((freestyle|maven)-multi-branch-project|com\\.github\\.mjdetullio\\.jenkins\\.plugins\\.multibranch\\.(FreeStyle|Maven)MultiBranchProject)( plugin=\".*?\")?.+")) {

            File templateConfigFile = new File(new File(configFile.getParentFile(), TEMPLATE), "config.xml");

            if (templateConfigFile.isFile()) {
                String templateXml = FileUtils.readFileToString(templateConfigFile);

                int start = templateXml.indexOf(projectAmpStartTag);
                int end = templateXml.indexOf("</hudson.security.AuthorizationMatrixProperty>");

                if (start != -1 && end != -1) {
                    String ampSettings = templateXml.substring(start + projectAmpStartTag.length(), end);

                    xml = xml.replaceFirst("(?m)^  ", "  <properties>\n    "
                            + "<com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty>"
                            + ampSettings
                            + "</com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty>"
                            + "\n  </properties>\n  ");
                }
            }
        }

        FileUtils.writeStringToFile(configFile, xml);
    }
}

From source file:com.cedarsoft.io.LinkUtils.java

/**
 * Checks whether a given file is a symbolic link.
 *
 * @param file the file/*from  w  w w. j  a  va2 s  . c o m*/
 * @return whether the given file is a symbolic link
 *
 * @throws IOException if any.
 */
public boolean isSymbolicLink(@Nonnull File file) throws IOException {
    return !file.getAbsoluteFile().equals(file.getCanonicalFile());
}

From source file:org.sonatype.nexus.yum.internal.support.YumNexusTestSupport.java

protected String osIndependentUri(File file) throws IOException {
    return file.getCanonicalFile().toURI().toString();
}

From source file:org.sonar.cxx.preprocessor.SourceCodeProvider.java

public void setIncludeRoots(List<String> includeRoots, String baseDir) {
    for (String tmp : includeRoots) {

        File includeRoot = new File(tmp);
        if (!includeRoot.isAbsolute()) {
            includeRoot = new File(baseDir, tmp);
        }/*from  w  w w .  ja  v a  2s.com*/

        try {
            includeRoot = includeRoot.getCanonicalFile();
        } catch (java.io.IOException io) {
            LOG.error("cannot get canonical form of: '{}'", includeRoot);
        }

        if (includeRoot.isDirectory()) {
            LOG.debug("storing include root: '{}'", includeRoot);
            this.includeRoots.add(includeRoot);
        } else {
            LOG.warn("the include root {} doesnt exist", includeRoot.getAbsolutePath());
        }
    }
}

From source file:org.xwiki.environment.internal.ServletEnvironmentTest.java

@Test
public void getPermanentDirectoryWhenSetWithAPI() throws Exception {
    File permanentDirectory = new File("/permanent");
    this.environment.setPermanentDirectory(permanentDirectory);
    assertEquals(permanentDirectory.getCanonicalFile(),
            this.environment.getPermanentDirectory().getCanonicalFile());
}

From source file:io.neba.core.logviewer.LogFiles.java

private File getConfiguredLogfile(Configuration logConfiguration) throws IOException {
    Dictionary properties = logConfiguration.getProperties();
    if (properties == null) {
        return null;
    }/*  w  ww  .ja va2s. c  o  m*/

    String logFilePath = (String) properties.get(LOG_FILE_PROPERTY);
    if (isEmpty(logFilePath)) {
        return null;
    }

    File logFile = new File(logFilePath);
    if (!logFile.isAbsolute()) {
        logFile = new File(this.slingHomeDirectory, logFilePath);
    }

    return logFile.getCanonicalFile();
}

From source file:org.apache.maven.plugin.surefire.booterclient.ForkConfigurationTest.java

public void testExceptionWhenCurrentDirectoryIsNotRealDirectory()
        throws IOException, SurefireBooterForkException {
    // SUREFIRE-1136
    File baseDir = new File(FileUtils.getTempDirectory(),
            "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic(3));
    baseDir.mkdirs();/* w w w. jav  a2 s . c om*/
    baseDir.deleteOnExit();

    File cwd = new File(baseDir, "cwd.txt");
    FileUtils.touch(cwd);
    cwd.deleteOnExit();

    ForkConfiguration config = getForkConfiguration(null, "java", cwd.getCanonicalFile());

    try {
        config.createCommandLine(Collections.<String>emptyList(), true, false, null, 1);
    } catch (SurefireBooterForkException sbfe) {
        // To handle issue with ~ expansion on Windows
        String absolutePath = cwd.getCanonicalPath();
        assertEquals("WorkingDirectory " + absolutePath + " exists and is not a directory", sbfe.getMessage());
        return;
    }

    fail();
}

From source file:org.pentaho.reporting.libraries.resourceloader.loader.file.FileResourceLoader.java

/**
 * Derives a new resource key from the given key. If neither a path nor new factory-keys are given, the parent key is
 * returned.//from   w w w  .j  ava2 s. c  om
 *
 * @param parent      the parent
 * @param path        the derived path (can be null).
 * @param factoryKeys the optional factory keys (can be null).
 * @return the derived key.
 * @throws org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException if the key cannot be derived
 *                                                                                     for any reason.
 */
public ResourceKey deriveKey(final ResourceKey parent, final String path, final Map factoryKeys)
        throws ResourceKeyCreationException {
    if (isSupportedKey(parent) == false) {
        throw new ResourceKeyCreationException("Assertation: Unsupported parent key type");
    }

    try {
        final File target;
        if (path != null) {
            final File parentResource = (File) parent.getIdentifier();
            final File parentFile = parentResource.getCanonicalFile().getParentFile();
            if (parentFile == null) {
                throw new FileNotFoundException("Parent file does not exist");
            }
            target = new File(parentFile, path);
            if (target.exists() == false || target.isFile() == false) {
                throw new ResourceKeyCreationException(
                        "Malformed value: " + path + " (" + target + "): File does not exist.");
            }

        } else {
            target = (File) parent.getIdentifier();
        }

        final Map map;
        if (factoryKeys != null) {
            map = new HashMap();
            map.putAll(parent.getFactoryParameters());
            map.putAll(factoryKeys);
        } else {
            map = parent.getFactoryParameters();
        }
        return new ResourceKey(parent.getSchema(), target, map);
    } catch (IOException ioe) {
        throw new ResourceKeyCreationException("Failed to create key", ioe);
    }
}

From source file:org.echocat.jomon.resources.FileBasedResourceRepository.java

protected boolean targetFileIsTheSame(@Nonnull Resource resource, @Nonnull File targetFile) throws IOException {
    final boolean result;
    if (resource instanceof FileResource) {
        result = ((FileEnabledResource) resource).getFile().getCanonicalFile()
                .equals(targetFile.getCanonicalFile());
    } else {// w  w  w. java 2  s .  c om
        result = false;
    }
    return result;
}