Example usage for com.google.common.io Resources getResource

List of usage examples for com.google.common.io Resources getResource

Introduction

In this page you can find the example usage for com.google.common.io Resources getResource.

Prototype

public static URL getResource(String resourceName) 

Source Link

Document

Returns a URL pointing to resourceName if the resource is found using the Thread#getContextClassLoader() context class loader .

Usage

From source file:org.killbill.xmlloader.UriAccessor.java

public static InputStream accessUri(URI uri) throws IOException, URISyntaxException {
    final String scheme = uri.getScheme();

    final URL url;
    if (scheme == null) {
        uri = new URI(Resources.getResource(uri.toString()).toExternalForm());
    } else if (scheme.equals(URI_SCHEME_FOR_CLASSPATH)) {
        if (uri.toString().startsWith(URI_SCHEME_FOR_ARCHIVE_FILE)) {
            return getInputStreamFromJarFile(uri);
        } else {// w  ww  .j  a va  2  s.c o m
            return UriAccessor.class.getResourceAsStream(uri.getPath());
        }
    } else if (scheme.equals(URI_SCHEME_FOR_FILE) && !uri.getSchemeSpecificPart().startsWith("/")) { // interpret URIs of this form as relative path uris
        uri = new File(uri.getSchemeSpecificPart()).toURI();
    }
    url = uri.toURL();
    return url.openConnection().getInputStream();
}

From source file:com.streamsets.datacollector.dev.standalone.DevPipelineOperationsIT.java

private static String getPipelineJson() throws Exception {
    URI uri = Resources.getResource("dev_pipeline_operations.json").toURI();
    return new String(Files.readAllBytes(Paths.get(uri)), StandardCharsets.UTF_8);
}

From source file:com.cloudera.crash.generics.CustomData.java

public static Schema schemaFromResource(String resource) {
    try {// w  w w . ja v a 2s .  c om
        return new Schema.Parser().parse(Resources.getResource(resource).openStream());
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:org.apache.sentry.provider.file.PolicyFiles.java

public static void copyToDir(File dest, String... resources) throws FileNotFoundException, IOException {
    for (String resource : resources) {
        LOGGER.info("Copying " + resource + " to " + dest);
        Resources.copy(Resources.getResource(resource), new FileOutputStream(new File(dest, resource)));
    }/*from  www .j a  v  a 2 s.  co m*/
}

From source file:com.netflix.iep.config.ScopedPropertiesLoader.java

public static Properties load(String[] propFiles) {
    List<URL> propUrls = new ArrayList<>();
    for (int i = 0; i < propFiles.length; i++) {
        String propFile = propFiles[i];
        try {//from w  ww  .  j av  a2  s  .com
            propUrls.add((Resources.getResource(propFile)));
        } catch (IllegalArgumentException e) {
            LOGGER.debug("ignoring " + propFile + " - does not exist");
        }
    }

    StringBuilder debug = new StringBuilder("# Generated properties\n");

    Properties finalProps = new Properties();

    for (URL propUrl : propUrls) {
        LOGGER.debug("loading properties from " + propUrl);
        String propData;
        try {
            propData = Resources.toString(propUrl, Charsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String propString = ConfigFile.toPropertiesString(System.getenv(), propData);
        debug.append(propString).append("\n");

        Properties props = ConfigFile.loadProperties(System.getenv(), propData);
        LOGGER.debug("loading properties: " + props);
        finalProps.putAll(props);
        LOGGER.info("loaded properties file " + propUrl);
    }

    LOGGER.debug("properties debug: " + debug);
    return finalProps;
}

From source file:org.trnltk.morphology.lexicon.DictionaryLoader.java

public static HashSet<Lexeme> loadDefaultMasterDictionary() {
    final CharSource charSource = Resources.asCharSource(Resources.getResource("master-dictionary.dict"),
            Charset.forName("utf-8"));
    return new DictionaryLoader().load(charSource);
}

From source file:de.unima.core.io.file.XESImporter.java

private static InputStream openStream(final String resourceName) {
    final URL fileUrl = Resources.getResource(resourceName);
    try {/*from w ww . j a v a 2  s .c om*/
        return Resources.asByteSource(fileUrl).openStream();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.sentry.core.common.utils.PolicyFiles.java

public static void copyToDir(File dest, String... resources) throws FileNotFoundException, IOException {
    for (String resource : resources) {
        LOGGER.debug("Copying " + resource + " to " + dest);
        Resources.copy(Resources.getResource(resource), new FileOutputStream(new File(dest, resource)));
    }//w  ww.  java 2s .c  om
}

From source file:org.dswarm.graph.utils.GraphDatabaseUtils.java

public static String determineMapDBIndexStoreDir(final GraphDatabaseService database) {

    final URL resource = Resources.getResource("dmpgraph.properties");
    final Properties properties = new Properties();

    try {// w ww  .j  a  v a2 s  .co m

        properties.load(resource.openStream());
    } catch (final IOException e) {

        LOG.error("Could not load dmpgraph.properties", e);
    }

    final String indexStoreDir = properties.getProperty("index_store_dir");

    // TODO: find a better way to determine the store dir for the statement index
    String storeDir = null;

    if (indexStoreDir != null && !indexStoreDir.trim().isEmpty()) {

        storeDir = indexStoreDir;
    } else if (getGraphDatabaseStoreDir(database) != null) {

        final String databaseStoreDir = getGraphDatabaseStoreDir(database);

        if (databaseStoreDir != null) {

            final Path databaseStoreDirPath = Paths.get(databaseStoreDir);

            if (databaseStoreDirPath != null) {

                if (Files.exists(databaseStoreDirPath)) {

                    final String newIndexStoreDir = databaseStoreDir + File.separator + MapDBUtils.INDEX_DIR;

                    final Path newIndexStoreDirPath = Paths.get(newIndexStoreDir);

                    if (Files.notExists(newIndexStoreDirPath)) {

                        try {

                            Files.createDirectory(newIndexStoreDirPath);

                            storeDir = newIndexStoreDir;
                        } catch (final IOException e) {

                            LOG.debug("couldn't create map db index directory");
                        }
                    } else {

                        storeDir = newIndexStoreDir;
                    }
                }
            }
        }
    }

    if (storeDir == null) {

        // fallback default
        storeDir = System.getProperty("user.dir") + "/target";

        LOG.debug("utilise fallback default for mapdb index store dir");
    }

    LOG.debug("mapdb index store dir '{}'", storeDir);

    return storeDir;
}

From source file:com.boundary.metrics.vmware.VMWareClientFactory.java

public static VMwareClient createClient(String propertyFile) throws URISyntaxException, IOException {
    final String URL = "com.boundary.metrics.vmware.client.url";
    final String USER = "com.boundary.metrics.vmware.client.user";
    final String PASSWORD = "com.boundary.metrics.vmware.client.password";

    File propertiesFile = new File(Resources.getResource(propertyFile).toURI());
    assumeTrue(propertiesFile.exists());
    Reader reader = new FileReader(propertiesFile);
    Properties clientProperties = new Properties();
    clientProperties.load(reader);/*from  w  ww.  j a va2s.  c o  m*/
    String url = clientProperties.getProperty(URL);
    String user = clientProperties.getProperty(USER);
    String password = clientProperties.getProperty(PASSWORD);

    return new VMwareClient(new URI(url), user, password, VMWareClientFactory.class.toString());
}