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:org.mayocat.search.elasticsearch.AbstractGenericEntityMappingGenerator.java

@Override
public Map<String, Object> generateMapping() {
    ObjectMapper mapper = new ObjectMapper();
    try {// w  ww . j  a  va 2 s. c om
        try {
            String fullJson = Resources.toString(Resources.getResource(getMappingFileName(forClass())),
                    Charsets.UTF_8);

            // There is a full mapping JSON file

            Map<String, Object> mapping = mapper.readValue(fullJson, new TypeReference<Map<String, Object>>() {
            });

            // check if "addons" mapping present, and merge it in if not
            if (!hasAddonsMapping(mapping)) {
                insertAddonsMapping(mapping);
            }

            return mapping;
        } catch (IllegalArgumentException e) {

            // This means the full mapping has not been found
            try {
                String propertiesJson = Resources.toString(
                        Resources.getResource(getPropertiesMappingFileName(forClass())), Charsets.UTF_8);

                // There is a mapping just for properties
                final Map<String, Object> properties = mapper.readValue(propertiesJson,
                        new TypeReference<Map<String, Object>>() {
                        });

                Map<String, Object> mapping = new HashMap<String, Object>();

                Map<String, Object> entity = new HashMap<String, Object>();
                Map<String, Object> entityProperties = new HashMap<String, Object>() {
                    {
                        // the "properties" property of the properties of the product object
                        put("properties", new HashMap<String, Object>() {
                            {
                                // the "properties" of the property "properties"
                                // of the properties of the product object
                                put("properties", properties);
                            }
                        });
                    }
                };

                if (Slug.class.isAssignableFrom(this.forClass())) {
                    entityProperties.put("slug", new HashMap<String, Object>() {
                        {
                            put("index", "not_analyzed");
                            put("type", "string");
                        }
                    });
                }

                entity.put("properties", entityProperties);
                mapping.put(getEntityName(forClass()), entity);

                insertAddonsMapping(mapping);

                return mapping;
            } catch (IllegalArgumentException e1) {

                // There is no mapping at all.
                return null;
            }
        }
    } catch (IOException e1) {
        return null;
    }
}

From source file:com.google.gerrit.httpd.raw.IndexServlet.java

IndexServlet(String canonicalURL, @Nullable String cdnPath) throws URISyntaxException {
    String resourcePath = "com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy";
    SoyFileSet.Builder builder = SoyFileSet.builder();
    builder.add(Resources.getResource(resourcePath));
    SoyTofu.Renderer renderer = builder.build().compileToTofu().newRenderer("com.google.gerrit.httpd.raw.Index")
            .setContentKind(SanitizedContent.ContentKind.HTML).setData(getTemplateData(canonicalURL, cdnPath));
    indexSource = renderer.render().getBytes();
}

From source file:org.apache.mahout.classifier.sgd.pserver.AdultDataParser.java

@Override
public Iterator<AdultData> iterator() {
    try {/*w  w  w. j ava 2s .  c  o  m*/
        return new AbstractIterator<AdultData>() {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(Resources.getResource(resourceName).openStream()));
            Iterable<String> fieldNames = onSemi.split(input.readLine());

            @Override
            protected AdultData computeNext() {
                try {
                    String line = input.readLine();
                    if (line == null) {
                        return endOfData();
                    }

                    return new AdultData(fieldNames, onSemi.split(line));
                } catch (IOException e) {
                    throw new RuntimeException("Error reading data", e);
                }
            }
        };
    } catch (IOException e) {
        throw new RuntimeException("Error reading data", e);
    }
}

From source file:com.mapr.synth.samplers.StreetNameSampler.java

public StreetNameSampler() {
    Splitter onTabs = Splitter.on("\t");
    try {/*from w  w w .  ja  v a2s. co  m*/
        for (String line : Resources.readLines(Resources.getResource("street-name-seeds"), Charsets.UTF_8)) {
            if (!line.startsWith("#")) {
                Iterator<Multinomial<String>> i = sampler.iterator();
                for (String name : onTabs.split(line)) {
                    i.next().add(name, 1);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource", e);
    }
}

From source file:org.graylog2.bindings.MessageFilterBindings.java

@Override
protected void configure() {
    Multibinder<MessageFilter> messageFilters = Multibinder.newSetBinder(binder(), MessageFilter.class);
    messageFilters.addBinding().to(StaticFieldFilter.class);
    messageFilters.addBinding().to(ExtractorFilter.class);
    messageFilters.addBinding().to(RulesFilter.class);
    messageFilters.addBinding().to(StreamMatcherFilter.class);

    // built it drools rules
    final Multibinder<URI> rulesUrls = Multibinder.newSetBinder(binder(), URI.class);
    try {/*  w  w  w.j  a  va2  s  . c o m*/
        final URI blacklistRulesUri = Resources.getResource("blacklist.drl").toURI();
        rulesUrls.addBinding().toInstance(blacklistRulesUri);
    } catch (URISyntaxException e) {
        // Ignore
    }
}

From source file:co.cask.cdap.gateway.handlers.VersionHandler.java

private String determineVersion() {
    try {//w  ww  .j  a  v  a2  s  . co m
        String version = Resources.toString(Resources.getResource("VERSION"), Charsets.UTF_8);
        if (!version.equals("${project.version}")) {
            return version.trim();
        }
    } catch (IOException e) {
        LOG.warn("Failed to determine current version", e);
    }
    return "unknown";
}

From source file:org.gbif.registry2.utils.JsonBackedData.java

private String getJson(String file) {
    try {//from w w w  . j a  va2s.  c om
        return Resources.toString(Resources.getResource(file), Charsets.UTF_8);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.mahout.classifier.sgd.bankmarketing.TelephoneCallParser.java

@Override
public Iterator<TelephoneCall> iterator() {
    try {/*from w  ww.j  av  a 2s. c  o m*/
        return new AbstractIterator<TelephoneCall>() {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(Resources.getResource(resourceName).openStream()));
            Iterable<String> fieldNames = onSemi.split(input.readLine());

            @Override
            protected TelephoneCall computeNext() {
                try {
                    String line = input.readLine();
                    if (line == null) {
                        return endOfData();
                    }

                    return new TelephoneCall(fieldNames, onSemi.split(line));
                } catch (IOException e) {
                    throw new RuntimeException("Error reading data", e);
                }
            }
        };
    } catch (IOException e) {
        throw new RuntimeException("Error reading data", e);
    }
}

From source file:se.kth.karamel.common.util.IoUtils.java

public static String readContentFromClasspath(String path) throws IOException {
    URL url = Resources.getResource(path);
    if (url == null) {
        throw new IOException("No config.props file found in cookbook");
    }/*  w w w. j  ava2  s  .c  om*/
    return Resources.toString(url, Charsets.UTF_8);
}

From source file:com.stratio.deep.aerospike.AerospikeJavaRDDFT.java

/**
 * Imports dataset./* w ww  .  j av a  2s. c  o  m*/
 *
 * @throws java.io.IOException
 */
private static void dataSetImport() throws IOException, ParseException {
    URL url = Resources.getResource(DATA_SET_NAME);
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(new FileReader(url.getFile()));
    JSONObject jsonObject = (JSONObject) obj;

    String id = (String) jsonObject.get("id");
    JSONObject metadata = (JSONObject) jsonObject.get("metadata");
    JSONArray cantos = (JSONArray) jsonObject.get("cantos");

    Key key = new Key(NAMESPACE_ENTITY, SET_NAME, id);
    Bin binId = new Bin("id", id);
    Bin binMetadata = new Bin("metadata", metadata);
    Bin binCantos = new Bin("cantos", cantos);
    aerospike.put(null, key, binId, binMetadata, binCantos);
    aerospike.createIndex(null, NAMESPACE_ENTITY, SET_NAME, "id_idx", "id", IndexType.STRING);

    Key key2 = new Key(NAMESPACE_CELL, SET_NAME, 3);
    Bin bin_id = new Bin("_id", "3");
    Bin bin_number = new Bin("number", 3);
    Bin bin_text = new Bin("message", "new message test");
    aerospike.put(null, key2, bin_id, bin_number, bin_text);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "num_idx", "number", IndexType.NUMERIC);
    aerospike.createIndex(null, NAMESPACE_CELL, SET_NAME, "_id_idx", "_id", IndexType.STRING);
}