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.cache.cluster;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class PortalCacheClusterLink {
024    
025            public void afterPropertiesSet() {
026                    _portalCacheClusterChannels = new ArrayList<PortalCacheClusterChannel>(
027                            _channelNumber);
028    
029                    for (int i = 0; i < _channelNumber; i++) {
030                            _portalCacheClusterChannels.add(
031                                    _portalCacheClusterChannelFactory.
032                                            createPortalCacheClusterChannel());
033                    }
034    
035                    if (_portalCacheClusterChannelSelector == null) {
036                            _portalCacheClusterChannelSelector =
037                                    new UniformPortalCacheClusterChannelSelector();
038                    }
039            }
040    
041            public void destroy() {
042                    for (PortalCacheClusterChannel portalCacheClusterChannel :
043                                    _portalCacheClusterChannels) {
044    
045                            portalCacheClusterChannel.destroy();
046                    }
047            }
048    
049            public long getSubmittedEventNumber() {
050                    return _portalCacheClusterChannelSelector.getSelectedNumber();
051            }
052    
053            public void sendEvent(PortalCacheClusterEvent portalCacheClusterEvent) {
054                    PortalCacheClusterChannel portalCacheClusterChannel =
055                            _portalCacheClusterChannelSelector.select(
056                                    _portalCacheClusterChannels, portalCacheClusterEvent);
057    
058                    portalCacheClusterChannel.sendEvent(portalCacheClusterEvent);
059            }
060    
061            public void setChannelNumber(int channelNumber) {
062                    _channelNumber = channelNumber;
063            }
064    
065            public void setPortalCacheClusterChannelFactory(
066                    PortalCacheClusterChannelFactory portalCacheClusterChannelFactory) {
067    
068                    _portalCacheClusterChannelFactory = portalCacheClusterChannelFactory;
069            }
070    
071            public void setPortalCacheClusterChannelSelector(
072                    PortalCacheClusterChannelSelector portalCacheClusterChannelSelector) {
073    
074                    _portalCacheClusterChannelSelector = portalCacheClusterChannelSelector;
075            }
076    
077            private static final int _DEFAULT_CHANNEL_NUMBER = 10;
078    
079            private int _channelNumber = _DEFAULT_CHANNEL_NUMBER;
080            private PortalCacheClusterChannelFactory _portalCacheClusterChannelFactory;
081            private List<PortalCacheClusterChannel> _portalCacheClusterChannels;
082            private PortalCacheClusterChannelSelector
083                    _portalCacheClusterChannelSelector;
084    
085    }