001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.openejb.cluster.infra;
021:
022: import java.net.URI;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Map;
026: import java.util.Set;
027:
028: /**
029: *
030: * @version $Rev:$ $Date:$
031: */
032: public class BasicNetworkConnectorTracker implements
033: NetworkConnectorTracker {
034: private final Map<Object, Set<URI>> idToLocations;
035: private final Map<String, Set<URI>> nodeNameToLocations;
036:
037: public BasicNetworkConnectorTracker() {
038: idToLocations = new HashMap<Object, Set<URI>>();
039: nodeNameToLocations = new HashMap<String, Set<URI>>();
040: }
041:
042: public Set<URI> getConnectorURIs(Object deploymentId)
043: throws NetworkConnectorTrackerException {
044: Set<URI> locations;
045: synchronized (idToLocations) {
046: locations = idToLocations.get(deploymentId);
047: if (null == locations) {
048: throw new NetworkConnectorTrackerException("["
049: + deploymentId + "] is not registered");
050: }
051: locations = new HashSet<URI>(locations);
052: }
053: return locations;
054: }
055:
056: public void registerNetworkConnectorLocations(Object deploymentId,
057: String nodeName, Set<URI> locations) {
058: synchronized (idToLocations) {
059: Set<URI> allLocations = idToLocations.get(deploymentId);
060: if (null == allLocations) {
061: allLocations = new HashSet<URI>();
062: idToLocations.put(deploymentId, allLocations);
063: }
064: allLocations.addAll(locations);
065:
066: allLocations = nodeNameToLocations.get(nodeName);
067: if (null == allLocations) {
068: allLocations = new HashSet<URI>();
069: nodeNameToLocations.put(nodeName, allLocations);
070: }
071: allLocations.addAll(locations);
072: }
073: }
074:
075: public void unregisterNetworkConnectorLocations(
076: Object deploymentId, String nodeName, Set<URI> locations) {
077: synchronized (idToLocations) {
078: Set<URI> allLocations = idToLocations.get(deploymentId);
079: if (null == allLocations) {
080: return;
081: }
082: allLocations.removeAll(locations);
083: if (allLocations.isEmpty()) {
084: idToLocations.remove(deploymentId);
085: }
086:
087: allLocations = nodeNameToLocations.get(nodeName);
088: allLocations.removeAll(locations);
089: if (allLocations.isEmpty()) {
090: nodeNameToLocations.remove(nodeName);
091: }
092: }
093: }
094:
095: public void unregisterNetworkConnectorLocations(String nodeName) {
096: synchronized (idToLocations) {
097: Set<URI> locationsToRemove = nodeNameToLocations
098: .remove(nodeName);
099: if (null == locationsToRemove) {
100: return;
101: }
102: Map<Object, Set<URI>> clonedIdToLocations = new HashMap<Object, Set<URI>>(
103: idToLocations);
104: for (Map.Entry<Object, Set<URI>> entry : clonedIdToLocations
105: .entrySet()) {
106: Set<URI> allLocations = entry.getValue();
107: allLocations.removeAll(locationsToRemove);
108: if (allLocations.isEmpty()) {
109: idToLocations.remove(entry.getKey());
110: }
111: }
112: }
113: }
114:
115: }
|