001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.management;
016    
017    import com.liferay.portal.kernel.cluster.ClusterNode;
018    import com.liferay.portal.kernel.cluster.ClusterNodeResponse;
019    import com.liferay.portal.kernel.cluster.ClusterNodeResponses;
020    import com.liferay.portal.kernel.cluster.FutureClusterResponses;
021    import com.liferay.portal.kernel.exception.LoggedExceptionInInitializerError;
022    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
023    import com.liferay.portal.kernel.util.MethodHandler;
024    import com.liferay.portal.model.ClusterGroup;
025    import com.liferay.portal.service.ClusterGroupLocalServiceUtil;
026    
027    import java.lang.reflect.Method;
028    
029    /**
030     * @author Shuyang Zhou
031     * @author Raymond Aug??
032     */
033    public class PortalManagerUtil {
034    
035            public static MethodHandler createManageActionMethodHandler(
036                    ManageAction<?> manageAction) {
037    
038                    return new MethodHandler(_MANAGE_METHOD, manageAction);
039            }
040    
041            public static PortalManager getPortalManager() {
042                    PortalRuntimePermission.checkGetBeanProperty(PortalManagerUtil.class);
043    
044                    return _portalManager;
045            }
046    
047            public static FutureClusterResponses manage(
048                            ClusterGroup clusterGroup, ManageAction<?> manageAction)
049                    throws ManageActionException {
050    
051                    ManageAction<FutureClusterResponses> manageActionWrapper =
052                            new ClusterManageActionWrapper(clusterGroup, manageAction);
053    
054                    return getPortalManager().manage(manageActionWrapper);
055            }
056    
057            public static <T> T manage(ManageAction<T> manageAction)
058                    throws ManageActionException {
059    
060                    return getPortalManager().manage(manageAction);
061            }
062    
063            public static void manageAsync(
064                            ClusterNode clusterNode, ManageAction<?> manageAction)
065                    throws Exception {
066    
067                    ClusterGroup clusterGroup =
068                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
069    
070                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
071    
072                    manage(clusterGroup, manageAction);
073            }
074    
075            public static <T> T manageSync(
076                            ClusterNode clusterNode, ManageAction<T> manageAction)
077                    throws Exception {
078    
079                    ClusterGroup clusterGroup =
080                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
081    
082                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
083    
084                    FutureClusterResponses futureClusterResponses = manage(
085                            clusterGroup, manageAction);
086    
087                    ClusterNodeResponses clusterNodeResponses =
088                            futureClusterResponses.get();
089    
090                    ClusterNodeResponse clusterNodeResponse =
091                            clusterNodeResponses.getClusterResponse(
092                                    clusterNode.getClusterNodeId());
093    
094                    return (T)clusterNodeResponse.getResult();
095            }
096    
097            public void setPortalManager(PortalManager portalManager) {
098                    PortalRuntimePermission.checkSetBeanProperty(getClass());
099    
100                    _portalManager = portalManager;
101            }
102    
103            private static final Method _MANAGE_METHOD;
104    
105            private static PortalManager _portalManager;
106    
107            static {
108                    try {
109                            _MANAGE_METHOD = PortalManagerUtil.class.getDeclaredMethod(
110                                    "manage", ManageAction.class);
111                    }
112                    catch (Exception e) {
113                            throw new LoggedExceptionInInitializerError(e);
114                    }
115            }
116    
117    }