Search in sources :

Example 31 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project OpenClinica by OpenClinica.

the class QueryStore method init.

public void init() {
    String dbFolder = resolveDbFolder();
    PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(resourceLoader);
    try {
        Resource[] resources = resourceResolver.getResources("classpath:queries/" + dbFolder + "/**/*.properties");
        for (Resource r : resources) {
            Properties p = new Properties();
            p.load(r.getInputStream());
            fileByName.put(StringUtils.substringBeforeLast(r.getFilename(), "."), p);
        }
    } catch (IOException e) {
        throw new BeanInitializationException("Unable to read files from directory 'classpath:queries/" + dbFolder + "'", e);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Properties(java.util.Properties)

Example 32 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project ORCID-Source by ORCID.

the class TestXmlValidity method testAllOrcidMessages.

@Test
public void testAllOrcidMessages() throws IOException {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources("classpath*:**/*message-latest.xml");
    for (Resource resource : resources) {
        LOG.info("Found resource: {}", resource);
        InputStream is = null;
        try {
            is = resource.getInputStream();
            OrcidMessage message = (OrcidMessage) unmarshaller.unmarshal(is);
            validationManager = getValidationManager(message.getMessageVersion());
            validationManager.validateMessage(message);
        } catch (IOException e) {
            Assert.fail("Unable to read resource: " + resource + "\n" + e);
        } catch (JAXBException e) {
            Assert.fail("ORCID message is not well formed: " + resource + "\n" + e);
        } catch (OrcidValidationException e) {
            Assert.fail("Validation failed: " + resource + "\n" + e.getCause());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}
Also used : InputStream(java.io.InputStream) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) JAXBException(javax.xml.bind.JAXBException) Resource(org.springframework.core.io.Resource) OrcidValidationException(org.orcid.core.exception.OrcidValidationException) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Test(org.junit.Test)

Example 33 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project OpenClinica by OpenClinica.

the class CoreResources method copyBaseToDest.

private void copyBaseToDest(ResourceLoader resourceLoader) {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    Resource[] resources;
    try {
        /*
             * Use classpath* to search for resources that match this pattern in ALL of the jars in the application
             * class path. See:
             * http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources
             * .html#resources-classpath-wildcards
             */
        resources = resolver.getResources("classpath*:properties/xslt/*.xsl");
    } catch (IOException ioe) {
        logger.debug(ioe.getMessage(), ioe);
        throw new OpenClinicaSystemException("Unable to read source files", ioe);
    }
    File dest = new File(getField("filePath") + "xslt");
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new OpenClinicaSystemException("Copying files, Could not create direcotry: " + dest.getAbsolutePath() + ".");
        }
    }
    for (Resource r : resources) {
        File f = new File(dest, r.getFilename());
        try {
            FileOutputStream out = new FileOutputStream(f);
            IOUtils.copy(r.getInputStream(), out);
            out.close();
        } catch (IOException ioe) {
            logger.debug(ioe.getMessage(), ioe);
            throw new OpenClinicaSystemException("Unable to copy file: " + r.getFilename() + " to " + f.getAbsolutePath(), ioe);
        }
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) FileOutputStream(java.io.FileOutputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) File(java.io.File)

Example 34 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project Asqatasun by Asqatasun.

the class LanguageDetector method initProfiles.

/**
     * Initialise the language profiles needed by the detector. This
     * initialisation has to be performed only once.
     */
private void initProfiles() {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    List<String> profiles = new ArrayList<>();
    DetectorFactory.setSeed(0L);
    try {
        for (Resource rs : resolver.getResources(profilePath)) {
            StringWriter writer = new StringWriter();
            IOUtils.copy(rs.getInputStream(), writer);
            profiles.add(writer.toString());
        }
        DetectorFactory.loadProfile(profiles);
    } catch (IOException | LangDetectException ex) {
        LOGGER.warn(ex);
    }
}
Also used : StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) LangDetectException(com.cybozu.labs.langdetect.LangDetectException)

Aggregations

PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)34 Resource (org.springframework.core.io.Resource)22 Test (org.junit.Test)15 IOException (java.io.IOException)11 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)11 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)10 File (java.io.File)9 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)9 ClassPathResource (org.springframework.core.io.ClassPathResource)7 ArrayList (java.util.ArrayList)6 FileOutputStream (java.io.FileOutputStream)4 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)4 UrlResource (org.springframework.core.io.UrlResource)3 URL (java.net.URL)2 Properties (java.util.Properties)2 DataSource (javax.sql.DataSource)2 Ignore (org.junit.Ignore)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 LangDetectException (com.cybozu.labs.langdetect.LangDetectException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1