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(Class<?> contextClass, String resourceName) 

Source Link

Document

Given a resourceName that is relative to contextClass , returns a URL pointing to the named resource.

Usage

From source file:com.nesscomputing.event.amqp.QPidUtils.java

public void startup() throws Exception {
    final URL configUrl = Resources.getResource(QPidUtils.class, "/qpid/config.xml");
    final File configFile = new File(configUrl.toURI());
    BrokerOptions options = new BrokerOptions();
    options.setConfigFile(configFile.getAbsolutePath());

    // XXX: this requires a log4j file to not be in a JAR, which sucks.

    //        final URL log4jUrl = Resources.getResource(QPidUtils.class, "/log4j.xml");
    //        final File log4jFile = new File(log4jUrl.toURI());
    //        options.setLogConfigFile(log4jFile.getAbsolutePath());

    port = findUnusedPort();// www  .j a  v a2  s . c  om
    options.addPort(port);

    System.setProperty("QPID_HOME", configFile.getParentFile().getAbsolutePath());

    b = new Broker();
    b.startup(options);
}

From source file:org.incodehq.amberg.vshcolab.modules.work.fixture.viewmodel.VshImport.java

@Override
protected void execute(final ExecutionContext executionContext) {
    final URL excelResource = Resources.getResource(VshImport.class, "VSH Sample data.xlsx");
    final ExcelFixture excelFixture = new ExcelFixture(excelResource, ProjektRowHandler.class,
            VerfahrenRowHandler.class);
    executionContext.executeChild(this, excelFixture);
}

From source file:com.nesscomputing.amqp.QPidProvider.java

@Override
public void startup() throws Exception {
    final URL configUrl = Resources.getResource(QPidProvider.class, "/qpid/config.xml");
    final File configFile = new File(configUrl.toURI());
    BrokerOptions options = new BrokerOptions();
    options.setConfigFile(configFile.getAbsolutePath());

    // XXX: this fails if log4j.xml is in a JAR

    //        final URL log4jUrl = Resources.getResource(QPidProvider.class, "/log4j.xml");
    //        final File log4jFile = new File(log4jUrl.toURI());
    //        options.setLogConfigFile(log4jFile.getAbsolutePath());

    port = findUnusedPort();//w w w .ja  v a  2  s  .  co m
    options.addPort(port);

    System.setProperty("QPID_HOME", configFile.getParentFile().getAbsolutePath());

    b = new Broker();
    b.startup(options);
}

From source file:org.sonar.server.qualityprofile.ws.ProfilesWs.java

private static void defineIndexAction(NewController controller) {
    WebService.NewAction action = controller.createAction("index").setDescription("Get a profile.")
            .setSince("3.3").setDeprecatedSince("5.2").setHandler(RailsHandler.INSTANCE)
            .setResponseExample(Resources.getResource(ProfilesWs.class, "example-index.json"));

    action.createParam("language").setDescription("Profile language").setRequired(true).setExampleValue("java");
    action.createParam("name").setDescription(
            "Profile name. If no profile name is given, default profile for the given language will be returned")
            .setRequired(true).setExampleValue("Sonar way");
    RailsHandler.addFormatParam(action);
}

From source file:com.google.api.codegen.ruby.RubyApiaryNameMap.java

@SuppressWarnings("unchecked")
private static ImmutableMap<ResourceId, String> getNameMap() throws IOException {
    String data = Resources.toString(Resources.getResource(RubyApiaryNameMap.class, "apiary_names.yaml"),
            StandardCharsets.UTF_8);

    // Unchecked cast here.
    Map<String, String> nameData = (Map<String, String>) (new Yaml().load(data));
    ImmutableMap.Builder<ResourceId, String> builder = ImmutableMap.<ResourceId, String>builder();
    for (Map.Entry<String, String> entry : nameData.entrySet()) {
        builder.put(parseKey(entry.getKey()), entry.getValue());
    }//from ww w .ja  v  a2s.c om
    return builder.build();
}

From source file:com.teradata.tpcds.distribution.DistributionUtils.java

protected static Iterator<List<String>> getDistributionIterator(String filename) {
    URL resource = Resources.getResource(DistributionUtils.class, filename);
    checkState(resource != null, "Distribution file '%s' not found", filename);
    try {//  w  ww.  j  av a 2 s .  c o m
        // get an iterator that iterates over lists of the colon separated values from the distribution files
        return transform(filter(
                Resources.asCharSource(resource, StandardCharsets.ISO_8859_1).readLines().iterator(), line -> {
                    line = line.trim();
                    return !line.isEmpty() && !line.startsWith("--");
                }), line -> ImmutableList
                        .copyOf(Splitter.on(Pattern.compile("(?<!\\\\):")).trimResults().split(line)));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.provisionr.amazon.core.ImageTable.java

/**
 * Load the list of AMIs from a resource file (csv format)
 * <p/>/*from  w  w w .ja  v  a2  s. c  om*/
 * Note: the parser is doing only split by comma. There is no
 * support for escaping line components
 *
 * @param resource path to resource
 * @return an instance of {@see ImageTable}
 * @throws IOException
 */
public static ImageTable fromCsvResource(String resource) throws IOException {
    checkNotNull(resource, "resource is null");

    List<String> lines = Resources.readLines(Resources.getResource(ImageTable.class, resource), Charsets.UTF_8);
    checkArgument(!lines.isEmpty(), "the resource is an empty file");

    final ImmutableTable.Builder<String, String, String> table = ImmutableTable.builder();
    final Iterable<String> headers = extractHeaders(lines);

    int index = 0;
    for (String line : Iterables.skip(lines, 1)) {
        final Iterable<Table.Cell<String, String, String>> cells = combineHeadersWithLinePartsAsTableCells(
                index, headers, COMMA.split(line));
        for (Table.Cell<String, String, String> cell : cells) {
            table.put(cell);
        }
        index++;
    }

    return new ImageTable(table.build());
}

From source file:org.jclouds.util.ClassLoadingUtils.java

/**
 * Returns the url of a resource./*w  ww.  jav a  2 s .c o m*/
 * 1. The context class is being used.
 * 2. The thread context class loader is being used.
 * If both approach fail, returns null.
 *
 * @param contextClass
 * @param resourceName
 * @return
 */
public static URL loadResource(Class<?> contextClass, String resourceName) {
    URL url = null;
    if (contextClass != null) {
        url = Resources.getResource(contextClass, resourceName);
    }
    if (url == null && Thread.currentThread().getContextClassLoader() != null) {
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    }
    return url;
}

From source file:com.twitter.common.webassets.jquery.JQueryModule.java

@Override
protected void configure() {
    Registration.registerHttpAsset(binder(), "/js/jquery.min.js",
            Resources.getResource(JQueryModule.class, "js/jquery-1.8.2.min.js"),
            MediaType.JAVASCRIPT_UTF_8.toString(), true);
}

From source file:com.google.testing.security.firingrange.utils.Templates.java

/**
 * Extracts a template given the path relative to a {@code clazz}.
 * @throws IOException if it cannot find the template.
 *//*from  w ww.  j  av  a2 s  .  c  o m*/
public static String getTemplate(String relativePath, Class<? extends HttpServlet> clazz) throws IOException {
    Preconditions.checkArgument(!relativePath.contains(".."));
    try {
        URL resource = Resources.getResource(clazz, "data/" + relativePath);
        return templateToString(resource);
    } catch (IllegalArgumentException e) {
        logger.info("Cannot find template for " + relativePath);
        throw new IOException(errorTemplate());
    }
}