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

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

Introduction

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

Prototype

public static String toString(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all characters from a URL into a String , using the given character set.

Usage

From source file:zipkin.elasticsearch.ElasticsearchConfig.java

ElasticsearchConfig(Builder builder) {
    clusterName = checkNotNull(builder.cluster, "builder.cluster");
    hosts = checkNotNull(builder.hosts, "builder.hosts");
    index = checkNotNull(builder.index, "builder.index");

    try {/*  ww  w . j  a v  a2 s.  c o  m*/
        indexTemplate = Resources.toString(Resources.getResource("zipkin/elasticsearch/zipkin_template.json"),
                StandardCharsets.UTF_8).replace("${__INDEX__}", index);
    } catch (IOException e) {
        throw new AssertionError("Error reading jar resource, shouldn't happen.", e);
    }
}

From source file:ezbake.data.mongo.helper.MongoFindHelper.java

public MongoFindHelper(EzMongoHandler handler) {
    this.ezMongoHandler = handler;

    // Load up the JSON from the resources.  Throw RuntimeExecptions as there's no reason it should barf in prod
    try {/*from   w  w  w  . jav  a  2  s .com*/
        final URL sevURL = Thread.currentThread().getContextClassLoader()
                .getResource("securityExpressionViz.json");
        if (sevURL == null) {
            throw new RuntimeException("The securityExpressionVis was missing from the JAR");
        }
        securityExpressionViz = Resources.toString(sevURL, Charsets.UTF_8);

        final URL seoURL = Thread.currentThread().getContextClassLoader()
                .getResource("securityExpressionOperation.json");
        if (seoURL == null) {
            throw new RuntimeException("The securityExpressionVis was missing from the JAR");
        }
        securityExpressionOperation = Resources.toString(seoURL, Charsets.UTF_8);
    } catch (IOException e) {
        appLog.error(e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:com.facebook.presto.server.QueryExecutionResource.java

@GET
@Path("/ui/query-execution")
@Produces(MediaType.TEXT_HTML)//ww  w  .  j  av  a  2  s .  co m
public String getUi() throws IOException {
    return Resources.toString(getResource(getClass(), "query-execution.html"), StandardCharsets.UTF_8);
}

From source file:net.minecraftforge.gradle.patcher.TaskGenSubprojects.java

public TaskGenSubprojects() throws IOException {
    super();//from ww w .  j a  va2 s  .  c om
    resource = Resources.toString(Resources.getResource(TaskGenSubprojects.class, "globalGradle"),
            Constants.CHARSET);
}

From source file:org.kiji.bento.ConfigurationBuilder.java

/**
 * Constructs a new configuration builder.  The name specified for the configuration will be
 * used to generate a filename for the configuration. The filename will be prefixed with
 * "bento-" and suffixed with ".xml"./*from   w  ww  .  j  a v a2s . c o  m*/
 *
 * @param name The name of the configuration, for example, "mapred-site" or "core-site."
 * @param commentResource The name of a resource that contains a usage comment for the
 *     configuration.
 */
public ConfigurationBuilder(String name, String commentResource) {
    URL resourceURL = Resources.getResource(commentResource);
    String comment;
    try {
        comment = Resources.toString(resourceURL, Charset.forName("UTF-8"));
    } catch (IOException e) {
        throw new RuntimeException("Error when generating a Hadoop site XML file being: "
                + "comment could not be read from resource: " + commentResource);
    }
    mWriter = new ConfigurationWriter(comment);
    mFileName = "bento-" + name + ".xml";
}

From source file:org.thingsboard.server.dao.CustomSqlUnit.java

@Override
public void before() {
    cleanUpDb();/*from www. j  ava 2  s  . c o  m*/

    Connection conn = null;
    try {
        conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        for (String sqlFile : sqlFiles) {
            URL sqlFileUrl = Resources.getResource(sqlFile);
            String sql = Resources.toString(sqlFileUrl, Charsets.UTF_8);
            conn.createStatement().execute(sql);
        }
    } catch (IOException | SQLException e) {
        throw new RuntimeException("Unable to start embedded hsqldb. Reason: " + e.getMessage(), e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.sonar.plugins.java.JavaSonarWayProfile.java

private static String readResource(URL resource) {
    try {/*  www .j ava  2  s  .co m*/
        return Resources.toString(resource, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to read: " + resource, e);
    }
}

From source file:gg.uhc.githubreleasechecker.deserialization.LatestReleaseQueryer.java

public Release[] queryReleases() throws IOException {
    final String rawJson = Resources.toString(url, Charsets.UTF_8);

    return gson.fromJson(rawJson, Release[].class);
}

From source file:net.minecraftforge.gradle.common.BaseExtension.java

public BaseExtension(BasePlugin<? extends BaseExtension> plugin) {
    this.project = plugin.project;
    this.replacer = plugin.replacer;

    String version;/*from w  w  w . j  a  v a2  s. co  m*/
    try {
        URL url = BaseExtension.class.getClassLoader().getResource("forgegradle.version.txt");
        version = Resources.toString(url, Constants.CHARSET).trim();

        if (version.equals("${version}")) {
            version = "2.0-SNAPSHOT";// fallback
        }

    } catch (Exception e) {
        // again, the fallback
        version = "2.0-SNAPSHOT";
    }

    forgeGradleVersion = version;
}

From source file:com.stratio.decision.unit.engine.action.CassandraServer.java

/**
 * Set embedded cassandra up and spawn it in a new thread.
 * /*from   www .  jav a2  s  .  com*/
 * @throws TTransportException
 * @throws IOException
 * @throws InterruptedException
 */
public void start() throws TTransportException, IOException, InterruptedException, ConfigurationException {

    File dir = Files.createTempDir();
    String dirPath = dir.getAbsolutePath();
    System.out.println("Storing Cassandra files in " + dirPath);

    URL url = Resources.getResource("cassandra.yaml");
    String yaml = Resources.toString(url, Charsets.UTF_8);
    yaml = yaml.replaceAll("REPLACEDIR", dirPath);
    String yamlPath = dirPath + File.separatorChar + "cassandra.yaml";
    org.apache.commons.io.FileUtils.writeStringToFile(new File(yamlPath), yaml);

    // make a tmp dir and copy cassandra.yaml and log4j.properties to it
    copy("/log4j.properties", dir.getAbsolutePath());
    System.setProperty("cassandra.config", "file:" + dirPath + yamlFilePath);
    System.setProperty("log4j.configuration", "file:" + dirPath + "/log4j.properties");
    System.setProperty("cassandra-foreground", "true");

    cleanupAndLeaveDirs();

    try {
        executor.execute(new CassandraRunner());
    } catch (RejectedExecutionException e) {
        log.error("RejectError", e);
        return;
    }

    try {
        TimeUnit.SECONDS.sleep(WAIT_SECONDS);
    } catch (InterruptedException e) {
        log.error("InterrputedError", e);
        throw new AssertionError(e);
    }
}