Friday, August 27, 2010

Extreme productivity for Primefaces






I am ready to announce the next version of Deltaset - 0.8.2 ( http://www.deltaset.org/ )
Four days after the initial public release, several discussions and strong opinions from the community :) and the integration with the popular Primefaces (www.primefaces.org/) is ready :)

What is Deltaset Project promising ?


A full blown , Primefaces integrated, JSF 2.0 based, ready to run on Google Application Engine ( www.appspot.com ) application in less then 10 minutes.

How ?!


Just type:

mvn archetype:generate -DarchetypeCatalog=http://deltaset.googlecode.com/svn/maven2/

choose deltaset-primefaces-gae-archetype

and the fun begins :)

the live demos are here :

http://deltaset-primefaces.appspot.com/

http://deltaset-demo.appspot.com/

You can read more about the Deltaset project in my previous post :
Extreme productivity with JSF 2.0 and Deltaset

Have fun :)
And I'm definitely waiting for your feedback, questions and proposals ;)

Sunday, August 22, 2010

Extreme productivity with JSF 2.0 and Deltaset






I am going to introduce you to the newly created "Deltaset Project". I have just released the first publicly avaiable version - 0.8.1.
It is the logical 'next step' after my prevoius post : http://dmakariev.blogspot.com/2010/01/jsf-20-with-spring-3-and-spring.html
The number of people, who read it, is quite impressive.. 14,429... If you type "jsf 2.0 spring 3" you can see that same post, and refenreces to it,
occupying the first page of google, bing and yahoo. And definitly , I have not used any SEO techniques :).

I want to say big "THANK YOU GUYS !" to everyone who has appreciated that post; to all the great comments; to all the mails which I've received.

My biggest issue was that the ideas, which I've presented here, were not ready for support, change and mass usage. It was just a showcase of "how a java web application could be developed efficiently".

Now ... after several months of work, few sleepless nights, several small scale presentations, I'm ready to demonstrate the newly released productivity tool:

http://www.deltaset.org

The project is licensed under GPLv3, it is hosted on Google Hosting.
You can see the new demo at http://deltaset-demo.appspot.com

Quick Start


Deltaset requires you to download and install:
  1. Create project :
    mvn archetype:generate -DarchetypeCatalog=http://deltaset.googlecode.com/svn/maven2/
    you should choose number (1) - the 'deltaset-gae-archetype'.

  2. Add domain object (you should be in the base directory of the application - where the pom.xml was created):
    mvn -Pdeltaset -Dcmd=crud=Person:[firstName,lastName,birthDate:[@type:date]]
  3. Build :
    mvn package
  4. Run:
    dev_appserver target\YOUR_APP_NAME

Now... Let's create a small Pet Clinic :

  1. Open the script.ds file from the base directory of your project.
    At the end of the file you will see the model :

    enum=PetType:[cat,dog,fish]
    crud=Owner:[name,pets:[@type:OneToMany<Pet>,@mappedBy:owner]]
    crud=Pet:[name,type:Enum<PetType>,owner:ManyToOne<Owner>]
    enum=VetType:[dogvet,catvet,fishvet]
    crud=Vet:[name,speciality:Enum<VetType>]
    crud=Visit:[day:[@type:date],notes,vet:ManyToOne<Vet>,pet:ManyToOne<Pet>]

  2. Change it.. as you like :)
  3. Generate :
    mvn -Pdeltaset -Dcmd=file=script.ds
  4. Build and run.

What happened till now ?



First.. just few minutes have passed..

A brand new, full blown, java web application was created.

But what exactly was created?

During the 'archetype:generate' I have used "org.deltaset.demo" for groupId and package,
and "demo" for artifactId.

Here is the directory structure of the application:
+---src/main/java/org/deltaset/demo
+---main
¦ +---java
¦ ¦ +---org
¦ ¦ +---deltaset
¦ ¦ +---demo
¦ ¦ +---aspect
¦ ¦ +---config
¦ ¦ +---converter
¦ ¦ +---domain
¦ ¦ +---persistence
¦ ¦ +---repository
¦ ¦ +---resource
¦ ¦ +---user
¦ ¦ +---web
¦ +---resources
¦ ¦ +---META-INF
¦ ¦ +---org
¦ ¦ +---deltaset
¦ ¦ +---demo
¦ +---script
¦ ¦ +---templates
¦ ¦ +---mapped
¦ ¦ +---test
¦ +---webapp
¦ +---resources
¦ ¦ +---app
¦ ¦ +---css
¦ ¦ ¦ +---img
¦ ¦ +---internal
¦ +---secured
¦ ¦ +---person
¦ +---WEB-INF
+---test
+---resources
Let me give you brief explanation of what is contained in the folders :
for simplicity I will use
JAVA_FOLDER instead of src/main/java/org/deltaset/demo
WEB_FOLDER instead of src/main/webapp

JAVA_FOLDER/aspect - your aspectJ classes should go here. Currently there are 2 simple java classes, using the @Aspect syntax. The aspects could be used for crosscutting concepts like logging, performance measuring, etc.. or just ignored :)
JAVA_FOLDER/config - your Spring @Configuration java classes.
JAVA_FOLDER/converter - here would be generated the JAXB domain to xml or json converter classes / acctually delegating decorators :) /
JAVA_FOLDER/domain - your generated domain classes (enum and JPA @Entity classes )
JAVA_FOLDER/persistence - here is the AppEngineEntityManagerFactory SINGELTON.... implemented as recomended by Google Application Engine documentation,
and also the UnonwedLoader helper static class... needed for handling some of the relations in the entities.
JAVA_FOLDER/repository - your generated Repository classes ( @Repository form Spring)- / also known as DAOs :) /, they are defining the transaction boundaries (@Transactional from Spring) and the security access (@RolesAllowed - javax.annotation.security.RolesAllowed )to the domain objects.
JAVA_FOLDER/resource - your generated RESTfull endpoints
JAVA_FOLDER/user - this package is responcible for Login and Preferences handling... you will have to be chang it, according to your requirements.
JAVA_FOLDER/web - your generated JSF @ManagedBean classes ( using the javax.annotation.ManagedBean and injecting dependencies with @Inject)

src/main/resources - the standard Maven resources folder: persistence.xml, properties files, etc
src/main/script - here are the groovy and template files needed by the generator. The 'starting' point is the 'CommandProcessor.groovy'

WEB_FOLDER/resources - as defined by the JSF 2.0 spec. In this folder is the 'app' custom component jsf library.
interesting files are page.xhtml, internal/layout-two-column.xhtml and the menu.xhtml. You will have to customize them to fit your requirements.
WEB_FOLDER/secured - here would be generated more folders, with names created from the names of your domain classes.
Each folder will contain the needed xhtml files. For a real application you will spend some time in that folders during customizing the UI :)


This should be 'just enough description' to give you a quick start introduction for creating your own applications with "Deltaset"

What about your own archetype ?


I will make some copy/paste from the start page of http://www.deltaset.org :
You can easy create your own specific productivity environment, based on the project initially created with DeltaSet.
Just execute :
mvn install -Darchetype=true
If you want to use your newly created archetype.Then... type:
mvn archetype:generate -DarchetypeCatalog=local

Plain JSF 2.0 and Expression Language 2.2 on Google Application Engine


There might be some of you, who just want to use the JSF 2.0 / Expression Language 2.2 integration with Google Application Engine.

So... Hey.. you don't have to tear apart the 'deltaset-gae-archetype' folders.
just use number (2) from the archetype Catalog : 'hello-jsf-el-2-gae-archetype'

mvn archetype:generate -DarchetypeCatalog=http://deltaset.googlecode.com/svn/maven2/

Deltaset Technology highlights


  • JSF 2.0 with Expression Language 2.2
  • JSR 303: Bean Validation - using Hibernate Validator
  • JSR 330: Dependency Injection for Java - using Spring 3.x
  • integrated with Spring Security 3.x
  • JSR 311: JAX-RS 1.1 for building RESTful Web services - using Jersey 1.3
  • JAXB 2.1 - using https://jaxb.dev.java.net/
  • JPA 1.0 - using DataNucleus for Google Application Engine
  • Aspect Oriented Programming - using AspectJ
  • JPA 2.0 - using EclipseLink (under development...)
  • JQuery 1.4.2
  • JQuery UI 1.8.4

Deployment environments

  • Google Application Engine
  • Jetty/Tomcat - Servlet 2.5 (under developmet...)
  • Glassfish (under development...)

What's next ?

Hopefully soon I will cover the following topics:

  • JSF 2.0 and Expression Language 2.2 on Google Application - how was that made possible ?
  • Request only CRUD for JSF 2.0
  • Using JSF 2.0 templates as Lego Blocks
  • Modular applications with JSF 2.0 (no OSGi)
  • Multitenant applications with Deltaset
  • Code generation with Deltaset (how-to create your own productivity tool)
  • Deltaset Faces - why is it a cool custom component set ?
  • JSF 2.0 and HTML 5 in the context of Deltaset (and not only)
  • JSF 2.0 and Spring Security
  • Deltaset on Jetty/Tomcat/Glassfish

Was the project inspired by Spring Roo?

Well.. not at all :)

The initial idea was implemented in the end of 2007, it was based on JSF 1.2 and Richfaces, but never made it to the public (mainly due to lack of time.. ). It was used just in few proprietery projects.

During my "small scale" demonstrations, I was asked several times questions like:

"is there any relation to Spring Roo", "what is the difference with Spring Roo", "why don't you just create a jsf add-on for Spring Roo".

I've answered to them, and I want to answer in front of all of you, who might be thinking about them now.

The only relation with Spring Roo is that the both projects are trying to solve the issue with java productivity. And basically thats almost all in common :)

The aproaches are completely different. Spring Roo uses AspectJ inter-type declarations (ITDs) as a code generation technique.
Deltaset uses plain Groovy templates.

Spring Roo is 'roundtrip' generation
Deltaset is one-way.

Spring Roo tryes to manage your generated code 'invisibly' - it is trying to stay out of your way, and still to controll the artifacts, you don't want to manage by hand.
Deltaset just creates the needed files, and leaves everything else to you :)

Spring Roo claims that you will '100% develop in Java'. Here I disagree.. ITDs are definitly a brilliant code-generation architecture, but this is not 100% Java, this is AspectJ :),
If you want to use Spring Roo, you must use either Eclipse with AspectJ support , or SpringSource Tool Suite. They are great IDEs, but .. I like Netbeans :).
Also.. try to explain AspectJ and ITDs to junior level developers... or to anyone who doesn't have experience with AspectJ.. well it is not that easy.
Deltaset generates standard java files..

Spring Roo generates 'Rich Domain Objects'.Nice.. but.. try using method level security :).. not trivial, at least until the Spring Roo guys upgrade to Spring Security 3.0.3.RELEASE - https://jira.springframework.org/browse/SEC-1232 /
Deltaset generates plain old 3-tier architecture. It generates method-level security by default:)

Spring Roo uses Spring MVC and Google Web Toolkit
Deltaset uses JSF 2.0

So .. what is that comparasion showing.. : Deltaset is not copy of Spring Roo,

and by the way Spring Roo is Great Technology! ...quite polished :) I really like it !

As final words.. I hope that you, dear reader, have enjoyed that post... And even more... That you are conviced, you have to try the 'Deltaset way'.

Have fun :)
And I'm definitly waiting for your feedback, questions and proposals ;)

Monday, January 25, 2010

JSF 2.0 with Spring 3 and Spring Security 3 on Google Application Engine




In this post I'm going to show a simple fully integrated CRUD style application working on Google Application Engine.

The technologies used are :
  • JSF 2.0 - Mojarra 2.0.2
  • Spring 3.0.0.RELEASE
  • Spring Security 3.0.0.RELEASE
  • Hibernate Validator 4.0.2.GA as reference implementation of JSR 303: Bean Validation
  • AspectJ 1.6.7
  • Groovy and the gmaven-plugin

You can see a live demo at : http://sandcode.appspot.com
UPDATE(22 August 2010)
You can compare with : http://deltaset-demo.appspot.com
and see the project created based on the ideas in that post : http://www.deltaset.org


The application is build with Maven 2.2.1
For local execution you would need Google App Engine SDK 1.3.0 1.3.1

The archetype was updated on 11 February 2010, so that works for latest Google App Engine - 1.3.1

If you want to play and customize it you would need to have installed and configured Java, Maven and Google App Engine SDK

Choose gae-archetype after running :

mvn archetype:generate -DarchetypeCatalog=http://sandcode.googlecode.com/svn/maven2/


build the application :

mvn install


run it :

dev_appserver target/YOUR_ARTIFACT_ID


type in your browser: http://localhost:8080

enjoy :)

If you want to extend the application you can use the small generation utility coming with it :

mvn -Pcrud -Ddomain=Book -Dfields=title,author,remarks


The generator will create for you :

src/main/java/YOUR_EXAMPLE_PACKAGE_NAME/app/domain/Book.java
src/main/java/YOUR_EXAMPLE_PACKAGE_NAME/app/repository/BookRepository.java
src/main/java/YOUR_EXAMPLE_PACKAGE_NAME/app/web/BookController.java
src/main/webapp/secured/book/list.xhtml
src/main/webapp/secured/book/detail.xhtml
src/main/webapp/secured/book/form.xhtml


It will also extend the menu :

src/main/webapp/resources/app/menu.xhtml

with :

<app:menu-group groupname="GEN ADDED - Book ">
<app:menu-item itemoutcome="/secured/book/list" itemvalue="#{msg['book.view.list']}">
<app:menu-item itemoutcome="/secured/book/form" itemvalue="#{msg['book.view.form']}">
</app:menu-item>


Extend the application default Messages bundle :

src/main/resources/Messages.properties

with:

#labels generated for Book
book.view.detail=GEN Book detail
book.view.form=GEN Book form
book.view.list=GEN Book list
book.title=GEN Title
book.author=GEN Author
book.remarks=GEN Remarks


This was the 2 min intro.. depending on your internet connection you can have a nicely running GAE application for about 2-5 min :)

If you want to deploy it on your account of Google Application Engine you will have to edit by hand :

src/main/webapp/WEB-INF/appengine-web.xml

and then type :

appcfg update target/YOUR_ARTIFACT_ID


And... Some more... :

The application is having the following features :
layerd architecture - domain, service, controller
'request' based crud for JSF
localized - currently supports English and Bulgarian. To add another language you need to :
1. create a translated copy of Message.properties
2. edit faces-config.xml
3. download from http://www.famfamfam.com/lab/icons/flags/ the correct flag ;) and add it to src/main/webapp/resources/img/flag with the right name.
4. create a translated copy of ValidationMessages.properties (needed by Bean Validation)

Basic UI customization - the user can change the preffered language, the position of the menu and the colors of the application.
Using of JSF 2.0 custom components, templating.
Using of "JSR 303: Bean Validation".
Using of "JSR 330: Dependency Injection for Java" annotations with Spring 3 ( javax.annotation.ManagedBean , @Inject, @Named ).
Using of Java Config style for Spring 3 ( @Configuration ).
Using of @Bean annotation with inplace implementation of an interface, directly in the @Configuration bean.
Using of @RolesAllowed (javax.annotation.security.RolesAllowed) with Spring Security 3 for securing your application.
Using of 2 distinct roles ROLE_USER and ROLE_ADMIN, securing of methods called with h:commandLink (or h:commandButton)
'Native' integration between Spring Security and JSF : app/web/LoginBean.java ; src/main/webapp/login.xhtml
Almost NO xml configuration for spring : src/main/webapp/WEB-INF/applicationContext.xml
AspectJ for logging app/aspect/TimeExecutionLoggingAspect.java (well.. by default it is swithced off, but you can take a look in the pom.xml )
AspectJ and the Spring stereotype annotations (Repository, Service, etc..) can help you easy log only what you need : app/aspect/SystemArchitecture.java
AspectJ for static weaving of 3rd party jar files (in our case this is jsf-impl , because of a bug in the local Google App SDK ) : common/aspect/FixWebConfigurationAspect.java
Groovy gmaven-plugin and Groovy Templates for generation of files..
Using of box-shadow and border-radius css properties for IE : src/main/webapp/resources/css/gray.css

Enjoy :)
And I'm definitly waiting for your feedback, questions and proposals ;)

Friday, December 25, 2009

JSF 2.0 with Maven 2 plugins for Glassfish v3, Jetty and Tomcat





In this post I'm going to demonstrate how JSF 2.0 "Hello World" application could be developed with the Maven 2 plugins for Jetty, Tomcat and Glassfish v3. The application consists of two pages and request scoped managed bean. The first page ( hello.xhtml ) asks the user "What's your name?", validates the input , then the second page ( response.xhtml ) greets the user with "Hi". The two pages share common layout defined in template.xhtml and request scoped managed bean HelloWorld.java exposed under name "hi". The titles of the pages are set with UL expression #{hi.greetFrom('greeting passed as parameter')}. The template loads its css and also one image file with the new ResourceHandler API.

Prerequisites:
* Basic knowladge of Java and Maven 2.
* Installed Maven 2 ( http://maven.apache.org ). The sample was tested with version 2.2.1

Quick start:
The source can be downloaded from here.

or could be checkout with subversion from:
http://sandcode.googlecode.com/svn/trunk/jsf2/helloworld

Run the sample with one of the following commands :

* mvn package tomcat:run-war
* mvn package jetty:run-war
* mvn package -Pglassfish


In your browser type the URL : http://localhost:8080/helloworld


Some details :

In the pom.xml file there are defined two profiles.


1. The first profile <id>default</id> is active by default and is needed for running the application with servlet container like tomcat or jetty.

The needed dependencies are :

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>[2.0.1,)</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>[2.0.1,)</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>


The Tomcat plugin is defined by :

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<path>/${project.build.finalName}</path>
<!-- Embedded port -->
<port>8080</port>
</configuration>
</plugin>

Details about the Tomcat plugin you can find at :
http://mojo.codehaus.org/tomcat-maven-plugin/index.html


The Jetty plugin is defined by :

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<!-- Delete this block to have Jetty run default port (8080) -->
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
</connector>
</connectors>
<!-- force friendly name instead of artifact name + version -->
<contextPath>${project.build.finalName}</contextPath>
<!-- This parameter will auto-deploy modified classes. -->
<!-- You can save changes in a file or class and refresh your browser to view the changes. -->
<scanIntervalSeconds>3</scanIntervalSeconds>
</configuration>
</plugin>

Details about the Jetty plugin you can find at :
http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin
http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin

2. The second profile <id>glassfish</id> is needed for running the application with JEE compatible server. In our case I'm using embedded Glassfish v3.
The only needed dependency is :


<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>


And the GLassfish plugin is defined by :


<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.0</version>
<configuration>
<app>${project.build.directory}/${build.finalName}.war</app>
<port>8080</port>
<contextRoot>${build.finalName}</contextRoot>
<autoDelete>true</autoDelete>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>


that plugin is binded to the 'package' phase of maven for convenience. It could be executed also with :

mvn -Pglassfish embedded-glassfish:run

Details about the glassfish plugin you can find at :

http://docs.sun.com/app/docs/doc/821-1208/gijhs?a=view
http://docs.sun.com/app/docs/coll/1343.9 - section "Sun GlassFish Enterprise Server v3 Embedded Server Guide"


Interesing part of the web.xml is the following context parameter :


<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>


It is needed by JSF when running under tomcat or jetty, so that faces could load the UL v2.2, which is needed for passing parameters in the EL expressions in the xhtmls.
Also when running under plain servlet container the following dependency is needed:


<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
<scope>runtime</scope>
</dependency>

Example of passing parameters (from hello.xhtml):


<ui:define name="top">
#{hi.greetFrom('initial page')}
</ui:define>


Where 'greetFrom' method is defined in HelloWorld.java :


@ManagedBean(name = "hi")
@RequestScoped
public class HelloWorld {
...

public String greetFrom(String pageName) {
return "Hello World ! ( " + pageName + " )";
}

...
}


In JSF 2.0 one of the most powerful features is the PDL (Page Description Language), and especially the possibility for encapsulating common fragmets of a page in 'templates'.

In the sample you can see the usage of the template.xhtml :


<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="#{resource['css/default.css']}" rel="stylesheet" type="text/css" />
<link href="#{resource['css/cssLayout.css']}" rel="stylesheet" type="text/css" />
<title>Hello World (facelets template)</title>
</h:head>

<h:body>

<div id="top" class="top">
<div class="left"><h:graphicImage url="#{resource['delta.png']}" /></div>
<div class="right"><h2><ui:insert name="top">Top</ui:insert></h2></div>
<div style="clear:both"></div>
</div>

<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>

</h:body>

</html>


in hello.xhtml :


<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="./template.xhtml">

<ui:define name="top">
#{hi.greetFrom('initial page')}
</ui:define>

<ui:define name="content">
<h2>What's your name?</h2>
<h:form id="helloForm" >
<h:inputText id="username" value="#{hi.name}" required="true"/>

<h:commandButton id="submit" action="response" value="Submit"/><br/>
<h:message for="username" styleClass="error"/>
</h:form>
</ui:define>

</ui:composition>


and in response.xhtml:


<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
template="./template.xhtml">

<ui:define name="top">
#{hi.greetFrom('response page')}
</ui:define>

<ui:define name="content">
<h:form id="responseform">
<h2>Hi, #{hi.name} !</h2>
<h:commandButton id="back" value="Back" action="hello" />
</h:form>
</ui:define>

</ui:composition>


More about the new features of JSF 2.0 you can read here :

http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/