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.astamuse.asta4d.web.WebApplicatoinConfigurationInitializer.java

public void initConfigurationFromFile(ServletConfig sc, WebApplicationConfiguration conf) throws Exception {
    String[] fileNames = retrievePossibleConfigurationFileNames();
    InputStream input = null;//from  w w w .  j a v  a 2  s . com
    String fileType = null;

    // find file from classpath
    ClassLoader clsLoder = WebApplicatoinConfigurationInitializer.class.getClassLoader();
    for (String name : fileNames) {
        input = clsLoder.getResourceAsStream(name);
        if (input != null) {
            fileType = FilenameUtils.getExtension(name);
            break;
        }
    }

    // find from file system
    // I can do goto by while loop :)
    while (input == null) {

        // find key
        String fileKey = retrieveConfigurationFileNameKey();
        if (fileKey == null) {
            break;
        }

        // get path
        String filePath = retrieveConfigurationFileName(sc, fileKey);
        if (filePath == null) {
            break;
        }

        // load file
        File f = new File(filePath);
        input = new FileInputStream(f);
        fileType = FilenameUtils.getExtension(filePath);
        break;
    }

    if (input != null) {
        try {
            Initializer initializer = retrieveInitializer(fileType);
            initializer.initliaze(input, conf);
        } finally {
            input.close();
        }
    }
}

From source file:com.galeoconsulting.leonardinius.api.impl.ChainingClassLoader.java

@Override
public InputStream getResourceAsStream(String name) {
    for (ClassLoader classloader : classLoaders) {
        final InputStream inputStream = classloader.getResourceAsStream(name);
        if (inputStream != null) {
            return inputStream;
        }//from  w w  w . j  ava2 s .co  m
    }

    return null;
}

From source file:com.arvato.thoroughly.util.security.impl.DefaultEncryptionKeyRetrievalStrategy.java

private SecretKeySpec tryToLoadKeyFromClasspath() throws IOException, Exception {
    final ClassLoader classloader = getClassLoader();

    if (classloader != null) {
        final InputStream stream = classloader.getResourceAsStream(this.keyFilePath);
        final Throwable localThrowable3 = null;
        try {//w  w  w . j  av a 2 s .c  om
            final SecretKeySpec localSecretKeySpec = convertStreamToKey(stream);
            return localSecretKeySpec;
        } catch (final Throwable localThrowable4) {
            LOGGER.error(localThrowable4.getMessage());
        } finally {
            if (stream != null) {
                if (localThrowable3 != null) {
                    try {
                        stream.close();
                    } catch (final Throwable localThrowable2) {
                        localThrowable3.addSuppressed(localThrowable2);
                    }
                } else {
                    try {
                        stream.close();
                    } catch (final IOException e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
            }
        }
    }
    LOGGER.error("Error loading encryption key from classpath");
    return null;
}

From source file:com.adaptris.core.management.ManagementComponentFactory.java

private Object resolve(final String name, BootstrapProperties bootstrapProperties) throws Exception {
    final ClassLoader originalContectClassLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader classLoader = getClass().getClassLoader();
    try (final InputStream in = classLoader.getResourceAsStream(RESOURCE_PATH + name)) {
        if (in != null) {
            final Properties p = new Properties();
            p.load(in);//from  w w w .ja v a 2  s  . co m

            final String classloaderProperty = p.getProperty(CLASSLOADER_KEY);
            if (classloaderProperty != null) {
                log.debug("Using custom class loader " + classloaderProperty);
                final Class<ClassLoaderFactory> classLoaderFactoryClass = (Class<ClassLoaderFactory>) Class
                        .forName(classloaderProperty);
                final Constructor<ClassLoaderFactory> constructor = classLoaderFactoryClass
                        .getConstructor(BootstrapProperties.class);
                final ClassLoaderFactory classLoaderFactory = constructor.newInstance(bootstrapProperties);
                classLoader = classLoaderFactory.create(classLoader);
                Thread.currentThread().setContextClassLoader(classLoader);
            }
            final Object component = Class.forName(p.getProperty(PROPERTY_KEY), true, classLoader)
                    .newInstance();
            if (classloaderProperty != null) {
                invokeMethod(component, "setClassLoader", new Class[] { ClassLoader.class },
                        new ClassLoader[] { classLoader });
            }
            return component;
        }
        return Class.forName(name).newInstance();
    } finally {
        Thread.currentThread().setContextClassLoader(originalContectClassLoader);
    }
}

From source file:org.apache.maven.doxia.siterenderer.SkinResourceLoader.java

public synchronized InputStream getResourceStream(String name) throws ResourceNotFoundException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    if (name.startsWith("/")) {
        name = name.substring(1);/*from w  ww.j  a  v  a 2s . c om*/
    }

    return normalizeNewline(classLoader.getResourceAsStream(name));
}

From source file:wepa.service.initservice.InitService.java

public MultipartFile getImage(String imagePath) {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream(imagePath);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {/*from w  w  w  . j a v  a2 s.c  o m*/
        IOUtils.copy(is, outputStream);
    } catch (IOException ioe) {
        System.out.println("Problem while reading from the file: " + ioe.getMessage());
        return null;
    }

    byte[] bytes = outputStream.toByteArray();
    MultipartFile multipartFile = new MockMultipartFile("file", "hippo.jpg", "image/jpeg", bytes);
    return multipartFile;
}

From source file:hudson.plugins.dotnetasscript.main.DotNetAsScriptPluginFacade.java

/**
 * Gets the target resource file as string
 * @param fileName/*from w w  w. j ava2s  . c o  m*/
 * @return 
 * @throws java.io.IOException 
 */
public String getResourceFileContent(String fileName) throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);
    String content = IOUtils.toString(inputStream, ProjectConstants.ENCODING);
    return content;
}

From source file:org.apache.axis2.mtom.EchoRawMTOMTest.java

protected InputStream getResourceAsStream(String path) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    return cl.getResourceAsStream(path);
}

From source file:com.olp.jpa.domain.docu.ut.PreTest.java

@Test
//@Rollback(false)
public void loadTestData() {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    List<DeptBeanPub> list1;

    InputStream is1 = loader.getResourceAsStream("test_dept_data_01.xml");
    DataLoader<DeptBeanPub, Long> deptLoader = new DataLoader<>(DeptContainer.class, is1);
    list1 = deptLoader.load(deptSvc, false);

    if (is1 != null) {
        try {/*from w w  w . j  a va2  s  .com*/
            is1.close();
        } catch (IOException ex) {
            //no-op
        }
    }

    List<EmpBeanPub> list2;
    InputStream is2 = loader.getResourceAsStream("test_emp_data_01.xml");
    DataLoader<EmpBeanPub, Long> empLoader = new DataLoader<>(EmpContainer.class, is2);
    list2 = empLoader.load(empSvc, false);

    if (is2 != null) {
        try {
            is2.close();
        } catch (IOException ex) {
            // no-op  
        }
    }

}

From source file:org.gallery.persist.ImageDaoTest.java

/**
 * @param filename/* w  w  w .java  2s .c o m*/
 * @return
 * @throws IOException
 * @throws DataSetException
 */
private IDataSet getDataSet(String filename) throws IOException, DataSetException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = loader.getResourceAsStream(filename);
    assertNotNull(inputStream);
    Reader reader = new InputStreamReader(inputStream);
    return new FlatXmlDataSet(reader);
}