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.blackducksoftware.integration.hub.bamboo.HubBambooUtils.java

public List<String> createScanTargetPaths(final String targetPathText, final File workingDirectory)
        throws IOException {

    final List<String> scanTargets = new ArrayList<>();

    if (StringUtils.isNotBlank(targetPathText)) {
        final String[] scanTargetPathsArray = targetPathText.split("\\r?\\n");
        for (final String target : scanTargetPathsArray) {
            if (!StringUtils.isBlank(target)) {
                if (workingDirectory != null && StringUtils.isBlank(workingDirectory.getCanonicalPath())) {
                    scanTargets.add(target);

                } else {
                    final File tmpTarget = new File(target);
                    if (tmpTarget.isAbsolute()) {
                        scanTargets.add(tmpTarget.getCanonicalPath());
                    } else {
                        scanTargets.add(new File(workingDirectory, target).getCanonicalPath());
                    }/*from w w w  .  ja va 2 s.  co m*/
                }
            }
        }
    }

    return scanTargets;
}

From source file:com.vmware.qe.framework.datadriven.config.DDConfig.java

/**
 * Checks to see if the file path points to an existing file and tries to locate it
 * /*  ww  w.  ja va  2  s.  co  m*/
 * @param filePath file path
 * @return true if file path is valid, false otherwise
 */
private synchronized boolean checkFilePath(String filePath) {
    File f = new File(filePath);
    if (f.exists()) {
        return true;
    } else {
        if (f.isAbsolute()) {
            log.error("Config file doesn't exist. Check value for '" + filePath + "'");
        } else {
            log.error("Can not locate config file. Check value for '" + filePath
                    + "' (Please use absolute file path)");
        }
    }
    return false;
}

From source file:com.redhat.red.koji.build.Config.java

private String getPemPath(String pemPath) {
    File f = new File(pemPath);
    if (!f.isAbsolute()) {
        f = new File(configDir, pemPath);
        return f.getAbsolutePath();
    }//w  w  w  .j a va 2 s .co m

    return pemPath;
}

From source file:org.deegree.igeo.io.RemoteFSAccess.java

public URL getFileURL(String file) throws IOException {

    if (file.toLowerCase().startsWith("http://")) {
        return new URL(file);
    } else {//  ww  w .ja va2  s  . co m

        File fl = new File(file);
        if (fl.isAbsolute()) {
            fl = new File(fl.getName());
        }
        String req = HttpUtils.addAuthenticationForKVP("", this.appCont.getUser(), this.appCont.getPassword(),
                this.appCont.getCertificate(remoteAddr));
        StringBuilder sb = new StringBuilder(500);
        String path = StringTools.replace(fl.getPath(), "/", "2F", true);
        path = StringTools.replace(path, "\\", "2F", true);
        sb.append(remoteAddr).append('?').append(req).append('&');
        sb.append("FILE=").append(path).append("&ACTION=readFile");
        LOG.logDebug("file URL: ", sb);
        return new URL(sb.toString());
    }
}

From source file:org.compiere.mfg_scm.pluginManager.Mfg_scmUtils.java

/**
 * Tries to convert the specified base path and file name into a file
 * object. The parameter strings can be relative files, absolute files and
 * file URLs./* w w  w.ja v a  2s .  c  o m*/
 * 
 * @param basePath
 *            the base path
 * @param fileName
 *            the file name
 * @return the file object (<b>null</b> if no file can be obtained)
 */
public static File getFile(String basePath, String fileName) {
    File file = null;
    File absolute = null;
    String fileUrlProtocol = "file";
    URL url;

    /* Look if it is an URL */
    try {
        url = new URL(new URL(basePath), fileName);
    } catch (MalformedURLException ex) {
        try {
            url = new URL(fileName);
        } catch (MalformedURLException ex1) {
            url = null;
        }
    }

    /* If it is an URL */
    if (url != null) {
        if (fileUrlProtocol.equals(url.getProtocol()))
            return new File(url.getPath());
        return null;
    }

    /* If it is not an URL */
    if (fileName != null)
        absolute = new File(fileName);

    if (StringUtils.isEmpty(basePath) || (absolute != null && absolute.isAbsolute())) {
        file = new File(fileName);
    } else {
        StringBuffer fName = new StringBuffer();
        fName.append(basePath);

        if (!basePath.endsWith(File.separator))
            fName.append(File.separator);

        if (fileName.startsWith("." + File.separator)) {
            fName.append(fileName.substring(2));
        } else {
            fName.append(fileName);
        }
        // System.out.println("GetFile : " + fName);
        file = new File(fName.toString());
    }
    return file;
}

From source file:com.haulmont.cuba.web.gui.components.WebEmbedded.java

@Override
public void setSource(String src) {
    if (src != null) {
        if (src.startsWith("http") || src.startsWith("https")) {
            try {
                setSource(new URL(src));
            } catch (MalformedURLException e) {
                throw new RuntimeException("Unable to parse url for embedded source", e);
            }//  www.j av  a2  s .co m
        } else if (src.startsWith("theme://")) {
            String themeResource = src.substring("theme://".length());
            resource = new VersionedThemeResource(themeResource);
            component.setSource(resource);
        } else {
            File file = new File(src);
            if (!file.isAbsolute()) {
                Configuration configuration = AppBeans.get(Configuration.NAME);
                String root = configuration.getConfig(WebConfig.class).getResourcesRoot();
                if (root != null) {
                    if (!root.endsWith(File.separator)) {
                        root += File.separator;
                    }
                    file = new File(root + file.getPath());
                }
            }

            resource = new FileResource(file);
            component.setSource(resource);
        }
    } else {
        resetSource();
    }
}

From source file:com.npower.dm.multiplexor.Multiplexor.java

/**
 * @throws SmsException//from   www .  j a  va  2  s  .c o m
 */
private File getFile(String filename) throws SmsException {
    String home = System.getProperty("otas.dm.home");
    if (home == null || home.trim().length() == 0) {
        log.error("Please setup the property: otas.dm.home!");
        throw new SmsException("Missing property: otas.dm.home");
    }
    File file = new File(filename);
    if (!file.isAbsolute()) {
        File file4Importer = new File(new File(home), filename);
        return file4Importer;
    } else {
        return file;
    }
}

From source file:com.streamsets.pipeline.stage.destination.hdfs.metadataexecutor.HdfsConnectionConfig.java

public void init(Stage.Context context, String prefix, List<Stage.ConfigIssue> issues) {
    conf = new Configuration();
    conf.setClass("fs.file.impl", RawLocalFileSystem.class, FileSystem.class);

    if (hdfsKerberos) {
        conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION,
                UserGroupInformation.AuthenticationMethod.KERBEROS.name());
        try {/*w  ww.j a  va  2 s . co  m*/
            conf.set(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY,
                    "hdfs/_HOST@" + HadoopSecurityUtil.getDefaultRealm());
        } catch (Exception ex) {
            if (!hdfsConfigs.containsKey(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY)) {
                issues.add(context.createConfigIssue(Groups.HDFS.name(), null,
                        HdfsMetadataErrors.HDFS_METADATA_001, ex.toString()));
            }
        }
    }

    if (hdfsConfDir != null && !hdfsConfDir.isEmpty()) {
        File hadoopConfigDir = new File(hdfsConfDir);
        if (!hadoopConfigDir.isAbsolute()) {
            hadoopConfigDir = new File(context.getResourcesDirectory(), hdfsConfDir).getAbsoluteFile();
        }
        if (!hadoopConfigDir.exists()) {
            issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                    HdfsMetadataErrors.HDFS_METADATA_002, hadoopConfigDir.getPath()));
        } else if (!hadoopConfigDir.isDirectory()) {
            issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                    HdfsMetadataErrors.HDFS_METADATA_003, hadoopConfigDir.getPath()));
        } else {
            File coreSite = new File(hadoopConfigDir, "core-site.xml");
            if (coreSite.exists()) {
                if (!coreSite.isFile()) {
                    issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                            HdfsMetadataErrors.HDFS_METADATA_004, coreSite.getPath()));
                }
                conf.addResource(new Path(coreSite.getAbsolutePath()));
            }
            File hdfsSite = new File(hadoopConfigDir, "hdfs-site.xml");
            if (hdfsSite.exists()) {
                if (!hdfsSite.isFile()) {
                    issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                            HdfsMetadataErrors.HDFS_METADATA_004, hdfsSite.getPath()));
                }
                conf.addResource(new Path(hdfsSite.getAbsolutePath()));
            }
        }
    }

    // Unless user specified non-empty, non-null HDFS URI, we need to retrieve it's value
    if (StringUtils.isEmpty(hdfsUri)) {
        hdfsUri = conf.get("fs.defaultFS");
    }

    for (Map.Entry<String, String> config : hdfsConfigs.entrySet()) {
        conf.set(config.getKey(), config.getValue());
    }

    try {
        loginUgi = HadoopSecurityUtil.getLoginUser(conf);
        userUgi = HadoopSecurityUtil.getProxyUser(hdfsUser, context, loginUgi, issues, Groups.HDFS.name(),
                JOIN.join(prefix, "hdfsUser"));
    } catch (IOException e) {
        LOG.error("Can't create UGI", e);
        issues.add(context.createConfigIssue(Groups.HDFS.name(), null, HdfsMetadataErrors.HDFS_METADATA_005,
                e.getMessage(), e));
    }

    if (!issues.isEmpty()) {
        return;
    }

    try {
        fs = getUGI().doAs(
                (PrivilegedExceptionAction<FileSystem>) () -> FileSystem.newInstance(new URI(hdfsUri), conf));
    } catch (Exception ex) {
        LOG.error("Can't retrieve FileSystem instance", ex);
        issues.add(context.createConfigIssue(Groups.HDFS.name(), null, HdfsMetadataErrors.HDFS_METADATA_005,
                ex.getMessage(), ex));
    }
}

From source file:com.streamsets.pipeline.stage.destination.hdfs.metadataxecutor.HdfsConnectionConfig.java

public void init(Stage.Context context, String prefix, List<Stage.ConfigIssue> issues) {
    conf = new Configuration();
    conf.setClass("fs.file.impl", RawLocalFileSystem.class, FileSystem.class);

    if (hdfsKerberos) {
        conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION,
                UserGroupInformation.AuthenticationMethod.KERBEROS.name());
        try {//from  w  ww . j a v  a2s .c o m
            conf.set(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY,
                    "hdfs/_HOST@" + HadoopSecurityUtil.getDefaultRealm());
        } catch (Exception ex) {
            if (!hdfsConfigs.containsKey(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY)) {
                issues.add(context.createConfigIssue(Groups.HDFS.name(), null,
                        HdfsMetadataErrors.HDFS_METADATA_001, ex.toString()));
            }
        }
    }

    if (hdfsConfDir != null && !hdfsConfDir.isEmpty()) {
        File hadoopConfigDir = new File(hdfsConfDir);
        if (!hadoopConfigDir.isAbsolute()) {
            hadoopConfigDir = new File(context.getResourcesDirectory(), hdfsConfDir).getAbsoluteFile();
        }
        if (!hadoopConfigDir.exists()) {
            issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                    HdfsMetadataErrors.HDFS_METADATA_002, hadoopConfigDir.getPath()));
        } else if (!hadoopConfigDir.isDirectory()) {
            issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                    HdfsMetadataErrors.HDFS_METADATA_003, hadoopConfigDir.getPath()));
        } else {
            File coreSite = new File(hadoopConfigDir, "core-site.xml");
            if (coreSite.exists()) {
                if (!coreSite.isFile()) {
                    issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                            HdfsMetadataErrors.HDFS_METADATA_004, coreSite.getPath()));
                }
                conf.addResource(new Path(coreSite.getAbsolutePath()));
            }
            File hdfsSite = new File(hadoopConfigDir, "hdfs-site.xml");
            if (hdfsSite.exists()) {
                if (!hdfsSite.isFile()) {
                    issues.add(context.createConfigIssue(Groups.HDFS.name(), JOIN.join(prefix, "hdfsConfDir"),
                            HdfsMetadataErrors.HDFS_METADATA_004, hdfsSite.getPath()));
                }
                conf.addResource(new Path(hdfsSite.getAbsolutePath()));
            }
        }
    }

    // Unless user specified non-empty, non-null HDFS URI, we need to retrieve it's value
    if (StringUtils.isEmpty(hdfsUri)) {
        hdfsUri = conf.get("fs.defaultFS");
    }

    for (Map.Entry<String, String> config : hdfsConfigs.entrySet()) {
        conf.set(config.getKey(), config.getValue());
    }

    try {
        loginUgi = HadoopSecurityUtil.getLoginUser(conf);
    } catch (IOException e) {
        LOG.error("Can't create login UGI", e);
        issues.add(context.createConfigIssue(Groups.HDFS.name(), null, HdfsMetadataErrors.HDFS_METADATA_005,
                e.getMessage(), e));
    }

    if (!issues.isEmpty()) {
        return;
    }

    try {
        fs = getUGI().doAs(new PrivilegedExceptionAction<FileSystem>() {
            @Override
            public FileSystem run() throws Exception {
                return FileSystem.newInstance(new URI(hdfsUri), conf);
            }
        });
    } catch (Exception ex) {
        LOG.error("Can't retrieve FileSystem instance", ex);
        issues.add(context.createConfigIssue(Groups.HDFS.name(), null, HdfsMetadataErrors.HDFS_METADATA_005,
                ex.getMessage(), ex));
    }
}

From source file:com.npower.unicom.sync.AbstractImportDaemonPlugIn.java

/**
 * SyncProcessor//from  w  w w .  ja v  a2 s . c o m
 * @param requestFile
 * @return
 */
private SyncProcessor getProcessor(File requestFile) {
    SyncItemReader reader = getSyncItemReader(requestFile);
    SyncItemWriter writer = getSyncItemWriter(requestFile);
    File responseDir = new File(this.directory, "reponse");
    if (!responseDir.isAbsolute()) {
        responseDir = new File(System.getProperty("otas.dm.home"), this.directory + "/reponse");
    }
    SyncResultWriter result = new SyncResultWriter(requestFile, responseDir);

    SyncProcessor processor = new SyncProcessor(reader, writer, result);
    return processor;
}