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:org.apache.james.mailbox.elasticsearch.json.MessageToElasticSearchJsonTest.java

@Test
public void recursiveEmailShouldBeWellConvertedToJson() throws IOException {
    MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson(
            new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES);
    MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET,
            new SharedByteArrayInputStream(
                    IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/recursiveMail.eml"))),
            new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(),
            propertyBuilder, MAILBOX_ID);
    recursiveMail.setModSeq(MOD_SEQ);//from   ww  w  . j a v  a 2  s  .c om
    recursiveMail.setUid(UID);
    assertThatJson(messageToElasticSearchJson.convertToJson(recursiveMail,
            ImmutableList.of(new MockMailboxSession("username").getUser()))).when(IGNORING_ARRAY_ORDER)
                    .when(IGNORING_VALUES)
                    .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json")));
}

From source file:edu.umass.cs.msocket.proxy.console.ConsoleModule.java

/**
 * Extracts the commands from the command properties file as a single
 * <code>String</code> containing a list of comma-separated command classes.
 * //from w  ww. ja v  a2  s  . co m
 * @param moduleID module ID used as the key in the properties file
 * @return a single <code>String</code> containing a list of comma-separated
 *         command classes corresponding to the module identified by
 */
protected String loadCommandsFromProperties(String moduleID) {
    Properties props = new Properties();
    try {
        String propertiesFile = System.getProperty("console.commands", DEFAULT_COMMAND_PROPERTIES_FILE);
        props.load(ClassLoader.getSystemResourceAsStream(propertiesFile));
    } catch (IOException e) {
        // fail silently: no commands will be loaded
    }
    String commandClassesAsString = props.getProperty(moduleID, "");
    return commandClassesAsString;
}

From source file:com.safi.asterisk.handler.SafletEngine.java

public void loadEnvironmentProperties() {
    environmentProperties = new Properties();
    try {/*from  w w  w  .  ja va 2  s  .co m*/
        environmentProperties.load(ClassLoader.getSystemResourceAsStream(RESOURCES_ENVIRONMENT_PROPERTIES));
        originalProperties = (Properties) environmentProperties.clone();
    } catch (IOException e) {
        e.printStackTrace();
        log.error("Couldn't load environemnt properties", e);
    }

}

From source file:org.underworldlabs.util.FileUtils.java

/**
 * Copies the file at the specified from path to the 
 * specified to path.//from  ww w.j ava 2s. com
 *
 * @param from  - the from path
 * @param to  - the to path
 * @throws IOException
 */
public static void copyResource(String from, String to) throws IOException {
    InputStream in = null;
    OutputStream out = null;

    try {
        ClassLoader cl = FileUtils.class.getClassLoader();

        if (cl != null) {
            in = cl.getResourceAsStream(from);
        } else {
            in = ClassLoader.getSystemResourceAsStream(from);
        }

        out = new FileOutputStream(to);

        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        while (true) {
            synchronized (buffer) {
                int amountRead = in.read(buffer);
                if (amountRead == -1) {
                    break;
                }
                out.write(buffer, 0, amountRead);
            }
        }

        out.flush();

    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.fujitsu.dc.test.utils.Http.java

/**
 * ??HttpTest?.//w w  w .  j  ava2 s . co m
 * @param resPath ?
 * @return 
 */
public static Http request(String resPath) {
    Http ret = new Http();
    ret.is = ClassLoader.getSystemResourceAsStream("request/" + resPath);
    return ret;
}

From source file:org.ebayopensource.aegis.impl.JSONPolicyStore.java

private BufferedReader getReader() {
    String location = m_props.getProperty(PolicyStore.PolicyStoreLocation);
    BufferedReader rdr = null;/* w w  w.  ja v a2s .  c o m*/
    try {
        if (location.startsWith("classpath:")) {
            String cl = location.substring(10, location.length());
            Debug.message("JSONPolicyStrore", "getReader: cl=" + cl);

            Debug.message("JSONPolicyStrore", "getReader: res=" + ClassLoader.getSystemResourceAsStream(cl));
            rdr = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(cl)));
        } else {
            URL url = new URL(location);
            rdr = new BufferedReader(new InputStreamReader(url.openStream()));
        }

    } catch (Exception ex) {
        Debug.error("JSONPolicyStrore", "getReader failed : ex=", ex);
        PolicyException pe = new PolicyException("POLICY_DATASTORE_URLERROR", location);
        pe.initCause(ex);
        throw pe;
    }
    return rdr;
}

From source file:org.apache.ambari.server.state.services.AlertNoticeDispatchService.java

/**
 * {@inheritDoc}//ww w  .j  av  a 2  s  .  c om
 * <p/>
 * Parse the XML template for {@link Notification} content. If there is a
 * problem parsing the content, the service will still startup normally but
 * the {@link Notification} content will fallback to plaintext.
 */
@Override
protected void startUp() throws Exception {
    super.startUp();

    InputStream inputStream = null;
    String alertTemplatesFile = null;

    try {
        alertTemplatesFile = m_configuration.getAlertTemplateFile();
        if (null != alertTemplatesFile) {
            File file = new File(alertTemplatesFile);
            inputStream = new FileInputStream(file);
        }
    } catch (Exception exception) {
        LOG.warn("Unable to load alert template file {}", alertTemplatesFile, exception);
    }

    try {
        JAXBContext context = JAXBContext.newInstance(AlertTemplates.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // if the file provided via the configuration is not available, use
        // the internal one
        if (null == inputStream) {
            inputStream = ClassLoader.getSystemResourceAsStream(AMBARI_ALERT_TEMPLATES);
        }

        m_alertTemplates = (AlertTemplates) unmarshaller.unmarshal(inputStream);
    } catch (Exception exception) {
        LOG.error("Unable to load alert template file {}, outbound notifications will not be formatted",
                AMBARI_ALERT_TEMPLATES, exception);
    } finally {
        if (null != inputStream) {
            IOUtils.closeQuietly(inputStream);
        }
    }
}

From source file:org.apache.james.mailbox.elasticsearch.json.MailboxMessageToElasticSearchJsonTest.java

@Test
public void emailWithNoInternalDateShouldUseNowDate() throws IOException {
    MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson(
            new DefaultTextExtractor(), ZoneId.of("Europe/Paris"));
    MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(null, SIZE, BODY_START_OCTET,
            new SharedByteArrayInputStream(
                    IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("eml/recursiveMail.eml"))),
            new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(),
            propertyBuilder, MAILBOX_ID);
    mailWithNoInternalDate.setModSeq(MOD_SEQ);
    mailWithNoInternalDate.setUid(UID);
    assertThatJson(messageToElasticSearchJson.convertToJson(mailWithNoInternalDate,
            ImmutableList.of(new MockMailboxSession("username").getUser()))).when(IGNORING_ARRAY_ORDER)
                    .when(IGNORING_VALUES)
                    .isEqualTo(IOUtils.toString(ClassLoader.getSystemResource("eml/recursiveMail.json")));
}

From source file:org.elasticsearch.river.mongodb.RiverMongoDBTestAbstract.java

private static Settings loadSettings() {
    return settingsBuilder()
            .loadFromStream("settings.yml", ClassLoader.getSystemResourceAsStream("settings.yml")).build();
}

From source file:org.apache.tika.server.TikaResourceTest.java

@Test
public void testOCRLanguageConfig() throws Exception {
    if (!new TesseractOCRParser().hasTesseract(new TesseractOCRConfig())) {
        return;/*from   ww  w.jav a2s  .  c  o  m*/
    }

    Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
            .header(TikaResource.X_TIKA_PDF_HEADER_PREFIX + "OcrStrategy", "ocr_only")
            .header(TikaResource.X_TIKA_OCR_HEADER_PREFIX + "Language", "eng+fra")
            .header(TikaResource.X_TIKA_OCR_HEADER_PREFIX + "MinFileSizeToOcr", "10")
            .header(TikaResource.X_TIKA_OCR_HEADER_PREFIX + "MaxFileSizeToOcr", "1000000000")
            .put(ClassLoader.getSystemResourceAsStream("testOCR.pdf"));
    String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
    assertContains("Happy New Year 2003!", responseMsg);
}