Velocity - LIG Membres

publicité
Principe d’utilisation de Velocity dans
Javanaise
 Velocity est utilisé pour générer des classes d’interception
spécifiques aux objets métiers
 Ces classes implantent les mêmes méthodes / Interfaces
que les objets métier
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
4
Principe d’utilisation de Velocity
template
Hello $name,
Welcome to Velocity
contexte
Nom
name
Result (Writer)
ref. Objet Java
"World"
Hello World,
Welcome to Velocity
Velocity Engine
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
5
HelloWorld avec Velocity
(exemple tiré de www.javaworld.com/javaworld/jw-12-2001/jw-1228-velocity-p.html)
HelloWorld.java
import …;
public class Helloworld
public static void main
// get & initialize the
VelocityEngine ve = new
HelloWorld.vm
{
(String args[]) {
velocity engine
VelocityEngine().init();
Hello $name, Welcome to Velocity
// get the template
Template t = ve.getTemplate("helloworld.vm");
// create a context & add data
VelocityContext context = new VelocityContext();
context.put("name", "World");
// perform generation
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
6
Utilisation de Velocity dans Javanaise
Template
Result (Writer)
Contexte
package jvnGenerated;
public class Jvn$className {
…
#foreach ($meth in $methods)
#if ($util.isNotNull($ret))
...
#set ($needWriteLock =
($methodName.indexOf("W_") == 0))
#if ($needWriteLock)
obj.jvnLockWrite();
#elseif ($needReadLock)
obj.jvnLockRead();
#end
…
#end
}
© F. Boyer, UJF
Nom
ref. Objet Java
util
…
className
Sentence
…
public class JvnSentence
extends JvnObjectImpl
implements Serializable {
…
public SentenceJvn() {…}
methods
public void write(String text) {…}
arrayList of Method objects
(see java.lang.reflect)
public String read() {…}
}
Velocity Engine
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
7
Principes d'implémentation d’une classe
d’interception
// Remarque : différents principes d'implémentation peuvent être adoptés
public class JvnSentence {
…
// appel d'une méthode applicative
public void write(String s) {
jvnLockWrite();
// appelle la méthode write sur l'objet métier
...
jvnUnLock();
}
}
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
8
Principes d'implémentation de JVN2
Comment savoir si une méthode accède un objet en écriture
ou en lecture ?
 Le nom de la méthode suit une convention particulière (préfixe ou suffixe
particulier)
 La méthode lève des exceptions prédéfinies par Jvn (JvnReadException,
JvnWriteException)
 On utilise des annotations Java
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
9
Suppléments d’information : extrait de la
Javadoc / java.lang.Method
Class[] getExceptionTypes()
Returns an array of Class objects that represent the types of the exceptions declared to
be thrown by the underlying method represented by this Method object.
Int getModifiers()
Returns the Java language modifiers for the method represented by this Method
object, as an integer.
String getName()
Returns the name of the method represented by this Method object, as a String.
Class[] getParameterTypes()
Returns an array of Class objects that represent the formal parameter types, in
declaration order, of the method represented by this Method object.
Class getReturnType()
Returns a Class object that represents the formal return type of the method represented
by this Method object.
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
10
Suppléments d’information : VTL
- Affecter une valeur (chaîne) à une variable:
#set ($a = "Velocity")
Commentaire
## Ceci est un commentaire
-
Notation formelle
Class $className
-
$foo
$foo.getBar() ## est équivalent à $foo.Bar
$data.setUser("jon") ## est équivalent à #set( $data.User = "jon" )
$data.getRequest().getServerName() ## est équivalent à
$data.Request.ServerName ## est équivalent à
${data.Request.ServerName}
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
11
Références

http://jakarta.apache.org/velocity

http://velocity.apache.org/

http://velocity.apache.org/engine/releases/velocity-1.5/developer-guide.html

http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
© F. Boyer, UJF
Projet JAVANAISE, UE Etude et Projet d’intergiciels, M2PGI
12
Téléchargement