Search in sources :

Example 16 with PathMatchingResourcePatternResolver

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

the class CoreResources method copyConfig.

private void copyConfig() throws IOException {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    Resource[] resources = null;
    FileOutputStream out = null;
    Resource resource1 = null;
    Resource resource2 = null;
    resource1 = resolver.getResource("classpath:datainfo.properties");
    resource2 = resolver.getResource("classpath:extract.properties");
    String filePath = "$catalina.home/$WEBAPP.lower.config";
    filePath = replaceWebapp(filePath);
    filePath = replaceCatHome(filePath);
    File dest = new File(filePath);
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new OpenClinicaSystemException("Copying files, Could not create directory: " + dest.getAbsolutePath() + ".");
        }
    }
    File f1 = new File(dest, resource1.getFilename());
    File f2 = new File(dest, resource2.getFilename());
    if (!f1.exists()) {
        out = new FileOutputStream(f1);
        IOUtils.copy(resource1.getInputStream(), out);
        out.close();
    }
    if (!f2.exists()) {
        out = new FileOutputStream(f2);
        IOUtils.copy(resource2.getInputStream(), out);
        out.close();
    }
/*
         * 
         * for (Resource r: resources) { File f = new File(dest, r.getFilename()); if(!f.exists()){ out = new
         * FileOutputStream(f); IOUtils.copy(r.getInputStream(), out); out.close(); } }
         */
}
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) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) File(java.io.File)

Example 17 with PathMatchingResourcePatternResolver

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

the class CoreResources method copyImportRulesFiles.

private void copyImportRulesFiles() throws IOException {
    ByteArrayInputStream[] listSrcFiles = new ByteArrayInputStream[3];
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    String[] fileNames = { "rules.xsd", "rules_template.xml", "rules_template_with_notes.xml" };
    Resource[] resources = null;
    FileOutputStream out = null;
    resources = resolver.getResources("classpath*:properties/rules_template*.xml");
    File dest = new File(getField("filePath") + "rules");
    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());
        out = new FileOutputStream(f);
        IOUtils.copy(r.getInputStream(), out);
        out.close();
    }
    Resource[] r1 = resolver.getResources("classpath*:properties/" + fileNames[0]);
    File f1 = new File(dest, r1[0].getFilename());
    out = new FileOutputStream(f1);
    IOUtils.copy(r1[0].getInputStream(), out);
    out.close();
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) File(java.io.File)

Example 18 with PathMatchingResourcePatternResolver

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

the class CoreResources method copyODMMappingXMLtoResources.

private void copyODMMappingXMLtoResources(ResourceLoader resourceLoader) {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    String[] fileNames = { "cd_odm_mapping.xml" };
    Resource[] resources;
    try {
        resources = resolver.getResources("classpath*:properties/cd_odm_mapping.xml");
    } catch (IOException ioe) {
        OpenClinicaSystemException oe = new OpenClinicaSystemException("Unable to read source files");
        oe.initCause(ioe);
        oe.setStackTrace(ioe.getStackTrace());
        logger.debug(ioe.getMessage());
        throw oe;
    }
    File dest = null;
    try {
        dest = new File(getField("filePath"));
        if (!dest.exists()) {
            if (!dest.mkdirs()) {
                throw new OpenClinicaSystemException("Copying files, Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
        }
        File f = new File(dest, resources[0].getFilename());
        FileOutputStream out = new FileOutputStream(f);
        IOUtils.copy(resources[0].getInputStream(), out);
        out.close();
    } catch (IOException ioe) {
        OpenClinicaSystemException oe = new OpenClinicaSystemException("Unable to get web app base path");
        oe.initCause(ioe);
        oe.setStackTrace(ioe.getStackTrace());
        throw oe;
    }
}
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 19 with PathMatchingResourcePatternResolver

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

the class ActivitiEngine method autoDeploy.

private void autoDeploy() {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    String[] autoDeploymentFrom = wfConfiguration.getAutoDeploymentFrom();
    for (String adf : autoDeploymentFrom) {
        Resource[] resources;
        try {
            resources = resolver.getResources(adf);
        } catch (IOException e) {
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't get resources to be automatically deployed from " + adf, e);
            continue;
        }
        LOGGER.info("Auto deployment from " + adf + " yields " + resources.length + " resource(s)");
        for (Resource resource : resources) {
            try {
                autoDeployResource(resource);
            } catch (IOException | XPathExpressionException | RuntimeException e) {
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't deploy the resource " + resource, e);
            }
        }
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 20 with PathMatchingResourcePatternResolver

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

the class ClosureClassIgnoringComponentScanBeanDefinitionParser method configureScanner.

@Override
protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) {
    final ClassPathBeanDefinitionScanner scanner = super.configureScanner(parserContext, element);
    final ResourceLoader originalResourceLoader = parserContext.getReaderContext().getResourceLoader();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Scanning only this classloader:" + originalResourceLoader.getClassLoader());
    }
    ResourceLoader parentOnlyResourceLoader;
    try {
        parentOnlyResourceLoader = new ResourceLoader() {

            ClassLoader parentOnlyGetResourcesClassLoader = new ParentOnlyGetResourcesClassLoader(originalResourceLoader.getClassLoader());

            public Resource getResource(String location) {
                return originalResourceLoader.getResource(location);
            }

            public ClassLoader getClassLoader() {
                return parentOnlyGetResourcesClassLoader;
            }
        };
    } catch (Throwable t) {
        // restrictive classloading environment, use the original
        parentOnlyResourceLoader = originalResourceLoader;
    }
    final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(parentOnlyResourceLoader) {

        @Override
        protected Resource[] findAllClassPathResources(String location) throws IOException {
            Set<Resource> result = new LinkedHashSet<Resource>(16);
            if (BuildSettings.CLASSES_DIR != null) {
                @SuppressWarnings("unused") URL classesDir = BuildSettings.CLASSES_DIR.toURI().toURL();
                // only scan classes from project classes directory
                String path = location;
                if (path.startsWith("/")) {
                    path = path.substring(1);
                }
                Enumeration<URL> resourceUrls = getClassLoader().getResources(path);
                while (resourceUrls.hasMoreElements()) {
                    URL url = resourceUrls.nextElement();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Scanning URL " + url.toExternalForm() + " while searching for '" + location + "'");
                    }
                    /*
                    if (!warDeployed && classesDir!= null && url.equals(classesDir)) {
                        result.add(convertClassLoaderURL(url));
                    }
                    else if (warDeployed) {
                        result.add(convertClassLoaderURL(url));
                    }
                    */
                    result.add(convertClassLoaderURL(url));
                }
            }
            return result.toArray(new Resource[result.size()]);
        }
    };
    resourceResolver.setPathMatcher(new AntPathMatcher() {

        @Override
        public boolean match(String pattern, String path) {
            if (path.endsWith(".class")) {
                String filename = GrailsStringUtils.getFileBasename(path);
                if (filename.contains("$"))
                    return false;
            }
            return super.match(pattern, path);
        }
    });
    scanner.setResourceLoader(resourceResolver);
    return scanner;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResourceLoader(org.springframework.core.io.ResourceLoader) Resource(org.springframework.core.io.Resource) URL(java.net.URL) ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) AntPathMatcher(org.springframework.util.AntPathMatcher)

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