"Access to Liferay services in Velocity
Tags: 5.1.0 customization journal template service locator velocity
Table of Contents
* 1 FindService
* 2 FindExceptionSafeService
* 3 Service Locator doesn't work for me???
* 4 Related Articles
In your themes you sometimes want to invoke Liferay services. Liferay provides a utility to obtain references to services; $serviceLocator.
FindService
For example, suppose you need layoutLocalService:
#set($layoutLocalService = $serviceLocator.findService('com.liferay.portal.service.LayoutLocalService'))
#set($xyz = $layoutLocalService.getFriendlyURLLayout($layout.getOwnerId(), '/home'))
The services obtained from the findService method are the raw services and sometimes they can be difficult to work with because Velocity does not provide an exception handling mechanism.
FindExceptionSafeService
To handle this issue, more recent versions of Liferay (5.1.0+) provide a second method, findExceptionSafeService which wraps the underlying service with a proxy which catches any exceptions and simply returns null.
For example, suppose you want to see if some userId refers to an actual user:
#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))
The following method would typically crash the VM being processed, because it throws a NoSuchUserException if the user cannot be located.
#set($user = $userLocalService.getUserById(
$getterUtil.getLong("12345"))
Service Locator doesn't work for me???
ServiceLocator is a feature which unlocks a lot of power. This power can inadvertently fall into the wrong hands if for example Journal Template creation is granted to less trusted users. Therefore to prevent abuse, or simply to protect less aware portal admins, the feature is disabled by default for all Journal VM contexts.
To enable it, edit the value of journal.template.velocity.restricted.variables in portal.properties.
No comments:
Post a Comment