001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            private 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            private 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    }