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.ClusterNodeResponses;
019    import com.liferay.portal.kernel.cluster.FutureClusterResponses;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
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(_manageMethod, 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                    return (T)clusterNodeResponses.getClusterResponse(
091                            clusterNode).getResult();
092            }
093    
094            public void setPortalManager(PortalManager portalManager) {
095                    PortalRuntimePermission.checkSetBeanProperty(getClass());
096    
097                    _portalManager = portalManager;
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(PortalManagerUtil.class);
101    
102            private static Method _manageMethod;
103            private static PortalManager _portalManager;
104    
105            static {
106                    try {
107                            _manageMethod = PortalManagerUtil.class.getDeclaredMethod(
108                                    "manage", ManageAction.class);
109                    }
110                    catch (Exception e) {
111                            _log.error(e, e);
112                    }
113            }
114    
115    }