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:org.apache.drill.BaseTestQuery.java

public static String getFile(String resource) throws IOException {
    URL url = Resources.getResource(resource);
    if (url == null) {
        throw new IOException(String.format("Unable to find path %s.", resource));
    }/*from   ww w  . ja va2s  . c o m*/
    return Resources.toString(url, Charsets.UTF_8);
}

From source file:org.apache.drill.test.ClusterFixture.java

public static String getResource(String resource) throws IOException {
    // Unlike the Java routines, Guava does not like a leading slash.

    final URL url = Resources.getResource(trimSlash(resource));
    if (url == null) {
        throw new IOException(String.format("Unable to find resource %s.", resource));
    }/*ww w.j a va 2s  .c om*/
    return Resources.toString(url, Charsets.UTF_8);
}

From source file:fr.xebia.vcloud.AbstractVcloud.java

/**
 * Load the bootstrap script for a VM//from  w  ww  .  j  av a  2 s . c  o  m
 *
 * @param vmName the Name of the VM
 * @return The bootstrap script associated to this VM
 * @throws java.io.IOException Error while reading script
 */
public static String loadScriptByVmName(String vmName) throws IOException {
    Map<String, String> mapScripts = Maps.newHashMap();
    mapScripts.put(VM_APACHE_RP, "bootstrap_rp.sh");
    mapScripts.put(VM_MYSQL, "bootstrap_mysql.sh");
    mapScripts.put(VM_TOMCAT_1, "bootstrap_tomcat.sh");
    mapScripts.put(VM_TOMCAT_2, "bootstrap_tomcat.sh");
    String scriptName = mapScripts.get(vmName);
    URL resource = Resources.getResource(scriptName);
    String script = Resources.toString(resource, Charset.defaultCharset());
    return script;
}

From source file:com.epam.dlab.backendapi.core.commands.CommandExecutorMockAsync.java

/**
 * Write response file./*from   w  ww .ja v  a  2s.  c  o  m*/
 *
 * @param sourceFileName template file name.
 * @param targetFileName response file name.
 */
private void setResponse(String sourceFileName, String targetFileName) {
    String content;
    URL url = Resources.getResource(sourceFileName);
    try {
        content = Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new DlabException("Can't read resource " + sourceFileName + ": " + e.getLocalizedMessage(), e);
    }

    for (String key : parser.getVariables().keySet()) {
        String value = parser.getVariables().get(key);
        content = content.replace("${" + key.toUpperCase() + "}", value);
    }

    File fileResponse = new File(responseFileName);
    try (BufferedWriter out = new BufferedWriter(new FileWriter(fileResponse))) {
        Files.createParentDirs(fileResponse);
        out.write(content);
    } catch (IOException e) {
        throw new DlabException("Can't write response file " + targetFileName + ": " + e.getLocalizedMessage(),
                e);
    }
    log.debug("Create response file from {} to {}", sourceFileName, targetFileName);
}

From source file:org.pascani.dsl.lib.sca.PascaniUtils.java

/**
 * Registers the Pascani FScript procedures in the specified FraSCAti
 * runtime//from   w  ww.  j  a  v a 2  s .c om
 * 
 * @param bindingUri
 *            The URI where the FraSCAti runtime is running
 * @throws IOException
 *             If there is a problem loading the Pascani FScript procedures
 *             from the resources
 * @throws ScriptException
 *             If there is a problem executing any of the scripts
 */
private static void registerPascaniScripts(URI bindingUri) throws IOException, ScriptException {
    Boolean registered = registeredScripts.get(bindingUri);
    if (registered == null || !registered) {
        ClassLoader loader = MoreObjects.firstNonNull(Thread.currentThread().getContextClassLoader(),
                PascaniUtils.class.getClassLoader());
        URL url = loader.getResource("pascani.fscript");
        if (url == null)
            url = ClassLoader.getSystemResource("pascani.fscript");
        String fscript = "";
        if (url != null) {
            fscript = Resources.toString(url, Charset.defaultCharset());
            System.out.println("------ pascani.fscript LOADED FROM RESOURCES ------");
        } else {
            System.out.println("------ pascani.fscript NOT FOUND. USING BACKUP ------");
            fscript = "" + "function pascani-element-exists(selector) {\n" + "   return size($selector) > 0;\n"
                    + "}\n" + "action pascani-add-intent(parent, target, intentName, routingKey) {\n"
                    + "   -- 1. Create a new instance of the intent composite\n"
                    + "   clone = sca-new($intentName);\n"
                    + "   -- 2. Add the intent instance as a child of the target's parent\n"
                    + "   add-scachild($parent, $clone);\n" + "   intent = $parent/scachild::$intentName;\n"
                    + "   -- 3. Change the name of the intent component to be the routing key\n"
                    + "   set-name($intent, $routingKey);\n" + "   intent = $parent/scachild::$routingKey;\n"
                    + "   -- 4. Sets the routing key\n"
                    + "   property = $intent/scachild::probe/scaproperty::property;\n"
                    + "   set-value($property, concat(\"routingkey=\", $routingKey));\n"
                    + "   -- 5. Add the REST binding to the Resumable interface, and then add the SCA intent\n"
                    + "   add-scaintent($target, $intent);\n" + "   -- 6. Wire the event handler service\n"
                    + "   service = $intent/scachild::probe/scaservice::handler;\n"
                    + "   reference = $intent/scachild::primitiveIntentHandler/scareference::handler;\n"
                    + "   add-scawire($reference, $service);\n" + "   -- 7. Clean things up\n"
                    + "   sca-remove($intentName); \n" + "   set-state($intent, \"STARTED\");\n"
                    + "   set-state($parent, \"STARTED\");\n" + "}\n"
                    + "action pascani-remove-intent(parent, target, routingKey, randomName) {\n"
                    + "   -- 1. Remove the SCA intent\n" + "   intent = $parent/scachild::$routingKey;\n"
                    + "   remove-scaintent($target, $intent);\n"
                    + "   -- 2. Remove the intent component from the target's parent\n"
                    + "   set-state($intent, \"STOPPED\");\n"
                    + "   -- FraSCAti freezes when stopping a top level component.\n"
                    + "   -- This is a requirement for removing child components though.\n"
                    + "   -- set-state($parent, \"STOPPED\");\n" + "   -- remove-scachild($parent, $intent);\n"
                    + "   -- set-state($parent, \"STARTED\");\n"
                    + "   -- Instead of that, rename the component to a random name and shutdown probe & producer\n"
                    + "   pascani-probe-set($parent, $routingKey, \"shutdown=both\");"
                    + "   set-name($intent, $randomName);\n" + "}\n"
                    + "action pascani-probe-set(parent, routingKey, key_value) {\n"
                    + "   intent = $parent/scachild::$routingKey;\n"
                    + "   property = $intent/scachild::probe/scaproperty::property;\n"
                    + "   set-value($property, $key_value);\n" + "}";
        }
        List<String> scripts = FrascatiUtils.registerScript(fscript, bindingUri);
        registeredScripts.put(bindingUri, scripts.size() > 0);
    }
}

From source file:org.voltdb.utils.HTTPAdminListener.java

private void loadTemplate(Class<?> clz, String name) throws Exception {
    URL url = Resources.getResource(clz, name);
    String contents = Resources.toString(url, Charsets.UTF_8);
    m_htmlTemplates.put(name, contents);
}

From source file:com.aerofs.baseline.Service.java

private void displayBanner() {
    try {/*from  w  w  w.  ja  va2  s  .  c om*/
        String banner = Resources.toString(Resources.getResource("banner.txt"), Charsets.UTF_8);
        LOGGER.info("{}", banner);
    } catch (IllegalArgumentException | IOException e) { // happens if banner.txt doesn't exist in classpath
        LOGGER.info("starting {}", name);
    }
}

From source file:com.eucalyptus.context.ServiceContextManager.java

private String loadModel(final ComponentId componentId) {
    try {// w  w w  .  ja  va  2s  .c  o  m
        return Resources.toString(Resources.getResource(componentId.getServiceModelFileName()),
                Charset.defaultCharset());
    } catch (final Exception ex) {
        return EMPTY_MODEL;
    }
}

From source file:com.palantir.typescript.services.language.LanguageEndpoint.java

private static String readLibContents(String resourceName) {
    URL libUrl = LanguageEndpoint.class.getResource(resourceName);

    try {//www  .j a v a  2 s.  co  m
        return Resources.toString(libUrl, Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.opencastproject.util.IoSupport.java

/**
 * Load a classpath resource into a string using UTF-8 encoding and the class loader of the given class.
 *
 * @return the content of the resource wrapped in a Some or none in case of any error
 *///  w w  w . j  a  va2 s.  com
public static Option<String> loadFileFromClassPathAsString(String resource, Class<?> clazz) {
    try {
        final URL url = clazz.getResource(resource);
        return url != null ? some(Resources.toString(clazz.getResource(resource), Charset.forName("UTF-8")))
                : none(String.class);
    } catch (IOException e) {
        return none();
    }
}