« Indietro

Liferay's Architecture: Service Builder

Company Blogs 3 gennaio 2013 Da Jorge Ferrer Staff

One of Liferay's "secret" ingredients, specially with regards to its architecture, is Service Builder. This is the tool that glues together all of Liferay's layers and that hides the complexities of using Spring or Hibernate under the hood.

Service Builder was originally built when Liferay used EJBs for everything (in fact it's name back then was EJBBuilder). EJBs were actually built around several very sound architectural patterns, but unfortunately the implementation was not as good. One significant pain for developers using EJBs was that it was necessary to write many XML files and Java classes to do even the simplest thing. So Brian Chan, Liferay's founder, who is known for being super efficient, decided to build a tool to do it for him. When Hibernate came around Brian was able to use Service Builder to switch from Entity EJB's to Hibernate literally over a weekend (I was blown away back then). Later on Service Builder also allowed switching from Service EJBs to Spring, gaining a lot of flexibility.

Service Builder has grown a lot since then. I used to be very skeptic with source code generation until I got to know Service Biulder better. It doesn't pretend to be a generic code generation tool, but rather to meet the needs of Liferay's developers (including plugin developers). It is also very opinionated, in the sense that it doesn't offer many options, just the ones we think fit better in Liferay's context. Because of that it might not be a tool for everybody but in exchange it provides a great degree of consistency to anything develped with it and is easier to learn. The general idea is simple, it just takes an XML file as an input (usually called service.xml) and generates the necessary persistance, service layer, web services, ... infrastructure around it. Check Liferay's Developer's Guide for info on how to use it.

One key breakthrough in the evolution of Service Builder actually happened with a contribution (can't remember the name of the contributor, does anyone know?). Previously, all of the code that was going to be generated was included as Strings within Java code and a community member took the time to move all of it to Freemarker templates. That has made the evolution and maintainence of Service Builder so much better that I don't have words to thank him :)

Right now, for every file that Service Builder will generate there is a Freemarker (.ftl) file associated with it. For example, do you want to find out how a *ServiceImpl.java file is generated, you just have to look at service_impl.ftl within com/liferay/portal/tools/servicebuilder/dependencies 

That's it for now, hope you liked the entry and see you soon for the next one.

Commenti Autore Data
keep it coming Jorge! I am just loving this... Bharanidharan Viswanathan 4 gennaio 2013 6.26
Thanks for the tip on the freemarker templates! Everest Liu 14 gennaio 2013 9.23
Another great one, keep it up! Two typos you... Falko Werner 21 gennaio 2013 4.02
Thanks for providing valuable article.. Ashish Ranjan 21 gennaio 2013 5.32
I think the third party tool you were referring... Donald Barnes 16 agosto 2013 11.05
Hi Jorge, about Service Builder, can you... Denis Signoretto 1 ottobre 2013 5.59
Thanks Jorge for your kind words :) Service... Tariq Dweik 12 novembre 2013 16.15

keep it coming Jorge! I am just loving this series.
Inviato il 04/01/13 6.26.
Everest Liu
Thanks for the tip on the freemarker templates!
Inviato il 14/01/13 9.23.
Another great one, keep it up!

Two typos you might want to fix in paragraph three:
- Service Biulder
- develped
Inviato il 21/01/13 4.02.
Thanks for providing valuable article..
Inviato il 21/01/13 5.32.
I think the third party tool you were referring to which made your life easier may have been "Spring Surf" (which was formerly "Alfresco Surf") before Alfresco contributed it to Spring Source.
Inviato il 16/08/13 11.05.
Hi Jorge,

about Service Builder, can you clarify differences between fetch and and find generated methods?

Best Regards, Thanks.
Inviato il 01/10/13 5.59.
Thanks Jorge for your kind words emoticon

Service Builder migration to use Freemarker was done by me

Inviato il 12/11/13 16.15.