Example usage for java.io File canRead

List of usage examples for java.io File canRead

Introduction

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

Prototype

public boolean canRead() 

Source Link

Document

Tests whether the application can read the file denoted by this abstract pathname.

Usage

From source file:com.simpligility.maven.plugins.androidndk.AndroidNdk.java

public static void validateToolchainDirectory(File toolchainDirectory) throws MojoExecutionException {
    if (!toolchainDirectory.exists()) {
        //  if we got here, throw an error
        throw new MojoExecutionException("Toolchain directory " + toolchainDirectory + " does not exist");
    }/*w  ww.j av a2s  . com*/
    if (!toolchainDirectory.canRead()) {
        //  if we got here, throw an error
        throw new MojoExecutionException(
                "Toolchain directory " + toolchainDirectory + " exist but can not be read");
    }
}

From source file:net.roboconf.agent.internal.misc.UserDataUtils.java

/**
 * Configures the agent from VMWare./*ww w.jav  a  2 s.  c  o m*/
 * @param logger a logger
 * @return the agent's data, or null if they could not be parsed
 */
public static AgentProperties findParametersForVmware(Logger logger) {

    File propertiesFile = new File("/tmp/roboconf.properties");
    try {
        int retries = 30;
        while ((!propertiesFile.exists() || !propertiesFile.canRead()) && retries-- > 0) {
            logger.fine("Agent tries to read properties file " + propertiesFile + ": trial #" + (30 - retries));
            try {
                Thread.sleep(2000);

            } catch (InterruptedException e) {
                throw new IOException("Can't read properties file: " + e);
            }
        }

        AgentProperties result = AgentProperties.readIaasProperties(Utils.readPropertiesFile(propertiesFile));

        /*
         * HACK for specific IaaS configurations (using properties file in a VMWare-like manner)
         * Try to pick IP address... in the case we are on OpenStack or any IaaS with amazon-compatible API
         * Some configurations (with floating IPs) do not provide network interfaces exposing public IPs !
         */
        InputStream in = null;
        try {
            URL userDataUrl = new URL("http://169.254.169.254/latest/meta-data/public-ipv4");
            in = userDataUrl.openStream();
            ByteArrayOutputStream os = new ByteArrayOutputStream();

            Utils.copyStreamSafely(in, os);
            String ip = os.toString("UTF-8");
            if (!AgentUtils.isValidIP(ip)) {
                // Failed retrieving public IP: try private one instead
                Utils.closeQuietly(in);
                userDataUrl = new URL("http://169.254.169.254/latest/meta-data/local-ipv4");
                in = userDataUrl.openStream();
                os = new ByteArrayOutputStream();

                Utils.copyStreamSafely(in, os);
                ip = os.toString("UTF-8");
            }

            if (AgentUtils.isValidIP(ip))
                result.setIpAddress(os.toString("UTF-8"));

        } catch (IOException e) {
            Utils.logException(logger, e);
        } finally {
            Utils.closeQuietly(in);
        }
        /* HACK ends here (see comment above). Removing it is harmless on classical VMWare configurations. */

        return result;

    } catch (IOException e) {
        logger.fine("Agent failed to read properties file " + propertiesFile);
        return null;
    }
}

From source file:com.varaneckas.hawkscope.util.OSUtils.java

/**
 * Gets the File System Roots//  w w  w  .ja va 2s.c o m
 * 
 * @return
 */
public static List<File> getFileSystemRoots() {
    final List<File> roots = new LinkedList<File>();
    switch (CURRENT_OS) {
    case MAC: {
        final File[] volumes = new File("/Volumes").listFiles();
        if (volumes.length == 0) {
            //something's wrong..
            roots.add(new File("/"));
        } else {
            for (final File f : volumes) {
                if (f.canRead() && !isMacApp(f)) {
                    roots.add(f);
                }
            }
        }
        return roots;
    }
    case UNIX:
        roots.add(new File("/"));
        //Ubuntu
        File media = new File("/media");
        if (!media.exists()) {
            //Old School Unix fallback
            media = new File("/mnt");
        }
        if (media.exists()) {
            for (final File f : media.listFiles()) {
                if (f.isDirectory() && f.canRead()) {
                    roots.add(f);
                }
            }
        }
        return roots;
    default: {
        return Arrays.asList(File.listRoots());
    }
    }
}

From source file:net.sourceforge.vulcan.ant.AntBuildTool.java

public static File getAntHomeResource(String antHome, String resource) throws ConfigException {
    if (StringUtils.isBlank(antHome)) {
        throw new ConfigException("ant.home.required", null);
    }/*from   www . j  a  va 2s. c o m*/

    final File launcher = new File(antHome, resource);

    if (!launcher.canRead()) {
        throw new ConfigException("ant.home.missing.resource", new String[] { resource, antHome });
    }
    return launcher;
}

From source file:br.com.autonomiccs.cloudTraces.main.GoogleTracesToCloudTracesParser.java

private static void validateArguments(String[] args) {
    if (args.length != 1) {
        throw new GoogleTracesToCloudTracesException(
                "You should inform the full qualified path to the Google traces data set.");
    }//from w  w w  .j a  v  a 2s.  c  o  m
    File file = new File(args[0]);
    if (!file.exists()) {
        throw new GoogleTracesToCloudTracesException(String.format("File [%s] does not exist.", args[0]));
    }
    if (!file.canRead()) {
        throw new GoogleTracesToCloudTracesException(String.format("Cannot read file [%s] .", args[0]));
    }
}

From source file:it.geosolutions.geobatch.figis.intersection.Utilities.java

/*****
 * This method takes a wfs url, download the layername features, save it in
 * the figisTmpDir directory and return its SimpleFEatureCollection
 *
 * @param textUrl//from w w w.  j av a 2 s.  c o m
 * @param filename
 * @param destDir
 * @throws IOException
 * @throws MalformedURLException
 */
static String getShapeFileFromURLbyZIP(String textUrl, String figisTmpDir, String layername)
        throws MalformedURLException, IOException {
    // init the folder name where to save and uncompress the zip file
    String destDir = figisTmpDir + "/" + layername;

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Destination dir " + destDir);
    }

    File finalDestDir = new File(destDir);

    if (!finalDestDir.exists()) // check if the temp dir exist, if not:
                                // create it, download the zip and
                                // uncompress the files
    {
        finalDestDir.mkdir();
        try {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("downloading from : " + textUrl);
            }
            saveZipToLocal(textUrl, destDir, layername);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("download completed successfully");
            }
        } catch (Exception e) {
            LOGGER.error("Error downloading the zip file", e);
            throw new IOException("Error downloading the zip file", e);
        }
        try {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Extracting the zip file " + destDir + "/" + layername + ".zip");
            }
            Extract.extract(destDir + "/" + layername + ".zip");
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Extraction completed successfully");
            }
        } catch (Exception e) {
            // some exception during the file extraction, return a null
            // value
            LOGGER.error("Error extracting the zip file", e);
            throw new IOException("Error extracting the zip file", e);
        }
        // extractZipFile(destDir, layername);
    }

    // return the simple feature collection from the uncompressed shp file
    // name
    String shpfilename = figisTmpDir + "/" + layername + "/" + layername + "/" + layername + ".shp";
    File shpFile = new File(shpfilename);
    if (shpFile.exists() && shpFile.canRead() && shpFile.isFile()) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Shpfilename: " + shpfilename);
        }
        return shpfilename;
    } else {
        File shpFileDir = new File(figisTmpDir + "/" + layername + "/" + layername);
        if (shpFileDir.exists() && shpFileDir.isDirectory()) {
            File[] shpFiles = shpFileDir.listFiles(new FilenameFilter() {

                public boolean accept(File dir, String name) {
                    if (FilenameUtils.getExtension(name).equalsIgnoreCase("shp"))
                        return true;
                    return false;
                }
            });

            if (shpFiles != null && shpFiles.length == 1) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Shpfilename: " + shpfilename);
                }
                return shpFiles[0].getAbsolutePath();
            } else {
                LOGGER.error("Could not download shapefile from GeoServer");
                throw new IOException("Could not download shapefile from GeoServer");
            }
        } else {
            LOGGER.error("Could not download shapefile from GeoServer");
            throw new IOException("Could not download shapefile from GeoServer");
        }
    }
}

From source file:com.pieframework.model.repository.ModelStore.java

public static Repositories getRepositories(File file) throws SimpleSerializerException, IOException,
        ParserConfigurationException, SAXException, TransformerException {

    if (file == null || !file.canRead()) {
        throw new IllegalArgumentException("Cannot build Model from given file.");
    }/*from   w  w  w  .  j ava2 s .c  om*/

    StringWriter writer = new StringWriter();
    processXIncludes(file, writer);

    // Deserialize a System File
    Repositories repos = DefaultSerializer.read(Repositories.class, IOUtils.toInputStream(writer.toString()));

    return repos;
}

From source file:com.pieframework.model.repository.ModelStore.java

public static Operations getOperations(File file) throws SimpleSerializerException, IOException,
        ParserConfigurationException, SAXException, TransformerException {

    if (file == null || !file.canRead()) {
        throw new IllegalArgumentException("Cannot build Model from given file.");
    }//from  w  w  w .  j a va 2 s .  c o m

    StringWriter writer = new StringWriter();
    processXIncludes(file, writer);

    // Deserialize a System File
    Operations operations = DefaultSerializer.read(com.pieframework.model.operations.Operations.class,
            IOUtils.toInputStream(writer.toString()));

    return operations;
}

From source file:com.hoccer.tools.TestHelper.java

public static File[] getFiles(String dirName, final String pFileEnding) throws Exception {
    File dataDir = new File(dirName);
    Assert.assertTrue("dir should exists", dataDir.exists());
    Assert.assertTrue(dirName + " should be a directory", dataDir.isDirectory());
    Assert.assertTrue("dir should be readable", dataDir.canRead());

    FileFilter filter = new FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(pFileEnding);
        }/*from   www .j a  va  2s .  c  o  m*/
    };

    return dataDir.listFiles(filter);
}

From source file:com.pieframework.model.repository.ModelStore.java

public static Resources getResources(File file) throws SimpleSerializerException, IOException,
        ParserConfigurationException, SAXException, TransformerException {

    if (file == null || !file.canRead()) {
        throw new IllegalArgumentException("Cannot build Model from given file.");
    }//  w  ww. ja  v a2s .c o m

    StringWriter writer = new StringWriter();
    processXIncludes(file, writer);

    // Deserialize a System File
    Resources resources;
    resources = DefaultSerializer.read(com.pieframework.model.resources.Resources.class,
            IOUtils.toInputStream(writer.toString()));

    return resources;
}