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:com.netflix.astyanax.test.EmbeddedCassandra.java

public EmbeddedCassandra(File dataDir, String clusterName, int port, int storagePort) throws IOException {
    LOG.info("Starting cassandra in dir " + dataDir);
    this.dataDir = dataDir;
    dataDir.mkdirs();//w w w  . j  a  va2 s.co m

    InputStream is = null;

    try {
        URL templateUrl = EmbeddedCassandra.class.getClassLoader().getResource("cassandra-template.yaml");
        Preconditions.checkNotNull(templateUrl, "Cassandra config template is null");
        String baseFile = Resources.toString(templateUrl, Charset.defaultCharset());

        String newFile = baseFile.replace("$DIR$", dataDir.getPath());
        newFile = newFile.replace("$PORT$", Integer.toString(port));
        newFile = newFile.replace("$STORAGE_PORT$", Integer.toString(storagePort));
        newFile = newFile.replace("$CLUSTER$", clusterName);

        File configFile = new File(dataDir, "cassandra.yaml");
        Files.write(newFile, configFile, Charset.defaultCharset());

        LOG.info("Cassandra config file: " + configFile.getPath());
        System.setProperty("cassandra.config", "file:" + configFile.getPath());

        try {
            cassandra = new CassandraDaemon();
            cassandra.init(null);
        } catch (IOException e) {
            LOG.error("Error initializing embedded cassandra", e);
            throw e;
        }
    } finally {
        Closeables.closeQuietly(is);
    }
    LOG.info("Started cassandra deamon");
}

From source file:com.google.cloud.spanner.ReadFormatTestRunner.java

@Override
protected List<JSONObject> getChildren() {
    try {/* ww w  . j a va2 s .c  om*/
        List<JSONObject> children = new ArrayList<>();
        String jsonStr = Resources.toString(Resources.getResource(this.getClass(), "read_tests.json"),
                StandardCharsets.UTF_8);
        JSONObject json = new JSONObject(jsonStr);
        JSONArray testCases = json.getJSONArray("tests");
        for (int i = 0; i < testCases.length(); i++) {
            JSONObject testCase = testCases.getJSONObject(i);
            children.add(testCase);
        }
        return children;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:eu.redzoo.article.planetcassandra.reactive.CassandraDB.java

/**
 * executes a CQL file //  w  w w.j a va  2  s  .  c o  m
 * 
 * @param cqlFile    the CQL file name 
 * @throws IOException  if the file could not be found
 */
public void executeCqlFile(String cqlFile) throws IOException {
    File file = new File(cqlFile);
    if (file.exists()) {
        executeCql(Files.toString(new File(cqlFile), Charsets.UTF_8));
    } else {
        executeCql(Resources.toString(Resources.getResource(cqlFile), Charsets.UTF_8));
    }
}

From source file:org.cloudfoundry.community.servicebroker.s3.config.BrokerConfiguration.java

@Bean
public BucketGroupPolicy bucketGroupPolicy() throws IOException {
    URL url = new ClassPathResource("default-bucket-policy.json").getURL();
    String policyDocument = Resources.toString(url, Charsets.UTF_8);
    return new BucketGroupPolicy(policyDocument);
}

From source file:io.soliton.protobuf.plugin.ProtoServiceHandler.java

public void handle(ServiceDescriptorProto service) throws IOException {
    ImmutableList.Builder<ServiceHandlerData.Method> methods = ImmutableList.builder();
    for (MethodDescriptorProto method : service.getMethodList()) {
        ServiceHandlerData.Method methodData = new ServiceHandlerData.Method(method.getName(),
                CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, method.getName()),
                types.lookup(method.getInputType()).toString(),
                types.lookup(method.getOutputType()).toString());
        methods.add(methodData);/*from  w ww .java2s  .  c  om*/
    }

    String fullName = Joiner.on('.').skipNulls().join(protoPackage, service.getName());

    ServiceHandlerData.Service serviceData = new ServiceHandlerData.Service(service.getName(), fullName,
            methods.build());
    ServiceHandlerData data = new ServiceHandlerData(javaPackage, multipleFiles, serviceData);

    String template = Resources.toString(Resources.getResource(this.getClass(), "service_class.mvel"),
            Charsets.UTF_8);
    String serviceFile = (String) TemplateRuntime.eval(template,
            ImmutableMap.<String, Object>of("handler", data));

    CodeGeneratorResponse.Builder response = CodeGeneratorResponse.newBuilder();
    CodeGeneratorResponse.File.Builder file = CodeGeneratorResponse.File.newBuilder();
    file.setContent(serviceFile);
    file.setName(javaPackage.replace('.', '/') + '/' + service.getName() + ".java");
    if (!multipleFiles) {
        file.setName(javaPackage.replace('.', '/') + '/' + outerClassName + ".java");
        file.setInsertionPoint("outer_class_scope");
    }
    response.addFile(file);
    response.build().writeTo(output);
}

From source file:com.facebook.buck.tools.documentation.generator.skylark.rendering.SoyTemplateSkylarkSignatureRenderer.java

private static String loadTemplate(String templateName) throws IOException {
    URL template = Resources.getResource(SoyTemplateSkylarkSignatureRenderer.class, templateName);
    return Resources.toString(template, StandardCharsets.UTF_8);
}

From source file:org.jclouds.scriptbuilder.functionloader.osgi.BundleFunctionLoader.java

/**
 * Loads the function from the {@link Bundle} resources.
 * /*w  w w.  j  ava 2s.  co  m*/
 * @param function
 *           The function name to load.
 * @param family
 *           This operating system family of the function.
 * @return
 * @throws FunctionNotFoundException
 */
@Override
public String loadFunction(String function, OsFamily family) throws FunctionNotFoundException {
    try {
        return Resources.toString(bundleContext.getBundle().getResource(
                String.format("/functions/%s.%s", function, ShellToken.SH.to(family))), Charsets.UTF_8);
    } catch (IOException e) {
        throw new FunctionNotFoundException(function, family, e);
    }
}

From source file:com.twosigma.beaker.clojure.util.ClojureEvaluator.java

protected String initScriptSource() throws IOException {
    URL url = this.getClass().getClassLoader().getResource("init_clojure_script.txt");
    return Resources.toString(url, Charsets.UTF_8);
}

From source file:org.jsfr.json.BenchmarkCollectSingleValue.java

@Setup
public void setup() throws Exception {
    gson = new GsonBuilder().create();
    om = new ObjectMapper();
    gsonSurfer = JsonSurfer.gson();//from   w w  w .j av  a  2  s.  c om
    jacksonSurfer = JsonSurfer.jackson();
    simpleSurfer = JsonSurfer.simple();
    collectOneListener = new CollectOneListener(true);
    surfingConfiguration = SurfingConfiguration.builder().bind("$.store.book[0].author", collectOneListener)
            .build();
    json = Resources.toString(Resources.getResource("sample.json"), StandardCharsets.UTF_8);
}

From source file:google.registry.ui.server.SoyTemplateUtils.java

private static ImmutableMap<String, String> getCssRenames(URL cssMap, URL cssMapDebug) {
    try {/*w  w w .  j ava  2  s .c  om*/
        switch (ConsoleDebug.get()) {
        case RAW:
            return ImmutableMap.of(); // See firstNonNull() above for clarification.
        case DEBUG:
            return extractCssRenames(Resources.toString(cssMapDebug, UTF_8));
        default:
            return extractCssRenames(Resources.toString(cssMap, UTF_8));
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to load css map", e);
    }
}