
Listing 8. An aspect which spots remote calls but not local ones.
This is really just a refinement of listing 4.

--------------------------------------------------------------------

package com.wgrosso.rmi;

import java.rmi.*;
import java.rmi.server.*;

import org.aspectj.lang.*;

/*
	Logs all calls that come into a method that throws a remote exception.
	Uses target pointcut type to make sure target is instance of UnicastRemoteObject
*/

public aspect Aspect_LogRemoteMethodCalls {

	pointcut potentiallyRemoteMethodCall():
		target(UnicastRemoteObject) &&
		call(public * * (..) throws RemoteException);

	pointcut comWGrossoCode():
		execution(public * com.wgrosso..*.*(..));

	pointcut reallyRemoteMethodCall():
		potentiallyRemoteMethodCall () &&
		!cflowbelow(comWGrossoCode());

	before(): reallyRemoteMethodCall() {
		logRemoteCall(thisJoinPoint);
	}

	private void logRemoteCall(JoinPoint joinPoint) {
		try {
			System.out.println("Remote call to " + joinPoint.getTarget());
			System.out.println("\tCaller is " + RemoteServer.getClientHost());
			System.out.println("\tAguments are: " + joinPoint.getArgs());
		}
		catch (Throwable t) {
		/*
			Just in case.
		*/
			t.printStackTrace();
		}
	}
}