Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanClassLoader

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanClassLoader

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanClassLoader.

Prototype

@Nullable
ClassLoader getBeanClassLoader();

Source Link

Document

Return this factory's class loader for loading bean classes (only null if even the system ClassLoader isn't accessible).

Usage

From source file:com.mmnaseri.dragonfly.runtime.session.impl.AbstractSessionPreparator.java

private void handleClassLoader(ConfigurableListableBeanFactory beanFactory) {
    log.info("Setting the class loader for the entity context");
    entityContext.setDefaultClassLoader(beanFactory.getBeanClassLoader());
}

From source file:com.agileapes.motorex.cli.value.impl.SpringValueReaderContext.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final String[] names = beanFactory.getBeanDefinitionNames();
    for (String name : names) {
        final BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton()) {
            final String className = definition.getBeanClassName();
            try {
                final Class<?> beanType = ClassUtils.forName(className, beanFactory.getBeanClassLoader());
                if (ValueReader.class.isAssignableFrom(beanType)) {
                    register(beanFactory.getBean(name, ValueReader.class));
                }/*  www  .j  av  a  2  s . co m*/
            } catch (ClassNotFoundException e) {
                throw new BeanCreationNotAllowedException(name, "Failed to access bean");
            }
        }
    }
}

From source file:com.agileapes.motorex.cli.target.impl.SpringExecutionTargetContext.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final String[] names = beanFactory.getBeanDefinitionNames();
    for (String name : names) {
        final BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isSingleton()) {
            final String className = definition.getBeanClassName();
            try {
                final Class<?> beanClass = ClassUtils.forName(className, beanFactory.getBeanClassLoader());
                if (ExecutionTarget.class.isAssignableFrom(beanClass)) {
                    final ExecutionTarget target = beanFactory.getBean(name, ExecutionTarget.class);
                    if (target instanceof AbstractIdentifiedExecutionTarget) {
                        AbstractIdentifiedExecutionTarget executionTarget = (AbstractIdentifiedExecutionTarget) target;
                        executionTarget.setIdentifier(name);
                    }//w w w  .  j  a  v  a  2 s.c  o  m
                    register(target);
                }
            } catch (ClassNotFoundException e) {
                throw new BeanCreationException(name, "Could not access bean class");
            }
        }
    }
}

From source file:lodsve.core.condition.BeanTypeRegistry.java

private Class<?> getDirectFactoryBeanGeneric(ConfigurableListableBeanFactory beanFactory,
        BeanDefinition definition, String name) throws ClassNotFoundException, LinkageError {
    Class<?> factoryBeanClass = ClassUtils.forName(definition.getBeanClassName(),
            beanFactory.getBeanClassLoader());
    Class<?> generic = ResolvableType.forClass(factoryBeanClass).as(FactoryBean.class).resolveGeneric();
    if ((generic == null || generic.equals(Object.class))
            && definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
        generic = getTypeFromAttribute(definition.getAttribute(FACTORY_BEAN_OBJECT_TYPE));
    }//w  w w  .  j  a  v  a 2 s  .c om
    return generic;
}

From source file:com.mmnaseri.dragonfly.runtime.session.impl.AbstractSessionPreparator.java

@Override
public void postProcessSession(ConfigurableListableBeanFactory beanFactory) {
    log.warn(//from  w ww .  ja va 2  s . c  o  m
            "Preparing the session at runtime can be harmful to your performance. You should consider switching to "
                    + "the Maven plugin.");
    log.debug("Looking up the necessary components ...");
    if (parentClassLoader == null) {
        log.debug("Falling back to the application context class loader");
        parentClassLoader = beanFactory.getBeanClassLoader();
    }
    final DataAccessSession session = beanFactory.getBean(DataAccessSession.class);
    final Collection<SessionInitializationEventHandler> eventHandlers = beanFactory
            .getBeansOfType(SessionInitializationEventHandler.class, false, true).values();
    for (SessionInitializationEventHandler eventHandler : eventHandlers) {
        if (eventHandler instanceof DataAccessSessionAware) {
            DataAccessSessionAware aware = (DataAccessSessionAware) eventHandler;
            aware.setDataAccessSession(session);
        }
    }
    prepareResolvers(eventHandlers);
    registerEntities(eventHandlers);
    registerExtensions(eventHandlers);
    prepareStatements(eventHandlers);
    registerInterfaces(eventHandlers);
    initializeSession(session, beanFactory, eventHandlers);
    handleClassLoader(beanFactory);
}

From source file:org.apache.james.container.spring.lifecycle.osgi.OsgiLifecycleBeanFactoryPostProcessor.java

@Override
public void postProcessBeanFactory(BundleContext context, ConfigurableListableBeanFactory factory)
        throws BeansException, InvalidSyntaxException, BundleException {
    // We need to set the beanfactory by hand. This MAY be a bug in spring-dm but I'm not sure yet
    LogEnabledBeanPostProcessor loggingProcessor = new LogEnabledBeanPostProcessor();
    loggingProcessor.setBeanFactory(factory);
    loggingProcessor.setLogProvider(logProvider);
    loggingProcessor.setOrder(0);/*w  w w.  j  a  v a  2s  .c  o  m*/
    factory.addBeanPostProcessor(loggingProcessor);

    OSGIResourceAnnotationBeanPostProcessor resourceProcessor = new OSGIResourceAnnotationBeanPostProcessor();
    resourceProcessor.setBeanClassLoader(factory.getBeanClassLoader());
    resourceProcessor.setBeanFactory(factory);
    resourceProcessor.setBundleContext(context);
    resourceProcessor.setTimeout(60 * 1000);
    factory.addBeanPostProcessor(resourceProcessor);

    OSGIPersistenceUnitAnnotationBeanPostProcessor persistenceProcessor = new OSGIPersistenceUnitAnnotationBeanPostProcessor();
    persistenceProcessor.setBeanClassLoader(factory.getBeanClassLoader());
    persistenceProcessor.setBeanFactory(factory);
    persistenceProcessor.setBundleContext(context);
    persistenceProcessor.setTimeout(60 * 1000);
    factory.addBeanPostProcessor(persistenceProcessor);

    ConfigurableBeanPostProcessor configurationProcessor = new ConfigurableBeanPostProcessor();
    configurationProcessor.setBeanFactory(factory);
    configurationProcessor.setConfigurationProvider(confProvider);
    configurationProcessor.setOrder(2);
    factory.addBeanPostProcessor(configurationProcessor);

    InitDestroyAnnotationBeanPostProcessor annotationProcessor = new InitDestroyAnnotationBeanPostProcessor();
    annotationProcessor.setInitAnnotationType(PostConstruct.class);
    annotationProcessor.setDestroyAnnotationType(PreDestroy.class);
    factory.addBeanPostProcessor(annotationProcessor);

}

From source file:lodsve.core.condition.BeanTypeRegistry.java

private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition)
        throws Exception {
    if (definition instanceof AnnotatedBeanDefinition) {
        MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition)
                .getFactoryMethodMetadata();
        if (factoryMethodMetadata instanceof StandardMethodMetadata) {
            return ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod();
        }//from w  ww .  j  ava  2 s.c o  m
    }
    BeanDefinition factoryDefinition = beanFactory.getBeanDefinition(definition.getFactoryBeanName());
    Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(),
            beanFactory.getBeanClassLoader());
    return ReflectionUtils.findMethod(factoryClass, definition.getFactoryMethodName());
}

From source file:com.mmnaseri.dragonfly.runtime.session.ApplicationSessionPreparator.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory context) throws BeansException {
    showDeprecationWarning();/*from  w  w w .  j a v  a2s. co  m*/
    long time = System.nanoTime();
    log.warn(
            "Preparing the session at runtime can be harmful to your performance. You should consider switching to "
                    + "the Maven plugin.");
    log.debug("Looking up the necessary components ...");
    tableMetadataRegistry = context.getBean(TableMetadataRegistry.class);
    statementRegistry = context.getBean(StatementRegistry.class);
    final ModifiableEntityContext entityContext = context.getBean(ModifiableEntityContext.class);
    final ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
            false);
    log.info("Finding entity classes ...");
    log.debug("Looking for classes with @Entity");
    componentProvider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
    if (parentClassLoader == null) {
        log.debug("Falling back to the application context class loader");
        parentClassLoader = context.getBeanClassLoader();
    }
    for (String basePackage : basePackages) {
        final Set<BeanDefinition> beanDefinitions = componentProvider.findCandidateComponents(basePackage);
        for (BeanDefinition beanDefinition : beanDefinitions) {
            try {
                log.debug("Registering entity " + beanDefinition.getBeanClassName());
                //noinspection unchecked
                definitionContext.addDefinition(new ImmutableEntityDefinition<Object>(
                        ClassUtils.forName(beanDefinition.getBeanClassName(), parentClassLoader),
                        Collections.<Class<?>, Class<?>>emptyMap()));
            } catch (ClassNotFoundException e) {
                throw new FatalBeanException("Failed to retrieve class: " + beanDefinition.getBeanClassName(),
                        e);
            }
        }
    }
    componentProvider.resetFilters(false);
    log.info("Finding extensions to the data access ...");
    log.debug("Looking for classes with @Extension");
    componentProvider.addIncludeFilter(new AnnotationTypeFilter(Extension.class));
    final AnnotationExtensionMetadataResolver extensionMetadataResolver = new AnnotationExtensionMetadataResolver(
            metadataResolver);
    for (String basePackage : basePackages) {
        final Set<BeanDefinition> beanDefinitions = componentProvider.findCandidateComponents(basePackage);
        for (BeanDefinition beanDefinition : beanDefinitions) {
            try {
                log.debug("Registering extension " + beanDefinition.getBeanClassName());
                extensionManager.addExtension(extensionMetadataResolver
                        .resolve(ClassUtils.forName(beanDefinition.getBeanClassName(), parentClassLoader)));
            } catch (ClassNotFoundException e) {
                throw new FatalBeanException("Failed to retrieve class: " + beanDefinition.getBeanClassName(),
                        e);
            }
        }
    }
    log.info("Preparing entity statements for later use");
    final StatementRegistryPreparator preparator = new StatementRegistryPreparator(databaseDialect,
            resolverContext, tableMetadataRegistry);
    for (Class<?> entity : definitionContext.getEntities()) {
        preparator.addEntity(entity);
    }
    preparator.prepare(statementRegistry);
    log.info("Registering interfaces with the context");
    entityContext.setInterfaces(
            with(definitionContext.getEntities()).map(new Transformer<Class<?>, Map<Class<?>, Class<?>>>() {
                @Override
                public Map<Class<?>, Class<?>> map(Class<?> input) {
                    //noinspection unchecked
                    final EntityDefinition<Object> definition = extensionManager
                            .intercept(new ImmutableEntityDefinition<Object>((Class<Object>) input,
                                    Collections.<Class<?>, Class<?>>emptyMap()));
                    return definition.getInterfaces();
                }
            }));
    if (initializeSession) {
        final DataAccessSession session = context.getBean(DataAccessSession.class);
        session.initialize();
        session.markInitialized();
    }
    log.info("Setting the class loader for the entity context");
    entityContext.setDefaultClassLoader(context.getBeanClassLoader());
    time = System.nanoTime() - time;
    log.info("Session preparation took " + Math.round((double) time / 1000000d) / 1000d + " second(s)");
}

From source file:org.eclipse.gemini.blueprint.extender.internal.support.OsgiAnnotationPostProcessor.java

public void postProcessBeanFactory(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
        throws BeansException, OsgiException {

    Bundle bundle = bundleContext.getBundle();
    try {/*  www.  j av  a 2s.co  m*/
        // Try and load the annotation code using the bundle classloader
        Class<?> annotationBppClass = bundle.loadClass(ANNOTATION_BPP_CLASS);
        // instantiate the class
        final BeanPostProcessor annotationBeanPostProcessor = (BeanPostProcessor) BeanUtils
                .instantiateClass(annotationBppClass);

        // everything went okay so configure the BPP and add it to the BF
        ((BeanFactoryAware) annotationBeanPostProcessor).setBeanFactory(beanFactory);
        ((BeanClassLoaderAware) annotationBeanPostProcessor)
                .setBeanClassLoader(beanFactory.getBeanClassLoader());
        ((BundleContextAware) annotationBeanPostProcessor).setBundleContext(bundleContext);
        beanFactory.addBeanPostProcessor(annotationBeanPostProcessor);
    } catch (ClassNotFoundException exception) {
        log.info("Spring-DM annotation package could not be loaded from bundle ["
                + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]; annotation processing disabled...");
        if (log.isDebugEnabled())
            log.debug("Cannot load annotation injection processor", exception);
    }
}