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:cz.fb.dnepr.core.ssh.Utils.java

private static String getFile(String resource) {
    return Resources.getResource(resource).getFile();
}

From source file:com.streamsets.pipeline.stage.origin.mysql.Utils.java

public static void runInitScript(String initScriptPath, DataSource dataSource) throws SQLException {
    try (Connection conn = dataSource.getConnection()) {
        try {/*from www  . ja  v a  2  s.c  om*/
            URL resource = Resources.getResource(initScriptPath);
            String sql = Resources.toString(resource, Charsets.UTF_8);
            ScriptUtils.executeSqlScript(conn, initScriptPath, sql);
            conn.commit();
            conn.close();
        } catch (IOException | IllegalArgumentException e) {
            LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
            throw new SQLException("Could not load classpath init script: " + initScriptPath, e);
        } catch (ScriptException e) {
            LOGGER.error("Error while executing init script: {}", initScriptPath, e);
            throw new SQLException("Error while executing init script: " + initScriptPath, e);
        }
    }
}

From source file:org.trnltk.tokenizer.data.TokenizerTrainingData.java

public static TokenizerTrainingData createDefaultTrainingData() throws IOException {
    URL resourceURL = Resources.getResource("tokenizer/training-data.yaml");
    ByteSource byteSource = Resources.asByteSource(resourceURL);
    return createFromYamlByteSource(byteSource);
}

From source file:com.articulate.sigma.test.JsonReader.java

public static <T> Collection<T> transform(String resourcePath, Function<JSONObject, T> transformer) {
    Collection<T> result = Lists.newArrayList();

    URL jsonTests = Resources.getResource(resourcePath);
    System.out.println("Reading JSON file: " + jsonTests.getPath());
    JSONParser parser = new JSONParser();
    try {//from  w  w  w  . j  a v a  2 s. c o m
        Object obj = parser.parse(Resources.toString(jsonTests, Charsets.UTF_8));
        JSONArray jsonObject = (JSONArray) obj;
        ListIterator<JSONObject> li = jsonObject.listIterator();
        while (li.hasNext()) {
            JSONObject jo = li.next();
            result.add(transformer.apply(jo));
        }
    } catch (Exception e) {
        System.out.println("Parse exception reading: " + resourcePath);
        System.out.println(e.getMessage());
        e.printStackTrace();
        throw new RuntimeException("Parse exception reading: " + resourcePath);
    }
    return result;
}

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

public static void load(String propFile, Map<String, String> subs, Map<String, String> overrides)
        throws IOException {
    URL propUrl = Resources.getResource(propFile);
    String propData = Resources.toString(propUrl, Charsets.UTF_8);
    for (Map.Entry e : subs.entrySet()) {
        propData = propData.replaceAll("\\{" + e.getKey() + "\\}", (String) e.getValue());
    }/*from  ww w.ja va2 s  .  c om*/
    final Properties props = new Properties();
    props.load(new ByteArrayInputStream(propData.getBytes()));
    for (Map.Entry e : overrides.entrySet()) {
        props.setProperty((String) e.getKey(), (String) e.getValue());
    }
    Configuration.setBackingStore(new IConfiguration() {
        @Override
        public String get(String key) {
            return (String) props.getProperty(key);
        }
    });
}

From source file:com.openlattice.mail.utils.TemplateUtils.java

public static String loadTemplate(String templatePath) throws IOException {
    URL templateResource = Resources.getResource(templatePath);
    return Resources.toString(templateResource, Charsets.UTF_8);
}

From source file:keywhiz.testing.ContentCryptographers.java

public static SecretKey derivationKey() {
    if (derivationKey != null) {
        return derivationKey;
    }/*  w ww  .j av a  2s. co m*/

    char[] password = "CHANGE".toCharArray();
    try (InputStream in = Resources.getResource("derivation.jceks").openStream()) {
        KeyStore keyStore = KeyStore.getInstance("JCEKS");
        keyStore.load(in, password);
        derivationKey = (SecretKey) keyStore.getKey("basekey", password);
    } catch (CertificateException | UnrecoverableKeyException | KeyStoreException | NoSuchAlgorithmException
            | IOException e) {
        throw Throwables.propagate(e);
    }
    return derivationKey;
}

From source file:org.p2pvpn.tuntap.TunTap.java

/**
 * Load a libary (*.so or *.dll).//from w w  w . j ava 2s .c om
 * @param libs the libary names
 * @throws java.io.IOException
 */
static void loadLib(String... libs) throws Throwable {
    Throwable e = null;

    for (String lib : libs) {
        try {
            String path = Resources.getResource(lib).getPath();
            System.out.println(path);
            System.load(Resources.getResource(lib).getPath());
            break;
        } catch (Throwable eio) {
            e = eio;
        }
    }
    if (e != null)
        throw e;
}

From source file:eu.leads.processor.planner.ClassUtil.java

public static Set<Class> findClasses(Class type, String packageFilter) {
    Set<Class> classSet = new HashSet<Class>();

    String classpath = Resources.getResource("libs").getPath();// getResource(".").getPath();//System.getProperty("java.class.path");
    String[] paths = classpath.split(System.getProperty("path.separator"));

    for (String path : paths) {
        File file = new File(path);
        if (file.exists()) {
            findClasses(classSet, file, file, true, type, packageFilter);
        }// w w  w .  j ava  2s  .  c o  m
    }

    return classSet;
}

From source file:uk.os.vt.demo.Schemas.java

private static Metadata inflate(String resource) {
    final URL url = Resources.getResource(resource);
    Metadata inflated;/*w w w.j  a  va 2s .  c om*/
    try {
        String schemaString = Resources.toString(url, Charsets.UTF_8);
        inflated = new Metadata.Builder().setTileJson(new JSONObject(schemaString)).build();
    } catch (final IOException ex) {
        LOG.error("problem inflating local schema", ex);
        inflated = new Metadata.Builder().build();
    } catch (JSONException je) {
        LOG.error("problem inflating JSON", je);
        inflated = new Metadata.Builder().build();
    }
    return inflated;
}