Search in sources :

Example 36 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project com.revolsys.open by revolsys.

the class ModuleImport method postProcessBeanDefinitionRegistry.

private void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry) throws BeansException {
    beforePostProcessBeanDefinitionRegistry(registry);
    if (this.enabled) {
        final GenericApplicationContext beanFactory = getApplicationContext(registry);
        if (this.exportAllBeans) {
            for (final String beanName : beanFactory.getBeanDefinitionNames()) {
                if (!this.beanNamesNotToExport.contains(beanName)) {
                    registerTargetBeanDefinition(registry, beanFactory, beanName, beanName);
                    for (final String alias : beanFactory.getAliases(beanName)) {
                        if (!this.beanNamesNotToExport.contains(alias)) {
                            registerTargetBeanDefinition(registry, beanFactory, beanName, alias);
                        }
                    }
                }
            }
        } else {
            for (final String beanName : this.exportBeanNames) {
                if (!this.beanNamesNotToExport.contains(beanName)) {
                    registerTargetBeanDefinition(registry, beanFactory, beanName, beanName);
                }
            }
        }
        for (final Entry<String, String> exportBeanAlias : this.exportBeanAliases.entrySet()) {
            final String beanName = exportBeanAlias.getKey();
            final String alias = exportBeanAlias.getValue();
            if (!this.beanNamesNotToExport.contains(alias)) {
                registerTargetBeanDefinition(registry, beanFactory, beanName, alias);
            }
        }
        afterPostProcessBeanDefinitionRegistry(registry);
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext)

Example 37 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project com.revolsys.open by revolsys.

the class ScriptExecutorRunnable method runDo.

@Override
public void runDo() {
    final long startTime = System.currentTimeMillis();
    try {
        String logPath = null;
        final String logFileName = (String) this.attributes.get("logFile");
        if (logFileName != null && logFileName.trim().length() > 0) {
            final File logFile = new File(logFileName);
            final File parentFile = logFile.getParentFile();
            if (parentFile != null) {
                parentFile.mkdirs();
            }
            logPath = logFile.getAbsolutePath();
            ThreadLocalFileAppender.getAppender().setLocalFile(logPath);
        }
        if (this.logScriptInfo) {
            final StringBuilder message = new StringBuilder("Processing ");
            message.append(" -s ");
            message.append(this.script);
            if (logPath != null) {
                message.append(" -l ");
                message.append(logPath);
            }
            for (final Entry<String, Object> parameter : this.attributes.entrySet()) {
                message.append(" ");
                message.append(parameter.getKey());
                message.append("=");
                message.append(parameter.getValue());
            }
            Logs.info(this, message.toString());
        }
        ThreadSharedProperties.setProperties(this.attributes);
        final GenericApplicationContext applicationContext = new GenericApplicationContext();
        applicationContext.getBeanFactory().addPropertyEditorRegistrar(new ResourceEditorRegistrar());
        for (final Entry<String, Object> entry : this.beans.entrySet()) {
            final String key = entry.getKey();
            if (key.indexOf('.') == -1 && key.indexOf('[') == -1) {
                final Object value = entry.getValue();
                final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(Parameter.class);
                final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
                propertyValues.add("type", value.getClass());
                propertyValues.add("value", value);
                applicationContext.registerBeanDefinition(key, beanDefinition);
            }
        }
        final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(applicationContext);
        if (new File(this.script).exists()) {
            beanReader.loadBeanDefinitions("file:" + this.script);
        } else {
            beanReader.loadBeanDefinitions("classpath:" + this.script);
        }
        applicationContext.refresh();
        try {
            final Object bean = applicationContext.getBean("processNetwork");
            final ProcessNetwork pipeline = (ProcessNetwork) bean;
            pipeline.startAndWait();
        } finally {
            applicationContext.close();
            System.gc();
        }
    } catch (final BeanCreationException e) {
        final Throwable cause = getBeanExceptionCause(e);
        Logs.error(this, cause.getMessage(), cause);
        System.err.println(cause.getMessage());
        System.err.flush();
    } catch (final Throwable t) {
        Logs.error(this, t.getMessage(), t);
    }
    if (this.logScriptInfo) {
        final long endTime = System.currentTimeMillis();
        final long time = endTime - startTime;
        long seconds = time / 1000;
        final long minutes = seconds / 60;
        seconds = seconds % 60;
        Logs.info(this, minutes + " minutes " + seconds + " seconds");
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) ResourceEditorRegistrar(com.revolsys.beans.propertyeditor.ResourceEditorRegistrar) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ProcessNetwork(com.revolsys.parallel.process.ProcessNetwork) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) File(java.io.File)

Example 38 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project joinfaces by joinfaces.

the class JsfCdiToSpringApplicationBeanFactoryPostProcessorIT method testNoScopedClass.

@Test
public void testNoScopedClass() {
    GenericApplicationContext acx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);
    acx.registerBeanDefinition("noScopedClass", new AnnotatedGenericBeanDefinition(new StandardAnnotationMetadata(NoScopedClass.class)));
    acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition(ScopedBeansConfiguration.class));
    acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment()));
    acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment()));
    acx.refresh();
    assertThat(acx.getBeanDefinition("noScopedClass").getScope()).isEqualTo("");
    assertThat(acx.getBeanDefinition("noScopedBean").getScope()).isEqualTo("");
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 39 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project joinfaces by joinfaces.

the class JsfCdiToSpringApplicationBeanFactoryPostProcessorIT method testViewScopedClass.

@Test
public void testViewScopedClass() {
    GenericApplicationContext acx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);
    acx.registerBeanDefinition("viewScopedClass", new AnnotatedGenericBeanDefinition(new StandardAnnotationMetadata(ViewScopedClass.class)));
    acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition(ScopedBeansConfiguration.class));
    acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment()));
    acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment()));
    acx.refresh();
    assertThat(acx.getBeanDefinition("viewScopedClass").getScope()).isEqualTo(ViewScope.SCOPE_VIEW);
    assertThat(acx.getBeanDefinition("viewScopedBean").getScope()).isEqualTo(ViewScope.SCOPE_VIEW);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.Test)

Example 40 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-integration by spring-projects.

the class ExpressionEvaluatingMessageProcessorTests method testProcessMessageWithParameterCoercionToNonPrimitive.

@Test
public void testProcessMessageWithParameterCoercionToNonPrimitive() throws Exception {
    class TestTarget {

        @SuppressWarnings("unused")
        public String find(Resource[] resources) {
            return Arrays.toString(resources);
        }
    }
    Expression expression = expressionParser.parseExpression("#target.find(payload)");
    ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
    AbstractApplicationContext applicationContext = new GenericApplicationContext();
    processor.setBeanFactory(applicationContext);
    IntegrationEvaluationContextFactoryBean factoryBean = new IntegrationEvaluationContextFactoryBean();
    factoryBean.setApplicationContext(applicationContext);
    applicationContext.getBeanFactory().registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, factoryBean.getObject());
    applicationContext.refresh();
    processor.afterPropertiesSet();
    EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
    evaluationContext.setVariable("target", new TestTarget());
    String result = processor.processMessage(new GenericMessage<>("classpath*:*-test.xml"));
    assertTrue("Wrong result: " + result, result.contains("log4j2-test.xml"));
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)378 Test (org.junit.jupiter.api.Test)193 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)106 Test (org.junit.Test)74 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)72 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 ClassPathResource (org.springframework.core.io.ClassPathResource)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 QueueChannel (org.springframework.integration.channel.QueueChannel)16 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)15 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)15 GenericMessage (org.springframework.messaging.support.GenericMessage)14 Properties (java.util.Properties)13 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)13 File (java.io.File)12 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)12 Message (org.springframework.messaging.Message)12 InputStreamResource (org.springframework.core.io.InputStreamResource)11 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10