001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.framework;
018
019 import java.util.ArrayList;
020 import java.util.Iterator;
021 import java.util.List;
022 import java.util.Map;
023 import java.util.concurrent.ConcurrentHashMap;
024
025 import org.apache.servicemix.jbi.container.SubscriptionSpec;
026 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
027 import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
028
029 /**
030 * Maintains a registry of the applicable subscriptions currently active for the
031 * current components
032 *
033 * @version $Revision: 564607 $
034 */
035 public class SubscriptionRegistry {
036
037 private Map<SubscriptionSpec, InternalEndpoint> subscriptions = new ConcurrentHashMap<SubscriptionSpec, InternalEndpoint>();
038 private Registry registry;
039
040 public SubscriptionRegistry(Registry registry) {
041 this.registry = registry;
042 }
043
044 /**
045 * @param subscription
046 * @param endpoint
047 */
048 public void registerSubscription(SubscriptionSpec subscription, InternalEndpoint endpoint) {
049 subscriptions.put(subscription, endpoint);
050 }
051
052 /**
053 * @param subscription
054 * @return the ServiceEndpoint
055 */
056 public InternalEndpoint deregisterSubscription(SubscriptionSpec subscription) {
057 return (InternalEndpoint) subscriptions.remove(subscription);
058 }
059
060
061 /**
062 * @param exchange
063 * @return a List of matching endpoints - can return null if no matches
064 */
065 public List<InternalEndpoint> getMatchingSubscriptionEndpoints(MessageExchangeImpl exchange) {
066 List<InternalEndpoint> result = null;
067 for (Iterator iter = subscriptions.entrySet().iterator(); iter.hasNext();) {
068 Map.Entry entry = (Map.Entry) iter.next();
069
070 SubscriptionSpec subscription = (SubscriptionSpec) entry.getKey();
071 if (subscription.matches(registry, exchange)) {
072 if (result == null) {
073 result = new ArrayList<InternalEndpoint>();
074 }
075 InternalEndpoint endpoint = (InternalEndpoint) entry.getValue();
076 result.add(endpoint);
077 }
078 }
079 return result;
080 }
081
082 }