AspectJ

AspectJ Homepage.

This section describe a proposal, how can you use Crispy in a AspectJ envionment

It are two problems to solve:

  1. Where can find java.util.Properties to configure the Crispy net.sf.crispy.impl.ServiceManager
  2. Who interfaces are the Services.
For this two problems exist differents solutions. This is the reason, to divide this problem in a abstract aspect and in one possible extension from the abstract aspect. Both are proposal.

First, you can see the abstract aspect AbstractServiceBuilder:

public abstract aspect AbstractServiceBuilder {

	protected IServiceManager serviceManager = null;

	/**
	 * Create a configuration for a ServiceManager-Instance (first problem).
	 */
	public abstract Properties createProperties();
	
	/**
	 * Find all service interfaces for the ServiceManager to
	 * create the Service-Proxy-Instance (second problem).
	 */
	public abstract pointcut findServices();

	/**
	 * The Advice.
	 */
	Object around() : findServices() {		
		 serviceManager = new ServiceManager(createProperties());
		 FieldSignature fieldSignature = (FieldSignature) thisJoinPoint.getSignature();
		 Class serviceClass = fieldSignature.getFieldType();
		 Object serviceProxy = serviceManager.createService(serviceClass);
		 return serviceProxy;
	}
}

Second, you can see the concrete implementation from the AbstractServiceBuilder, the ServiceBuilder:

public aspect ServiceBuilder extends AbstractServiceBuilder {

	/**
	 * Capture all read access to fields in the
	 * net.sf.crispy.extension.aspectj.AspectJExample class, where
	 * fields from the package test.crispy.example.service.
	 */
	public pointcut findServices() :
		get (test.crispy.example.service.* net.sf.crispy.extension.aspectj.AspectJExample.*);
	
	/**
	 * Load Properties from a file, where the class is. 
	 */
	public Properties createProperties() {
		String propFileName = "aspect-test.properties";
		Class propClass = this.getClass();
		PropertiesLoader propertiesLoader = new ClassPropertiesLoader(propClass, propFileName);
		Properties properties = propertiesLoader.load();
		return properties;
	}
}

A example class, where the aspects be effective (this example starts your own MiniServer):

public class AspectJExample {

	private Echo echo = null;
	public Calculator calculator = null;
	
	public String echo (String echoString) {
		return echo.echo(echoString);
	}
	
	
	public static void main(String[] args) {
		
		MiniServer miniServer = new MiniRmiServer(1099);
		miniServer.addService("test.crispy.example.service.Echo", "test.crispy.example.service.EchoImpl");
		miniServer.addService("test.crispy.example.service.Calculator", "test.crispy.example.service.CalculatorImpl");
		miniServer.start(); 

		try {
			AspectJExample aspectJExample = new AspectJExample();
			System.out.println("Echo: " + aspectJExample.echo("Hello Crispy-AspectJ-Echo ..."));
			System.out.println("Calculator-add (2+3): " + aspectJExample.calculator.add(2, 3));
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			miniServer.stop();
		}
	}
}

The properties:

crispy.prop.server.url=rmi://localhost:1099
crispy.prop.executor.class=net.sf.crispy.impl.RmiExecutor