martedì 29 settembre 2009

New Hook Aspect to use AOP concepts inside Roma Framework

Hook Aspect

The Hook Aspect aims to write listeners of POJO events in simple way. The Hook Aspect allows the developer to extend an existent application in a flash. The Hook Aspect is based on the well known principles of Aspect Oriented Programming, AOP by now. The AOP supports the Object oriented paradigm. The two approaches can be used together to get the best of both.

You can hook any action of any POJO at runtime as well as read/write of any fields.

Using the Hook Aspect you can change and customize your application behaviour in a non-invasive mode.

Scope

Each hook belong to a scope. Scope can be:

  • SESSION, means that all the registered POJOs that are part of the current User Session will be waked up when the event is raised

  • APPLICATION, means that the POJOs are components declared into the IoC container (Spring IoC).

Using the scope APPLICATION you must assure that the POJO loaded by Spring IoC use the annotation @CoreClass(loading=EARLY) on top of the class declaration (look at the example below). This is the only way to assure to activate the Roma configuration when the application starts.

Below a table of the hooks available for the action:

@HookAction

Description

onBeforeAction

Invoked just before an action is called

onAroundAction

Invoked in place of the original action

onAfterAction

Invoked just after an action is called

onBeforeFieldRead

Invoked just before a field is read

onAroundFieldRead

Invoked in place of the original read. The action must return the value to return in place of the original

onAfterFieldRead

Invoked just after a field is read

onBeforeFieldWrite

Invoked just before a field is written

onAroundFieldWrite

Invoked in place of the original write. The action must return the value to assign to the field in place of the original

onAfterFieldWrite

Invoked just after a field is written



@HookField

Description

field

Copy the content of the field hooked everytime changes

Change the behaviour at run-time

In this example you need to avoid further login into the application for a while. The classic path would be to change the code or to write a parameter to switch on/off this feature.

Using a hook makes all things much easyer. Please note that the hook hookAroundAction means in-place-of. This action will be called in place of the original one.

ApplicationControl Example





@CoreClass(loading = LOADING_MODE.EARLY)
public class ApplicationControl {
@HookAction(hookAroundAction="CustomLogin.login", scope=HookScope.APPLICATION)
public void hook() {
// DISABLE THE LOGIN
System.out.println("Application in mantainance: login disabled");
RomaFrontend.flow().forward(
new MessageOk("login", "Login temporary disabled").
setMessage("Login is temporary disabled, please try in few minutes"),
"screen:popup:alert");
}
}

In the same way you can customize strategies, alghoritms, navigation paths all by using the Hook Aspect.

Collect results

This is an example of a simple profiler that record in a Map in memory the times the actions are called.


Remember that to use the scope “application” you need to instantiate the component as singleton by the IoC container (Spring IoC at this time). Add this line at the end of applicationContext.xml file of your application:

<bean id="ProfilerDemo" singleton="true" class="com.orientechnologies.moobilis.server.view.domain.ProfilerDemo" />


And create a class in this way:


ProfilerDemo Example:


import java.util.HashMap;

import org.romaframework.aspect.hook.annotation.HookAction;
import org.romaframework.aspect.hook.annotation.HookScope;

@CoreClass(loading = LOADING_MODE.EARLY)
public class ProfilerDemo {

protected HashMap<String, Long> totalTimes = new HashMap<String, Long>();

@HookAction(hookAfterAction = "*", scope = HookScope.APPLICATION)
public void profileAllActions() {
Long times = totalTimes.get("allActions");
if (times == null)
times = new Long(0);
times = times + 1;
totalTimes.put("allActions", times);
}

@HookAction(hookAfterAction = "*Login*", scope = HookScope.APPLICATION)
public void profileAllLoginActions() {
Long times = totalTimes.get("loginActions");
if (times == null)
times = new Long(0);
times = times + 1;
totalTimes.put("loginActions", times);
}
}

Nessun commento:

Posta un commento