Integration, development and stubs

In my current project we have a fair bit of integration. Loose coupling and all that is excellent, but it does create some challenges:

  • Integrating systems are only available inside the firewall. Sometimes it is relevant to test and develop outside our normal network.
  • Different integrating systems have different availability. You can’t rely on all of them beeing available all the time.

To overcome these we want to be able to stub the integrating systems. But introducing stubs also leads to some complications:

  • The test team needs to be able to check which systems are stubbed and which systems have real integration when testing.
  • The test team will need different configurations at different times, and I’m too lazy to package 20 installations. 😉

So what I need is:

  • A webinterface to display the status of the integration points (stub or real implementation)
  • A webinterface to change which points are stubbed or not

The solution

Since we are using Spring all the wiring and configuration is stored in XML files, and I could probably manipulate files on the disk, but it didn’t sound like an attractive solution. After asking a bit around at the forum I settled on a proxy based solution in the spirit of Spring’s LocalStatelessSessionProxyFactoryBean. Implementation was easy enough, and it seems to work. In it’s essence it is a proxy and a proxyfactory wrapped in one class (FactoryBean and MethodInterceptor is implemented) that handles which class will be invoked (stub/real).

It also has some shortcomings that you should be aware of:

  • No security as to who can use it
  • It is not tested with proxies inside the proxy, but I guess that will work.
  • You should probably restart your app before making config changes. Any state in the beans real/stub will be missing when switching.

The config

The config should be pretty self explanatory with two beans, one interface and one boolean injected into the ProxyFactory. The stubEnabled boolean could be omitted. It defaults to running the real implementation.


The proxy

The proxy is the object that sits around the real and stub implementations. It will direct the execution to the correct bean depending on it’s settings.

The signature looks like this (the larger methods impls is further down):

public class IntegrationPointProxyFactoryBean implements FactoryBean, MethodInterceptor, InitializingBean {
	/* Required interface methods. See Spring docs for info. */
	public Object getObject() {
		return this.proxy;
	public boolean isSingleton() {
		return false;
	public Class getObjectType() {
		if (this.proxy == null) {
			return this.businessInterface;
		} else {
			return this.proxy.getClass();
	public Object invoke(MethodInvocation invocation) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException;
	public void afterPropertiesSet();

	/* New methods specific for this bean */
	public void setBusinessInterface(Class businessInterface);
	public void setRealImplementation(Object realImplementation);
	public void setStubImplementation(Object stubImplementation);
	public void setStubEnabled(boolean stubEnabled);

The set* methods are just plain old dependency injection methods that sets a class variable, I won’t write about them. To make the solution better you might want to do some before/after consistency checks in them. The interesting stuff happens in the afterPropertiesSet and invoke methods.


public void afterPropertiesSet() {
	/* A bunch of consistency checks for null etc */

	// Initialise the proxyobject which is this object
	this.proxy = ProxyFactory.getProxy(this.businessInterface, this);

Thats pretty much all you need to do to create a proxy with Springs utilities. The object you are assigning as the proxy must implement the MethodInterceptor interface.

That was the config, the rest of the stuff happens runtime in the invoke method.


public Object invoke(MethodInvocation invocation) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	Method aopMethod = invocation.getMethod();
	Object runObject = null;
	if (this.stubEnabled) {
		runObject = this.stubImplementation;
	} else {
		runObject = this.realImplementation;
	Method classInvocation = runObject.getClass().getMethod(aopMethod.getName(), aopMethod.getParameterTypes());

	return classInvocation.invoke(runObject, invocation.getArguments());

The controller

This is all nice and dandy, but it won’t do you much good if you don’t have something to control and display this behaviour. You need a controller doing the following:

  • Retrieve all beans of type IntegrationProxtFactoryBean and pass them to the JSP
  • If enabled, switch implementation between real and stub


public class IntegrationPointController extends AbstractController {
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
		Map beans = this.getApplicationContext().getBeansOfType(IntegrationPointProxyFactoryBean.class);

		// Switch implementasjon
		if (request.getParameter("point") != null) {
			String pointName = request.getParameter("point");
			IntegrationPointProxyFactoryBean pointBean = (IntegrationPointProxyFactoryBean) beans.get(pointName);

		Map model = new HashMap();
		model.put("points", beans);

		return new ModelAndView("config/showPoints", model);

And the JSP is something like this (not the prettiest code, but this is just util stuff anyway):

[ ">Switch]

This is the first time I try to do such an extensive article, I hope it turned out alright. Let me know if you think otherwise. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *