Example usage for org.springframework.core.io ClassPathResource getInputStream

List of usage examples for org.springframework.core.io ClassPathResource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io ClassPathResource getInputStream.

Prototype

@Override
public InputStream getInputStream() throws IOException 

Source Link

Document

This implementation opens an InputStream for the given class path resource.

Usage

From source file:com.google.api.ads.adwords.jaxws.extensions.AwReporting.java

/**
 * Prints the sample properties file on the default output.
 *//*from w w w. j a  v a  2s. com*/
private static void printSamplePropertiesFile() {

    System.out.println("\n  File: aw-report-sample.properties example");

    ClassPathResource sampleFile = new ClassPathResource("aw-report-sample.properties");
    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(sampleFile.getInputStream());
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileScanner != null) {
            fileScanner.close();
        }
    }
}

From source file:com.ephesoft.dcma.util.BackUpFileService.java

/**
 * Open backUpService.properties containing backup configuration parameters, and populate a corresponding Properties object.
 *//*from   w ww.j  a  v  a2s .  com*/
private static void fetchConfig() {

    ClassPathResource classPathResource = new ClassPathResource(BACKUP_PROPERTY_FILE);

    InputStream input = null;
    try {
        input = classPathResource.getInputStream();
        backUpFileConfig.load(input);
    } catch (IOException ex) {
        LOGGER.error("Cannot open and load backUpService properties file.", ex);

    } finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ex) {
            LOGGER.error("Cannot close backUpService properties file.", ex);
        }
    }
}

From source file:com.ephesoft.dcma.util.BackUpFileService.java

/**
 * Open application.properties containing backup configuration parameters, and populate a corresponding Properties object.
 *///from  www.j  a  va 2  s .  co m
private static void fetchApplicationConfig() {

    ClassPathResource classPathResource = new ClassPathResource(APPLICATION_PROPERTY_FILE);

    InputStream input = null;
    try {
        input = classPathResource.getInputStream();
        applicationConfig.load(input);
    } catch (IOException ex) {
        LOGGER.error("Cannot open and load application properties file.", ex);
        // System.exit(1);
    } finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ex) {
            LOGGER.error("Cannot close application properties file.", ex);
        }
    }
}

From source file:com.github.vanroy.springdata.jest.MappingBuilder.java

private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, boolean isRootObject,
        String idFieldName, String nestedObjectFieldName, boolean nestedOrObjectField, FieldType fieldType,
        Field fieldAnnotation) throws IOException {

    java.lang.reflect.Field[] fields = retrieveFields(clazz);

    if (!isRootObject && (isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField)) {
        String type = FieldType.Object.toString().toLowerCase();
        if (nestedOrObjectField) {
            type = fieldType.toString().toLowerCase();
        }/*w ww.  jav  a  2 s . co m*/
        XContentBuilder t = xContentBuilder.startObject(nestedObjectFieldName).field(FIELD_TYPE, type);

        if (nestedOrObjectField && FieldType.Nested == fieldType && fieldAnnotation.includeInParent()) {
            t.field("include_in_parent", fieldAnnotation.includeInParent());
        }
        t.startObject(FIELD_PROPERTIES);
    }

    for (java.lang.reflect.Field field : fields) {

        if (field.isAnnotationPresent(Transient.class) || isInIgnoreFields(field)) {
            continue;
        }

        if (field.isAnnotationPresent(Mapping.class)) {
            String mappingPath = field.getAnnotation(Mapping.class).mappingPath();
            if (isNotBlank(mappingPath)) {
                ClassPathResource mappings = new ClassPathResource(mappingPath);
                if (mappings.exists()) {
                    xContentBuilder.rawField(field.getName(), mappings.getInputStream());
                    continue;
                }
            }
        }

        boolean isGeoPointField = isGeoPointField(field);
        boolean isCompletionField = isCompletionField(field);

        Field singleField = field.getAnnotation(Field.class);
        if (!isGeoPointField && !isCompletionField && isEntity(field) && isAnnotated(field)) {
            if (singleField == null) {
                continue;
            }
            boolean nestedOrObject = isNestedOrObjectField(field);
            mapEntity(xContentBuilder, getFieldType(field), false, EMPTY, field.getName(), nestedOrObject,
                    singleField.type(), field.getAnnotation(Field.class));
            if (nestedOrObject) {
                continue;
            }
        }

        MultiField multiField = field.getAnnotation(MultiField.class);

        if (isGeoPointField) {
            applyGeoPointFieldMapping(xContentBuilder, field);
        }

        if (isCompletionField) {
            CompletionField completionField = field.getAnnotation(CompletionField.class);
            applyCompletionFieldMapping(xContentBuilder, field, completionField);
        }

        if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
            applyDefaultIdFieldMapping(xContentBuilder, field);
        } else if (multiField != null) {
            addMultiFieldMapping(xContentBuilder, field, multiField, isNestedOrObjectField(field));
        } else if (singleField != null) {
            addSingleFieldMapping(xContentBuilder, field, singleField, isNestedOrObjectField(field));
        }
    }

    if (!isRootObject && isAnyPropertyAnnotatedAsField(fields) || nestedOrObjectField) {
        xContentBuilder.endObject().endObject();
    }
}

From source file:com.ephesoft.dcma.util.WebServiceUtil.java

/**
 * To fetch Configuration./*from   w  ww. j  a  v a2s . c o m*/
 * @return Properties
 * @throws IOException in case of error
 */
public static Properties fetchConfig() throws IOException {

    final ClassPathResource classPathResource = new ClassPathResource(BACKUP_PROPERTY_FILE);
    final Properties properties = new Properties();

    InputStream input = null;
    try {
        input = classPathResource.getInputStream();
        properties.load(input);
    } finally {
        if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
    return properties;
}

From source file:com.oreilly.rdf.tenuki.InputStreamChangesetTest.java

@Before
public void setUp() throws Exception {
    ClassPathResource changesetResource = new ClassPathResource("changeset.xml");
    changeset = new InputStreamChangeset(changesetResource.getInputStream());

}

From source file:com.mulesoft.example.fileconsumer.FileConsumerTestCase.java

@Override
protected void doSetUp() throws Exception {
    super.doSetUp();
    properties = new Properties();
    ClassPathResource location = new ClassPathResource("fileConsumer.properties");
    properties.load(location.getInputStream());
    location = new ClassPathResource("topicNames.properties");
    properties.load(location.getInputStream());
}

From source file:org.zrgs.spring.statemachine.CommonConfiguration.java

@Bean
public String stateChartModel() throws IOException {
    ClassPathResource model = new ClassPathResource("statechartmodel.txt");
    InputStream inputStream = model.getInputStream();
    Scanner scanner = new Scanner(inputStream);
    String content = scanner.useDelimiter("\\Z").next();
    scanner.close();//from w  w  w.  ja  v a 2  s . c o m
    return content;
}

From source file:org.cloudfoundry.tools.io.ClassPathFileTest.java

@Test
public void shouldLoadUsingExactPath() throws Exception {
    ClassPathResource resource = new ClassPathResource("/org/cloudfoundry/tools/io/a.txt");
    System.out.println(resource.getInputStream());
    File file = new ClassPathFile("/org/cloudfoundry/tools/io/a.txt");
    assertThat(file.getContent().asString(), is("a"));
}

From source file:com.wavemaker.commons.io.ClassPathFileTest.java

@Test
public void shouldLoadUsingExactPath() throws Exception {
    ClassPathResource resource = new ClassPathResource("/com/wavemaker/runtime/core/io/a.txt");
    System.out.println(resource.getInputStream());
    File file = new ClassPathFile("/com/wavemaker/runtime/core/io/a.txt");
    assertThat(file.getContent().asString(), Matchers.is("a"));
}