« Back

Liferay's Architecture: Service Builder

Company Blogs January 3, 2013 By 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.

Threaded Replies Author Date
keep it coming Jorge! I am just loving this... Bharanidharan Viswanathan January 4, 2013 6:26 AM
Thanks for the tip on the freemarker templates! Everest Liu January 14, 2013 9:23 AM
Another great one, keep it up! Two typos you... Falko Werner January 21, 2013 4:02 AM
Thanks for providing valuable article.. Ashish Ranjan January 21, 2013 5:32 AM
I think the third party tool you were referring... Donald Barnes August 16, 2013 11:05 AM
Hi Jorge, about Service Builder, can you... Denis Signoretto October 1, 2013 5:59 AM
Thanks Jorge for your kind words :) Service... Tariq Dweik November 12, 2013 4:15 PM

keep it coming Jorge! I am just loving this series.
Posted on 1/4/13 6:26 AM.
Everest Liu
Thanks for the tip on the freemarker templates!
Posted on 1/14/13 9:23 AM.
Another great one, keep it up!

Two typos you might want to fix in paragraph three:
- Service Biulder
- develped
Posted on 1/21/13 4:02 AM.
Thanks for providing valuable article..
Posted on 1/21/13 5:32 AM.
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.
Posted on 8/16/13 11:05 AM.
Hi Jorge,

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

Best Regards, Thanks.
Posted on 10/1/13 5:59 AM.
Thanks Jorge for your kind words emoticon

Service Builder migration to use Freemarker was done by me

https://issues.liferay.com/browse/LEP-4274
Posted on 11/12/13 4:15 PM.