O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18.6. Implementing the Proxy Pattern

Problem

You want to apply the proxy pattern using AspectJ.

Solution

The proxy pattern allows the developer to provide a surrogate object in place of the actual object in case access to the real object needs to be delegated or controlled.

Example 18-9 uses the Director aspect-oriented design pattern (see Chapter 23) to provide an abstract implementation of the proxy pattern.

Example 18-9. Using an abstract aspect to define the proxy pattern

public abstract aspect ProxyPattern { protected interface Subject { } protected abstract pointcut requestTriggered( ); private pointcut accessByCaller(Object caller) : requestTriggered( ) && this(caller); private pointcut accessByUnknown( ) : requestTriggered( ) && !accessByCaller(Object); Object around(Object caller, Subject subject) : accessByCaller(caller) && target(subject) { if (reject(caller, subject, thisJoinPoint)) { return rejectRequest(caller, subject, thisJoinPoint); } else if (delegate(caller, subject, thisJoinPoint)) { return delegateRequest(caller, subject, thisJoinPoint); } return proceed(caller, subject); } Object around(Subject subject) : accessByUnknown( ) && target(subject) { // Without a caller then reject does not really make sense // as there is no way of deciding to reject or not if (delegate(null, subject, thisJoinPoint)) { return delegateRequest(null, subject, thisJoinPoint); } return proceed(subject); } protected abstract boolean reject( Object caller, Subject subject, JoinPoint joinPoint); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required