Escolar Documentos
Profissional Documentos
Cultura Documentos
Source™
Aspect-Oriented Middleware
Middleware Applications with Plain Old Java Objects
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 1
Topics
Professional Open Source™
– What is AOM/AOP?
– JBoss AOP framework
– Prepackaged Middleware Aspects
• Dynamic Aspects
• Metadata Driven Aspects
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 2
1
What is AOP?
Professional Open Source™
Class1 Class1
CROSS HIERARCHY
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 3
What is AOM?
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 4
2
2. Component Level in AOM
Professional Open Source™
2)
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 5
3. Class Level
Professional Open Source™
3)
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 6
3
Cross-cutting Concern
Professional Open Source™
public
publicclass
classBankAccount
BankAccount { {
public
publicvoid
voidwithdraw(double
withdraw(doubleamount)
amount) { { What’s
What’s wrong?
wrong?
long start = System.currentTimeMillis();
long start = System.currentTimeMillis(); •• Difficult
Difficult to
to enable/disable
enable/disable
try
try{ { •• Have
Have to modify all
to modify all methods
methods
Connection with
with the
the same
same functionality
functionality
Connectioncon con==datasource.getConnection();
datasource.getConnection();
con.execute…(“select •• Bloated
Bloated code
code
con.execute…(“select……blahblahblah”);
blah”);
this.balance -= amount;
this.balance -= amount;
con.commit();
con.commit();
}}
finally
finally { {
long
longtotalTime
totalTime==System.currentTimeMillis()
System.currentTimeMillis()- -start;
start;
System.out.println(“withdraw
System.out.println(“withdrawtook
took““++totalTime);
totalTime);
}}
}}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 7
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8
4
Terminology
Professional Open Source™
Advice
– Encapsulates cross-cutting code
– An “event handler” for an execution point in code
– Behavior you want to weave into your Java classes (metrics code)
Aspect
– Java class that defines advices
Pointcut
– AOP’s “query” language
– Declaratively selects any point within Java code
– For example, a method call, field access, instance construction
Invocation
– Object instance that represents an invocation and its context
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 9
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 10
5
Aspect and Advice
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 11
Aspects
Professional Open Source™
public
publicclass
classMetricsAspect
MetricsAspect{{
public
publicObject
Objectprofile(MethodInvocation
profile(MethodInvocationinvocation)
invocation)throws
throwsThrowable
Throwable{{ Advice
Advice
try
try{{
long
longstart
start==System.currentTimeMillis();
System.currentTimeMillis();
return
returninvocation.invokeNext();
invocation.invokeNext();
}}
finally
finally {{
long
longtotalTime
totalTime==System.currentTimeMillis()
System.currentTimeMillis()--start;
start;
System.out.println(invocation.getMethod()
System.out.println(invocation.getMethod()++““took
took““++totalTime)
totalTime)
}}
}}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 12
6
Aspects
Professional Open Source™
public
publicclass
classMetricsAspect
MetricsAspect{ {
public
publicObject
Objectprofile(MethodInvocation
profile(MethodInvocationinvocation)
invocation)throws
throwsThrowable
Throwable{ { Advice
Advice
long start = System.currentTimeMillis();
long start = System.currentTimeMillis();
try
try{ {
return
returninvocation.invokeNext();
invocation.invokeNext();
}}
finally
finally { {
long
longtotalTime
totalTime==System.currentTimeMillis()
System.currentTimeMillis()- -start;
start;
System.out.println(invocation.getMethod()
System.out.println(invocation.getMethod()++““took took““++totalTime)
totalTime)
}}
}}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 13
Aspect Scope
Professional Open Source™
PER_VM
– Only one instance is created in the VM
– All threads execute on the same aspect instance
PER_CLASS
– One instance per adviced class is created.
– All threads executing on any instance of adviced class use the same aspect
instance
PER_INSTANCE
– One instance for every instanced object of an adviced class
– Threads that execute on the same object instance use the same aspect instance
PER_JOINPOINT
– One instance on every matched join point for every instance of adviced class
– Threads that execute on the same field, method or constructor of an object
instance share the same aspect instance
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 14
7
Aspect Configuration
Professional Open Source™
jboss-aop.xml
<aop>
<aop>
<aspect
<aspectclass=“org.jboss.MetricsAspect”/>
class=“org.jboss.MetricsAspect”/>
<!–
<!–pointcut
pointcutdeclarations
declarations(will
(willsee
seelater)
later)-->
-->
......
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 15
Aspect Initialization
Professional Open Source™
jboss-aop.xml
<aop>
<aop>
<aspect
<aspectclass=“org.jboss.MetricsAspect”>
class=“org.jboss.MetricsAspect”>
<attribute
<attributename
name==”LogFile”>PerformanceMetrics.log</attribute>
”LogFile”>PerformanceMetrics.log</attribute>
</aspect>
</aspect>
......
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 16
8
Aspect Initialization
Professional Open Source™
jboss-aop.xml
<aop>
<aop>
<aspect
<aspectname=“MetricsAspect”
name=“MetricsAspect”factory
factory==“org.acme.MetricsAspectFactory”/>
“org.acme.MetricsAspectFactory”/>
......
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 17
Pointcut Language
Declarative Selectors in JBossAOP
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 18
9
Pointcuts and Bindings
Professional Open Source™
jboss-aop.xml
<aop>
<aop>
<aspect
<aspectclass=“org.jboss.MetricsAspect”/>
class=“org.jboss.MetricsAspect”/>
<bind
<bindpointcut=“execution(public
pointcut=“execution(publicvoid
voidBankAccount->credit(java.lang.String,
BankAccount->credit(java.lang.String,int))”>
int))”>
<advice
<adviceaspect=“org.jboss.MetricsAspect”
aspect=“org.jboss.MetricsAspect”name=“profile”/>
name=“profile”/>
</bind>
</bind>
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 19
<!--
<!-- Advice
Adviceall
alltoString()
toString()methods
methodsininorg.jboss
org.jbosspackage
package-->-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(publicjava.lang.String
java.lang.Stringorg.jboss.*->toString())”>
org.jboss.*->toString())”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”formatStrings”/>
”formatStrings”/>
</bind>
</bind>
<!–
<!–Advice
Adviceall
alldeploy()
deploy()methods
methodsininMainDeployer
MainDeployerclass
classregardless
regardlessof
ofparameter
parameterdeclarations
declarations-->
-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(publicvoid
voidorg.jboss.deployment.MainDeployer->deploy(..))”
org.jboss.deployment.MainDeployer->deploy(..))”>>
<advice
<adviceaspect
aspect==“com.acme.MyAspect”
“com.acme.MyAspect”name
name==“authorizeDeploy”/>
“authorizeDeploy”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 20
10
Pointcuts and Bindings
Professional Open Source™
<!--
<!-- Advice
Adviceall
alltoString()
toString()methods
methodsininorg.jboss.ejb.Container
org.jboss.ejb.Containerand
andits
itssubclasses
subclasses-->
-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(publicString
String $instanceof{org.jboss.ejb.Container}->toString())”>
$instanceof{org.jboss.ejb.Container}->toString())”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”formatStrings”/>
”formatStrings”/>
</bind>
</bind>
<!–
<!–Advice
Adviceall
alldeploy()
deploy()methods
methodsininMainDeployer
MainDeployerclass
classthat
thatdeclare
declaregiven
givenexceptions
exceptions-->
-->
<bind
<bind pointcut = “execution(public void org.jboss.deployment.MainDeployer->deploy(..)throws
pointcut = “execution(public void org.jboss.deployment.MainDeployer->deploy(..) throws
org.jboss.deployment.DeploymentException,
org.jboss.deployment.DeploymentException,java.net.MalformedURLException)”>
java.net.MalformedURLException)”>
<advice
<adviceaspect
aspect==“com.acme.MyAspect”
“com.acme.MyAspect”namename==“authorizeDeploy”/>
“authorizeDeploy”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 21
<!--
<!-- Advice
Adviceall
allnon-static
non-staticmethods
methodsininorg.jboss.ejb.*
org.jboss.ejb.*package
package-->-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(public!static
!static** org.jboss.ejb.*->*(..))”>
org.jboss.ejb.*->*(..))”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”logCall”
”logCall”/>/>
</bind>
</bind>
<!--
<!-- Advice
Adviceall
allnon-public
non-publicConstructors
Constructorsininorg.jboss.ejb.Container
org.jboss.ejb.Containerclass
class-->
-->
<bind pointcut = “execution(!public org.jboss.ejb.Container->new(..))”>
<bind pointcut = “execution(!public org.jboss.ejb.Container->new(..))”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”logCall”
”logCall”/>/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 22
11
Pointcuts and Bindings
Professional Open Source™
<!--
<!-- Pointcut
Pointcuton
onaamethod
method-->
-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(publicvoid
voidorg.jboss.deployment.MainDeployer->deploy(..))”>
org.jboss.deployment.MainDeployer->deploy(..))”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”namename==”authorizeDeploy”/>
”authorizeDeploy”/>
</bind>
</bind>
<!–
<!–Pointcut
Pointcuton
oninstance
instancefield
fieldaccess
access-->
-->
<bind
<bindpointcut
pointcut==”field(public
”field(publicint
intcom.acme.PersonVO->SSN)”>
com.acme.PersonVO->SSN)”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”namename==”invariant”/>
”invariant”/>
</bind>
</bind>
<!–
<!–Pointcut
Pointcuton
oninstance
instancefield
fieldwrite
writeaccess
access-->
-->
<bind
<bindpointcut
pointcut==”set(public
”set(publicint
intcom.acme.PersonVO->age)”>
com.acme.PersonVO->age)”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”precondition”/>
”precondition”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 23
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 24
12
Pointcut Composition
Professional Open Source™
<!--
<!-- Pointcut
Pointcuton
onaacall
callto
toMyClass
MyClassconstructor
constructorwhen
whencalled
calledwithin
withinthe
theFoo
Fooclass
class-->
-->
<bind pointcut = “within(org.wombat.Foo) AND call(public void org.acme.MyClass->new(..))”>
<bind pointcut = “within(org.wombat.Foo) AND call(public void org.acme.MyClass->new(..))”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name
name==”countMyClassOfFoo”/>
”countMyClassOfFoo”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 25
Pointcut References
Professional Open Source™
With pointcut composition, expressions may get long and hard to read
– Repeating long expressions is not ideal
You can create named pointcut expressions and reference them in bindings
– <pointcut> tag
– Helps keeping the declarations more human-readable
<!–
<!–Select
Selectall
allpublic
publicmethods
methods-->
-->
<pointcut
<pointcutname
name==”public.methods”
”public.methods”
expr
expr ==”execution(public
”execution(public***->*(..))”/>
*->*(..))”/>
<!–
<!–Select
Selectall
allpublic
publicconstructors
constructors-->
-->
<pointcut name = ”public.constructors”
<pointcut name = ”public.constructors”
expr
expr ==”execution(public
”execution(public*->new(..))”/>
*->new(..))”/>
<!--
<!-- Trace
Traceall
allpublic
publicmethods
methodsand
andconstructors
constructors-->
-->
<bind
<bindpointcut
pointcut==“public.methods
“public.methodsOR
ORpublic.constructors”>
public.constructors”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name
name==”tracePublicMethodsAndConstructors”/>
”tracePublicMethodsAndConstructors”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 26
13
Group Types with TypeDef
Professional Open Source™
For complex group of types or type conditions that resolve to a class, you
can create a new type group with <typedef>
– Useful especially when you have to repeat the group declaration a lot
– Instead of writing many pointcuts individually, create a new type definition
– Operators class(), has(), hasfield() and $instanceof{} are allowed in the typedef
clause
– You don’t have to rely as much on wildcard patterns that may have unexpected
consequences later as the software evolves
<!–
<!–Create
Createaanew
newtype
typedefinition
definitionout
outof
ofvalue
valueobject
objectclasses
classesinindifferent
differentpackages
packages-->
-->
<typedef name = ”ValueObjects”
<typedef name = ”ValueObjects”
expr
expr ==”$instanceof{org.acme.ValueObject}
”$instanceof{org.acme.ValueObject}OR OR$instanceof{org.wombat.ValueObject}
$instanceof{org.wombat.ValueObject}AND
AND......”/>
”/>
<!--
<!-- count
countcreation
creationof
ofvalue
valueobjects
objectsfrom
fromaagiven
givenset
setof
ofvalue
valueobject
objectclasses
classesand
andtheir
theirsubclasses
subclasses-->
-->
<bind
<bindpointcut
pointcut==“execution(public
“execution(public$typedef{ValueObjects}->new(..))”>
$typedef{ValueObjects}->new(..))”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name name==”countCreatedVOs”/>
”countCreatedVOs”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 27
Stack Advices
Professional Open Source™
<bind
<bindpointcut
pointcut==”all(com.acme.BusinessObject)”>
”all(com.acme.BusinessObject)”>
<advice
<adviceaspect
aspect==”com.acme.MyAspect”
”com.acme.MyAspect”name
name==”myAdvice”/>
”myAdvice”/>
<advice
<advice aspect = ”com.acme.OtherAspect”name
aspect = ”com.acme.OtherAspect” name==”someAdvice”/>
”someAdvice”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 28
14
Stack Advices
Professional Open Source™
<stack
<stackname
name==”MyContainer”>
”MyContainer”>
<advice
<advicename
name==”log”
”log” aspect
aspect==”com.acme.MethodLogAspect”/>
”com.acme.MethodLogAspect”/>
<advice
<advicename
name==”security”
”security”aspect
aspect==”com.acme.SecurityAspect”/>
”com.acme.SecurityAspect”/>
<advice
<advicename
name==”tx”
”tx”aspect
aspect==”com.acme.TransactionAspect”/>
”com.acme.TransactionAspect”/>
</stack>
</stack>
<bind
<bindpointcut
pointcut==”all(com.acme.BusinessObject)”>
”all(com.acme.BusinessObject)”>
<stack-ref
<stack-refname
name==”MyContainer”/>
”MyContainer”/>
</bind>
</bind>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 29
Dynamic AOP
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 30
15
Dynamic AOP
Professional Open Source™
public
publicinterface
interfaceInstanceAdvised
InstanceAdvised{{
public
publicInstanceAdvisor
InstanceAdvisor_getInstanceAdvisor();
_getInstanceAdvisor();
public
publicvoid
void_setInstanceAdvisor(InstanceAdvisor
_setInstanceAdvisor(InstanceAdvisornewAdvisor);
newAdvisor);
}}
public
publicinterface
interfaceAdvised
Advisedextends
extendsInstanceAdvised
InstanceAdvised{{
Advisor _getAdvisor();
Advisor _getAdvisor();
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 31
Dynamic AOP
Professional Open Source™
public
public interface
interface InstanceAdvisor
InstanceAdvisor
{{
public
public SimpleMetaData
SimpleMetaData getMetaData();
getMetaData();
public
public Interceptor[]
Interceptor[] getInterceptors();
getInterceptors();
public
public Interceptor[]
Interceptor[] getInterceptors(Interceptor[]
getInterceptors(Interceptor[] baseChain);
baseChain);
public
public boolean
boolean hasAspects();
hasAspects();
public
public void
void insertInterceptor(Interceptor
insertInterceptor(Interceptor interceptor);
interceptor);
public
public void
void removeInterceptor(String
removeInterceptor(String name);
name);
public
public void
void appendInterceptor(Interceptor
appendInterceptor(Interceptor interceptor);
interceptor);
public
public void
void insertInterceptorStack(String
insertInterceptorStack(String stackName);
stackName);
public
public void
void removeInterceptorStack(String
removeInterceptorStack(String name);
name);
public
public void
void appendInterceptorStack(String
appendInterceptorStack(String stackName);
stackName);
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 32
16
Dynamic Pointcuts
Professional Open Source™
AdviceBinding
AdviceBinding binding
binding == new
new AdviceBinding("execution(POJO->new(..))",
AdviceBinding("execution(POJO->new(..))", null);
null);
binding.addInterceptor(SimpleInterceptor.class);
binding.addInterceptor(SimpleInterceptor.class);
AspectManager.instance().addBinding(binding);
AspectManager.instance().addBinding(binding);
jboss-aop.xml
<aop>
<aop>
<prepare
<prepareexpr="execution(POJO->new(..))"/>
expr="execution(POJO->new(..))"/>
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 33
Annotated Aspects
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 34
17
Java Annotations
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 35
Define Annotation
Professional Open Source™
public
public@interface
@interfaceReadLock
ReadLock{}{}
public
public@interface
@interfaceWriteLock
WriteLock{}{}
class
classXX{ {
@ReadLock
@ReadLockpublic
publicObject
ObjectgetData()
getData() { {. .. .. .} }
@WriteLock
@WriteLockpublic
publicvoid
voidsetData(Object
setData(Objectdata) data){ {. .. .. .} }
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 36
18
Define Annotation
Professional Open Source™
public
public @interface
@interface WriteLock
WriteLock {{
int
int timeout();
timeout();
}}
class
class XX {{
@WriteLock(timeout
@WriteLock(timeout == 5000)
5000)
public
public void
void setData(Object
setData(Object data)
data) {{ .. .. .. }}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 37
JBossAOP comes with a precompiler that you can use against Java 1.4
compliant codebase
– Annotation types are defined as interfaces
– Annotations are part of javadoc comments
package
package com.mypackage;
com.mypackage;
public
public interface
interface RWLock
RWLock {{
int
int timeout();
timeout();
}}
/**
/**
** @@com.mypackage.RWLock
@@com.mypackage.RWLock (timeout=3)
(timeout=3)
*/*/
public
public int
int myMethod()
myMethod() {{ …
… }}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 38
19
Annotation Compiler for Java 1.4
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 39
org.jboss.aop.Aspect
– Marks a class as an aspect
org.jboss.aop.PointcutDef
– Defines a named pointcut declaration
org.jboss.aop.Bind
– Binds an advice within aspect with a given pointcut
@Aspect
@Aspect (scope
(scope == Scope.PER_VM)
Scope.PER_VM)
public
public class
class MyAspect
MyAspect {{
@PointcutDef
@PointcutDef (”execution(\”public
(”execution(\”public ** com.acme.*->create(..)\”)
com.acme.*->create(..)\”)
public
public static
static Pointcut
Pointcut createMethods;
createMethods;
@Bind
@Bind (pointcut
(pointcut == ”createMethods”)
”createMethods”)
public
public Object
Object countInstances(Invocation
countInstances(Invocation invocation)
invocation) {{ …
… }}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 40
20
Annotations in Pointcuts
Professional Open Source™
all(@com.acme.createMethod)
– All methods marked with @createMethod annotation
withincode(@javax.ejb.Entity->new(..))
– Public constructors of EJB3 entity classes
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 41
Aspect Examples
POJO Middleware
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 8/2/2005 42
21
ReadWriteLock Aspect
Professional Open Source™
public
publicclass
classReadWriteLockAspect
ReadWriteLockAspect{{
java.util.concurrent.ReentrantReadWriteLock
java.util.concurrent.ReentrantReadWriteLocklock
lock==new
newReentrantReadWriteLock();
ReentrantReadWriteLock();
public ObjectreadLock(Invocation
publicObject readLock(Invocationinvocation)
invocation)throws
throwsThrowable
Throwable{{
try {
try {
lock.readLock().lock();
lock.readLock().lock();
return
returninvocation.invokeNext();
invocation.invokeNext();
}}
finally
finally {{
lock.readLock().unlock();
lock.readLock().unlock();
}}
}}
public
publicObject
ObjectwriteLock(Invocation
writeLock(Invocationinvocation)
invocation)throws
throwsThrowable
Throwable{{
try
try{{
lock.writeLock().lock();
lock.writeLock().lock();
return
returninvocation.invokeNext();
invocation.invokeNext();
}}
finally
finally {{
lock.writeLock().unlock();
lock.writeLock().unlock();
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 43
ReadWriteLock Aspect
Professional Open Source™
<aop>
<aop>
<aspect
<aspectclass=“ReadWriteLockAspect”
class=“ReadWriteLockAspect”scope=“PER_INSTANCE”/>
scope=“PER_INSTANCE”/>
<bind
<bindpointcut=“execution(!static
pointcut=“execution(!static***->@org.jboss.ReadLock(..))”>
*->@org.jboss.ReadLock(..))”>
<advice
<adviceaspect=“ReadWriteLockAspect”
aspect=“ReadWriteLockAspect”name=“readLock”/>
name=“readLock”/>
</bind>
</bind>
<bind
<bindpointcut=“execution(!static
pointcut=“execution(!static***->@org.jboss.WriteLock(..))”>
*->@org.jboss.WriteLock(..))”>
<advice
<adviceaspect=“ReadWriteLockAspect”
aspect=“ReadWriteLockAspect”name=“writeLock”/>
name=“writeLock”/>
</bind>
</bind>
</aop>
</aop>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 44
22
Asynchronous Invocations
Professional Open Source™
Asynchronous Aspect
– Tag Methods
– Invoke them in background
– Obtain response asynchronously
– In-VM only
– Working on Remote asynchronous invocations
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 45
Asynchronous Invocations
Professional Open Source™
import
importorg.jboss.aspects.asynchronous.aspects.jboss.Asynchronous;
org.jboss.aspects.asynchronous.aspects.jboss.Asynchronous;
public
publicclass
classPOJO
POJO{{
public
publicvoid
voidprocessBusinessModel(){...}
processBusinessModel(){...}
@Asynchronous
@Asynchronous
public
publiclong
longprocessBusinessModel(...){}
processBusinessModel(...){}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 46
23
Asynchronous Invocations
Professional Open Source™
POJO
POJOpojo pojo==new
newPOJO(...);
POJO(...);
long
longresult result==pojo.processBusinessModel(...);
pojo.processBusinessModel(...); ////non-blocking
non-blockingcallcall
AsynchronousFacade
AsynchronousFacadefaçade façade==(AsynchronousFacade)pojo;
(AsynchronousFacade)pojo;
......
////do dosomesomeworkwork
......
ifif(facade.isDone())
(facade.isDone()){{
ifif(facade.getResponseCode()
(facade.getResponseCode()== ==OK)
OK) ////Test
Testresponse
responsecode
codereturned
returned
result
result==((Long)facade.getReturnValue()).longValue();
((Long)facade.getReturnValue()).longValue(); ////get
getmethod
methodresponse
response
else
elseifif(facade.getResponseCode()==TIMEOUT)
(facade.getResponseCode()==TIMEOUT){...} {...} ////Test
Testififmethod
methodtimed
timedout
out
else
else{{
AsynchronousResponse
AsynchronousResponseresponse response==facade.waitForResponse();
facade.waitForResponse(); ////blocking
blockingcall
call
IfIf(response.getResponseCode()==OK)
(response.getResponseCode()==OK) //// getmethod
get methodresponse
response
result=((Long)response.getReturnValue()).longValue();
result=((Long)response.getReturnValue()).longValue();
else
elseifif(response.getReponseCode()==TIMEOUT)
(response.getReponseCode()==TIMEOUT){...} {...} ////Test
Testififmethod
methodtimed
timedout
out
}}
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 47
Transaction Demarcation
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 48
24
Transaction Demarcation
Professional Open Source™
@Tx(TxType.REQUIRED)
@Tx(TxType.REQUIRED)
public
public void
void somepojoMethod()
somepojoMethod() {{ …
… }}
<metadata
<metadata tag="transaction"
tag="transaction" class="org.jboss.test.POJO">
class="org.jboss.test.POJO">
<method
<method name="somepojoMethod“>
name="somepojoMethod“>
<value>RequiresNew</value>
<value>RequiresNew</value>
</method>
</method>
</metadata>
</metadata>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 49
Transactional Locking
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 50
25
Transactional Locking
Professional Open Source™
@TxSynchronized
@TxSynchronized
public
public void
void somepojoMethod()
somepojoMethod() {{ …
… }}
<metadata
<metadata tag="transactionSynchronized"
tag="transactionSynchronized" class="org.jboss.test.POJO">
class="org.jboss.test.POJO">
<method
<method name="somepojoMethod“/>
name="somepojoMethod“/>
</metadata>
</metadata>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 51
Roled-based Security
Professional Open Source™
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 52
26
Role-Based security
Professional Open Source™
<exclude-list>
<exclude-list>
<description>Methods
<description>Methodsthat
thatconnect
connectbe
beused</description>
used</description>
<method>
<method>
<method-name>excluded</method-name>
<method-name>excluded</method-name>
</method>
</method>
</exclude-list>
</exclude-list>
</metadata>
</metadata>
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 53
Role-Based security
Professional Open Source™
@Unchecked
@Uncheckedpublic
publicPOJO()
POJO(){}{}
@Exclude
@Excludepublic
publicexcludedMethod()
excludedMethod(){…}
{…}
@Permissions({“admin”,
@Permissions({“admin”,“manager”})
“manager”})
public
publicvoid
voidsomeMethod()
someMethod(){…}
{…}
@Permissions({“user”})
@Permissions({“user”})
public
publicstatic
staticint
intstatus;
status;
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 54
27
Role-based Security
Professional Open Source™
import
importjava.security.Principal;
java.security.Principal;
public
publicObject
Objectinvoke(org.jboss.aop.joinpoint.Invocation
invoke(org.jboss.aop.joinpoint.Invocationinvocation)
invocation)throws
throwsThrowable
Throwable
{{
Principal
Principalprincipal
principal==(Principal)invocation.getMetaData("security",
(Principal)invocation.getMetaData("security","principal");
"principal");
……
}}
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 55
Conclusion
Professional Open Source™
AOP Framework
– Hot deployment
– Dynamic API
– Metadata Integration
– Seamless integration with JBoss architecture
Aspect-Oriented Middleware
– POJO based development
– Free developers from specifications
© JBoss
JBoss,Inc.,
Inc. 2003-2005. 56
28