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

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

Introduction

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

Prototype

public static URL getResource(String resourceName) 

Source Link

Document

Returns a URL pointing to resourceName if the resource is found using the Thread#getContextClassLoader() context class loader .

Usage

From source file:com.streamsets.datacollector.hdfs.standalone.HdfsDestinationPipelineRunIT.java

@Override
protected String getPipelineJson() throws Exception {
    URI uri = Resources.getResource("hdfs_destination_pipeline_run.json").toURI();
    String pipelineJson = new String(Files.readAllBytes(Paths.get(uri)), StandardCharsets.UTF_8);
    pipelineJson = pipelineJson.replaceAll("/uri", miniDFS.getURI().toString());
    return pipelineJson;
}

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

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

    boolean tryLock = lockRunning.tryLock();

    try {/*w w  w.jav  a2 s .c  om*/
        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.stratio.es.ESJavaRDDFT.java

/**
 * Imports dataset//from  w  w  w  .j a va  2 s. co  m
 *
 * @throws java.io.IOException
 */
private static void dataSetImport()
        throws IOException, ExecutionException, IOException, InterruptedException, ParseException {

    JSONParser parser = new JSONParser();
    URL url = Resources.getResource(DATA_SET_NAME);
    Object obj = parser.parse(new FileReader(url.getFile()));

    JSONObject jsonObject = (JSONObject) obj;

    IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id")
            .setSource(jsonObject.toJSONString()).execute().actionGet();

    String json2 = "{" +

            "\"message\":\"" + MESSAGE_TEST + "\"" + "}";

    IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true)
            .setSource(json2).setReplicationType(ReplicationType.ASYNC).execute().actionGet();

    String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
            + "\"message\":\"trying out Elasticsearch\"" + "}";

    IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true).setSource(json).execute()
            .actionGet();

    String index = response.getIndex();
    String _type = response.getType();
    String _id = response.getId();
    try {
        CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE).execute().actionGet();

        SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT).execute()
                .actionGet();

        //searchResponse.getHits().hits();
        //assertEquals(searchResponse.getCount(), 1);
    } catch (AssertionError | Exception e) {
        cleanup();
        e.printStackTrace();
    }
}

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

private void cleanUpDb() {
    Connection conn = null;/*from  w ww .  j  av  a  2  s  . c om*/
    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:org.jsfr.json.BenchmarkCollectSingleValue.java

@Setup
public void setup() throws Exception {
    gson = new GsonBuilder().create();
    om = new ObjectMapper();
    gsonSurfer = JsonSurfer.gson();//from  w ww  .  j a va2  s.  c o  m
    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:exercise.basic.ResponseDemoResource.java

/**
 * @return/*from w  ww.j a va  2 s  .c  o m*/
 */
@GET
@Path("sampleimage.png")
public Response anydata() {
    URL resurl = Resources.getResource("exercise/sampleimage.png");
    final ByteSource byteSource = Resources.asByteSource(resurl);
    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            byteSource.copyTo(os);
            os.flush();
        }
    };
    return Response.status(Status.OK).type("image/png").entity(stream).build();
}

From source file:com.google.gerrit.server.mail.MailSoyTofuProvider.java

private void addTemplate(SoyFileSet.Builder builder, String name) throws ProvisionException {
    // Load as a file in the mail templates directory if present.
    Path tmpl = site.mail_dir.resolve(name);
    if (Files.isRegularFile(tmpl)) {
        String content;//from   ww w .  j  a  v  a  2s.  c  o m
        try (Reader r = Files.newBufferedReader(tmpl, StandardCharsets.UTF_8)) {
            content = CharStreams.toString(r);
        } catch (IOException err) {
            throw new ProvisionException("Failed to read template file " + tmpl.toAbsolutePath().toString(),
                    err);
        }
        builder.add(content, tmpl.toAbsolutePath().toString());
        return;
    }

    // Otherwise load the template as a resource.
    String resourcePath = "com/google/gerrit/server/mail/" + name;
    builder.add(Resources.getResource(resourcePath));
}

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

@Setup
public void setup(final Blackhole blackhole) throws IOException {
    simpleSurfer = JsonSurfer.simple();/* w  w w. ja va 2s .  c  o  m*/
    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:org.graylog2.ElasticsearchBase.java

private Properties loadProperties(String resourceName) {
    final Properties properties = new Properties();
    final URL resource = Resources.getResource(resourceName);
    try (InputStream stream = resource.openStream()) {
        properties.load(stream);//  ww  w . ja  v  a2  s  . c o  m
    } catch (IOException e) {
        throw new UncheckedIOException("Error while reading test properties", e);
    }
    return properties;
}

From source file:feign.TrustingSSLSocketFactory.java

private TrustingSSLSocketFactory(String serverAlias) {
    try {//w  ww.  j a  va  2s  . c  o  m
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(new KeyManager[] { this }, new TrustManager[] { this }, new SecureRandom());
        this.delegate = sc.getSocketFactory();
    } catch (Exception e) {
        throw propagate(e);
    }
    this.serverAlias = serverAlias;
    if (serverAlias.isEmpty()) {
        this.privateKey = null;
        this.certificateChain = null;
    } else {
        try {
            KeyStore keyStore = loadKeyStore(
                    Resources.newInputStreamSupplier(Resources.getResource("keystore.jks")));
            this.privateKey = (PrivateKey) keyStore.getKey(serverAlias, KEYSTORE_PASSWORD);
            Certificate[] rawChain = keyStore.getCertificateChain(serverAlias);
            this.certificateChain = Arrays.copyOf(rawChain, rawChain.length, X509Certificate[].class);
        } catch (Exception e) {
            throw propagate(e);
        }
    }
}