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.cache.cluster.clusterlink;
016    
017    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterChannel;
018    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterChannelFactory;
019    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterException;
020    import com.liferay.portal.kernel.cluster.Priority;
021    
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.concurrent.atomic.AtomicInteger;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class ClusterLinkPortalCacheClusterChannelFactory
030            implements PortalCacheClusterChannelFactory {
031    
032            public PortalCacheClusterChannel createPortalCacheClusterChannel()
033                    throws PortalCacheClusterException {
034    
035                    int count = _counter.getAndIncrement();
036    
037                    if (count >= _priorities.size()) {
038                            throw new IllegalStateException(
039                                    "Cannot create more than " + _priorities.size() + " channels");
040                    }
041    
042                    return new ClusterLinkPortalCacheClusterChannel(
043                            _destinationName, _priorities.get(count));
044            }
045    
046            public void setDestinationName(String destinationName) {
047                    _destinationName = destinationName;
048            }
049    
050            public void setPriorities(List<Priority> priorities) {
051                    _priorities = priorities;
052    
053                    Collections.sort(priorities);
054            }
055    
056            private AtomicInteger _counter = new AtomicInteger(0);
057            private String _destinationName;
058            private List<Priority> _priorities;
059    
060    }