当我的插件部署到Liferay时,如何运行某些操作?

时间:2015-06-25 09:46:41

标签: liferay liferay-6 portlet

我希望在插件 portlet hook 主题)后立即运行一些代码Liferay提供。

我无法在liferay-plugin-package.xmlliferay-portlet.xmlliferay-hook.xmlliferay-look-and-feel.xml中找到类似启动监听器的内容。

2 个答案:

答案 0 :(得分:5)

还有另一种方式。您可以使用StartupAction。它只是一个事件,它会在插件的每次启动时触发。此方法仅在服务器启动或部署时触发一次。认识到,doRun方法得到一个companyIds的String数组作为参数。 companyId表示门户实例(Liferay内部实例。不是另一个应用程序服务器。)

例如,我有一个portlet,我依赖一些自定义字段存在。所以我有这个整洁的小班:

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是:

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文件中,执行以下操作:

    <?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中添加以下行:

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

记得替换类名;)

答案 1 :(得分:1)

如果您的插件中有portlet,则可以在Portlet.init(PortletConfig)中运行代码。

但这并非总是可行,因为

  • 你没有portlet
  • 你没有自己的portlet实现(例如,因为你使用JSF)
  • 代码不属于任何portlet,您不希望将其随机添加到其中一个

在这种情况下,您还有两个选择:

创建为每个门户网站实例调用的启动操作(请参阅answer of gevatterjan)。如果您需要为每个门户网站实例运行代码,这是最佳解决方案。

或者您使用ServletContextListenerPortalLifecycle的组合,该插件在插件可用时启动一次。如果您只想运行一次,这是最好的解决方案,例如创建调度程序或替换liferay-hook.xml resp未涵盖的Liferay函数。 portal.properties

@WebListener
public class MyStartupListener implements ServletContextListener, PortalLifecycle {

    @Override
    public void contextInitialized(final ServletContextEvent sce) {
        // Wait until the portal is ready
        PortalLifecycleUtil.register(this, PortalLifecycle.METHOD_INIT);
    }

    @Override
    public void portalInit() {
        // Here comes our initialization code
        ...
    }

    @Override
    public void contextDestroyed(final ServletContextEvent event) {
        // Here comes our uninitialization code, if any
        ...
    }

    @Override
    public void portalDestroy() {
        // Ignore
    }

}

您可以调用任何Liferay函数,但要小心:

您的主题未连接到任何请求 - 目前没有门户网站实例Company),网站Group),< em> page (Layout)或 user 。您可以使用适当的*ServiceLocalUtil函数找到所有这些函数。对于某些操作,您需要为CompanyThreadLocalPricipalThreadLocalPermissionThreadLocal中的线程设置这些值。