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.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            @Override
042            public FutureClusterResponses action() throws ManageActionException {
043                    try {
044                            return doAction();
045                    }
046                    catch (SystemException se) {
047                            throw new ManageActionException(
048                                    "Failed to execute cluster manage action", se);
049                    }
050            }
051    
052            protected FutureClusterResponses doAction()
053                    throws ManageActionException, SystemException {
054    
055                    MethodHandler manageActionMethodHandler =
056                            PortalManagerUtil.createManageActionMethodHandler(_manageAction);
057    
058                    ClusterRequest clusterRequest = null;
059    
060                    if (_clusterGroup.isWholeCluster()) {
061                            clusterRequest = ClusterRequest.createMulticastRequest(
062                                    manageActionMethodHandler);
063                    }
064                    else {
065                            verifyClusterGroup();
066    
067                            clusterRequest = ClusterRequest.createUnicastRequest(
068                                    manageActionMethodHandler,
069                                    _clusterGroup.getClusterNodeIdsArray());
070                    }
071    
072                    return ClusterExecutorUtil.execute(clusterRequest);
073            }
074    
075            protected void verifyClusterGroup() throws ManageActionException {
076                    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();
077    
078                    String[] requiredClusterNodesIds =
079                            _clusterGroup.getClusterNodeIdsArray();
080    
081                    for (String requiredClusterNodeId : requiredClusterNodesIds) {
082                            boolean verified = false;
083    
084                            Iterator<ClusterNode> itr = clusterNodes.iterator();
085    
086                            while (itr.hasNext()) {
087                                    ClusterNode clusterNode = itr.next();
088    
089                                    String clusterNodeId = clusterNode.getClusterNodeId();
090    
091                                    if (clusterNodeId.equals(requiredClusterNodeId)) {
092                                            itr.remove();
093    
094                                            verified = true;
095    
096                                            break;
097                                    }
098                            }
099    
100                            if (!verified) {
101                                    throw new ManageActionException(
102                                            "Cluster node " + requiredClusterNodeId +
103                                                    " is not available");
104                            }
105                    }
106            }
107    
108            private ClusterGroup _clusterGroup;
109            private ManageAction<?> _manageAction;
110    
111    }