22
Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client: Robert Berry, IBM UK Arthur Octopus The AspectJ Mascot

Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Embed Size (px)

Citation preview

Page 1: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System

Jeff Dalton

February 28th, 2003

Advisor: David G. Hannay

Client: Robert Berry, IBM UKArthur Octopus

The AspectJ Mascot

Page 2: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Agenda

AOP & Cocoon Background

Refactoring Goals & Process

Challenges and Obstacles

The Future

Page 3: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

What is Aspect Oriented Programming?

Concern – “a specific requirement or consideration that must be addressed in order to satisfy the overall system goal.”

AOP introduces a new unit of modularization—an aspect–that crosscuts other modules.

Aspect = pointcut + advice + intertype declaration– intertype declaration

Fields and methods that the aspect manages on behalf of other types

- The Java AOP implementation

Page 4: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

A Cross-cutting concern

• Logging in org.Apache.Tomcat- Red shows lines of code that handles logging

- Not in one place. Not in a small number of places.

Page 5: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Apache Cocoon: Dynamic multi-channel web publishing

“Apache Cocoon is a web development framework built around the concepts of separation of concerns and component-based web development.”

– Cocoon website

Usage example:– Take data from a database and publish the content onto a web portal in HTML,

PDF, Office format, and text format all simultaneously.

Page 6: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Performance as crosscutting concern

Performance optimizations: Caching & Pooling– Effective techniques to improve performance

Caching – recycling of resources that are expensive to create and discard where more than one user can use a single object

– “Read only”– A.K.A. – “Shared Object Resource Allocation”

Page 7: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Cocoon Caching Architecture

Caching happens at the pipeline level with each different phase of the transformation being cached, if possible.

Caching

Page 8: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Cocoon Caching Architecture

SAX events and caching management is done in a central location: CacheManager

However, to be cached the classes must implement the abstract interface ...cocoon.caching.CacheableProcessingComponent

Two methods to be implemented:– Serializable generateKey();– SourceValdity generateValidity();

Page 9: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Refactoring Cocoon Caching

The Challenge: – Implementing the CacheableProcessingComponent and its methods

is the crosscutting concern, we need to abstract it.

Accomplished through– Introductions – in AspectJ “Intertype Declarations”

The addition of methods, fields, interfaces, or inheritance information that does not directly affect the class’s behavior

Simple in concept, not trivial in practice.– Large, complex web application (~4600 class files with all blocks)

Page 10: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

AOP Adoption

Val

ue

Development Support forExisting Code

• 15 minutes• 30 lines• unpluggable• testing• tracing• performance measurement

• limited use• design• error handling• standards & contracts• monitoring

AddAuxiliaryFunctionality

Re-factor CoreFunctionality

ReusableLibraries

Aspect-OrientedArchitecture

• module design• persistence• management• security• feature variations

• aspect libraries• design patterns• enterprise, department standards

• new services, programming model• product lines• extend J2EE• Web services, mobile, P2P...

Page 11: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Goals

Project Task– Refactor the caching system in Cocoon

Goals- Gain experience with application of AOP to a large scale, well

structured application environment.

- Identify and quantify opportunities for using AOP to simplify an already modular code base.     

- Use AOP to enhance modularity/componentization of Cocoon.

Page 12: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Process: Approach

1. Used AspectJ to define a compiler warning in order to identify locations in Cocoon that implement the CacheableProcessingComponent abstract interface.

2. Wrote aspects to encapsulate the functionality.

3. Removed the code encapsulated into aspects from the base classes identified in step 1.

4. Wove the aspects into Cocoon using AspectJ compiler.

Page 13: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Step 1: Identify Locations

public aspect ExploreCaching {

declare warning: staticinitialization(org.apache.cocoon.caching.CacheableProcessingComponent+) && !within(org.apache.cocoon.caching..*): "Class implements cacheable";

}

Page 14: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Step 2: Write Caching Aspects

Excerpt:

privileged public aspect CachingAspect {

/* Ascii art generator */ declare parents: org.apache.cocoon.generation.asciiart.AsciiArtSVGGenerator implements

CacheableProcessingComponent;

public Serializable org.apache.cocoon.generation.asciiart.AsciiArtSVGGenerator.generateKey() {

return this.inputSource.getURI(); }

public SourceValidity org.apache.cocoon.generation.asciiart.AsciiArtSVGGenerator.generateValidity(){

return this.inputSource.getValidity(); } ….

Introduce Interface

Introduce Methods

Page 15: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Step 3 & 4: Remove References & Weave

Code Removed

Page 16: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Testing & Verification

Finished:– Cocoon without caching deployed

In process– Integrating AspectJ Compiler into build process

Page 17: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Challenges & Obstacles

Domain knowledge is the biggest challenge– Ex. Cache (external pipeline cache) vs. Store (Internal Object cache)

Eclipse AspectJ Development Tools (1.1.4)– Scalability issues: over 4600 classes in Cocoon– Bug: Memory Leak (Fix coming in 1.2 – ‘Lancaster’)

– Bug: Abstract class that extend abstract class with concrete subclasses are woven too early - (Fixed in 1.1.6)

Ongoing modifications to cocoon caching system

Page 18: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Future work

Use AOP to refactor the ‘internal cache’

Refactor other non-performance crosscutting concerns into aspects

– Object Recycling, Logging, etc…

An event-aware AOP caching system– Use AOP to catch SAX events and perform cache

invalidations

Page 19: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

The Future: AOP

New versions, better tools– AspectJ 1.2 – “Lancaster” for Eclipse 3.0

– “Aspect Mining” – Better ways towards aspect discovery Concern Modeling Environment

– http://www.eclipse.org/cme/– “To general software developers it offers a suite of tools for use in creating,

manipulating, and evolving aspect-oriented software, across the full software lifecycle”

– IBM Watson Lab – Yorktown Heights Aspect Mining Tool More work to be done: AI and pattern recognition

– AOSD 2004 in Lancaster, UK, March 22-25th

Page 20: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Results I

Question– How should you structure your application to simplify AO extendibility?

Answer– Good coding practice makes AO adoption easier

Standard variable naming Standard accessor methods

Question– How can we improve the AO implementation?

Answer– Refactor ‘similar’ code to remove differences

Question– How should aspects be viewed on a design level?

Answer– Still open!

Page 21: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Results II

Summary:

Abstracted 39 implementations of CacheableProcessingComponent.

Used AOP to condense and remove 24 methods from the base implementation.

Example: Duplicate code:

public Serializable org.apache.cocoon.transformation.LexicalTransformer.generateKey() {

return this.lexiconSource.getURI();}public Serializable

org.apache.cocoon.transformation.ParserTransformer.generateKey() {return this.grammarSource.getURI();

}

Page 22: Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:

Questions

Oliver the Octopus

Concept and name credited to:

AspectJ team, IBM Hursley, UK

AOP: The here, the now, and the future!