Example usage for java.io File isAbsolute

List of usage examples for java.io File isAbsolute

Introduction

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

Prototype

public boolean isAbsolute() 

Source Link

Document

Tests whether this abstract pathname is absolute.

Usage

From source file:com.thoughtworks.go.util.FileUtil.java

public static File applyBaseDirIfRelative(File baseDir, File actualFileToUse) {
    if (actualFileToUse == null) {
        return baseDir;
    }/*from   ww  w .j  a  va2s.  c  om*/
    if (actualFileToUse.isAbsolute()) {
        return actualFileToUse;
    }

    if (StringUtils.isBlank(baseDir.getPath())) {
        return actualFileToUse;
    }

    return new File(baseDir, actualFileToUse.getPath());

}

From source file:it.geosolutions.geostore.core.security.password.URLMasterPasswordProvider.java

/**
 * Writes the master password in the file
 * @param url//from  ww  w .ja v  a2  s.c  o m
 * @param configDir
 * @return
 * @throws IOException
 */
static OutputStream output(URL url, File configDir) throws IOException {
    //check for file URL
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        File f;
        try {
            f = new File(url.toURI());
        } catch (URISyntaxException e) {
            f = new File(url.getPath());
        }
        if (!f.isAbsolute()) {
            //make relative to config dir
            f = new File(configDir, f.getPath());
        }
        return new FileOutputStream(f);
    } else {
        URLConnection cx = url.openConnection();
        cx.setDoOutput(true);
        return cx.getOutputStream();
    }
}

From source file:org.apache.oozie.service.Services.java

public static void setOozieHome() throws ServiceException {
    oozieHome = System.getProperty(OOZIE_HOME_DIR);
    if (oozieHome == null) {
        throw new ServiceException(ErrorCode.E0000);
    }//from ww w.j  av  a2s . c o m
    File file = new File(oozieHome);
    if (!file.isAbsolute()) {
        throw new ServiceException(ErrorCode.E0003, oozieHome);
    }
    if (!file.exists()) {
        throw new ServiceException(ErrorCode.E0004, oozieHome);
    }
}

From source file:ch.admin.suis.msghandler.util.FileUtils.java

/**
 * Returns a {@link File} object pointing to the absolute path for the supplied name. If the
 * <code>name</code> parameter already denotes an absolute path, then a {@link File} object is created solely for this
 * path. Otherwise a {@link File} object is created treating the
 * <code>parent</code> parameter as the parent directory and the
 * <code>name</code> parameter as a child path relative to that parent.
 *
 * @param parent parent directory//w  w w.j av a2 s  . co m
 * @param name   child path relative to that parent
 * @return The object pointing to the absolute path
 */
public static File createPath(String parent, String name) {
    File nameFile = new File(name);
    if (nameFile.isAbsolute()) {
        return nameFile;
    } else if (StringUtils.isEmpty(parent)) { // parent == null || parent.isEmpty()
        return nameFile.getAbsoluteFile();
    } else {
        File concatFile = new File(new File(parent), name);
        return concatFile.isAbsolute() ? concatFile : concatFile.getAbsoluteFile();
    }
}

From source file:ddf.security.PropertiesLoader.java

/**
 * Will attempt to load properties from a file using the given classloader. If that fails,
 * several other methods will be tried until the properties file is located.
 *
 * @param propertiesFile//from  w ww.j a  va  2 s. co  m
 * @param classLoader
 * @return Properties
 */
public static Properties loadProperties(String propertiesFile, ClassLoader classLoader) {
    boolean error = false;
    Properties properties = new Properties();
    if (propertiesFile != null) {
        try {
            LOGGER.debug("Attempting to load properties from {} with Spring PropertiesLoaderUtils.",
                    propertiesFile);
            properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile);
        } catch (IOException e) {
            error = true;
            LOGGER.error("Unable to load properties using default Spring properties loader.", e);
        }
        if (error || properties.isEmpty()) {
            if (classLoader != null) {
                try {
                    LOGGER.debug(
                            "Attempting to load properties from {} with Spring PropertiesLoaderUtils with class loader.",
                            propertiesFile);
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile, classLoader);
                    error = false;
                } catch (IOException e) {
                    error = true;
                    LOGGER.error("Unable to load properties using default Spring properties loader.", e);
                }
            } else {
                try {
                    LOGGER.debug(
                            "Attempting to load properties from {} with Spring PropertiesLoaderUtils with class loader.",
                            propertiesFile);
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile,
                            PropertiesLoader.class.getClassLoader());
                    error = false;
                } catch (IOException e) {
                    error = true;
                    LOGGER.error("Unable to load properties using default Spring properties loader.", e);
                }
            }
        }

        if (error || properties.isEmpty()) {
            LOGGER.debug("Attempting to load properties from file system: {}", propertiesFile);
            File propFile = new File(propertiesFile);
            // If properties file has fully-qualified absolute path (which
            // the blueprint file specifies) then can load it directly.
            if (propFile.isAbsolute()) {
                LOGGER.debug("propertiesFile {} is absolute", propertiesFile);
                propFile = new File(propertiesFile);
            } else {
                String karafHome = System.getProperty("karaf.home");
                if (karafHome != null && !karafHome.isEmpty()) {
                    propFile = new File(karafHome, propertiesFile);
                } else {
                    karafHome = System.getProperty("ddf.home");
                    if (karafHome != null && !karafHome.isEmpty()) {
                        propFile = new File(karafHome, propertiesFile);
                    } else {
                        propFile = new File(propertiesFile);
                    }
                }
            }
            properties = new Properties();

            try (InputStreamReader reader = new InputStreamReader(new FileInputStream(propertiesFile),
                    StandardCharsets.UTF_8)) {
                properties.load(reader);
            } catch (FileNotFoundException e) {
                error = true;
                LOGGER.error("Could not find properties file: {}", propFile.getAbsolutePath(), e);
            } catch (IOException e) {
                error = true;
                LOGGER.error("Error reading properties file: {}", propFile.getAbsolutePath(), e);
            }
        }
        if (error || properties.isEmpty()) {
            LOGGER.debug("Attempting to load properties as a resource: {}", propertiesFile);
            InputStream ins = PropertiesLoader.class.getResourceAsStream(propertiesFile);
            if (ins != null) {
                try {
                    properties.load(ins);
                    ins.close();
                } catch (IOException e) {
                    LOGGER.error("Unable to load properties: {}", propertiesFile, e);
                } finally {
                    IOUtils.closeQuietly(ins);
                }
            }
        }

        //replace any ${prop} with system properties
        Properties filtered = new Properties();
        for (Map.Entry<?, ?> entry : properties.entrySet()) {
            filtered.put(StrSubstitutor.replaceSystemProperties(entry.getKey()),
                    StrSubstitutor.replaceSystemProperties(entry.getValue()));
        }
        properties = filtered;

    } else {
        LOGGER.debug("Properties file must not be null.");
    }

    return properties;
}

From source file:net.sf.jabref.logic.util.io.FileUtil.java

/**
 * Converts an absolute filename to a relative one, if necessary.
 * Returns the parameter fileName itself if no shortening is possible
 * <p>//from ww w.j  a v a2  s .  c  o  m
 * This method works correctly only if dirs are sorted decent in their length
 * i.e. /home/user/literature/important before /home/user/literature
 *
 * @param fileName the filename to be shortened
 * @param dirs     directories to check.
 */
public static File shortenFileName(File fileName, List<String> dirs) {
    if ((fileName == null) || !fileName.isAbsolute() || (dirs == null)) {
        return fileName;
    }

    for (String dir : dirs) {
        if (dir != null) {
            File result = shortenFileName(fileName, dir);
            if ((result != null) && !result.equals(fileName)) {
                return result;
            }
        }
    }
    return fileName;
}

From source file:org.codice.ddf.security.common.PropertiesLoader.java

/**
 * Will attempt to load properties from a file using the given classloader. If that fails,
 * several other methods will be tried until the properties file is located.
 *
 * @param propertiesFile/*from w ww.j a  va 2s  .c om*/
 * @param classLoader
 * @return Properties
 */
public static Properties loadProperties(String propertiesFile, ClassLoader classLoader) {
    boolean error = false;
    Properties properties = new Properties();
    if (propertiesFile != null) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from " + propertiesFile
                        + " with Spring PropertiesLoaderUtils.");
            }
            properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile);
        } catch (IOException e) {
            error = true;
            logger.error("Unable to load properties using default Spring properties loader.", e);
        }
        if (error || properties.isEmpty()) {
            if (classLoader != null) {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile, classLoader);
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            } else {
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Attempting to load properties from " + propertiesFile
                                + " with Spring PropertiesLoaderUtils with class loader.");
                    }
                    properties = PropertiesLoaderUtils.loadAllProperties(propertiesFile,
                            PropertiesLoader.class.getClassLoader());
                    error = false;
                } catch (IOException e) {
                    error = true;
                    logger.error("Unable to load properties using default Spring properties loader.", e);
                }
            }
        }

        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties from file system: " + propertiesFile);
            }
            File propFile = new File(propertiesFile);
            // If properties file has fully-qualified absolute path (which
            // the blueprint file specifies) then can load it directly.
            if (propFile.isAbsolute()) {
                logger.debug("propertiesFile {} is absolute", propertiesFile);
                propFile = new File(propertiesFile);
            } else {
                // Otherwise need to prepend parent path which is based on
                // the installation directory
                String karafHome = System.getProperty("karaf.home");
                if (karafHome != null && !karafHome.isEmpty()) {
                    propFile = new File(karafHome, propertiesFile);
                } else {
                    karafHome = System.getProperty("ddf.home");
                    if (karafHome != null && !karafHome.isEmpty()) {
                        propFile = new File(karafHome, propertiesFile);
                    } else {
                        propFile = new File(propertiesFile);
                    }
                }
            }
            properties = new Properties();
            try (FileReader reader = new FileReader(propFile)) {
                properties.load(reader);
            } catch (FileNotFoundException e) {
                error = true;
                logger.error("Could not find properties file: " + propFile.getAbsolutePath(), e);
            } catch (IOException e) {
                error = true;
                logger.error("Error reading properties file: " + propFile.getAbsolutePath(), e);
            }
        }
        if (error || properties.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting to load properties as a resource: " + propertiesFile);
            }
            InputStream ins = PropertiesLoader.class.getResourceAsStream(propertiesFile);
            if (ins != null) {
                try {
                    properties.load(ins);
                    ins.close();
                } catch (IOException e) {
                    logger.error("Unable to load properties: " + propertiesFile, e);
                } finally {
                    try {
                        ins.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Properties file must not be null.");
        }
    }

    return properties;
}

From source file:it.geosolutions.geostore.core.security.password.URLMasterPasswordProvider.java

static InputStream input(URL url, File configDir) throws IOException {
    //check for a file url
    if (url == null) {
        //default master password
        url = URLMasterPasswordProvider.class.getClassLoader().getResource("passwd");
    }//  w ww  .  ja v a 2  s . c  om
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        File f;
        try {
            f = new File(url.toURI());
        } catch (URISyntaxException e) {
            f = new File(url.getPath());
        }

        //check if the file is relative
        if (!f.isAbsolute()) {
            //make it relative to the config directory for this password provider
            f = new File(configDir, f.getPath());
        }
        return new FileInputStream(f);
    } else {
        return url.openStream();
    }
}

From source file:net.sf.jabref.logic.util.io.FileUtil.java

private static File shortenFileName(File fileName, String directory) {
    if ((fileName == null) || !fileName.isAbsolute() || (directory == null)) {
        return fileName;
    }//ww  w.  ja  va  2 s.  c om

    String dir = directory;
    String longName;
    if (OS.WINDOWS) {
        // case-insensitive matching on Windows
        longName = fileName.toString().toLowerCase();
        dir = dir.toLowerCase();
    } else {
        longName = fileName.toString();
    }

    if (!dir.endsWith(FILE_SEPARATOR)) {
        dir = dir.concat(FILE_SEPARATOR);
    }

    if (longName.startsWith(dir)) {
        // result is based on original name, not on lower-cased name
        String newName = fileName.toString().substring(dir.length());
        return new File(newName);
    } else {
        return fileName;
    }
}

From source file:org.beigesoft.uml.service.swing.CreatorDoclet.java

protected static void setOptions(RootDoc root) throws Exception {
    pathUmlProject = null;/*www.  j  ava2s. c om*/
    pathMavenProject = null;
    pathJavaDoc = null;
    for (String[] option : root.options()) {
        if (option[0].equals(OPTION_PATH_UML_PROJECT) && option.length == 2) {
            pathUmlProject = option[1].trim();
            root.printNotice("Uml project path is " + pathUmlProject);
        } else if (option[0].equals("-d") && option.length == 2) {
            pathJavaDoc = option[1].trim();
            root.printNotice("Javadoc path is " + pathJavaDoc);
        } else if (option[0].equals("-sourcepath") && option.length == 2) {
            String pathJavaSource = option[1].trim();
            int firstSeparator = pathJavaSource.indexOf(File.pathSeparatorChar);
            if (firstSeparator != -1) {
                pathJavaSource = pathJavaSource.substring(0, firstSeparator).trim();
            }
            root.printNotice("Firts java source path is " + pathJavaSource);
            int mvnSrcIdx = pathJavaSource.indexOf("/src/test/");
            if (mvnSrcIdx == -1) {
                mvnSrcIdx = pathJavaSource.indexOf("/src/main/");
            }
            if (mvnSrcIdx != -1) {//maven project
                pathMavenProject = pathJavaSource.substring(0, mvnSrcIdx) + File.separator + "src";
                root.printNotice("Maven project path is " + pathMavenProject);
            }
        }
        if (pathUmlProject != null && pathMavenProject != null && pathJavaDoc != null) {
            break;
        }
    }
    if (pathJavaDoc == null) {
        root.printError("Can not find javadoc path!");
    }
    if (pathUmlProject == null) {
        if (pathMavenProject != null) {
            pathUmlProject = pathMavenProject + File.separator + "uml";
            root.printNotice("Uml project path is " + pathUmlProject);
        } else {
            root.printError(
                    "Doclet UML project path option: [" + OPTION_PATH_UML_PROJECT + "] must be settled!");
        }
    }
    if (pathUmlProject != null) {
        File file = new File(pathUmlProject);
        if (!file.isAbsolute()) {
            throw new Exception("Doclet UML project path option: [" + OPTION_PATH_UML_PROJECT
                    + "] must be settled as absolute path "
                    + "or nothing (assume path is [mvnproject]/src/uml)!");
        }
    }
}