Search in sources :

Example 26 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project new-cloud by xie-summer.

the class MybatisConfigurer method sqlSessionFactoryBean.

@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setTypeAliasesPackage(MybatisConstant.MODEL_PACKAGE);
    // 分页插件
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("reasonable", "true");
    properties.setProperty("supportMethodsArguments", "true");
    properties.setProperty("returnPageInfo", "check");
    properties.setProperty("params", "count=countSql");
    pageHelper.setProperties(properties);
    bean.setPlugins(new Interceptor[] { pageHelper });
    // 添加XML目录
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    bean.setMapperLocations(resolver.getResources("classpath:mapper/**.xml"));
    return bean.getObject();
}
Also used : PageHelper(com.github.pagehelper.PageHelper) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Properties(java.util.Properties) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 27 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project spring-framework by spring-projects.

the class AbstractBeanDefinitionReader method loadBeanDefinitions.

/**
 * Load bean definitions from the specified resource location.
 * <p>The location can also be a location pattern, provided that the
 * ResourceLoader of this bean definition reader is a ResourcePatternResolver.
 * @param location the resource location, to be loaded with the ResourceLoader
 * (or ResourcePatternResolver) of this bean definition reader
 * @param actualResources a Set to be filled with the actual Resource objects
 * that have been resolved during the loading process. May be {@code null}
 * to indicate that the caller is not interested in those Resource objects.
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 * @see #getResourceLoader()
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource)
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
 */
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
    ResourceLoader resourceLoader = getResourceLoader();
    if (resourceLoader == null) {
        throw new BeanDefinitionStoreException("Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");
    }
    if (resourceLoader instanceof ResourcePatternResolver) {
        // Resource pattern matching available.
        try {
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            int count = loadBeanDefinitions(resources);
            if (actualResources != null) {
                Collections.addAll(actualResources, resources);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Loaded " + count + " bean definitions from location pattern [" + location + "]");
            }
            return count;
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", ex);
        }
    } else {
        // Can only load single resources by absolute URL.
        Resource resource = resourceLoader.getResource(location);
        int count = loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
        }
        return count;
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 28 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project spring-framework by spring-projects.

the class ResourceEditorRegistrar method registerCustomEditors.

/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
    doRegisterEditor(registry, Resource.class, baseEditor);
    doRegisterEditor(registry, ContextResource.class, baseEditor);
    doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
    doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
    doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
    doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
    doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
    doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
    ClassLoader classLoader = this.resourceLoader.getClassLoader();
    doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
    doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
    doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));
    if (this.resourceLoader instanceof ResourcePatternResolver) {
        doRegisterEditor(registry, Resource[].class, new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
    }
}
Also used : FileEditor(org.springframework.beans.propertyeditors.FileEditor) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStreamEditor(org.springframework.beans.propertyeditors.InputStreamEditor) URIEditor(org.springframework.beans.propertyeditors.URIEditor) ContextResource(org.springframework.core.io.ContextResource) Resource(org.springframework.core.io.Resource) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ResourceEditor(org.springframework.core.io.ResourceEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) InputSourceEditor(org.springframework.beans.propertyeditors.InputSourceEditor) ReaderEditor(org.springframework.beans.propertyeditors.ReaderEditor) PathEditor(org.springframework.beans.propertyeditors.PathEditor) URLEditor(org.springframework.beans.propertyeditors.URLEditor)

Example 29 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver 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 30 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver 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)

Aggregations

ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)65 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)62 Resource (org.springframework.core.io.Resource)49 IOException (java.io.IOException)15 Bean (org.springframework.context.annotation.Bean)11 ArrayList (java.util.ArrayList)10 PageHelper (com.github.pagehelper.PageHelper)9 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)9 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)9 MetadataReader (org.springframework.core.type.classreading.MetadataReader)9 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)9 File (java.io.File)8 Properties (java.util.Properties)8 InputStream (java.io.InputStream)5 URL (java.net.URL)5 FileOutputStream (java.io.FileOutputStream)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 SocketChannel (io.netty.channel.socket.SocketChannel)2