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