001    /**
002     * Copyright (c) 2000-2010 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.ClusterExecutorUtil;
018    import com.liferay.portal.kernel.cluster.ClusterNode;
019    import com.liferay.portal.kernel.cluster.ClusterRequest;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.MethodHandler;
022    import com.liferay.portal.model.ClusterGroup;
023    
024    import java.util.List;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class ClusterManageActionWrapper implements ManageAction {
030    
031            public ClusterManageActionWrapper(
032                    ClusterGroup clusterGroup, ManageAction manageAction) {
033    
034                    _clusterGroup = clusterGroup;
035                    _manageAction = manageAction;
036            }
037    
038            public void action() throws ManageActionException {
039                    try {
040                            doAction();
041                    }
042                    catch (SystemException se) {
043                            throw new ManageActionException(
044                                    "Failed to execute cluster manage action", se);
045                    }
046            }
047    
048            private void doAction() throws ManageActionException, SystemException {
049                    MethodHandler manageActionMethodHandler =
050                            PortalManagerUtil.createManageActionMethodHandler(_manageAction);
051    
052                    ClusterRequest clusterRequest = null;
053    
054                    if (_clusterGroup.isWholeCluster()) {
055                            clusterRequest = ClusterRequest.createMulticastRequest(
056                                    manageActionMethodHandler);
057                    }
058                    else {
059                            verifyClusterGroup();
060    
061                            clusterRequest = ClusterRequest.createUnicastRequest(
062                                    manageActionMethodHandler,
063                                    _clusterGroup.getClusterNodeIdsArray());
064                    }
065    
066                    ClusterExecutorUtil.execute(clusterRequest);
067            }
068    
069            private void verifyClusterGroup() throws ManageActionException {
070                    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();
071    
072                    String[] requiredClusterNodesIds =
073                            _clusterGroup.getClusterNodeIdsArray();
074    
075                    for (String requiredClusterNodeId : requiredClusterNodesIds) {
076                            for (ClusterNode clusterNode : clusterNodes) {
077                                    String clusterNodeId = clusterNode.getClusterNodeId();
078    
079                                    if (clusterNodeId.equals(requiredClusterNodeId)) {
080                                            clusterNodes.remove(clusterNode);
081    
082                                            continue;
083                                    }
084                            }
085    
086                            throw new ManageActionException(
087                                    "Cluster node " + requiredClusterNodeId + " is not available");
088                    }
089            }
090    
091            private ClusterGroup _clusterGroup;
092            private ManageAction _manageAction;
093    
094    }