Example usage for java.lang ClassLoader getSystemResourceAsStream

List of usage examples for java.lang ClassLoader getSystemResourceAsStream

Introduction

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

Prototype

public static InputStream getSystemResourceAsStream(String name) 

Source Link

Document

Open for reading, a resource of the specified name from the search path used to load classes.

Usage

From source file:com.moviejukebox.tools.PropertiesUtil.java

/**
 * Set the properties filename with a warning if the file is not found
 *
 * @param streamName//  www .  ja v a  2 s . c  o m
 * @param warnFatal
 * @return
 */
public static boolean setPropertiesStreamName(final String streamName, boolean warnFatal) {
    LOG.info("Using properties file '{}'", FilenameUtils.normalize(streamName));
    InputStream propertiesStream = ClassLoader.getSystemResourceAsStream(streamName);

    try {
        if (propertiesStream == null) {
            propertiesStream = new FileInputStream(streamName);
        }

        try (Reader reader = new InputStreamReader(propertiesStream, PROPERTIES_CHARSET)) {
            PROPS.load(reader);
        }
    } catch (IOException error) {
        // Output a warning if required.
        if (warnFatal) {
            LOG.error(
                    "Failed loading file {}: Please check your configuration. The properties file should be in the classpath.",
                    streamName, error);
        } else {
            LOG.debug("Warning (non-fatal): User properties file '{}', not found.", streamName);
        }
        return Boolean.FALSE;
    } finally {
        try {
            if (propertiesStream != null) {
                propertiesStream.close();
            }
        } catch (IOException e) {
            // Ignore
        }
    }
    return Boolean.TRUE;
}

From source file:io.cloudslang.content.xml.actions.ApplyXslTransformationTest.java

@Before
public void setUp() throws Exception {
    applyXslTransformation = new ApplyXslTransformation();
    xml = join(readLines(ClassLoader.getSystemResourceAsStream("applyxslres/testTransform.xml"),
            Charset.forName("UTF-8")), IOUtils.LINE_SEPARATOR);
    xsl = join(readLines(ClassLoader.getSystemResourceAsStream("applyxslres/xslTemplate.xsl"),
            Charset.forName("UTF-8")), IOUtils.LINE_SEPARATOR);
    invalidXml = join(readLines(ClassLoader.getSystemResourceAsStream("applyxslres/invalid.xml"),
            Charset.forName("UTF-8")), IOUtils.LINE_SEPARATOR);
    resultHtml = join(readLines(ClassLoader.getSystemResourceAsStream("applyxslres/result.html"),
            Charset.forName("UTF-8")), IOUtils.LINE_SEPARATOR);
}

From source file:com.octo.mbo.data.Pptx4JUpdatablePackageTest.java

@Test
public void loadPackageTest() throws CopyNotesException, InvalidFormatException {
    InputStream testFileStream = ClassLoader.getSystemResourceAsStream("pptx-test.pptx");

    Pptx4jPackageFactory factory = new Pptx4jPackageFactory();
    Pptx4jPackage pptx4jPackage = factory.loadPackage(null, testFileStream);
    Assert.assertEquals(26, pptx4jPackage.pMLpkg.getParts().getParts().size());
    Assert.assertNotNull(pptx4jPackage.pMLpkg.getParts().get(new PartName("/ppt/slides/slide1.xml")));
    Assert.assertNotNull(pptx4jPackage.pMLpkg.getParts().get(new PartName("/ppt/slides/slide2.xml")));
    Assert.assertNull(pptx4jPackage.pMLpkg.getParts().get(new PartName("/ppt/slides/slide3.xml")));
}

From source file:com.jostrobin.battleships.view.sound.DefaultSoundEffects.java

private void playSound(final String path) {
    if (enabled) {

        InputStream is = ClassLoader.getSystemResourceAsStream(path);
        AudioInputStream audioInputStream = null;
        try {//  ww  w .  j  a v  a 2  s. c  o m
            audioInputStream = AudioSystem.getAudioInputStream(is);
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
            while (clip.isRunning()) {
                Thread.sleep(10);
            }
        } catch (Exception e) {
            logger.warn("Could not play sound effect", e);
        } finally {
            IOUtils.closeSilently(is, audioInputStream);
        }
    }
}

From source file:org.dizitart.no2.ui.data.DataGenerator.java

private static Note randomNote() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream("note.text");
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

    String strLine;//from  w ww .  j  a  v a  2s  . c  om
    int line = random.nextInt(49);
    int count = 0;
    try {
        while ((strLine = br.readLine()) != null) {
            if (count == line) {
                Note note = new Note();
                note.setNoteId(line);
                note.setText(strLine);
                return note;
            }
            count++;
        }
    } catch (IOException e) {
        // ignore
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            // ignore
        }
    }
    return null;
}

From source file:org.apache.camel.component.schematron.processor.SchematronProcessorTest.java

@Test
public void testInValidXML() throws Exception {

    String payload = IOUtils.toString(ClassLoader.getSystemResourceAsStream("xml/article-2.xml"));
    logger.info("Validating payload: {}", payload);
    // validate/*from w ww  . ja va  2 s . c  o m*/
    String result = getProcessor("sch/schematron-2.sch").validate(payload);
    logger.info("Schematron Report: {}", result);
    // should throw two assertions because of the missing chapters in the XML.
    assertEquals("A chapter should have a title", Utils.evaluate("//svrl:failed-assert/svrl:text", result));
    assertEquals("'chapter' element has more than one title present",
            Utils.evaluate("//svrl:successful-report/svrl:text", result).trim());

}

From source file:com.xpfriend.fixture.runner.example.ExampleJob.java

/**
 * db.properties ?????// w  w w  .  j  a  va  2  s.c o  m
 * @return ?
 */
private static Properties getProperties() throws IOException {
    Properties props = new Properties();
    InputStream is = ClassLoader.getSystemResourceAsStream("db.properties");
    try {
        props.load(is);
    } finally {
        is.close();
    }
    return props;
}

From source file:org.apache.geode.security.ExampleSecurityManagerTest.java

@Before
public void setUp() throws Exception {
    // resource file
    this.jsonResource = "org/apache/geode/security/templates/security.json";
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(this.jsonResource);

    assertThat(inputStream).isNotNull();

    // non-resource file
    this.jsonFile = new File(temporaryFolder.getRoot(), "security.json");
    IOUtils.copy(inputStream, new FileOutputStream(this.jsonFile));

    // string/*from w ww.  ja  va  2 s .co  m*/
    this.json = FileUtils.readFileToString(this.jsonFile, "UTF-8");
    this.exampleSecurityManager = new ExampleSecurityManager();
}

From source file:com.tacitknowledge.util.migration.jdbc.loader.FileLoadingUtility.java

/**
 * Gets an input stream by first checking the current classloader,
 * then trying to use the system classloader, and finally, trying
 * to access the file on the file system.  If the file is not found,
 * an <code>IllegalArgumentException</code> will be thrown.
 *
 * @return the file as an input stream/*from w  w  w  .  j  a v a 2 s.c  om*/
 */
public InputStream getResourceAsStream() {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    if (stream == null) {
        stream = ClassLoader.getSystemResourceAsStream(fileName);

    }
    if (stream == null) {
        File f = new File(fileName);
        try {
            stream = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            log.error("The file: " + fileName + " was not found.", e);
            throw new IllegalArgumentException("Must have a valid file name.");
        }
    }
    return stream;
}

From source file:org.apache.geode.security.templates.SampleSecurityManagerTest.java

@Before
public void setUp() throws Exception {
    // resource file
    this.jsonResource = "org/apache/geode/security/templates/security.json";
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(this.jsonResource);

    assertThat(inputStream).isNotNull();

    // non-resource file
    this.jsonFile = new File(temporaryFolder.getRoot(), "security.json");
    IOUtils.copy(inputStream, new FileOutputStream(this.jsonFile));

    // string/*ww w  .  ja v  a  2s.c  o  m*/
    this.json = FileUtils.readFileToString(this.jsonFile, "UTF-8");
    this.sampleSecurityManager = new SampleSecurityManager();
}