Our documentation has been relocated here, the Liferay Developer Network. Please update your bookmarks!

Custom Java Tools in the Script Engine

Users of the Script Engine face several challenges, including debugging and logging challenges. One approach to overcome these challenges is to develop custom Java utilities that can be called from your scripts. These utilities can write to a custom log file or the Liferay log file. You can also place breakpoints in your utility code and step through it using your favorite debugger.

Liferay’s use of Spring and PortletBeanLocatorUtil makes calling these Java utilities from your script easy, regardless of the scripting language you’re using.

Let’s begin by creating a Liferay Hook project. If you’re using Liferay IDE or Liferay Developer Studio, select FileNewLiferay Project. Name the project script-utils and accept the display name generated by the wizard. Be sure to select Hook for the Plugin Type and then select Finish.

13-new-liferay-project.png

Figure 3.1: Creating a new utilities project is easy if you use Liferay IDE or Liferay Developer Studio.

You’re using a Liferay Hook Plugin to deploy your utility, but you’re not using any of the typical hook features. You just need a way to make your code available to the portal and the Hook Plugin is the least obtrusive way to do this. This means you don’t need to add anything to the liferay-hook.xml file. Instead, you’ll begin by adding your utility code.

You’ll be following the Dependency Injection design pattern so begin by creating the interface. Right-click on the docroot/WEB-INF/src folder and select NewInterface. You’ll create your interface in the com.liferay.sample package. Name it ScriptUtil.

13-new-interface.png

Figure 3.2: Create a new Java interface which you’ll implement in the next step.

Next, add two methods to the interface.

package com.liferay.sample;

public interface ScriptUtil {

    public String operationOne(); 

    public String operationTwo(String name); 

}

Next, create the implementation class. Right-click on the docroot/WEB-INF/src folder and select NewClass. Create the class in the com.liferay.sample package and name it ScriptUtilImpl. Be sure to select com.liferay.sample.ScripUtil as the Interface.

13-new-class.png

Figure 3.3: Create a new Java Class that implements the interface you created earlier.

Next, add implementations for the two methods.

package com.liferay.sample;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

public class ScriptUtilImpl implements ScriptUtil {

    @Override
    public String operationOne() { 

        return "Hello out there!"; 
    } 

    @Override
    public String operationTwo(String name) { 

        _log.debug("Inside of Operation Two");

        return "Hello " + name + "!"; 
    }

    private static Log _log = LogFactoryUtil.getLog(ScriptUtilImpl.class); 

}

Liferay makes extensive use of the Spring Framework and you’ll be using it here to inject your implementation class into the application. Spring needs a bean definition which you’ll declare in an XML file named hook-spring.xml. Create a docroot/WEB-INF/src/META-INF directory, create the hook-spring.xml file in this folder, and add the following code to hook-spring.xml:

<?xml version="1.0"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        default-destroy-method="destroy" default-init-method="afterPropertiesSet"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="com.liferay.sample.ScriptUtil" class="com.liferay.sample.ScriptUtilImpl" />
</beans>

Upon deployment, you’ll need the portal to create a BeanLocator for your plugin. The BeanLocator reads the bean definitions you provided. Create a docroot/WEB-INF/web.xml file in your project and add the following code to it:

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <context-param>
                <param-name>portalContextConfigLocation</param-name>
                <param-value>/WEB-INF/classes/META-INF/hook-spring.xml</param-value>
        </context-param>
</web-app>

If your project already contains a docroot/WEB-INF/web.xml file, you can simply add the contents of the <context-param> element inside of the <web-app> element. Save all of the changes you’ve made and deploy the hook. Once the hook has been deployed successfully, the ScriptUtil can be used in your script engine code.

To see the ScriptUtil code in action, navigate back to the Control PanelServer AdministrationScript. Change the script type to Groovy and enter the following script:

myUtil = com.liferay.portal.kernel.bean.PortletBeanLocatorUtil.locate(
    "script-utils-hook", "com.liferay.sample.ScriptUtil")

println(myUtil.operationOne())

println(myUtil.operationTwo("Joe Bloggs"))

Click Execute and you should see the results of your script displayed right under the script console.

0 (0 Votes)
Leveraging the Script Engine in Workflow Previous