Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 1
AspectWerkz
Vortrag von Eva Stöwe
Im Rahmen des Seminars
„Component and Aspect Engineering“
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 2
Allgemeines Modell 1: XML-zentriert
zu JointPoints Definition der Advices Definition der Introductions Definition der Pointcuts Aufbau eines Aspekt
Modell 2: selbstdefinierend Aspekte Behandlung von Pointcuts Behandlung von Introductions Behandlung von Advices
Vergleich der Modelle
Überblick
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 3
Was bietet AspectWerkz?
Aspects, Advices und Introductions als Java-Klassen Laufzeit Bytecode-Modifikation JoinPoint-Modell für alle static und member Felder/-Methoden,
Exceptions und Caller Side Pointcuts, dabei sehr granular Möglichkeit, Interfaces und Implementierungen zu bestehenden
Klassen hinzuzufügen Alles über Laufzeitattribute "Heiße" Aktivierung Baukastenprinzip Externe Konfiguration per XML-Datei Einfache Bedienung und Konfiguration Sehr schnell
Allgemeines
Modell 1
Modell 2
Vergleich
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 4
Zwei Modelle
Modell 1: XML zentriert Historische Variante Aspekt in externen XML-Files
Modell 2: Selbstdefinierende Aspekte MetaData basiert Aspekte sind einfache Java-Klassen XML deklariert lediglich die Aspekte Benötigt post-compiling-Phase (bis Java 1.5)
Allgemeines
Modell 1
Modell 2
Vergleich
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 5
AOP System
Aspecte benötigen ein System, in dem sie laufen Derzeit unterstützt AspectWerkz nur ein System pro JVM. Das System wird per XML definiert.
Über Attribut name kann zur Laufzeit das System angesprochen werden
Entweder Modell 1 (XML) oder Modell 2 (selbstdefinierend)
<!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" "http://aspectwerkz.codehaus.org/dtd/aspectwerkz.dtd"><aspectwerkz> <system id="system name"> ... <aspect name="MyAspect"> ... </aspect> </system></aspectwerkz>
<!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" "http://aspectwerkz.codehaus.org/dtd/aspectwerkz.dtd"><aspectwerkz> <system id="system name"> ... <use-aspect class="MySelfDefinedAspect"/>
</system></aspectwerkz>
Allgemeines
Modell 1
Modell 2
Vergleich
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 6
Deployment Model
Es werden 4 verschiedene Deployment-Arten angeboten, die den Scope der Advices und Introductions regeln: perJVM – eine Instanz für die ganze JVM. (Singelton) perClass – eine Instanz per Klasse perInstance – eine Instanz per Klasseninstanz perThread – eine Instanz per Thread
Intern wird das Prototype Design Pattern verwendet, so daß es immer noch eine zusätzliche Prototyp-Instanz gibt.
Allgemeines
Modell 1
Modell 2
Vergleich
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 7
Allgemeines Modell 1: XML-zentriert
zu JointPoints Definition der Advices Definition der Introductions Definition der Pointcuts Aufbau eines Aspekt
Modell 2: selbstdefinierend Aspekte Behandlung von Pointcuts Behandlung von Introductions Behandlung von Advices
Vergleich der Modelle
Überblick
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 8
Modell 1: JoinPoints
Die JoinPoint Klasse implementiert das JoinPoint-Konzept z.b. einen genau bestimmten Punkt im Programmablauf Ein JoinPoint wir durch einen Pointcut selektiert
4 Unterklassen von JoinPoint: MethodJoinPoint FieldJoinPoint ThrowsJoinPoint CallerSideJoinPoint
Nur, wenn man in einer Advice auf speziellen Metadaten eines JoinPoints zugreifen möchte, wird dies relevant, da man dafür zum speziellen Typ casten muß
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 9
Modell 1: Advices
Advices definieren, was bei einem JoinPoint ausgeführt wird 4 Arten werden unterstützt:
AroundAdvice (Abfangen von Methodenaufrufen) PreAdvice (Feldzuweisungen oder Caller Side Pointcuts) PostAdvice (Feldzuweisungen oder Caller Side Pointcuts) ThrowsAdvice (Abfangen von Exceptions)
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 10
Modell 1: Beispiel: Around Advice
AroundAdvice wird implementiert durch Erben der AroundAdvice-Klasse Implementieren der abstrakten Methode
Object execute(final JoinPoint jp)
public class MyAroundAdvice extends AroundAdvice { public MyAroundAdvice() { super(); } public Object execute(final JoinPoint joinPoint) throws Throwable
{ // do some stuff Object result = joinPoint.proceed(); // do some more stuff return result; }}
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 11
Modell 1: Advice-Attribute
3 Attribute müssen per XML gesetzt werden: Name: Eindeutiger Name der Advice Advice: Klassenname auf den sich die Advice beziehen soll deployment-model: Deployment der Advice
(perJVM (default), perClass, perInstance, perThread)
Es können auch weitere Parameter übergeben werden.
<advice-def name="advices/caching" class="advices.CachingAdvice" deployment-model="perInstance"> <param name="timeout" value="10"/></advice-def>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 12
Modell 1: Introductions
Eine Introduction ermöglicht es einer Klasse neue Interfaces oder Implementationen (Methods, Fields) zu erben.
Beide sind wiederum reine Java-Klassen oder –Interfaces. Es muß kein zusätzliches Interface geerbt werden. Von der Introduction muß lediglich
das Marker-Interface introduced implementiert werden und es muß einen parameterlosen Constructor geben.
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 13
Modell 1: Introductions Attribute
4 Attribute sind per XML zu setzen: Name: Eindeutiger Name Interface: voller Name des Interfaces Implementation: voller Name der Implementation deployment-model: (optional) Deployment der Advice
(perJVM (default), perClass, perInstance, perThread)
<introduction-def name="java/io/Serializable" interface="java.io.Serializable"/><introduction-def name="mixins/Mixin" interface="mixins.Mixin" implementation="mixins.MixinImpl" deployment-model="perThread"/>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 14
Modell 1: Pointcuts
Pointcuts selektieren wohldefinierte Punkte im Programmablauf. 6 Arten werden unterstützt:
MethodPointcut getFieldPointcut setFieldPointcut ThrowsPointcut CallerSidePointcut CFlowPointcut
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 15
Modell 1: Pointcut Attribute
3 Attribute müssen per XML angegeben werden: Name: eindeutiger Name innerhalb des Aspektes Type: Method, setField, getField, throws, callerSide oder cflow Pattern:Selektiert die JoinPoints für den Pointcut
Pointcut-Definitionen werden einfach in die Aspekt-Definition eingefügt.
<aspect ...> <pointcut-def name="pc1" type="method" pattern="* foo.Bar.method(..)"/> <pointcut-def name="pc2" type="setField" pattern="* foo.Bar.m_field"/> <pointcut-def name="pc3" type="getField" pattern="* foo.Bar.m_field"/> <pointcut-def name="pc4" type="throws" pattern="*
foo.Bar.method(..)#java.lang.Exception"/> <pointcut-def name="pc5" type="callerSide" pattern="foo.Caller->String
foo.Callee.method()"/> <pointcut-def name="pc6" type="cflow" pattern="* Transaction.begin(..)"/> ...</aspect>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 16
Modell 1: Aspekte
Aspekte werden durch ihre Pointcuts, Advices und Introductions definiert.
Aspekte können durch ein extends-Attribut von einem abstrakten Aspekt erben.
Ein abstrakter Aspekt wird durch einen abstract-aspekt-Tag definiert.
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 17
Modell 1: Aspect Attribute
2 Attribute sind per XML zu setzen: Name: Eindeutiger Name des Aspects Extends: gibt einen abstarkten Aspect an, der von diesem
implementiert wird (optional)
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 18
Modell 1: (leeres) Aspekt-Beispiel
<abstract-aspect name="MyAbstractAspect">
</abstract-aspect>
<aspect name="MyAspect" extends="MyAbstractAspect">
</aspect>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 19
Modell 1: Aspekte mit Pointcuts
<abstract-aspect name="MyAbstractAspect">
</abstract-aspect>
<aspect name="MyAspect" extends="MyAbstractAspect">
<pointcut-def name="facadeCalls" type="cflow" pattern="* *..facade.*.*(..)"/>
<pointcut-def name="setters" type="method" pattern="String domain.*.set*(..)"/>
<pointcut-def name="getters" type="method" pattern="String domain.*.get*(..)"/>
<pointcut-def name="persistentFields" type="setField" pattern="* domain.*.*">
</aspect>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 20
Modell 1: JointPoints Advices Introductions Pointcuts AspektaufbauModell 1: Introductions Hinzufügen
Introductions werden mit dem bind-introduction-Tag festgelegt Attribut class: selektiert die Struktur der Klassen die die
Introduction(s) verwenden soll
Referenzen zu Introductions werden mit dem introduction-ref Tag beschrieben:
<introduction-ref name="nameOfIntroduction"/>
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 21
Modell 1: Aspekt mit Introductions
<abstract-aspect name="MyAbstractAspect">
</abstract-aspect>
<aspect name="MyAspect" extends="MyAbstractAspect">
<bind-introduction class="domain.*">
<introduction-ref name="serializable"/>
<introduction-ref name="mixin"/>
</bind-introduction>
<pointcut-def name="facadeCalls" type="cflow" pattern="* *..facade.*.*(..)"/>
<pointcut-def name="setters" type="method" pattern="String domain.*.set*(..)"/>
<pointcut-def name="getters" type="method" pattern="String domain.*.get*(..)"/>
<pointcut-def name="persistentFields" type="setField" pattern="* domain.*.*">
</aspect>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 22
Modell 1: Advices Hinzufügen
Advice-Pointcut-Bindungen werden durch bind-advice-Tag festgelegt Attribut expression: beliebiger algebraischer Ausdruck
basierend auf den Namen der Pointcuts Attribut cflow: legt fest, ob der Ausdruck Teil eines control flows
sein soll. cflow wird durch seinen pointcut-Namen definiert.
Referenzen zu Advices werden mit dem advice-ref Tag beschrieben:
<advice-ref name="nameOfAdvice"/>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 23
Modell 1: Aspekt mit Advices
<abstract-aspect name="MyAbstractAspect"> <bind-advice cflow="facadeCalls" pointcut="setters AND !getters"> <advices-ref name="log_and_cache"/> </bind-advice> <bind-advice pointcut="persistentFields"> <advice-ref name="persistent"/> </bind-advice></abstract-aspect><aspect name="MyAspect" extends="MyAbstractAspect"> <bind-introduction class="domain.*"> <introduction-ref name="serializable"/> <introduction-ref name="mixin"/> </bind-introduction> <pointcut-def name="facadeCalls" type="cflow" pattern="* *..facade.*.*(..)"/> <pointcut-def name="setters" type="method" pattern="String domain.*.set*(..)"/> <pointcut-def name="getters" type="method" pattern="String domain.*.get*(..)"/> <pointcut-def name="persistentFields" type="setField" pattern="* domain.*.*"></aspect>
Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 24
Modell 1: JointPoints Advices Introductions Pointcuts AspektaufbauModell 1: kompletter Aspekt
<abstract-aspect name="MyAbstractAspect"> <bind-advice cflow="facadeCalls" pointcut="setters AND !getters"> <advices-ref name="log_and_cache"/> </bind-advice> <bind-advice pointcut="persistentFields"> <advice-ref name="persistent"/> </bind-advice></abstract-aspect><aspect name="MyAspect" extends="MyAbstractAspect"> <bind-introduction class="domain.*"> <introduction-ref name="serializable"/> <introduction-ref name="mixin"/> </bind-introduction> <pointcut-def name="facadeCalls" type="cflow" pattern="* *..facade.*.*(..)"/> <pointcut-def name="setters" type="method" pattern="String domain.*.set*(..)"/> <pointcut-def name="getters" type="method" pattern="String domain.*.get*(..)"/> <pointcut-def name="persistentFields" type="setField" pattern="* domain.*.*"></aspect>
<introduction-def name="java/io/Serializable" interface="java.io.Serializable"/><introduction-def name="mixins/Mixin" interface="mixins.Mixin" implementation="mixins.MixinImpl" deployment-model="perThread"/>
<advice-def name="advices/caching" class="advices.CachingAdvice" deployment-model="perInstance"> <param name="timeout" value="10"/></advice-def>
public class MyAroundAdvice extends AroundAdvice { public MyAroundAdvice() { super(); } public Object execute(final JoinPoint joinPoint) throws Throwable
{ // do some stuff Object result = joinPoint.proceed(); // do some more stuff return result; }}
<!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" "http://aspectwerkz.codehaus.org/dtd/aspectwerkz.dtd"><aspectwerkz> <system id="system name"> ... <aspect name="MyAspect"> ... </aspect> </system></aspectwerkz>
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 25
Allgemeines Modell 1: XML-zentriert
zu JointPoints Definition der Advices Definition der Introductions Definition der Pointcuts Aufbau eines Aspekt
Modell 2: selbstdefinierend Aspekte Behandlung von Pointcuts Behandlung von Introductions Behandlung von Advices
Vergleich der Modelle
Überblick
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 26
Modell 2: Aspekte
Aspekte sind Java Klassen die Aspect extenden Metadata Aspect mit 2 Parametern
anonymer Parameter für des Deployments
– perJVM (default), perClass, perInstance, perThread Name: Name des Aspekts (default: Aspect Class Name)
Besonderheit: Aspekte können abstract sein und von verschiedenen Implementierungen benutzt werden
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 27
Modell 2: leerer Aspekt
/** * @Aspect perInstance name=SomeNameForMyAspect */public class MyAspect extends Aspect {}
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 28
Modell 2: Definition der Pointcuts
Pointcuts sind Felder vom Typ Pointcut Metadata über den Typ des Pointcuts (Execution, Call, Set,
Get, Cflow, Throws)
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 29
Modell 2: Aspekt mit Pointcut
/** * @Aspect perInstance name=SomeNameForMyAspect */public class MyAspect extends Aspect { /** * @Execution * com.mypackage.Target.*(..) */ Pointcut pc1;}
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 30
Modell 2: Introductions
Introductions reiner Interfaces sind Felder mit dem neuen Interface als Typ Metadata Implements <classPattern>
Introductions von Implementationen (Mixins) sind Public Inner-Class, die neue Interfaces implementiert
parameterloser Constructor
Metadata Introduce <classPattern> Optionales Attribut deployment
Metadata kann bei overriding in abstrakten Aspekten entfallen Man kann Implementationen zur Laufzeit austauschen
gegen beliebige Klasse (muß nicht innere Klasse sein) mit gleichen Interfaces
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 31
Modell 2: Aspekt mit Introduction
/** * @Aspect perInstance name=SomeNameForMyAspect */public class MyAspect extends Aspect { /** * @Implements com.mypackage.* */ MarkerInterface anIntroduction;
/** * @Execution * com.mypackage.Target.*(..) */ Pointcut pc1;}
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 32
Modell 2: Beispiel innere Klasse
/** * @Aspect perInstance name=SomeNameForMyAspect */public class MyAspect extends Aspect { /**
* @Introduce pc1
*/
public class Introduction implements ToBeIntroduced {
// introduced methods implementation
...
}
/**
* @Class com.package.Foo
*/
Pointcut pc1;
}
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 33
Modell 2: Hinzufügen der Advices
Advices sind reguläre Methoden mit der Signatur
public Object <name of method>(JoinPoint joinPoint) throws Throwable
Typ (Around , Before, After, Throws) per Metadata Muster der Methoden / Felder an die es gebunden werden soll per
Metadata
Das Muster kann fast jeder algebraische Ausdruck sein || , OR , && , AND, !, NOT werden unterstützt
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 34
Modell 2: kompletter Aspekt/** * @Aspect perInstance name=SomeNameForMyAspect */public class MyAspect extends Aspect { /** * @Implements com.mypackage.* */ MarkerInterface anIntroduction; /** * @Execution * com.mypackage.Target.*(..) */ Pointcut pc1; /** * @Around pc1 */ public Object advice1(final JoinPoint joinPoint) throws Throwable { // do some stuff Object result = joinPoint.proceed(); // do some other stuff return result; }}
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 35
Modell 2: Aspect XML Definition
XML-Definition ist lediglich die Deklaration des konkreten Aspekts im System:<use-aspect class="package.NonAbstractAspect"/>
Modell 2: Aspekte Pointcuts Introductions Advices
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 36
Allgemeines Modell 1: XML-zentriert
zu JointPoints Definition der Advices Definition der Introductions Definition der Pointcuts Aufbau eines Aspekt
Modell 2: selbstdefinierend Aspekte Behandlung von Pointcuts Behandlung von Introductions Behandlung von Advices
Vergleich der Modelle
Überblick
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 37
Wahl des richtigen Modells
XML zentriertes Modell Definition der Pointcut-Ausdrücke in speziellen Dateien Aspekte können durch reines XML-Editieren angepaßt werden- Führt zu einer größeren Datenmenge, da mehrere Klassen benötigt
werden (eine für jede Advice). - Implementation ist getrennt von der Definition und dadurch
schwerer anzupassen, zu pflegen und wiederzuverwerten. Selbstdefinierende Aspekte
Ermöglicht eigenständige Aspect Komponenten Dadurch leichter zu pflegen, anzupassen und wiederzuverwenden Dadurch leichter Aspect-Bibliotheken zu erstellen - Benötigt zusätzlichen post-compilation Schritt um die Metadaten zu
verarbeiten (bis Java 1.5)
Allgemeines
Modell 1
Modell 2
Vergleich
Universität Bonn, Seminar „Component and Aspect Engineering“ WS 2003/04, Eva Stöwe 38
Was kann man damit tun?
Offline Bytecode-Modifikation Bytecode-Modifikation zur Laufzeit „Hot-deployment“
Introductions austauschen Advices „einschalten“ gegeneinander austauschen wieder ausschalten ohne neu Laden zu müssen
Remote Proxy Server funktioniert sogar bei Client-Server-Architekturen
„Hot-Swap“