01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.common;
18:
19: import java.util.Collection;
20: import java.util.HashMap;
21: import java.util.Iterator;
22: import java.util.Map;
23:
24: public class Registry {
25:
26: protected ServiceMixComponent component;
27: protected Map<String, Endpoint> endpoints;
28: protected Map<String, ServiceUnit> serviceUnits;
29:
30: public Registry(ServiceMixComponent component) {
31: this .component = component;
32: this .endpoints = new HashMap<String, Endpoint>();
33: this .serviceUnits = new HashMap<String, ServiceUnit>();
34: }
35:
36: public Endpoint getEndpoint(String key) {
37: return this .endpoints.get(key);
38: }
39:
40: public boolean isRegistered(ServiceUnit su) {
41: return isServiceUnitRegistered(su.getName());
42: }
43:
44: public boolean isServiceUnitRegistered(String name) {
45: return this .serviceUnits.containsKey(name);
46: }
47:
48: public ServiceUnit getServiceUnit(String name) {
49: return this .serviceUnits.get(name);
50: }
51:
52: public void registerEndpoint(Endpoint ep) {
53: String key = EndpointSupport.getKey(ep);
54: if (this .endpoints.put(key, ep) != null) {
55: throw new IllegalStateException(
56: "An endpoint is already registered for key: " + key);
57: }
58: }
59:
60: public void unregisterEndpoint(Endpoint ep) {
61: this .endpoints.remove(EndpointSupport.getKey(ep));
62: }
63:
64: public void registerServiceUnit(ServiceUnit su) {
65: this .serviceUnits.put(su.getName(), su);
66: Collection endpoints = (Collection) su.getEndpoints();
67: for (Iterator iter = endpoints.iterator(); iter.hasNext();) {
68: Endpoint ep = (Endpoint) iter.next();
69: registerEndpoint(ep);
70: }
71: }
72:
73: public void unregisterServiceUnit(ServiceUnit su) {
74: this .serviceUnits.remove(su.getName());
75: Collection endpoints = (Collection) su.getEndpoints();
76: for (Iterator iter = endpoints.iterator(); iter.hasNext();) {
77: Endpoint ep = (Endpoint) iter.next();
78: unregisterEndpoint(ep);
79: }
80: }
81:
82: }
|