1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }