| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveevents; |
| 16 | |
| 17 | import java.util.HashMap; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.apache.commons.logging.Log; |
| 21 | |
| 22 | /** |
| 23 | * Default implementation of a ChannelRepository. |
| 24 | * @author Jean-Francois Poilpret |
| 25 | */ |
| 26 | public class ChannelRepositoryImpl implements ChannelRepository |
| 27 | { |
| 28 | public ChannelRepositoryImpl(Log logger, Map<String, ChannelContribution> config) |
| 29 | { |
| 30 | _logger = logger; |
| 31 | _channels = new HashMap<String, Object>(config); |
| 32 | } |
| 33 | |
| 34 | @SuppressWarnings("unchecked") |
| 35 | synchronized public Channel<?> getChannel(String name) |
| 36 | { |
| 37 | Object info = _channels.get(name); |
| 38 | if (info == null) |
| 39 | { |
| 40 | _logger.warn("getChannel() undefined channel name <" + name + ">"); |
| 41 | return null; |
| 42 | } |
| 43 | if (info instanceof Channel) |
| 44 | { |
| 45 | return (Channel) info; |
| 46 | } |
| 47 | |
| 48 | ChannelContribution contrib = (ChannelContribution) info; |
| 49 | Channel<?> channel; |
| 50 | if (contrib.isThreadSafe()) |
| 51 | { |
| 52 | channel = new SyncChannelImpl(name, |
| 53 | contrib.getPullConsumerPriority(), |
| 54 | contrib.getEventClass(), |
| 55 | contrib.isLogEvents()); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | channel = new ChannelImpl(name, |
| 60 | contrib.getPullConsumerPriority(), |
| 61 | contrib.getEventClass(), |
| 62 | contrib.isLogEvents()); |
| 63 | } |
| 64 | _channels.put(name, channel); |
| 65 | return channel; |
| 66 | } |
| 67 | |
| 68 | private final Log _logger; |
| 69 | private final Map<String, Object> _channels; |
| 70 | } |