Combination View Flat View Tree View
Threads [ Previous | Next ]
Eric Le Bail
Small changes in ServiceBuilder to allow Customization using inheritance
May 29, 2012 1:20 PM
Answer

Eric Le Bail

Rank: New Member

Posts: 5

Join Date: April 3, 2012

Recent Posts

Hello,

I have a suggestion on ServiceBuilder.

I suggest we do the folowing small changes into the Service Builder class:

In all the the methods that generate the content of the file using the code:
"String content = _processTemplate( ..."
I suggest we exctract a sub methods with "protected" visibility.

Here an exemple :

Original method (from 6.1CE version)
 1private void _createPersistenceImpl(Entity entity) throws Exception {
 2        Map<String, Object> context = _getContext();
 3
 4        context.put("entity", entity);
 5        context.put(
 6            "referenceList", _mergeReferenceList(entity.getReferenceList()));
 7
 8        // Content
 9
10        Logger.selectLoggerLibrary(Logger.LIBRARY_NONE);
11
12        [b]String content = _processTemplate(_tplPersistenceImpl, context);
13[/b]
14        Logger.selectLoggerLibrary(Logger.LIBRARY_AUTO);
15
16        // Write file
17
18        File ejbFile = new File(
19            _outputPath + "/service/persistence/" + entity.getName() +
20                "PersistenceImpl.java");
21
22        writeFile(ejbFile, content, _author);
23    }


This method becomes:
 1private void _createPersistenceImpl(Entity entity) throws Exception {
 2        Map<String, Object> context = _getContext();
 3
 4        context.put("entity", entity);
 5        context.put(
 6            "referenceList", _mergeReferenceList(entity.getReferenceList()));
 7
 8        // Content
 9
10        Logger.selectLoggerLibrary(Logger.LIBRARY_NONE);
11
12        [b]String content = generatePersistenceImplContent(context);[/b]
13
14        Logger.selectLoggerLibrary(Logger.LIBRARY_AUTO);
15
16        // Write file
17
18        File ejbFile = new File(
19            _outputPath + "/service/persistence/" + entity.getName() +
20                "PersistenceImpl.java");
21
22        writeFile(ejbFile, content, _author);
23    }
24
25    /**
26     * This methods generates the entity.getName()+"PersistenceImpl.java" file content
27     * In oder to customize the generated file please redefine this methods.
28     *
29     * @param context
30     * @return String
31     * @throws Exception
32     */
33    protected String generatePersistenceImplContent(Map<String, Object> context)
34            throws Exception {
35        String content = _processTemplate(_tplPersistenceImpl, context);
36        return content;
37    }


By doing this change, we allow developpers to do something like:
 1public class CustomServiceBuilder extends ServiceBuilder {
 2
 3   
 4    /**
 5     * This methods generates the entity.getName()+"PersistenceImpl.java" file content
 6     * In oder to customize the generated file please redefine this methods.
 7     *
 8     * @param context
 9     * @return String
10     * @throws Exception
11     */
12    protected String generatePersistenceImplContent(Map<String, Object> context)
13            throws Exception {
14       
15                String content = super.generatePersistenceImplContent(context);
16        // I add here my custom content for this file.
17        content = content + _processTemplate(_customtplPersistenceImpl, context);
18        //
19        return content;
20    }
21
22}


Regards,
Eric
David H Nebinger
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 29, 2012 1:25 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6274

Join Date: September 1, 2006

Recent Posts

What benefit does this actually provide?

I mean, all of the generated code comes from the freemarker template files anyway, so why do you need to extend SB in this way that you can't do directly in the freemarker template?
Eric Le Bail
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 1:40 AM
Answer

Eric Le Bail

Rank: New Member

Posts: 5

Join Date: April 3, 2012

Recent Posts

Hello,

The benefits this provides:
- Have the possibility to create our own Freemarker templates (with our custom code) and make the service builder execute them.
- Avoid modifying the Liferay's freemarker templates
to avoid having to re-do the customization with each new liferay's version.
to respect the liferay's code during the customization process.

The missing part of my example is:
I need to update the "build-service" ANT task to execute the CustomServiceBuilder class instead of the standard ServiceBuilder.
I'm working on that.

Regards,
Eric
David H Nebinger
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 5:37 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6274

Join Date: September 1, 2006

Recent Posts

Hmm, you can already override the templates. Some are overridable via command line arguments, but all of them can be overridden using system properties. This way you can use your own templates already and not affect Liferay's templates at all, and it also doesn't take an extension class and/or modification to the build process to get this done...

In general I have no argument w/ extending and enhancing service builder, but at first glance your suggestion seemed to imply you were not aware that the functionality already exists.
David H Nebinger
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 5:40 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6274

Join Date: September 1, 2006

Recent Posts

Oh, and BTW, the trick to remember is that the build process puts the project's WEB-INF/lib jars at the front of the classpath when invoking SB...

The project being the containing project that has the service.xml file that is to be processed...
Eric Le Bail
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 11:04 AM
Answer

Eric Le Bail

Rank: New Member

Posts: 5

Join Date: April 3, 2012

Recent Posts

David H Nebinger:
Oh, and BTW, the trick to remember is that the build process puts the project's WEB-INF/lib jars at the front of the classpath when invoking SB...

The project being the containing project that has the service.xml file that is to be processed...


If I undestand you well:
I create my own version of an existing Freemarker template with the same name and same Package.
I create a jar with it.
I put this jar into the "lib" dir of my portlet projet
I launch the build-service task.
And my updated template will be processed instead of the original one.

I'm right ?

Regards,
Eric
David H Nebinger
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 12:02 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6274

Join Date: September 1, 2006

Recent Posts

Eric Le Bail:
I'm right ?


Yep, that should do it. It doesn't have to be a jar, though. You should just be able to replicate the com.liferay.portal.tools.servicebuilder.dependencies package in the WEB-INF/classes directory and drop your templates in there, too.
Eric Le Bail
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 11:01 AM
Answer

Eric Le Bail

Rank: New Member

Posts: 5

Join Date: April 3, 2012

Recent Posts

David H Nebinger:
Hmm, you can already override the templates. Some are overridable via command line arguments, but all of them can be overridden using system properties. This way you can use your own templates already and not affect Liferay's templates at all, and it also doesn't take an extension class and/or modification to the build process to get this done...

In general I have no argument w/ extending and enhancing service builder, but at first glance your suggestion seemed to imply you were not aware that the functionality already exists.


Hello david,

Many thanks for your reply.

You right I was not aware of that.
I'll go to look for that.

My point is more to be able to extend the existing template than to override them.
But the way you propose is ok for me.

Regards,
Eric
David H Nebinger
RE: Small changes in ServiceBuilder to allow Customization using inheritanc
May 30, 2012 12:04 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6274

Join Date: September 1, 2006

Recent Posts

Eric Le Bail:
My point is more to be able to extend the existing template than to override them.


I don't believe freemarker supports template inheritance. I think you'd have to use a different template file, a copy of the original w/ your changes added.

But then all you should have to do is use the command line argument (for those that are supported) or an environment property to have SB use your template in place of the original.