Search in sources :

Example 1 with ScopedObject

use of org.springframework.aop.scope.ScopedObject in project camunda-bpm-platform by camunda.

the class ProcessScope method createDirtyCheckingProxy.

private Object createDirtyCheckingProxy(final String name, final Object scopedObject) throws Throwable {
    ProxyFactory proxyFactoryBean = new ProxyFactory(scopedObject);
    proxyFactoryBean.setProxyTargetClass(this.proxyTargetClass);
    proxyFactoryBean.addAdvice(new MethodInterceptor() {

        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            Object result = methodInvocation.proceed();
            persistVariable(name, scopedObject);
            return result;
        }
    });
    return proxyFactoryBean.getProxy(this.classLoader);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ScopedObject(org.springframework.aop.scope.ScopedObject)

Example 2 with ScopedObject

use of org.springframework.aop.scope.ScopedObject in project spring-framework by spring-projects.

the class ScopingTests method testScopedProxyConfiguration.

@Test
public void testScopedProxyConfiguration() throws Exception {
    TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedInterfaceDep");
    ITestBean spouse = singleton.getSpouse();
    boolean condition = spouse instanceof ScopedObject;
    assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
    String beanName = "scopedProxyInterface";
    String scopedBeanName = "scopedTarget." + beanName;
    // get hidden bean
    assertThat(spouse.getName()).isEqualTo(flag);
    ITestBean spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
    // the scope proxy has kicked in
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // create a new bean
    customScope.createNewScope = true;
    // get the bean again from the BF
    spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    // make sure the name has been updated
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // get the bean again
    spouseFromBF = (ITestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ScopedObject(org.springframework.aop.scope.ScopedObject) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 3 with ScopedObject

use of org.springframework.aop.scope.ScopedObject in project spring-framework by spring-projects.

the class ScopingTests method testRawScopes.

@Test
public void testRawScopes() throws Exception {
    String beanName = "scopedProxyInterface";
    // get hidden bean
    Object bean = ctx.getBean("scopedTarget." + beanName);
    boolean condition = bean instanceof ScopedObject;
    assertThat(condition).isFalse();
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) ScopedObject(org.springframework.aop.scope.ScopedObject) Test(org.junit.jupiter.api.Test)

Example 4 with ScopedObject

use of org.springframework.aop.scope.ScopedObject in project spring-framework by spring-projects.

the class ScopingTests method testScopedProxyConfigurationWithClasses.

@Test
public void testScopedProxyConfigurationWithClasses() throws Exception {
    TestBean singleton = (TestBean) ctx.getBean("singletonWithScopedClassDep");
    ITestBean spouse = singleton.getSpouse();
    boolean condition = spouse instanceof ScopedObject;
    assertThat(condition).as("scoped bean is not wrapped by the scoped-proxy").isTrue();
    String beanName = "scopedProxyClass";
    String scopedBeanName = "scopedTarget." + beanName;
    // get hidden bean
    assertThat(spouse.getName()).isEqualTo(flag);
    TestBean spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isEqualTo(spouse.getName());
    // the scope proxy has kicked in
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // create a new bean
    customScope.createNewScope = true;
    flag = "boo";
    // get the bean again from the BF
    spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    // make sure the name has been updated
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
    assertThat(spouseFromBF).isNotSameAs(spouse);
    // get the bean again
    spouseFromBF = (TestBean) ctx.getBean(scopedBeanName);
    assertThat(spouseFromBF.getName()).isSameAs(spouse.getName());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ScopedObject(org.springframework.aop.scope.ScopedObject) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 5 with ScopedObject

use of org.springframework.aop.scope.ScopedObject in project camunda-bpm-platform by camunda.

the class ProcessScope method get.

public Object get(String name, ObjectFactory<?> objectFactory) {
    ExecutionEntity executionEntity = null;
    try {
        logger.fine("returning scoped object having beanName '" + name + "' for conversation ID '" + this.getConversationId() + "'. ");
        ProcessInstance processInstance = Context.getExecutionContext().getProcessInstance();
        executionEntity = (ExecutionEntity) processInstance;
        Object scopedObject = executionEntity.getVariable(name);
        if (scopedObject == null) {
            scopedObject = objectFactory.getObject();
            if (scopedObject instanceof ScopedObject) {
                ScopedObject sc = (ScopedObject) scopedObject;
                scopedObject = sc.getTargetObject();
                logger.fine("de-referencing " + ScopedObject.class.getName() + "#targetObject before persisting variable");
            }
            persistVariable(name, scopedObject);
        }
        return createDirtyCheckingProxy(name, scopedObject);
    } catch (Throwable th) {
        logger.warning("couldn't return value from process scope! " + ExceptionUtils.getFullStackTrace(th));
    } finally {
        if (executionEntity != null) {
            logger.fine("set variable '" + name + "' on executionEntity# " + executionEntity.getId());
        }
    }
    return null;
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) ExecutionEntity(org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) ScopedObject(org.springframework.aop.scope.ScopedObject)

Aggregations

ScopedObject (org.springframework.aop.scope.ScopedObject)7 Test (org.junit.jupiter.api.Test)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 ExecutionEntity (org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity)1 ProcessInstance (org.camunda.bpm.engine.runtime.ProcessInstance)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 SimpleMapScope (org.springframework.context.testfixture.SimpleMapScope)1