001    /**
002     * Copyright (c) 2000-present 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() throws ManageActionException {
053                    MethodHandler manageActionMethodHandler =
054                            PortalManagerUtil.createManageActionMethodHandler(_manageAction);
055    
056                    ClusterRequest clusterRequest = null;
057    
058                    if (_clusterGroup.isWholeCluster()) {
059                            clusterRequest = ClusterRequest.createMulticastRequest(
060                                    manageActionMethodHandler);
061                    }
062                    else {
063                            verifyClusterGroup();
064    
065                            clusterRequest = ClusterRequest.createUnicastRequest(
066                                    manageActionMethodHandler,
067                                    _clusterGroup.getClusterNodeIdsArray());
068                    }
069    
070                    return ClusterExecutorUtil.execute(clusterRequest);
071            }
072    
073            protected void verifyClusterGroup() throws ManageActionException {
074                    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();
075    
076                    String[] requiredClusterNodesIds =
077                            _clusterGroup.getClusterNodeIdsArray();
078    
079                    for (String requiredClusterNodeId : requiredClusterNodesIds) {
080                            boolean verified = false;
081    
082                            Iterator<ClusterNode> itr = clusterNodes.iterator();
083    
084                            while (itr.hasNext()) {
085                                    ClusterNode clusterNode = itr.next();
086    
087                                    String clusterNodeId = clusterNode.getClusterNodeId();
088    
089                                    if (clusterNodeId.equals(requiredClusterNodeId)) {
090                                            itr.remove();
091    
092                                            verified = true;
093    
094                                            break;
095                                    }
096                            }
097    
098                            if (!verified) {
099                                    throw new ManageActionException(
100                                            "Cluster node " + requiredClusterNodeId +
101                                                    " is not available");
102                            }
103                    }
104            }
105    
106            private final ClusterGroup _clusterGroup;
107            private final ManageAction<?> _manageAction;
108    
109    }