001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
024    import com.liferay.portal.kernel.util.MethodHandler;
025    import com.liferay.portal.model.ClusterGroup;
026    import com.liferay.portal.service.ClusterGroupLocalServiceUtil;
027    
028    import java.lang.reflect.Method;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Raymond Aug??
033     */
034    public class PortalManagerUtil {
035    
036            public static MethodHandler createManageActionMethodHandler(
037                    ManageAction<?> manageAction) {
038    
039                    return new MethodHandler(_manageMethod, manageAction);
040            }
041    
042            public static PortalManager getPortalManager() {
043                    PortalRuntimePermission.checkGetBeanProperty(PortalManagerUtil.class);
044    
045                    return _portalManager;
046            }
047    
048            public static FutureClusterResponses manage(
049                            ClusterGroup clusterGroup, ManageAction<?> manageAction)
050                    throws ManageActionException {
051    
052                    ManageAction<FutureClusterResponses> manageActionWrapper =
053                            new ClusterManageActionWrapper(clusterGroup, manageAction);
054    
055                    return getPortalManager().manage(manageActionWrapper);
056            }
057    
058            public static <T> T manage(ManageAction<T> manageAction)
059                    throws ManageActionException {
060    
061                    return getPortalManager().manage(manageAction);
062            }
063    
064            public static void manageAsync(
065                            ClusterNode clusterNode, ManageAction<?> manageAction)
066                    throws Exception {
067    
068                    ClusterGroup clusterGroup =
069                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
070    
071                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
072    
073                    manage(clusterGroup, manageAction);
074            }
075    
076            public static <T> T manageSync(
077                            ClusterNode clusterNode, ManageAction<T> manageAction)
078                    throws Exception {
079    
080                    ClusterGroup clusterGroup =
081                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
082    
083                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
084    
085                    FutureClusterResponses futureClusterResponses = manage(
086                            clusterGroup, manageAction);
087    
088                    ClusterNodeResponses clusterNodeResponses =
089                            futureClusterResponses.get();
090    
091                    ClusterNodeResponse clusterNodeResponse =
092                            clusterNodeResponses.getClusterResponse(clusterNode);
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 Log _log = LogFactoryUtil.getLog(PortalManagerUtil.class);
104    
105            private static Method _manageMethod;
106            private static PortalManager _portalManager;
107    
108            static {
109                    try {
110                            _manageMethod = PortalManagerUtil.class.getDeclaredMethod(
111                                    "manage", ManageAction.class);
112                    }
113                    catch (Exception e) {
114                            _log.error(e, e);
115                    }
116            }
117    
118    }