001    /**
002     * Copyright (c) 2000-2012 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.util.MethodHandler;
023    import com.liferay.portal.model.ClusterGroup;
024    import com.liferay.portal.service.ClusterGroupLocalServiceUtil;
025    
026    import java.lang.reflect.Method;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class PortalManagerUtil {
032    
033            public static MethodHandler createManageActionMethodHandler(
034                    ManageAction<?> manageAction) {
035    
036                    return new MethodHandler(_manageMethod, manageAction);
037            }
038    
039            public static FutureClusterResponses manage(
040                            ClusterGroup clusterGroup, ManageAction<?> manageAction)
041                    throws ManageActionException {
042    
043                    ManageAction<FutureClusterResponses> manageActionWrapper =
044                            new ClusterManageActionWrapper(clusterGroup, manageAction);
045    
046                    return _portalManager.manage(manageActionWrapper);
047            }
048    
049            public static <T> T manage(ManageAction<T> manageAction)
050                    throws ManageActionException {
051    
052                    return _portalManager.manage(manageAction);
053            }
054    
055            public static void manageAsync(
056                            ClusterNode clusterNode, ManageAction<?> manageAction)
057                    throws Exception {
058    
059                    ClusterGroup clusterGroup =
060                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
061    
062                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
063    
064                    manage(clusterGroup, manageAction);
065            }
066    
067            public static <T> T manageSync(
068                            ClusterNode clusterNode, ManageAction<T> manageAction)
069                    throws Exception {
070    
071                    ClusterGroup clusterGroup =
072                            ClusterGroupLocalServiceUtil.createClusterGroup(0);
073    
074                    clusterGroup.setClusterNodeIds(clusterNode.getClusterNodeId());
075    
076                    FutureClusterResponses futureClusterResponses = manage(
077                            clusterGroup, manageAction);
078    
079                    ClusterNodeResponses clusterNodeResponses =
080                            futureClusterResponses.get();
081    
082                    return (T)clusterNodeResponses.getClusterResponse(
083                            clusterNode).getResult();
084            }
085    
086            public void setPortalManager(PortalManager portalManager) {
087                    _portalManager = portalManager;
088            }
089    
090            private static Log _log = LogFactoryUtil.getLog(PortalManagerUtil.class);
091    
092            private static Method _manageMethod;
093            private static PortalManager _portalManager;
094    
095            static {
096                    try {
097                            _manageMethod = PortalManagerUtil.class.getDeclaredMethod(
098                                    "manage", ManageAction.class);
099                    }
100                    catch (Exception e) {
101                            _log.error(e, e);
102                    }
103            }
104    
105    }