Search in sources :

Example 1 with SofaModuleProperties

use of com.alipay.sofa.isle.spring.config.SofaModuleProperties in project sofa-boot by alipay.

the class BeanHierarchyTest method refreshApplication.

private void refreshApplication(ApplicationRuntimeModel application) throws Exception {
    DefaultListableBeanFactory rootBeanFactory = new DefaultListableBeanFactory();
    ConfigurableApplicationContext rootApplicationContext = new GenericApplicationContext(rootBeanFactory);
    rootApplicationContext.refresh();
    SofaModuleProperties sofaModuleProperties = new SofaModuleProperties();
    sofaModuleProperties.setBeanLoadCost(1);
    rootBeanFactory.registerSingleton("sofaModuleProperties", sofaModuleProperties);
    rootBeanFactory.registerSingleton(SofaBootConstants.PROCESSORS_OF_ROOT_APPLICATION_CONTEXT, new HashMap<>());
    SpringContextLoader springContextLoader = new DynamicSpringContextLoader(rootApplicationContext);
    for (DeploymentDescriptor dd : application.getResolvedDeployments()) {
        if (dd.isSpringPowered()) {
            springContextLoader.loadSpringContext(dd, application);
            ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) dd.getApplicationContext();
            dd.startDeploy();
            ctx.refresh();
            dd.deployFinish();
        }
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringContextLoader(com.alipay.sofa.isle.loader.SpringContextLoader) DynamicSpringContextLoader(com.alipay.sofa.isle.loader.DynamicSpringContextLoader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DynamicSpringContextLoader(com.alipay.sofa.isle.loader.DynamicSpringContextLoader) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Example 2 with SofaModuleProperties

use of com.alipay.sofa.isle.spring.config.SofaModuleProperties in project sofa-boot by alipay.

the class DynamicSpringContextLoader method loadSpringContext.

@Override
public void loadSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) throws Exception {
    SofaModuleProperties sofaModuleProperties = rootApplicationContext.getBean(SofaModuleProperties.class);
    BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(sofaModuleProperties.getBeanLoadCost(), deployment.getModuleName());
    beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    GenericApplicationContext ctx = sofaModuleProperties.isPublishEventToParent() ? new GenericApplicationContext(beanFactory) : new SofaModuleApplicationContext(beanFactory);
    ctx.setId(deployment.getModuleName());
    String activeProfiles = sofaModuleProperties.getActiveProfiles();
    if (StringUtils.hasText(activeProfiles)) {
        String[] profiles = activeProfiles.split(SofaBootConstants.PROFILE_SEPARATOR);
        ctx.getEnvironment().setActiveProfiles(profiles);
    }
    setUpParentSpringContext(ctx, deployment, application);
    final ClassLoader moduleClassLoader = deployment.getClassLoader();
    ctx.setClassLoader(moduleClassLoader);
    CachedIntrospectionResults.acceptClassLoader(moduleClassLoader);
    // set allowBeanDefinitionOverriding
    ctx.setAllowBeanDefinitionOverriding(sofaModuleProperties.isAllowBeanDefinitionOverriding());
    ctx.getBeanFactory().setBeanClassLoader(moduleClassLoader);
    ctx.getBeanFactory().addPropertyEditorRegistrar(new PropertyEditorRegistrar() {

        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Class.class, new ClassEditor(moduleClassLoader));
            registry.registerCustomEditor(Class[].class, new ClassArrayEditor(moduleClassLoader));
        }
    });
    deployment.setApplicationContext(ctx);
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(ctx);
    beanDefinitionReader.setValidating(true);
    beanDefinitionReader.setNamespaceAware(true);
    beanDefinitionReader.setBeanClassLoader(deployment.getApplicationContext().getClassLoader());
    beanDefinitionReader.setResourceLoader(ctx);
    loadBeanDefinitions(deployment, beanDefinitionReader);
    addPostProcessors(beanFactory);
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) SofaModuleApplicationContext(com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext) QualifierAnnotationAutowireCandidateResolver(org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Example 3 with SofaModuleProperties

use of com.alipay.sofa.isle.spring.config.SofaModuleProperties in project sofa-boot by alipay.

the class DeploymentExceptionTest method test.

@Test(expected = DeploymentException.class)
public void test() throws Exception {
    // create ApplicationRuntimeModel with dependency problem
    ApplicationRuntimeModel application = new ApplicationRuntimeModel();
    application.setAppName("testCase");
    application.setModuleDeploymentValidator(new DefaultModuleDeploymentValidator());
    DeploymentDescriptorConfiguration deploymentDescriptorConfiguration = new DeploymentDescriptorConfiguration(Collections.singletonList(SofaBootConstants.MODULE_NAME), Collections.singletonList(SofaBootConstants.REQUIRE_MODULE));
    Properties props = new Properties();
    props.setProperty(SofaBootConstants.MODULE_NAME, "com.alipay.test");
    props.setProperty(SofaBootConstants.REQUIRE_MODULE, "com.alipay.dependency");
    URL fileUrl = new URL("file:/demo/path/isle-module.config");
    application.addDeployment(DeploymentBuilder.build(fileUrl, props, deploymentDescriptorConfiguration, ApplicationRuntimeModelTest.class.getClassLoader()));
    // mock ApplicationContext
    AbstractApplicationContext applicationContext = mock(AbstractApplicationContext.class);
    when(applicationContext.getBean(SofaBootConstants.APPLICATION, ApplicationRuntimeModel.class)).thenReturn(application);
    ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
    when(applicationContext.getEnvironment()).thenReturn(environment);
    when(environment.getProperty(SofaBootConstants.APP_NAME_KEY)).thenReturn("testCase");
    new SpringContextInstallStage(applicationContext, new SofaModuleProperties()).process();
}
Also used : DeploymentDescriptorConfiguration(com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringContextInstallStage(com.alipay.sofa.isle.stage.SpringContextInstallStage) DefaultModuleDeploymentValidator(com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties) Properties(java.util.Properties) URL(java.net.URL) ApplicationRuntimeModel(com.alipay.sofa.isle.ApplicationRuntimeModel) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties) Test(org.junit.Test)

Example 4 with SofaModuleProperties

use of com.alipay.sofa.isle.spring.config.SofaModuleProperties in project sofa-boot by sofastack.

the class DynamicSpringContextLoader method loadSpringContext.

@Override
public void loadSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) throws Exception {
    SofaModuleProperties sofaModuleProperties = rootApplicationContext.getBean(SofaModuleProperties.class);
    BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(sofaModuleProperties.getBeanLoadCost(), deployment.getModuleName());
    beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    GenericApplicationContext ctx = sofaModuleProperties.isPublishEventToParent() ? new GenericApplicationContext(beanFactory) : new SofaModuleApplicationContext(beanFactory);
    ctx.setId(deployment.getModuleName());
    String activeProfiles = sofaModuleProperties.getActiveProfiles();
    if (StringUtils.hasText(activeProfiles)) {
        String[] profiles = activeProfiles.split(SofaBootConstants.PROFILE_SEPARATOR);
        ctx.getEnvironment().setActiveProfiles(profiles);
    }
    setUpParentSpringContext(ctx, deployment, application);
    final ClassLoader moduleClassLoader = deployment.getClassLoader();
    ctx.setClassLoader(moduleClassLoader);
    CachedIntrospectionResults.acceptClassLoader(moduleClassLoader);
    // set allowBeanDefinitionOverriding
    ctx.setAllowBeanDefinitionOverriding(sofaModuleProperties.isAllowBeanDefinitionOverriding());
    ctx.getBeanFactory().setBeanClassLoader(moduleClassLoader);
    ctx.getBeanFactory().addPropertyEditorRegistrar(new PropertyEditorRegistrar() {

        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Class.class, new ClassEditor(moduleClassLoader));
            registry.registerCustomEditor(Class[].class, new ClassArrayEditor(moduleClassLoader));
        }
    });
    deployment.setApplicationContext(ctx);
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(ctx);
    beanDefinitionReader.setValidating(true);
    beanDefinitionReader.setNamespaceAware(true);
    beanDefinitionReader.setBeanClassLoader(deployment.getApplicationContext().getClassLoader());
    beanDefinitionReader.setResourceLoader(ctx);
    loadBeanDefinitions(deployment, beanDefinitionReader);
    addPostProcessors(beanFactory);
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) SofaModuleApplicationContext(com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext) QualifierAnnotationAutowireCandidateResolver(org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Example 5 with SofaModuleProperties

use of com.alipay.sofa.isle.spring.config.SofaModuleProperties in project sofa-boot by sofastack.

the class BeanHierarchyTest method refreshApplication.

private void refreshApplication(ApplicationRuntimeModel application) throws Exception {
    DefaultListableBeanFactory rootBeanFactory = new DefaultListableBeanFactory();
    ConfigurableApplicationContext rootApplicationContext = new GenericApplicationContext(rootBeanFactory);
    rootApplicationContext.refresh();
    SofaModuleProperties sofaModuleProperties = new SofaModuleProperties();
    sofaModuleProperties.setBeanLoadCost(1);
    rootBeanFactory.registerSingleton("sofaModuleProperties", sofaModuleProperties);
    rootBeanFactory.registerSingleton(SofaBootConstants.PROCESSORS_OF_ROOT_APPLICATION_CONTEXT, new HashMap<>());
    SpringContextLoader springContextLoader = new DynamicSpringContextLoader(rootApplicationContext);
    for (DeploymentDescriptor dd : application.getResolvedDeployments()) {
        if (dd.isSpringPowered()) {
            springContextLoader.loadSpringContext(dd, application);
            ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) dd.getApplicationContext();
            dd.startDeploy();
            ctx.refresh();
            dd.deployFinish();
        }
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringContextLoader(com.alipay.sofa.isle.loader.SpringContextLoader) DynamicSpringContextLoader(com.alipay.sofa.isle.loader.DynamicSpringContextLoader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DynamicSpringContextLoader(com.alipay.sofa.isle.loader.DynamicSpringContextLoader) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties)

Aggregations

SofaModuleProperties (com.alipay.sofa.isle.spring.config.SofaModuleProperties)6 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 ApplicationRuntimeModel (com.alipay.sofa.isle.ApplicationRuntimeModel)2 DeploymentDescriptor (com.alipay.sofa.isle.deployment.DeploymentDescriptor)2 DeploymentDescriptorConfiguration (com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration)2 DefaultModuleDeploymentValidator (com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator)2 FileDeploymentDescriptor (com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor)2 DynamicSpringContextLoader (com.alipay.sofa.isle.loader.DynamicSpringContextLoader)2 SpringContextLoader (com.alipay.sofa.isle.loader.SpringContextLoader)2 SofaModuleApplicationContext (com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext)2 BeanLoadCostBeanFactory (com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)2 SpringContextInstallStage (com.alipay.sofa.isle.stage.SpringContextInstallStage)2 URL (java.net.URL)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 QualifierAnnotationAutowireCandidateResolver (org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2