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.common;
018
019 import java.util.Collection;
020 import java.util.HashMap;
021 import java.util.Iterator;
022 import java.util.Map;
023
024 public class Registry {
025
026 protected ServiceMixComponent component;
027 protected Map<String, Endpoint> endpoints;
028 protected Map<String, ServiceUnit> serviceUnits;
029
030 public Registry(ServiceMixComponent component) {
031 this.component = component;
032 this.endpoints = new HashMap<String, Endpoint>();
033 this.serviceUnits = new HashMap<String, ServiceUnit>();
034 }
035
036 public Endpoint getEndpoint(String key) {
037 return this.endpoints.get(key);
038 }
039
040 public boolean isRegistered(ServiceUnit su) {
041 return isServiceUnitRegistered(su.getName());
042 }
043
044 public boolean isServiceUnitRegistered(String name) {
045 return this.serviceUnits.containsKey(name);
046 }
047
048 public ServiceUnit getServiceUnit(String name) {
049 return this.serviceUnits.get(name);
050 }
051
052 public void registerEndpoint(Endpoint ep) {
053 String key = EndpointSupport.getKey(ep);
054 if (this.endpoints.put(key, ep) != null) {
055 throw new IllegalStateException("An endpoint is already registered for key: " + key);
056 }
057 }
058
059 public void unregisterEndpoint(Endpoint ep) {
060 this.endpoints.remove(EndpointSupport.getKey(ep));
061 }
062
063 public void registerServiceUnit(ServiceUnit su) {
064 this.serviceUnits.put(su.getName(), su);
065 Collection endpoints = (Collection) su.getEndpoints();
066 for (Iterator iter = endpoints.iterator(); iter.hasNext();) {
067 Endpoint ep = (Endpoint) iter.next();
068 registerEndpoint(ep);
069 }
070 }
071
072 public void unregisterServiceUnit(ServiceUnit su) {
073 this.serviceUnits.remove(su.getName());
074 Collection endpoints = (Collection) su.getEndpoints();
075 for (Iterator iter = endpoints.iterator(); iter.hasNext();) {
076 Endpoint ep = (Endpoint) iter.next();
077 unregisterEndpoint(ep);
078 }
079 }
080
081 }