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.usergrid.persistence.index.migration.EsIndexMappingMigrationPlugin.java

/**
 * Get the content from our mappings file
 * @return/*w w  w. j  a v  a2  s  . c  om*/
 */
private String getMappingsContent() {
    URL url = Resources.getResource("org/apache/usergrid/persistence/index/usergrid-mappings.json");
    try {
        return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException("Unable to read mappings file", e);
    }
}

From source file:org.onosproject.drivers.netconf.MockTemplateRequestDriver.java

public void load(Class<? extends Object> reference, String pattern, DeviceId id, String... reponseNames) {
    for (String name : reponseNames) {
        String key = name;//w ww.  j a va 2 s.c  o m
        String resource;

        // If the template name begins with a '/', then assume it is a full path
        // specification
        if (name.charAt(0) == '/') {
            int start = name.lastIndexOf('/') + 1;
            int end = name.lastIndexOf('.');
            if (end == -1) {
                key = name.substring(start);
            } else {
                key = name.substring(start, end);
            }
            resource = name;
        } else {
            resource = String.format(pattern, name);
        }

        log.debug("LOAD RESPONSE TEMPLATE: '{}' as '{}' from '{}'", name, key, resource);

        try {
            DeviceId use = id;
            if (use == null) {
                use = DEFAULT_RESPONSES_ID;
            }
            Map<String, String> deviceResponses = responses.get(use);
            if (deviceResponses == null) {
                deviceResponses = new HashMap<String, String>();
                responses.put(use, deviceResponses);
            }
            deviceResponses.put(name,
                    Resources.toString(Resources.getResource(reference, resource), StandardCharsets.UTF_8));
        } catch (IOException ioe) {
            log.error("Unable to load NETCONF response template '{}' from '{}'", key, resource, ioe);
        }
    }
}

From source file:com.eucalyptus.cassandra.config.CassandraSysUtil.java

static String generateCassandraYaml(final String name, final String bindAddr, final Set<String> seeds,
        final String dir, final boolean autoBootstrap) throws IOException {
    final String cassandraYamlTemplate = Resources.toString(Resources.getResource(YAML_RESOURCE_NAME),
            StandardCharsets.UTF_8);
    return Templates.prepare(YAML_RESOURCE_NAME).withProperty("cluster_name", name).withProperty("seeds", seeds)
            .withProperty("bind_addr", bindAddr).withProperty("port_native", 8787)
            .withProperty("port_storage", 8782).withProperty("port_storage_ssl", 8783)
            .withProperty("dir_lib", dir).withProperty("auto_bootstrap", autoBootstrap)
            .evaluate(cassandraYamlTemplate);
}

From source file:org.apache.zeppelin.submarine.job.thread.JobRunThread.java

public void run() {
    boolean tryLock = lockRunning.tryLock();
    if (false == tryLock) {
        LOGGER.warn("Can not get JobRunThread lockRunning!");
        return;//from  w  w w. j a  va  2 s  .c om
    }

    SubmarineUI submarineUI = submarineJob.getSubmarineUI();
    try {
        InterpreterContext intpContext = submarineJob.getIntpContext();
        String noteId = intpContext.getNoteId();
        String userName = intpContext.getAuthenticationInfo().getUser();
        String jobName = SubmarineUtils.getJobName(userName, noteId);

        if (true == running.get()) {
            String message = String.format("Job %s already running.", jobName);
            submarineUI.outputLog("WARN", message);
            LOGGER.warn(message);
            return;
        }
        running.set(true);

        Properties properties = submarineJob.getProperties();
        HdfsClient hdfsClient = submarineJob.getHdfsClient();
        File pythonWorkDir = submarineJob.getPythonWorkDir();

        submarineJob.setCurrentJobState(EXECUTE_SUBMARINE);

        String algorithmPath = properties.getProperty(SubmarineConstants.SUBMARINE_ALGORITHM_HDFS_PATH, "");
        if (!algorithmPath.startsWith("hdfs://")) {
            String message = "Algorithm file upload HDFS path, " + "Must be `hdfs://` prefix. now setting "
                    + algorithmPath;
            submarineUI.outputLog("Configuration error", message);
            return;
        }

        List<ParagraphInfo> paragraphInfos = intpContext.getIntpEventClient().getParagraphList(userName,
                noteId);
        String outputMsg = hdfsClient.saveParagraphToFiles(noteId, paragraphInfos,
                pythonWorkDir == null ? "" : pythonWorkDir.getAbsolutePath(), properties);
        if (!StringUtils.isEmpty(outputMsg)) {
            submarineUI.outputLog("Save algorithm file", outputMsg);
        }

        HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, true);

        URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA);
        String template = Resources.toString(urlTemplate, Charsets.UTF_8);
        Jinjava jinjava = new Jinjava();
        String submarineCmd = jinjava.render(template, jinjaParams);
        // If the first line is a newline, delete the newline
        int firstLineIsNewline = submarineCmd.indexOf("\n");
        if (firstLineIsNewline == 0) {
            submarineCmd = submarineCmd.replaceFirst("\n", "");
        }

        StringBuffer sbLogs = new StringBuffer(submarineCmd);
        submarineUI.outputLog("Submarine submit command", sbLogs.toString());

        long timeout = Long
                .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout));
        CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell);
        cmdLine.addArgument(submarineCmd, false);
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchDog);
        StringBuffer sbLogOutput = new StringBuffer();
        executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
            @Override
            protected void processLine(String line, int level) {
                line = line.trim();
                if (!StringUtils.isEmpty(line)) {
                    sbLogOutput.append(line + "\n");
                }
            }
        }));

        if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) {
            executor.setWorkingDirectory(new File(System.getProperty("user.home")));
        }

        Map<String, String> env = new HashMap<>();
        String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE);
        if (StringUtils.equals(launchMode, "yarn")) {
            // Set environment variables in the submarine interpreter container run on yarn
            String javaHome, hadoopHome, hadoopConf;
            javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME);
            hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME);
            hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR);
            env.put("JAVA_HOME", javaHome);
            env.put("HADOOP_HOME", hadoopHome);
            env.put("HADOOP_HDFS_HOME", hadoopHome);
            env.put("HADOOP_CONF_DIR", hadoopConf);
            env.put("YARN_CONF_DIR", hadoopConf);
            env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`");
            env.put("ZEPPELIN_FORCE_STOP", "true");
        }

        LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd);
        AtomicBoolean cmdLineRunning = new AtomicBoolean(true);
        executor.execute(cmdLine, env, new DefaultExecuteResultHandler() {
            @Override
            public void onProcessComplete(int exitValue) {
                String message = String.format("jobName %s ProcessComplete exit value is : %d", jobName,
                        exitValue);
                LOGGER.info(message);
                submarineUI.outputLog("JOR RUN COMPLETE", message);
                cmdLineRunning.set(false);
                submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_FINISHED);
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s",
                        jobName, e.getExitValue(), e.getMessage());
                LOGGER.error(message);
                submarineUI.outputLog("JOR RUN FAILED", message);
                cmdLineRunning.set(false);
                submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
            }
        });
        int loopCount = 100;
        while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) {
            Thread.sleep(1000);
        }
        if (watchDog.isWatching()) {
            watchDog.destroyProcess();
            Thread.sleep(1000);
        }
        if (watchDog.isWatching()) {
            watchDog.killedProcess();
        }

        // Check if it has been submitted to YARN
        Map<String, Object> jobState = submarineJob.getJobStateByYarn(jobName);
        loopCount = 50;
        while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) {
            Thread.sleep(3000);
            jobState = submarineJob.getJobStateByYarn(jobName);
        }

        if (!jobState.containsKey("state")) {
            String message = String.format("JOB %s was not submitted to YARN!", jobName);
            LOGGER.error(message);
            submarineUI.outputLog("JOR RUN FAILED", message);
            submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR);
        submarineUI.outputLog("Exception", e.getMessage());
    } finally {
        running.set(false);
        lockRunning.unlock();
    }
}

From source file:io.soabase.admin.details.IndexServlet.java

private synchronized void rebuild() {
    Map<String, Entry> newFiles = Maps.newHashMap();
    for (Mapping mapping : mappings) {
        try {/*from www . j av a2 s  .c  om*/
            String content = Resources.toString(Resources.getResource(mapping.file), Charsets.UTF_8);
            newFiles.put(mapping.key, new Entry(content, "")); // temp entry
        } catch (IOException e) {
            // TODO logging
            throw new RuntimeException(e);
        }
    }

    try {
        doReplacements(componentManager, newFiles);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    // zero out the millis since the date we get back from If-Modified-Since will not have them
    lastModified.set((System.currentTimeMillis() / 1000) * 1000);
    files.set(newFiles);
    builtFromVersion.set(componentManager.getVersion());
}

From source file:org.glowroot.config.PluginCacheBase.java

private static List<PluginDescriptor> readPluginDescriptors(List<URL> descriptorURLs)
        throws IOException, URISyntaxException {
    List<PluginDescriptor> pluginDescriptors = Lists.newArrayList();
    for (URL url : descriptorURLs) {
        try {/*from   ww w.j av  a2 s  .  c  om*/
            String content = Resources.toString(url, Charsets.UTF_8);
            PluginDescriptor pluginDescriptor = mapper.readValue(content, PluginDescriptor.class);
            pluginDescriptors.add(pluginDescriptor);
        } catch (JsonProcessingException e) {
            logger.error("error parsing plugin descriptor: {}", url.toExternalForm(), e);
        }
    }
    return sorted(pluginDescriptors);
}

From source file:org.tdwg.dwca.wikipedia.ChecklistBuilder.java

private Dataset buildEml() throws IOException {
    Dataset dataset = new Dataset();
    dataset.setTitle(cfg.getLanguage().getTitleEnglish() + " Wikipedia - Species Pages");
    dataset.setLanguage(Language.ENGLISH);
    dataset.setDataLanguage(cfg.getLanguage());
    String description = Resources.toString(Resources.getResource("description.txt"), Charsets.UTF_8);
    dataset.setDescription(description.replaceAll("$LANGUAGE", cfg.getLanguage().getTitleEnglish())
            .replaceAll("$DATE", DateFormatUtils.ISO_DATE_FORMAT.format(modifiedDate)));
    dataset.setPubDate(modifiedDate);/*from w  w  w . j a  v a 2  s.c om*/
    dataset.setHomepage(URI.create("http://" + cfg.lang + ".wikipedia.org"));
    addDeveloper(dataset, ContactType.METADATA_AUTHOR, ContactType.ORIGINATOR,
            ContactType.ADMINISTRATIVE_POINT_OF_CONTACT);

    DataDescription d = new DataDescription();
    d.setUrl(URI.create(url.toString()));
    d.setFormat("XML");
    d.setName("Wikipedia Article Dump");
    dataset.getDataDescriptions().add(d);

    return dataset;
}

From source file:com.google.api.codegen.config.DependenciesConfig.java

public static DependenciesConfig loadFromURL(URL url) throws IOException {
    String contents = Resources.toString(url, StandardCharsets.UTF_8);
    return createFromString(contents);
}

From source file:org.apache.zeppelin.shell.TerminalInterpreter.java

public void createTerminalDashboard(String noteId, String paragraphId, int port) {
    String hostName = "", hostIp = "";
    URL urlTemplate = Resources.getResource("ui_templates/terminal-dashboard.jinja");
    String template = null;/*from  w w w  .  j a  va2s .  c  o m*/
    try {
        template = Resources.toString(urlTemplate, Charsets.UTF_8);
        InetAddress addr = InetAddress.getLocalHost();
        hostName = addr.getHostName().toString();
        hostIp = RemoteInterpreterUtils.findAvailableHostAddress();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    Jinjava jinjava = new Jinjava();
    HashMap<String, Object> jinjaParams = new HashMap();
    Date now = new Date();
    String terminalServerUrl = "http://" + hostIp + ":" + port + "?noteId=" + noteId + "&paragraphId="
            + paragraphId + "&t=" + now.getTime();
    jinjaParams.put("HOST_NAME", hostName);
    jinjaParams.put("HOST_IP", hostIp);
    jinjaParams.put("TERMINAL_SERVER_URL", terminalServerUrl);
    String terminalDashboardTemplate = jinjava.render(template, jinjaParams);

    LOGGER.info(terminalDashboardTemplate);
    try {
        intpContext.out.setType(InterpreterResult.Type.ANGULAR);
        InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
        outputUI.clear();
        outputUI.write(terminalDashboardTemplate);
        outputUI.flush();
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:com.google.template.soy.msgs.SoyMsgBundleHandler.java

/**
 * Reads a translated messages resource and creates a SoyMsgBundle.
 *
 * @param inputResource The resource to read from.
 * @return The message bundle created from the messages resource.
 * @throws IOException If there's an error while accessing the resource.
 * @throws SoyMsgException If there's an error while processing the messages.
 */// www . ja  va 2s. co m
public SoyMsgBundle createFromResource(URL inputResource) throws IOException, SoyMsgException {

    try {
        String inputFileContent = Resources.toString(inputResource, UTF_8);
        return msgPlugin.parseTranslatedMsgsFile(inputFileContent);

    } catch (SoyMsgException sme) {
        sme.setFileOrResourceName(inputResource.toString());
        throw sme;
    }
}