Upload
laurent-guerin
View
1.255
Download
4
Embed Size (px)
DESCRIPTION
Devoxx 2011 Conference
Citation preview
A pragmatic scaffolding approach
to build Java Web Applications
Laurent Guérin
Telosys Project Leader
Sogeti & OW2 Consortium
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://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
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Introduction
OUTLINE :
- Code generation
- Telosys approach
- The framework
- The tools (demo)
3
About code generation …
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Model Driven
• "Model-First" approach ( MDD, MDA, … )
• Code generation from an existing model
• "Model Driven" => You need a model !
• Model => substantial initial investment
• Process : keep the model up to date !
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
5
Split !
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Scaffolding
• "Database-first" approach
• Very efficient
• No customization between the database
metadata and the generator
• Not enough information …
– Java field name
– Java field type
– Validation rules : min, max, regexp, …
– Boolean storage values ( 1/0, "T"/"F", … )
– etc …
• Cannot store/reuse further information
Database
TOOL
Code
Metadata
Template
6
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Wizards
• Code generation without model / DB
• IDE tool => Needs user inputs
• Can also use existing artefacts
as input
– Java class Java class
– WSDL file Java classes
• Efficient only for a few kinds of
targets
• Can only be considered as
"development accelerators"
Code
Workspacefiles
I.D.E.
“Java Model”org.eclipse.jdt.core
7
Bulkgeneration
The Telosys approach
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Telosys approach
• What we want …
– to be able to generate code quickly for entities
management (like scaffolding) with a simple tool
– to add and keep specific information about entities
=> we need a repository to store them (kind of model)
• What we don’t want …
– to spend a lot of time to build a model
– to input further information for each generation
• What we must have …
– a database (“database first”)
– an IDE (Eclipse) that is the developer’s daily tool 9
+DB
Metadata
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Telosys approach
• The 3 steps …
Database
Repository
generator
Metadata
Repository(Lightweight
model)
Code
Code
generator
Template
generates
updates
modify & add
information
Use the
“Database-first”
approach to build
a “lightweight model”
Customize the
“lightweight model”
Generate
the code
1 2 3
updates customizes
10
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The tripod of success
• The framework is self-sufficient to build a
complete Web application
( "global" / "full stack" framework )
• The tools are provided as Eclipse plugins in
order to make the programmer's life easier
( agile )Method
Framework Tools
( code generation ) ( global & simple )
What a
development
project needs…
11
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Project’s philosophy
• “Keep it simple and stupid”
( KISP ? )
– lightweight framework
– small tools (easy to install & to use)
• “Conform with the standards”
– servlet/JSP/JSPX, JDBC, JPA, http, XML, …
• “Remain open”
– Open Source (hosted by OW2 consortium)
– Open Format (lightweight model = XML)
– Customizable templates different targets :
Telosys fwk + other Java Frameworks, Flex, PHP, … 12
The Telosys framework
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The framework
• Global framework “all in one“
• Only one jar (+ JDBC) => no "jar hell" !
• Designed for "data centric" web applications
• Light (just needs a servlet container and a
database) & based on simple concepts14
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The "global effect"
• “Global” means … less “glue”, less objects (less memory footprint), less configuration, …
=> Coherence & Efficiency
• “Modular” (modules usable separately)
PersistencePresentation Application
ScreenMaps
Services
ScreenManagers
Business
DAO
Java Beans ( POJO )
XML conv
Authenti-cation
Commons, logging, I18N, …
AJAXJSP + taglib
FrameworkJavascript
SQL
Services
Natively AJAX
One framework that covers all the application layers
"VO"
Very simple objects( "Pure POJO" )
Thin persistence layer
15
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The "client-server" paradigm
• Once the screen is loaded in the browser, it
converses with the server via AJAX requests
( XML / http )
Client-sideScreen
Manager
ServiceJavaS
cri
pt
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
Action
Serv
ice
http
16
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
DataSet
VO List
VO Bean
The "Data Objects"
• VO Bean (POJO = “entity record”)
• VO List (selectable/sortable list of POJO)
• DataSet (grid of objects)
DatabaseData
Access
LayerXML
Con-
verters
ParsersFiles
XML
< … >
</ … >
Book
.java
BookList
.java
BookConv
.java
BookDAO
.java
Book
Table
Code
generator
17
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The "Data Access Layer"
• Why another persistence framework ?
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, …
Both are useful,
You have the choice …
18
Java objects
rule the persistence
DB
new new
DB
Database records
rule the persistence
new legacy
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
"Record Persistence" examples
EmployeeDAO dao = getDAO();
Employee employee = new Employee();
employee.setCode( "123" ); // primary key
int r = dao.load(employee, db);
if ( r > 0 ) { // if found
// update
employee.setFirstName("Bill");
dao.update(employee, db);
}
else {
// create
employee.setLastName("Wayne");
employee.setFirstName("John");
employee.setEmail("[email protected]");
dao.insert(employee, db);
}
if (dao.exists(employee,db)){...}
dao.delete(employee, db);
The developer decides…
. What to do
. How to do it
. When to do it
db.commit();
db.rollback();
A "record" = a bean (POJO)
Each bean is managed by a dedicated DAO
EmployeeList list = new EmployeeList();
ListQuery query = dao.createQuery(…);
int count = dao.count(query, db);
dao.loadList(query, list, db);
dao.updateList(list, db);
19
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Links or no links ?
• You have the choice …
• Links can be generated if an “object graph
approach” is chosen (ie with JPA persistence)
• With a “record approach” : no need to manage
links between beans, the framework uses
“aggregations” to assemble required beans
Aggregation
(composite)
20
@ManyToOne
@OneToMany
@ManyToMany
@OneToOne
Code
generator
DAO
Code
generator
Bean
Bean
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The screens
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
21
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Screen view example
JSP or JSPX, but no HTML !
22
<%@ 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 >
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Conventions
• "Framework" => "Frame" !
• "CoC" ( Conventions over Configuration )
– The developers know where the files are
– The tools know where to generate files
• Example : 1 Java package 1 web folder
1 screen = +
23
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
What is a Telosys project ?
• Just a standard Java Web Application project
• It can be organized for Maven, or not…
• In Eclipse :
1) Create a new "Web Dynamic project"
2) Import the "Telosys Starter Kit“24
That’s all !
The project is ready
for development and
code generation
src (server side)
TelosysTools (+)
WebContent (client side)
deployment
.war
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo
• How to create a Telosys project …
1) Create a Dynamic
Web Project 2) Import the
Telosys starter kit
25
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Test and initialize the Telosys project …
3) Test the
project
( run it )
4) Define the
conventions
26
The Telosys Tools
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The big picture
Template
.vm
Repository.dbrep
( XML )
Velocity
Context
Repository
generator
Code generator
28
WizardsEditors Properties
.jsp / .jspx
.js
.java
other …
Database
Meta-data
Everything can be
done in Eclipse
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
The demo
• Connection to the database
• Repository generation
• Repository customization
• Basic objects generation (Bean, List, DAO, Conv.)
• Screens generation (server side & client side)
• Template customization
• Links management
• Beans generation for JPA & Validators
• New specific template
29
See demo screenshots at
the end of this presentation
Conclusion
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Conclusion
31
Database Repository Tools Initial code
and the project is
on the rails…
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
In the future …
• Main goals in the 2012 roadmap
– Repository
• Extendable/unlimited information for
each entity and each Java field
– Screens design
• Official release of the Screen Builder
plugin (WYSIWYG)
– Java Portals
• JSR-168 portlet to embed
screen maps in a portal page
– NoSQL
• DAO for GAE/BigTable & Cassandra
32
Still a lot
of work to
improve
the rails …
Questions ?
Appendix
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Framework features
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
35
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Telosys Screen Builder
36
A visual tool to design the Telosys Screens
( beta version )
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Follow the project …
• 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
37
The demo…
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Repository
Generator
Demo
• Use the database meta-data to generate the
initial repository
Database
Repository.dbrep
( XML )
Generationrules
39
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• The generated repository is editable
with an Eclipse plugin editor
O/R Mapping
Database Java Attributes
Java Classes
Database
updatable
40
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Java fields :
– Each Java field can have additional information
– Useful for
• JSP/TagLib user input validation
• "Bean Validation " annotations ( JSR 303 )
or "Hibernate Validators"
41
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Bulk generation
42
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• 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
43
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Entities links
– The foreign keys can be used to generate
links between entities
–Useful for JPA beans generation
44
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Generator project’s variables
– Each project can
define its own
specific variables…
… and use them
in the templates
45
#if ( $bean_jpa == "1" )
$link.jpaAnnotations(4)
#end
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• 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 …
46
HTML documentation ; ${BEANNAME}.html ; doc/html ; vo_doc_html.vm
vo_doc_html.vm
Title File Folder Template
Devoxx 2011 – Laurent Guérin – JAVA scaffolding with Telosys ( http://www.telosys.org )
Demo (cont’d)
• Using wizards
47
New …
The end