Sogeti telosys@md day2011
description
Transcript of Sogeti telosys@md day2011
Génération de code
à partir d’un « modèle léger »
Laurent Guérin
Telosys Project Leader
Sogeti & OW2 Consortium
Paris - 24 novembre 2011
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
Introduction
Laurent Guérin : Person
Sogeti : Company Capgemini : Company
OW2 : Consortium
Telosys : Project
belongs to
leads
hosts
: Plateform
: IDE
: License
2
France : Country
located in
Contexte & Objectifs
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Constat :
– Généralisation de Java :-)
– Projet Java = projet lourd :-(
• Génération de code ?
Oui, mais …
– solution légère et efficace,
adaptée aux projets à cycle court
– solution simple, utilisable par tous
– environnement léger (installation en quelques
heures/minutes)
• Objectif « démarrage rapide »
4
Contexte
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Un framework global, auto-suffisant pour
construire une application complète
• Des outils (plugins Eclipse) pour générer le code
initial de l’application et guider le développeur.
Puis ouverture sur d’autres cibles…
Method
Framework Tools
( code generation ) ( global & simple )
5
Le projet Telosys
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
“Keep it simple and stupid” ( KISP ? )
– framework simple et léger
– « petits outils » (faciles à installer et à utiliser)
“Respects des standards”
– servlet/JSP/JSPX, JDBC, JPA, http, XML, …
“Ouverture”
– Open Source ( projet / OW2 consortium )
– Format ouvert (modèle léger = XML)
– Templates adaptables différentes cibles :
Telosys fwk, autres Frameworks Java, Flex, PHP, …
6
Philosophie du projet
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Ne pas réinventer la roue (il existe déjà de
nombreux outils), apporter une alternative
• Offrir une boite à outils intégrée dans Eclipse,
immédiatement utilisable par des développeurs
non initiés au MDD
• La génération de code pour tous !
7
Outillage
sophistication +-
Le framework
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Framework global “all in one“
• Un seul jar (+ JDBC) => pas de "jar hell" !
• Basé sur des concepts simples
• Léger (servlet container + base de données)
9
Le framework
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• “Global” => moins de “glue”, moins d’objets (empreinte mémoire réduite), moins de configuration, … => plus homogène
• “Modulaire” (utilisation partielle)
PersistencePresentation Application
ScreenMaps
Services
ScreenManagers
Business
DAO
Java Beans ( POJO )
XML conv
Authenti-cation
Commons, logging, I18N, …
AJAXJSP + taglib
FrameworkJavascript
SQL
Services
Natively AJAX
Un seul frameworkqui couvre toutes les couches de l’application
"VO"
Very simple objects( "Pure POJO" )
Thin persistence layer
10
« Global effect »
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Une fois chargé dans le navigateur, un écran
dialogue avec le server via des requêtes AJAX
• Découplage ( facilite la génération de code )
Client-sideScreen
Manager
ServiceJa
va
Sc
rip
t
Fra
mew
ork
Server-side<request screen-name="Employee"
screen-id="0" action="get" >
</request>
<response screen-name="Employee"
screen-id="0" action="get" >
<return code="0" message="Ok" >
</return>
<data>
<employee id="23" lastName="Wayne"
firstName="John" manager="0" email="" />
</data>
</response>
<response service-name="Add">
<return code="0" message="Ok" />
<values> <value result="8" /> </values>
<data> </data>
</response>
http://myhost:8080/.../Add.svc?p1=2&p2=6
Actio
nS
erv
ice
http
11
Le paradigme « client-serveur »
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Une alternative aux “ORM” classiques
ORM : JPA (or Hibernate) …High level & "Object Oriented"Hides the complexity Sometimes misunderstood by the developers ( "lazy/eager" loading, cache effects, "attached/detached" entities, "owning side"/"inverse side" links, … )
JDBC APILow level
Telosys DAL (based on DAO pattern)Intermediate level & "Record Oriented"One Bean = One RecordThe developer keeps the control No links, no cache, no byte-code enhancement Very simple API : load, save, insert, update, exists, loadList, updateList, …
Les deux sont utiles.
Au choix …
12
Java objects
rule the persistence
DB
new new
DB
Database records
rule the persistence
new legacy
La persistance
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Le problème : le code technique et spécifique
• Les outils de génération de code sont une
réponse possible.
Exemples :
Ruby on Rails, Grails, Spring Roo, etc…
13
Un framework ne suffit pas…
Plate-forme (Java EE)
Framework
Application
« boilerplate »
Code générique Code spécifique
Outils
Génération de code
Les différentes approches
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Démarche MDD classique, MDA, etc …
• « Model - First » => concevoir un model !
– investissement initial important
– nécessite de respecter le process (formation)
• Incompatible avec un démarrage rapide
Code
MODEL
Part
name
Message
name
0..n+part 0..n
Service
name
Port
name
0..n+port 0..n
Port Type
nameBinding
name1
+binding
1
1
+type
1
Param
name
1+message 1
Output
Input
StartWithExtensions
BindingFaultFault
name1
+message
11
BindingOperation
name
0..n+operation 0..n0..1 +input0..1
0..1+output 0..1
0..n
Operation
name0..1
+input
0..1
0..1
+output
0..1
0..n+fault 0..n
0..n+operation 0..n
11
1
+fault 0..n
X M I( XML )
TemplateTOOL
15
Split !
Approche « Model First »
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Scaffolding de type « Database - First »
• Très efficace ( rapide )
• Mais pas d’action possible sur l’outil
de génération
• Pas assez d’informations …
– Noms/types des attributs Java
– Règles de validation : min, max, regexp, …
– Représentation des Booléens
(1/0, "T"/"F", … )
– etc …
• Il faudrait injecter/stocker des informations
complémentaires… un modèle ?
Database
TOOL
Code
Metadata
Template
16
Approche « scaffolding »
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Bien intégré dans l’IDE :-)
• Informations en entrée :
– Saisie utilisateur :-(
– Artefacts existants dans le projet
• Java class Java class
• WSDL file Java classes
• Génération unitaire,
non automatisable
• Peuvent être utilisés comme
des « accélérateurs »
( après la génération de masse )
Code
Workspacefiles
I.D.E.
“Java Model”org.eclipse.jdt.core
17
Bulkgeneration
Les « wizards »
L’approche « Telosys Tools »
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Les souhaits :
– générer rapidement le code initial des écrans de
mise à jour des entités d’une base de données
(notion de scaffolding / database first)
– avec un outil simple, bien intégré dans l’IDE
(comme les wizards)
– modifier/adapter les « patrons » de
génération (notion de templates)
– modifier (raffiner) les informations brutes
issues des méta-data, pouvoir y ajouter
des informations complémentaires
– lancer des générations de masse
19
DB
Metadata
Expression du besoin
Template
Modèle ?
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Un processus en 3 étapes
Database
Repository
generator
Metadata
Repository(Lightweight
model)
Code
Code
generator
Template
generates
updates
modify & add
information
Approche
“Database-first”
pour initialiser un
“modèle léger”
Adapter / enrichir
le “modèle léger”
Générer
le code
initial
1 2 3
updates customizes
20
Telosys Tools : le processus
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Modèle ( « léger » )
– seules les entités sont gérées
– représentation graphique inutile
– simple fichier XML (indépendant d’Eclipse)
– adhérence Eclipse : limitée à l’ IHM
• Moteur de génération de code
( « templating engine » )
– simple
– connu
– open source
– utilisable avec les wizards
21
Basique
mais suffisant
Telosys Tools
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org 22
workspaceRepository
(model)
O/R Mapping
Database Java Attributes
Java Classes
Database
modifiable
Fichier
.dbrep
Editeur de modèle
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
public class $class.name implements Serializable {
private static final long serialVersionUID = 1L;
//--- ATTRIBUTES
#foreach( $field in $class.attributes )
private $field.formatedType(10) $field.formatedName(12) ;
#end
//--- CONSTRUCTOR(S)
public ${class.name}()
{
super();
}
//--- GETTERS & SETTERS
#foreach( $field in $class.attributes )
#if ( $field.setter ) public void ${field.setter}( $field.type value )
{
this.$field.name = value;
}
#end
#if ( $field.getter ) public $field.type ${field.getter}()
{
return this.$field.name;
}
#end
#end
23
Extrait d’un template (génération d’un bean)
Repository
(model)
Velocity
Context
$class
etc…
cf documentation
sur le site Apache
Velocity ( templating engine )
Qu’est-ce qu’on génère ?
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
code « jetable »
DataSet
VO List
VO Bean
• VO Bean (POJO = “entity record”)
• VO List (liste de bean avec tri & sélection)
• DAO
• Convertisseurs
DatabaseData
Access
LayerXML
Con-
verters
ParsersFiles
XML
< … >
</ … >
Book
.java
BookList
.java
BookConv
.java
BookDAO
.java
Book
Table
Code
generator
25
Les artefacts générés N fois
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Approche “graphe d’objets” : les liens peuvent
être générés (persistance de type JPA)
• Approche “record” : les liens ne sont pas
nécessaires, assemblage possible par
“agrégation”
Aggregation
(composite)
26
@ManyToOne
@OneToMany
@ManyToMany
@OneToOne
Code
generator
DAO
Code
generator
Bean
Bean
Persistance : objet ou record ?
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
Book (bean)
ScreenData
(aggregation
0 .. N objects)
ScreenManager ScreenProcedure
AuthorList
Shop (bean)
ScreenMap
DataSet
BookProc
.java
BookMgr
.java
BookData
.java
Book.jsp
( or .jspx)
Book.js
M
V CAJAX
acts on …
request
response
Code
generator
27
code « initial » à reprendre
(ne pas régénérer après évolution)
Les artefacts générés 1 fois
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
JSP ou JSPX, pas de HTML !
28
<%@ taglib uri="/telosys/widgets" prefix="t" %>
<t:screenbody>
<t:label id="L_EmpId" x="40" y="60" txt="Employee Id : " cl="PK_field_label" />
<t:label id="L_LastName" x="40" y="100" txt="Last name : " />
<t:label id="L_FirstName" x="40" y="140" txt="First name : " />
<t:label id="L_Birthdate" x="40" y="180" txt="Birthdate : " />
<t:field x="200" y="60" id="EmpId" cl="PK_field" size="6" maxlength="6" />
<t:field x="200" y="100" id="LastName" size="30" maxlength="30" value="Aaaa" />
<t:button x="460" y="100" id="b1" txt="My button" onclick="b1_onClick();" />
<t:field x="200" y="140" id="FirstName" size="30" maxlength="30" value="Georges" />
<t:field x="200" y="180" id="Birthdate" size="10" maxlength="10" value="" type="Date" />
<t:radiogroup id="Contract" x="200" y="220">
<t:radioitem id="R_field61" x="0" y="0" value="1" txt="C.D.D. (1)" />
<t:radioitem id="R_field62" x="0" y="20" value="2" txt="C.D.I. (2)" />
</t:radiogroup >
<t:checkbox id="Manager" x="340" y="220" value="1" valueoff="9" txt="Manager" />
</t:screenbody >
Screen body (exemple)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• "Framework" => "Frame" !
• "CoC" ( Conventions over Configuration )
– Les développeurs savent où placer les fichiers
– Les outils savent où générer les fichiers et n’ont pas à
modifier les fichiers de configuration
• Exemple : Java : 1 package Web : 1 répertoire
1 écran = +
29
Conventions
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Une application Java Web standard
• Le projet peut être organisé pour Maven,
ou pas …
30
src (server side)
TelosysTools (+)
WebContent (client side)
déploiement
.war
Qu’est-ce qu’un projet Telosys ?
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Création d’un nouveau projet Telosys …
1) Création d’un projet
« Dynamic Web Project » 2) Importation du
« Telosys starter kit »
31
Démo avec Eclipse
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
3) Tester le
projet (run)
4) Définir les
conventions
32
Démo avec Eclipse (suite)
Utilisation des Telosys Tools
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
Template
.vm
Repository.dbrep
( XML )
Velocity
Context
Repository
generator
Code generator
34
WizardsEditors Properties
.jsp / .jspx
.js
.java
other …
Database
Meta-data
Tout peut être fait
dans EclipseUtilisable en ligne
de commande
Vue d’ensemble
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Si ça fonctionne pour le framework Telosys …
ça peut aussi être utilisé pour …
d’ autres frameworks Java
d’ autres langages/plates-formes
35
- Spring MVC
- Vaadin
- etc…
- Flex (MXML & Action Script)
- PHP
- etc …
Généralisation : nouvelles cibles
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Connexion à la base de données
• Génération du « repository »
• Modification/adaptation du « repository »
• Génération des objets basiques (Bean, List, DAO,
Conv.)
• Génération des écrans (server side & client side)
• Modification des template
• Gestion des liens inter-entités
• Génération des beans pour JPA & Validators
• Ajout d’un nouveau template spécifique
36
La démo …
Conclusion
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org 38
Base de
donnéesModèle
légerOutils Code initial
Conclusion
et le projet est
sur les rails…
Gain (J/H)
Temps de mise en œuvre
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Roadmap 2012 / principales évolutions
– Repository
• Extension (sans limite) des informations pour chaque
entité et chaque champ
• Templates pour des collections d’entités
– Screens design
• Release officielle du plugin Screen Builder (WYSIWYG)
–Portails
• Portlet JSR-168 pour embarquer des écrans Telosys
dans une page de portail
– NoSQL
• DAO pour GAE/BigTable & Cassandra
– etc …39
Le future
Questions ?
Appendix
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
Services
Text/CSV files parser
File upload/download
Internationalization
DataSet
XML converters
SQL DAO
Selectable/Sortable
beans lists
AuthenticationLogging
Commons/util classes
Widgets TagLib
JavaScript framework
Screen Maps
42
Framework features
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org 43
A visual tool to design the Telosys Screens
( beta version )
Telosys Screen Builder
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Web site : http://www.telosys.org
• LinkedINhttp://www.linkedin.com/groups?gid=1340197
• Twitter http://twitter.com/telosys
• Viadeo (in french)
http://www.viadeo.com/hu03/0024uydjnnwew6j/telosys
44
Follow the project …
The demo…
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
Repository
Generator
• Use the database meta-data to generate the
initial repository
Database
Repository.dbrep
( XML )
Generationrules
46
Demo
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• The generated repository is editable
with an Eclipse plugin editor
O/R Mapping
Database Java Attributes
Java Classes
Database
updatable
47
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Java fields :
– Each Java field can have additional information
– Useful for
• JSP/TagLib user input validation
• "Bean Validation " annotations ( JSR 303 )
or "Hibernate Validators"
48
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Bulk generation
49
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Generator templates
– The generator is based on "Velocity"
– The templates ( ".vm" files ) can be modified in order
to conform with each project requirements
• coding style
• comments
• etc…i.e. :
add/remove
serialization
50
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Entities links
– The foreign keys can be used to generate
links between entities
–Useful for JPA beans generation
51
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Generator project’s variables
– Each project can
define its own
specific variables…
… and use them
in the templates
52
#if ( $bean_jpa == "1" )
$link.jpaAnnotations(4)
#end
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Adding a new specific template
– 1) create the new “.vm” file
– 2) add it in the “templates.cfg” file
–3) use it in the tool …
53
HTML documentation ; ${BEANNAME}.html ; doc/html ; vo_doc_html.vm
vo_doc_html.vm
Title File Folder Template
Demo (cont’d)
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )www.telosys.org
• Using wizards
54
New …
Demo (cont’d)
The end