Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

„Component and Aspect Engineering“

Ähnliche Präsentationen


Präsentation zum Thema: "„Component and Aspect Engineering“"—  Präsentation transkript:

1 „Component and Aspect Engineering“
AspectWerkz Vortrag von Eva Stöwe Im Rahmen des Seminars „Component and Aspect Engineering“

2 Überblick 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

3 Was bietet AspectWerkz?
Allgemeines Modell 1 Modell 2 Vergleich 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

4 Zwei Modelle Modell 1: XML zentriert
Allgemeines Modell 1 Modell 2 Vergleich 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)

5 AOP System Aspecte benötigen ein System, in dem sie laufen
Allgemeines Modell 1 Modell 2 Vergleich 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" " <aspectwerkz> <system id="system name"> ... <aspect name="MyAspect"> </aspect> </system> </aspectwerkz> <!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" " <aspectwerkz> <system id="system name"> ... <use-aspect class="MySelfDefinedAspect"/> </system> </aspectwerkz>

6 Allgemeines Modell 1 Modell 2 Vergleich 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.

7 Überblick 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

8 Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau 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ß

9 Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau 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)

10 Modell 1: Beispiel: Around Advice
JointPoints Advices Introductions Pointcuts Aspektaufbau 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;

11 Modell 1: Advice-Attribute
JointPoints Advices Introductions Pointcuts Aspektaufbau 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>

12 Modell 1: Introductions
JointPoints Advices Introductions Pointcuts Aspektaufbau 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.

13 Modell 1: Introductions Attribute
JointPoints Advices Introductions Pointcuts Aspektaufbau 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"/>

14 Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau Modell 1: Pointcuts Pointcuts selektieren wohldefinierte Punkte im Programmablauf. 6 Arten werden unterstützt: MethodPointcut getFieldPointcut setFieldPointcut ThrowsPointcut CallerSidePointcut CFlowPointcut

15 Modell 1: Pointcut Attribute
JointPoints Advices Introductions Pointcuts Aspektaufbau 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>

16 Modell 1: JointPoints Advices Introductions Pointcuts Aspektaufbau 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.

17 Modell 1: Aspect Attribute
JointPoints Advices Introductions Pointcuts Aspektaufbau 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)

18 Modell 1: (leeres) Aspekt-Beispiel
JointPoints Advices Introductions Pointcuts Aspektaufbau Modell 1: (leeres) Aspekt-Beispiel <abstract-aspect name="MyAbstractAspect"> </abstract-aspect> <aspect name="MyAspect" extends="MyAbstractAspect"> </aspect>

19 Modell 1: Aspekte mit Pointcuts
JointPoints Advices Introductions Pointcuts Aspektaufbau 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>

20 Modell 1: Introductions Hinzufügen
JointPoints Advices Introductions Pointcuts Aspektaufbau Modell 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"/>

21 Modell 1: Aspekt mit Introductions
JointPoints Advices Introductions Pointcuts Aspektaufbau 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>

22 Modell 1: Advices Hinzufügen
JointPoints Advices Introductions Pointcuts Aspektaufbau 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"/>

23 Modell 1: Aspekt mit Advices
JointPoints Advices Introductions Pointcuts Aspektaufbau 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"/> </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>

24 Modell 1: kompletter Aspekt
JointPoints Advices Introductions Pointcuts Aspektaufbau Modell 1: kompletter Aspekt 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; <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"/> </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> <!DOCTYPE aspectwerkz PUBLIC "-//AspectWerkz//DTD//EN" " <aspectwerkz> <system id="system name"> ... <aspect name="MyAspect"> </aspect> </system> </aspectwerkz> <advice-def name="advices/caching" class="advices.CachingAdvice" deployment-model="perInstance"> <param name="timeout" value="10"/> </advice-def> <introduction-def name="java/io/Serializable" interface="java.io.Serializable"/> <introduction-def name="mixins/Mixin" interface="mixins.Mixin" implementation="mixins.MixinImpl" deployment-model="perThread"/>

25 Überblick 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

26 Modell 2: Aspekte Aspekte sind Java Klassen die Besonderheit:
Pointcuts Introductions Advices 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

27 Modell 2: leerer Aspekt Modell 2: /**
Aspekte Pointcuts Introductions Advices Modell 2: leerer Aspekt /** perInstance name=SomeNameForMyAspect */ public class MyAspect extends Aspect { }

28 Modell 2: Definition der Pointcuts
Aspekte Pointcuts Introductions Advices Modell 2: Definition der Pointcuts Pointcuts sind Felder vom Typ Pointcut Metadata über den Typ des Pointcuts (Execution, Call, Set, Get, Cflow, Throws)

29 Modell 2: Aspekt mit Pointcut
Aspekte Pointcuts Introductions Advices Modell 2: Aspekt mit Pointcut /** perInstance name=SomeNameForMyAspect */ public class MyAspect extends Aspect { * com.mypackage.Target.*(..) Pointcut pc1; }

30 Modell 2: Introductions
Aspekte Pointcuts Introductions Advices 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

31 Modell 2: Aspekt mit Introduction
Aspekte Pointcuts Introductions Advices Modell 2: Aspekt mit Introduction /** perInstance name=SomeNameForMyAspect */ public class MyAspect extends Aspect { com.mypackage.* MarkerInterface anIntroduction; * com.mypackage.Target.*(..) Pointcut pc1; }

32 Modell 2: Beispiel innere Klasse
Aspekte Pointcuts Introductions Advices Modell 2: Beispiel innere Klasse /** perInstance name=SomeNameForMyAspect */ public class MyAspect extends Aspect { pc1 public class Introduction implements ToBeIntroduced { // introduced methods implementation ... } com.package.Foo Pointcut pc1;

33 Modell 2: Hinzufügen der Advices
Aspekte Pointcuts Introductions Advices 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

34 Modell 2: kompletter Aspekt
Aspekte Pointcuts Introductions Advices Modell 2: kompletter Aspekt /** perInstance name=SomeNameForMyAspect */ public class MyAspect extends Aspect { com.mypackage.* MarkerInterface anIntroduction; * com.mypackage.Target.*(..) Pointcut pc1; pc1 public Object advice1(final JoinPoint joinPoint) throws Throwable { // do some stuff Object result = joinPoint.proceed(); // do some other stuff return result; }

35 Modell 2: Aspect XML Definition
Aspekte Pointcuts Introductions Advices Modell 2: Aspect XML Definition XML-Definition ist lediglich die Deklaration des konkreten Aspekts im System: <use-aspect class="package.NonAbstractAspect"/>

36 Überblick 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

37 Wahl des richtigen Modells
Allgemeines Modell 1 Modell 2 Vergleich 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)

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“


Herunterladen ppt "„Component and Aspect Engineering“"

Ähnliche Präsentationen


Google-Anzeigen