Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:com.spotify.heroic.metric.datastax.schema.AbstractCassandraSchema.java

private String loadTemplate(final String path, final Map<String, String> values) throws IOException {
    final String string;
    final ClassLoader loader = ManagedSetupConnection.class.getClassLoader();

    try (final InputStream is = loader.getResourceAsStream(path)) {
        if (is == null) {
            throw new IOException("No such resource: " + path);
        }/*from   w ww . ja v a2s.c o m*/

        string = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
    }

    return new StrSubstitutor(values, "{{", "}}").replace(string);
}

From source file:com.cognifide.aet.runner.conversion.SuiteMergeStrategyTest.java

private Suite readSuite(String resource) throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    String json = IOUtils.toString(classLoader.getResourceAsStream(resource));
    return GSON.fromJson(json, new TypeToken<Suite>() {
    }.getType());/* w  w w.  j av a  2 s.  co  m*/
}

From source file:org.suren.autotest.web.framework.data.PropertiesDynamicData.java

@PostConstruct
public void init() throws IOException {
    ClassLoader loader = PropertiesDynamicData.class.getClassLoader();
    try (InputStream input = loader.getResourceAsStream(getPath())) {
        if (input == null) {
            logger.warn(String.format("Can not found file [%s] is class path.", path));
            return;
        }//w w  w.  j  a v  a2  s. c  om

        pro.load(input);
    }
}

From source file:cpcc.core.utils.ResourceStreamResponseTest.java

@Test
public void shouldReturnResourceAsStream() throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    byte[] expected = IOUtils.toByteArray(classLoader.getResourceAsStream(PNG_RESOURCE_NAME));
    assertThat(expected).hasSize(731);/*from   w w  w .ja va  2 s .c  o  m*/

    ResourceStreamResponse sut = new ResourceStreamResponse(CONTENT_TYPE_PNG, PNG_RESOURCE_NAME);

    byte[] actual = IOUtils.toByteArray(sut.getStream());

    assertThat(sut.getContentType()).isEqualTo(CONTENT_TYPE_PNG);
    assertThat(actual).isEqualTo(expected);
}

From source file:com.mec.DAO.Superior.SuperiorDAO.java

public Map<String, List<String>> getAll() throws IOException {
    Map<String, List<String>> s = new HashMap<>();
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream("superior.xlsx");
    Workbook workbook = new XSSFWorkbook(is);
    Sheet datatypeSheet = workbook.getSheetAt(0);
    for (Row row : datatypeSheet) {
        Cell cue = row.getCell(0);//from  w w w  . j  a  v a2s . c  o m
        Cell carrera = row.getCell(1);
        if (cue != null && carrera != null && !cue.toString().isEmpty()) {
            cue.setCellType(Cell.CELL_TYPE_STRING);
            if (s.containsKey(carrera.toString())) {
                s.get(carrera.toString()).add(cue.toString());
            } else {
                List<String> aux = new ArrayList<>();
                aux.add(cue.toString());
                s.put(carrera.toString(), aux);
            }
            //list.add(new Superior(cue.toString(), carrera.toString()));
        }
    }
    return s;
}

From source file:cpcc.core.utils.ResourceStreamResponseTest.java

@Test
public void shouldIgnoreCallsToPrepareResponse() throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    byte[] expected = IOUtils.toByteArray(classLoader.getResourceAsStream(PNG_RESOURCE_NAME));
    assertThat(expected).hasSize(731);/*from ww w .  ja  v  a 2 s.  c o m*/

    ResourceStreamResponse sut = new ResourceStreamResponse(CONTENT_TYPE_PNG, PNG_RESOURCE_NAME);

    Response response = mock(Response.class);
    sut.prepareResponse(response);

    verifyZeroInteractions(response);

    byte[] actual = IOUtils.toByteArray(sut.getStream());

    assertThat(sut.getContentType()).isEqualTo(CONTENT_TYPE_PNG);
    assertThat(actual).isEqualTo(expected);
}

From source file:com.mec.DAO.Superior.SuperiorDAO.java

public Map<String, List<String>> getByFiltro(String filtro) throws IOException {
    Map<String, List<String>> s = new HashMap<>();
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream("superior.xlsx");
    Workbook workbook = new XSSFWorkbook(is);
    Sheet datatypeSheet = workbook.getSheetAt(0);

    for (Row row : datatypeSheet) {

        Cell cue = row.getCell(0);/*from   w  ww .  j av a  2 s.  co  m*/
        Cell carrera = row.getCell(1);

        if (cue != null && carrera != null && !cue.toString().isEmpty()
                && clean((carrera.toString())).contains(clean(filtro))) {

            cue.setCellType(Cell.CELL_TYPE_STRING);
            if (s.containsKey(carrera.toString())) {
                s.get(carrera.toString()).add(cue.toString());
            } else {
                List<String> aux = new ArrayList<>();
                aux.add(cue.toString());
                s.put(carrera.toString(), aux);
            }

        }

    }
    return s;
}

From source file:au.edu.uts.eng.remotelabs.schedserver.server.impl.BundleResourceLoader.java

@Override
public InputStream getResourceStream(String path) throws ResourceNotFoundException {
    /* Check this bundles classloader for resource. */
    if ("VM_global_library.vm".equals(path)) {
        /* Special file requested during Velocity initialisation. */
        path = "/META-INF/" + path;
    }//  ww w  . ja  v a 2 s .  co  m

    InputStream is = BundleResourceLoader.class.getResourceAsStream(path);
    if (is == null) {
        /* Resource not found in this classloader, search through all registered
         * bundle classloaders to find the path. */
        for (ClassLoader cl : PageHostingServiceListener.getClassLoaders()) {
            is = cl.getResourceAsStream(path);
            if (is != null)
                break;
        }
    }

    if (is == null) {
        this.logger.warn("Velocity resource " + path + " not found.");
        throw new ResourceNotFoundException("Velocity resource " + path + " not found.");
    }

    return is;
}

From source file:gate.util.Files.java

/** Get a resource from the GATE ClassLoader as an InputStream.
  *//*from  w  ww  .  ja v a2  s  . com*/
public static InputStream getResourceAsStream(String resourceName) throws IOException {
    // Strip any leading '/'
    if (resourceName.charAt(0) == '/') {
        resourceName = resourceName.substring(1);
    }

    ClassLoader gcl = Gate.getClassLoader();
    if (gcl == null) {
        // if the GATE ClassLoader has not been initialised yet (i.e. this
        // method was called before Gate.init) then fall back to the current
        // classloader
        return Files.class.getClassLoader().getResourceAsStream(resourceName);
    } else {
        // if we can, get the resource through the GATE ClassLoader to allow
        // loading of resources from plugin JARs as well as gate.jar
        return gcl.getResourceAsStream(resourceName);
    }
    //return  ClassLoader.getSystemResourceAsStream(resourceName);
}

From source file:com.agimatec.validation.jsr303.ConstraintDefaults.java

private Map<String, Class[]> loadDefaultConstraints(String resource) {
    Properties constraintProperties = new Properties();
    final ClassLoader classloader = getClassLoader();
    InputStream stream = classloader.getResourceAsStream(resource);
    if (stream != null) {
        try {//w  w w  . java 2  s.  c  om
            constraintProperties.load(stream);
        } catch (IOException e) {
            log.error("cannot load " + resource, e);
        }
    } else {
        log.warn("cannot find " + resource);
    }
    Map<String, Class[]> loadedConstraints = new HashMap();
    for (Map.Entry entry : constraintProperties.entrySet()) {

        StringTokenizer tokens = new StringTokenizer((String) entry.getValue(), ", ");
        LinkedList classes = new LinkedList();
        while (tokens.hasMoreTokens()) {
            final String eachClassName = tokens.nextToken();

            Class constraintValidatorClass = SecureActions.run(new PrivilegedAction<Class>() {
                public Class run() {
                    try {
                        return Class.forName(eachClassName, true, classloader);
                    } catch (ClassNotFoundException e) {
                        log.error("Cannot find class " + eachClassName, e);
                        return null;
                    }
                }
            });

            if (constraintValidatorClass != null)
                classes.add(constraintValidatorClass);

        }
        loadedConstraints.put((String) entry.getKey(), (Class[]) classes.toArray(new Class[classes.size()]));

    }
    return loadedConstraints;
}