Tuesday, November 27, 2012

Source-code of a JEE project with Java, Groovy and TomEE

Attention: This is old stuff. New build instructions here

Lets create our own enterprise application. Together we are going to learn how to implement a JEE application from scratch. We can also use this project as reference for real-world projects we may have. First we define the name of our application. Lets call it photodb.

What is photodb? It is an on-line photo sharing application. The user can upload photos, add comments and tag them. The user can configure the access control for all the tags, making tagged photos public, private (the default) or give access for some of his/her friends. So, what is the difference between picasaweb and this application? Well, I am sure picasaweb is great and has a lot more features then photodb, but you cannot install it in your own machine. Also, you didn't have the pleasure of implementing it!

The project will be implemented with java and groovy. Groovy is a dynamic-typed language that is less verbose than java. It makes very easy to create JavaBeans and it is handy when we need to create JUnit classes with mocked objects. Java is a strong-typed language and is usually more efficient than groovy. The good news is that with Gmaven we can use the best of both worlds in the same project. We will also use javascript in order to create a single-page application, but this is subject of another post.

Make sure you have Java JDK, Maven and Subversion installed in your computer. The source-code of the project is available here. If you use an unix-like OS, you can have the server running by executing a feel simple commands. Windows users may need to extract photodb-day-1.tar.gz (See 7zip) and check the the Makefile content. This file details all you need to do to build the system.

 mkdir -p ~/dev/demo  
 cd ~/dev/demo  
 wget http://people.apache.org/~tveronezi/photodb-day-1.tar.gz  
 tar -zxvf photodb-day-1.tar.gz  
 cd photodb  
 make start-tomee  

The second execution of "make start-tomee" will take a matter of seconds to complete (8 seconds in my computer), but the first execution of it may take some time. It does a check-out and builds the trunk version of the Apache TomEE server; It builds the photodb project; It downloads the last stable version of the server; And then it starts the stable server with our photodb project. If something does not work, you may need to check if all the builds were successful. You can find more information about TomEE/OpenEJB builds here.

You can call "make kill-tomee" to kill the server. "make start-tomee" kills the TomEE server before starting it again. "make clean" kills any running instance of Tomee, removes the server binaries and cleans the photodb directory. Calling "make start-tomee" after "make clean" is the same as calling "make start-tomee" for the first time.

The web layer has two access points: index.html (http://localhost:8080/photodb-web) and the CommandServlet (http://localhost:8080/photodb-web/cmd) .The index page is still empty, but the CommandServlet can be used to execute three commands:

http://localhost:8080/photodb-web/cmd?strParam={cmdName:'CreatePhoto', path: 'myPhoto'}

http://localhost:8080/photodb-web/cmd?strParam={cmdName:'AddComment',comment:'my Comment',photoUid=2}


A new action does not require a new servlet. If we want to call another service, we can create a new class under the "photodb.web.command.impl" package. This class should implement "photodb.web.command.Command" and should have a constructor with no parameter. This way we avoid creating extra servlets and avoid having dependency on any proprietary class of the photodb-service sub-project (See wiki).

The servivice layer has only one access point. The only public API is the ServiceFacade. This interface will have all the methods available to the outside world (See wiki).


  • Photo tags
  • User login
  • User access
  • Web page
  • Upload file Servlet
  • "And something else... I am sure. :O)"

No comments:

Post a Comment