Example usage for org.springframework.core.io ResourceLoader getClassLoader

List of usage examples for org.springframework.core.io ResourceLoader getClassLoader

Introduction

In this page you can find the example usage for org.springframework.core.io ResourceLoader getClassLoader.

Prototype

@Nullable
ClassLoader getClassLoader();

Source Link

Document

Expose the ClassLoader used by this ResourceLoader.

Usage

From source file:org.data.support.beans.factory.xml.ResourceEntityResolver.java

/**
 * Create a ResourceEntityResolver for the specified ResourceLoader
 * (usually, an ApplicationContext).//from  w  w  w .  j a  v a  2  s  . co m
 * @param resourceLoader the ResourceLoader (or ApplicationContext)
 * to load XML entity includes with
 */
public ResourceEntityResolver(ResourceLoader resourceLoader) {
    super(resourceLoader.getClassLoader());
    this.resourceLoader = resourceLoader;
}

From source file:org.agilemicroservices.autoconfigure.orm.RepositoryConfigurationDelegate.java

/**
 * Creates a new {@link RepositoryConfigurationDelegate} for the given {@link RepositoryConfigurationSource} and
 * {@link ResourceLoader} and {@link Environment}.
 *
 * @param configurationSource must not be {@literal null}.
 * @param resourceLoader      must not be {@literal null}.
 * @param environment         must not be {@literal null}.
 *//*  w  w  w . ja va 2  s  .com*/
public RepositoryConfigurationDelegate(RepositoryConfigurationSource configurationSource,
        ResourceLoader resourceLoader, Environment environment) {
    Assert.notNull(resourceLoader);

    RepositoryBeanNameGenerator generator = new RepositoryBeanNameGenerator();
    generator.setBeanClassLoader(resourceLoader.getClassLoader());

    this.beanNameGenerator = generator;
    this.configurationSource = configurationSource;
    this.resourceLoader = resourceLoader;
    this.environment = defaultEnvironment(environment, resourceLoader);
    this.inMultiStoreMode = multipleStoresDetected();
}

From source file:com.baomidou.hibernateplus.HibernateSpringSessionFactoryBuilder.java

/**
 * Create a new LocalSessionFactoryBuilder for the given DataSource.
 *
 * @param dataSource     the JDBC DataSource that the resulting Hibernate SessionFactory should be using
 *                       (may be {@code null})
 * @param resourceLoader the ResourceLoader to load application classes from
 *//*w ww.j a  va  2s . c  om*/
public HibernateSpringSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) {
    this(dataSource, resourceLoader, new MetadataSources(
            new BootstrapServiceRegistryBuilder().applyClassLoader(resourceLoader.getClassLoader()).build()));
}

From source file:org.synyx.hades.dao.config.DaoConfigDefinitionParser.java

private Set<String> getDaoInterfacesForAutoConfig(final DaoConfigContext configContext,
        final ResourceLoader loader, final ReaderContext readerContext) {

    ClassPathScanningCandidateComponentProvider scanner = new GenericDaoComponentProvider();
    scanner.setResourceLoader(loader);//from  w  w  w.java 2s. c om

    TypeFilterParser parser = new TypeFilterParser(loader.getClassLoader(), readerContext);
    parser.parseFilters(configContext.getElement(), scanner);

    Set<BeanDefinition> findCandidateComponents = scanner
            .findCandidateComponents(configContext.getDaoBasePackageName());

    Set<String> interfaceNames = new HashSet<String>();
    for (BeanDefinition definition : findCandidateComponents) {
        interfaceNames.add(definition.getBeanClassName());
    }

    return interfaceNames;
}

From source file:com.baomidou.hibernateplus.HibernateSpringSessionFactoryBuilder.java

/**
 * Create a new LocalSessionFactoryBuilder for the given DataSource.
 *
 * @param dataSource      the JDBC DataSource that the resulting Hibernate SessionFactory should be using
 *                        (may be {@code null})
 * @param resourceLoader  the ResourceLoader to load application classes from
 * @param metadataSources the Hibernate MetadataSources service to use (e.g. reusing an existing one)
 * @since 4.3//w ww  . j ava  2s .c  o  m
 */
public HibernateSpringSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader,
        MetadataSources metadataSources) {
    super(metadataSources);

    getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
    if (dataSource != null) {
        getProperties().put(AvailableSettings.DATASOURCE, dataSource);
    }

    // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
    try {
        // Try Hibernate 5.2
        AvailableSettings.class.getField("CONNECTION_HANDLING");
        getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
    } catch (NoSuchFieldException ex) {
        // Try Hibernate 5.1
        try {
            AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
            getProperties().put("hibernate.connection.release_mode", "ON_CLOSE");
        } catch (NoSuchFieldException ex2) {
            // on Hibernate 5.0.x or lower - no need to change the default there
        }
    }

    getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader()));
    this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
}

From source file:org.codehaus.groovy.grails.context.annotation.ClosureClassIgnoringComponentScanBeanDefinitionParser.java

@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());
    }//from  w ww  . j  ava2 s.co m

    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);

            @SuppressWarnings("unused")
            URL classesDir = null;

            final boolean warDeployed = Metadata.getCurrent().isWarDeployed();
            if (!warDeployed) {
                BuildSettings buildSettings = BuildSettingsHolder.getSettings();
                if (buildSettings != null && buildSettings.getClassesDir() != null) {
                    classesDir = buildSettings.getClassesDir().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 = FilenameUtils.getBaseName(path);
                if (filename.indexOf("$") > -1)
                    return false;
            }
            return super.match(pattern, path);
        }
    });
    scanner.setResourceLoader(resourceResolver);
    return scanner;
}

From source file:org.grails.spring.context.annotation.ClosureClassIgnoringComponentScanBeanDefinitionParser.java

@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());
    }//  w  ww  .j  a v  a2  s  . c  o  m

    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;
}

From source file:org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser.java

private Set<String> getRepositoryInterfacesForAutoConfig(S config, ResourceLoader loader,
        ReaderContext reader) {/*  w  w w  . ja va  2  s. c  om*/

    ClassPathScanningCandidateComponentProvider scanner = new RepositoryComponentProvider(
            config.getRepositoryBaseInterface());
    scanner.setResourceLoader(loader);

    TypeFilterParser parser = new TypeFilterParser(loader.getClassLoader(), reader);
    parser.parseFilters(config.getSource(), scanner);

    Set<BeanDefinition> findCandidateComponents = scanner.findCandidateComponents(config.getBasePackage());

    Set<String> interfaceNames = new HashSet<String>();
    for (BeanDefinition definition : findCandidateComponents) {
        interfaceNames.add(definition.getBeanClassName());
    }

    return interfaceNames;
}

From source file:org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.java

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
    this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
    this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(resourceLoader.getClassLoader());
}