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

22.3. Applying Resource Pooling

Problem

You want to optimize the access to a commonly used resource by creating a reusable pool of those resources without affecting the existing operation of your application.

Solution

Using the Director aspect-oriented design pattern, create an abstract aspect that defines the roles of the Resource and Resource Pool as interfaces and the generic reusable behavior of resource pooling, as shown in Example 22-6. The abstract resource pooling aspect can then be specialized for each resource to be pooled within your target application.

Example 22-6. Defining the generic resource pooling behavior in an abstract aspect

public abstract aspect ResourcePoolingAspect { public interface Resource { } public interface ResourcePool { public void add(Resource resource); public Resource remove( ); } protected class ResourcePoolsCollection { WeakHashMap pools = new WeakHashMap( ); public void putResourcePool(ResourcePool pool, Class resourceClass) { pools.put(resourceClass, pool); } public ResourcePool getResourcePool(Class resourceClass) { return (ResourcePool) pools.get(resourceClass); } } protected ResourcePoolsCollection resourcePools = new ResourcePoolsCollection( ); public ResourcePoolingAspect( ) { initializeSpecificPool( ); } protected abstract void initializeSpecificPool( ); private pointcut excludeAspects( ) : !within(ResourcePoolingAspect+); public abstract pointcut catchResourceConstruction( ); public abstract pointcut catchResourceDestruction(Resource resource); ...

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