1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.cache.cluster.clusterlink;
16  
17  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterChannel;
18  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterChannelFactory;
19  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterException;
20  import com.liferay.portal.kernel.cluster.Priority;
21  
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  /**
27   * <a href="ClusterLinkPortalCacheClusterChannelFactory.java.html"><b><i>View
28   * Source</i></b></a>
29   *
30   * @author Shuyang Zhou
31   */
32  public class ClusterLinkPortalCacheClusterChannelFactory
33      implements PortalCacheClusterChannelFactory {
34  
35      public ClusterLinkPortalCacheClusterChannelFactory(
36          String destination, List<Priority> priorities) {
37  
38          _counter = new AtomicInteger(0);
39          _destination = destination;
40          _priorities = priorities;
41  
42          Collections.sort(priorities);
43      }
44  
45      public PortalCacheClusterChannel createPortalCacheClusterChannel()
46          throws PortalCacheClusterException {
47  
48          int count = _counter.getAndIncrement();
49  
50          if (count >= _priorities.size()) {
51              throw new IllegalStateException(
52                  "Cannot create more than " + _priorities.size() + " channels");
53          }
54  
55          return new ClusterLinkPortalCacheClusterChannel(
56              _destination, _priorities.get(count));
57      }
58  
59      private AtomicInteger _counter;
60      private String _destination;
61      private List<Priority> _priorities;
62  
63  }