Announcement Announcement Module
Collapse
No announcement yet.
Injection into MethodInterceptor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injection into MethodInterceptor

    Hi All,

    I am attempting to provide an invoker service api which will comes with a MethodInterceptor which is responsible for closing a context. The idea behind the MethodInterceptor is, I want to seamlessly perform some context operations for the invokee without them realising its happening.

    Here's what I have;

    Intended Usage:
    Code:
    JbpmContext jBpmCtx = getCurrentContext();
    FooService fooSrv = (FooService) ServiceInvokerUtil.getService("FooService", jBpmCtx);
    fooSrv.addBars("snafu");
    Service Wrapper:
    Code:
    public class ServiceInvokerUtil {
    
    	public static Object getService(String serviceName, JbpmContext jbpmContext)
    			throws ServiceInvocationException {
    		ApplicationContext ctx = new FileSystemXmlApplicationContext(
    				"config/invoker.xml");
    
    		ctx.getBean("contextManager", new Object[] { jbpmContext });
    
    		Object service = ctx.getBean(serviceName);
    
    		return service;
    	}
    
    }
    The Interceptor:
    Code:
    public class ServiceInvokerInterceptor implements MethodInterceptor {
    
    	private boolean isAsync;
    
    	private ContextManager context;
    
    	public Object invoke(MethodInvocation m) throws Throwable {
    		// Close the JBPMContext if this is Asynchronous invocation
    		JbpmContext jBpmContext = getContext().getContext();
    	        JbpmConfiguration config = jBpmContext.getJbpmConfiguration();
    		if (isAsync()) {
    			System.out.println("Executing Async Before Advice" + jBpmContext);
    			getContext().close();
    		} else {
    			System.out.println("Executing Before Advice");
    		}
    
    		// Invoke the 3rd party system
    		Object rspObj = m.proceed();
    
    		// Reload the JBPMContext
    		if (isAsync()) {
    			System.out.println("Executing Async After Advice");
    			config.createJbpmContext();
    		} else {
    			System.out.println("Executing After Advice");
    		}
    
    		return rspObj;
    	}
    
    // Async  and Context setters/getters ommitted !
    The weaving config:
    Code:
    	<!-- Bean configuration -->
    	    <bean id="proxyTemplate" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">
    	   	</bean>
    	   
    	    <bean id="syncInterceptor" class="com.mdsuk.bpe.extensions.invoker.ServiceInvokerInterceptor">
    	   		<property name="async"><value>false</value></property>
    	   		<property name="context"><ref local="contextManager"/></property>
    	    </bean>
    	   
    		<bean id="asyncInterceptor" class="com.mdsuk.bpe.extensions.invoker.ServiceInvokerInterceptor">
    			<property name="async"><value>true</value></property>
    
    	   		<property name="context"><ref local="contextManager"/></property>
    		</bean>
    		   
    		<bean id="contextManager" scope="prototype" class="com.mdsuk.bpe.extensions.invoker.ContextBeanFactory" factory-method="createContextBean">
    			<constructor-arg>
    				<null/>
    			</constructor-arg>
      		</bean>
    	   	
    	   	<!-- Bean Classes -->
    	   	<bean id="fooServiceTarget" class="com.mdsuk.bpe.extensions.invoker.test.FooServiceImpl">  	
    	   	</bean>
    	   	
    	   	<bean id="FooService" parent="proxyTemplate">
    			<property name="proxyInterfaces">
    	        	<value>com.mdsuk.bpe.extensions.invoker.test.FooService</value>
    	      	</property>
    	      	<property name="target">
    	         	<ref local="fooServiceTarget"/>
    	      	</property>
    	      	<property name="interceptorNames">
    	    		<value>asyncInterceptor</value>
    	    	</property>	      	
    		</bean>


    The problem:

    I want to be able to set the context into the method interceptor programatically, as each context is a new instance and specific to the invokee.

    I have tried to use a factory prototype bean and
    Code:
    		ctx.getBean("contextManager", new Object[] { jbpmContext });
    to pump in the context passed by the invokee, but no joy

    Can anyone point out what I'm doing wrong or if this is possible, I'm a Spring newbie so I may have made elementary mistakes here somewhere.

    Thanks in advance
    -- Pat
Working...
X