将插件部署到Liferay时如何执行某些操作? [英] How can I run some action when my plugin was deployed to Liferay?

查看:69
本文介绍了将插件部署到Liferay时如何执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 plugin ( portlet hook theme )启动后立即运行一些代码在Liferay中可用.

I want to run some code as soon as my plugin (portlet, hook or theme) is available in Liferay.

liferay-plugin-package.xmlliferay-portlet.xmlliferay-hook.xmlliferay-look-and-feel.xml中找不到类似启动侦听器的东西.

I can't find anything like a startup listener in liferay-plugin-package.xml, liferay-portlet.xml, liferay-hook.xml or liferay-look-and-feel.xml.

推荐答案

还有另一种方法.您可以利用StartupAction.它仅仅是一个事件,每次启动插件时都会触发该事件.此方法仅在服务器启动或部署时被触发一次.认识到,doRun方法获取一个由companyIds组成的String数组作为参数. companyId代表门户实例(Liferay内部实例.不是另一台应用程序服务器.)

There is another way. You could utilize the StartupAction. It is merely an Event, which get's triggered on every startup of a plugin. This method will only get triggered once on a server start or deploy. Recognize, that the doRun method get's a String array of companyIds as an argument. A companyId is representing a portal instance (the Liferay internal one. Not another application server.)

例如,我有一个Portlet,我依赖于其中的一些自定义字段来存在.所以我有这个整洁的小班:

For instance, I have a portlet where I rely on some custom fields to exist. So I have this neat little class:

package de.osc.kaleositeaddon.startup;

import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.SimpleAction;

public class StartupAction extends SimpleAction {

    @Override
    public void run(String[] companyIds) throws ActionException {
        setupExpandoAction.run(companyIds);
        importMessagesAction.run(companyIds);
    }

    private SetupExpandoAction setupExpandoAction = new SetupExpandoAction();
    private ImportMessagesAction importMessagesAction = new ImportMessagesAction();
}

SetupExpandoAction为:

And the SetupExpandoAction is:

package de.osc.kaleositeaddon.startup;

import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.SimpleAction;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.ResourceConstants;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.RoleConstants;
import com.liferay.portal.security.permission.ActionKeys;
import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portlet.expando.DuplicateColumnNameException;
import com.liferay.portlet.expando.DuplicateTableNameException;
import com.liferay.portlet.expando.model.ExpandoColumn;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
import com.liferay.portlet.expando.model.ExpandoTable;
import com.liferay.portlet.expando.model.ExpandoTableConstants;
import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
import com.liferay.portlet.journal.model.JournalArticle;

import de.osc.kaleositeaddon.service.constants.ExpandoConstants;

public class SetupExpandoAction extends SimpleAction {

    @Override
    public void run(String[] companyIds) throws ActionException {
        for (int i = 0; i < companyIds.length; i++) {
            long companyId = Long.parseLong(companyIds[i]);
            try {
                setupExpandoGroup(companyId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void setupExpandoGroup(long companyId) throws Exception {
        ExpandoTable table = null;
        try {
            table = ExpandoTableLocalServiceUtil.addTable(companyId, Group.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME);
        } catch (DuplicateTableNameException dtne) {
            table = ExpandoTableLocalServiceUtil.getTable(companyId, Group.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME);
        }
        /*
         * Setup StartDate
         */
        try {
            ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                    ExpandoConstants.EXPANDO_COLUMN_NAME_START_DATE, ExpandoColumnConstants.DATE);
            column.setDefaultData("");
            Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
            ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                    String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
        }
        catch (DuplicateColumnNameException dcne) {
        }
        /*
         * Setup EndDate
         */
        try {
            ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                    ExpandoConstants.EXPANDO_COLUMN_NAME_END_DATE, ExpandoColumnConstants.DATE);
            column.setDefaultData("");
            Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
            ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                    String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
        }
        catch (DuplicateColumnNameException dcne) {
        }


        try {
            ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                    ExpandoConstants.EXPANDO_COLUMN_NAME_CREATE_DATE, ExpandoColumnConstants.DATE);
            column.setDefaultData("");
            Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
            ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                    String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
        }
        catch (DuplicateColumnNameException dcne) {
        }

    }

}

在liferay-hook.xml文件中,执行以下操作:

In your liferay-hook.xml file, you do the following:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.0.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_0_0.dtd">

<hook>
    <portal-properties>portal.properties</portal-properties>        
</hook>

然后在portal.properties中添加以下行:

And in portal.properties you add this line:

application.startup.events=de.osc.kaleositeaddon.startup.StartupAction

记住要替换类名;)

这篇关于将插件部署到Liferay时如何执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆