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.zeppelin.submarine.job.thread.TensorboardRunThread.java

public void run() {
    SubmarineUI submarineUI = submarineJob.getSubmarineUI();

    boolean tryLock = lockRunning.tryLock();

    try {/*from w w  w. j a v a2 s.  co m*/
        Properties properties = submarineJob.getProperties();
        String tensorboardName = SubmarineUtils.getTensorboardName(submarineJob.getUserName());
        if (true == running.get()) {
            String message = String.format("tensorboard %s already running.", tensorboardName);
            submarineUI.outputLog("WARN", message);
            LOGGER.warn(message);
            return;
        }
        running.set(true);

        HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, false);
        // update jobName -> tensorboardName
        jinjaParams.put(SubmarineConstants.JOB_NAME, tensorboardName);

        URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_TENSORBOARD_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 container
            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`");
        }

        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", tensorboardName,
                        exitValue);
                LOGGER.info(message);
                submarineUI.outputLog("TENSORBOARD RUN COMPLETE", message);
                cmdLineRunning.set(false);
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s",
                        tensorboardName, e.getExitValue(), e.getMessage());
                LOGGER.error(message);
                submarineUI.outputLog("TENSORBOARD RUN FAILED", message);
                cmdLineRunning.set(false);
            }
        });
        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(tensorboardName);
        loopCount = 50;
        while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) {
            Thread.sleep(3000);
            jobState = submarineJob.getJobStateByYarn(tensorboardName);
        }

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

From source file:com.xemantic.tadedon.gwt.field.rebind.UiFieldAccessorGenerators.java

public static String readTemplateFile() {
    URL url = UiFieldAccessorGenerator.class.getResource("UiFieldAccessor.template");
    try {//from  w w  w. j a v a  2  s  . co m
        return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException("Cannot read resource from url: " + url, e);
    }
}

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

@Setup
public void setup(final Blackhole blackhole) throws IOException {
    simpleSurfer = JsonSurfer.simple();/*from w w  w. java 2 s . c om*/
    gsonSurfer = JsonSurfer.gson();
    jacksonSurfer = JsonSurfer.jackson();
    JsonPathListener blackHoleListener = new JsonPathListener() {
        @Override
        public void onValue(Object value, ParsingContext context) throws Exception {
            LOGGER.trace("Properties: {}", value);
            blackhole.consume(value);
        }
    };
    surfingConfiguration = SurfingConfiguration.builder().bind(jsonPath, blackHoleListener).skipOverlappedPath()
            .build();
    json = Resources.toString(Resources.getResource("allthethings.json"), StandardCharsets.UTF_8);
}

From source file:com.axemblr.provisionr.test.ProvisionrLiveTestSupport.java

public String getResourceAsString(String resource) throws IOException {
    return Resources.toString(Resources.getResource(resource), Charsets.UTF_8);
}

From source file:org.icgc.dcc.release.job.annotate.snpeff.SnpEffProcess.java

@SneakyThrows
private String getConfigFile() {
    // Template//w ww  . j  a  v  a  2 s.co  m
    val contents = Resources.toString(getResource("snpEff.config"), UTF_8).replace("${dataDir}",
            dataDir.getAbsolutePath());

    val configFile = File.createTempFile("snpEff", ".config");

    log.info("Creating temporary configuration file at '{}'...", configFile);
    Files.write(contents, configFile, UTF_8);

    return configFile.getAbsolutePath();
}

From source file:org.spongepowered.api.asset.Asset.java

/**
 * Reads this Asset in it's entirety as a {@link String} and returns the
 * result./*www  . ja  va 2  s  .com*/
 *
 * @param charset The charset to read the asset with
 * @return String representation of Asset
 * @throws IOException
 */
default String readString(Charset charset) throws IOException {
    checkNotNull(charset, "charset");
    return Resources.toString(getUrl(), charset);
}

From source file:org.isisaddons.app.kitchensink.fixture.blobclob.BlobClobObjectsFixture.java

private Clob newClob(final String name, final String mimeType, final String resourceName) throws IOException {
    final String contents = Resources.toString(Resources.getResource(getClass(), resourceName),
            Charset.forName("Cp1252"));
    return new Clob(name + "-" + resourceName, mimeType, contents);
}

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

private void cleanUpDb() {
    Connection conn = null;//from w w w . j a va 2s .com
    try {
        conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        URL dropAllTableSqlFileUrl = Resources.getResource(dropAllTablesSqlFile);
        String dropAllTablesSql = Resources.toString(dropAllTableSqlFileUrl, Charsets.UTF_8);
        conn.createStatement().execute(dropAllTablesSql);
    } catch (IOException | SQLException e) {
        throw new RuntimeException("Unable to clean up embedded hsqldb. Reason: " + e.getMessage(), e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:it.smartcommunitylab.aac.apimanager.wso2.WSO2APIManager.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from w  w w. j  av  a 2 s  .  c  o  m
public void publishAPI(String jsonModel, String name, String description, String context, String token)
        throws Exception {
    Map apis = pub.findAPI(name, token);

    List list = (List) apis.get("list");
    log.info("Checking publish API {} ...", name);
    // System.err.println(apis);
    if (list.isEmpty() || !list.stream().anyMatch(x -> name.equals(((Map) x).get("name")))) {
        String swagger = Resources.toString(Resources.getResource(jsonModel), Charsets.UTF_8);
        swagger = env.resolvePlaceholders(swagger);
        log.info("Publishing API {}", name);

        API api = new API();

        api.setName(name);
        api.setDescription(description);
        api.setContext(context);
        api.setVersion("1.0.0");
        api.setProvider("admin");
        api.setApiDefinition(swagger);
        api.setStatus("CREATED");
        api.setVisibility("PUBLIC");
        api.setSubscriptionAvailability("all_tenants");
        api.setIsDefaultVersion(true);
        api.setEndpointConfig(env.resolvePlaceholders(ENDPOINT_CONFIG));

        CorsConfiguration cors = new CorsConfiguration();
        cors.setCorsConfigurationEnabled(true);
        cors.setAccessControlAllowOrigins(Lists.newArrayList("*"));
        cors.setAccessControlAllowHeaders(Lists.newArrayList("authorization", "Access-Control-Allow-Origin",
                "Content-Type", "SOAPAction"));
        cors.setAccessControlAllowMethods(
                Lists.newArrayList("GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"));
        api.setCorsConfiguration(cors);

        API result = pub.publishAPI(api, token);
        pub.changeAPIStatus(result.getId(), "Publish", token);

        // System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result));
    }
}

From source file:org.apache.isis.core.runtime.services.userreg.EmailNotificationServiceDefault.java

protected String loadResource(final String resourceName) {
    final URL templateUrl = Resources.getResource(EmailNotificationServiceDefault.class, resourceName);
    try {/*  w  w  w  .j  a  v  a  2  s  .  co m*/
        return Resources.toString(templateUrl, Charsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Unable to read resource URL '%s'", templateUrl));
    }
}