Home » geronimo-2.2-source-release » org.apache.geronimo.openejb.cluster.infra » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.geronimo.openejb.cluster.infra;
   21   
   22   import java.net.URI;
   23   import java.util.HashMap;
   24   import java.util.HashSet;
   25   import java.util.Map;
   26   import java.util.Set;
   27   
   28   
   29   /**
   30    *
   31    * @version $Rev:$ $Date:$
   32    */
   33   public class BasicNetworkConnectorTracker implements NetworkConnectorTracker {
   34       private final Map<Object, Set<URI>> idToLocations;
   35       private final Map<String, Set<URI>> nodeNameToLocations;
   36       
   37       public BasicNetworkConnectorTracker() {
   38           idToLocations = new HashMap<Object, Set<URI>>();
   39           nodeNameToLocations = new HashMap<String, Set<URI>>();
   40       }
   41   
   42       public Set<URI> getConnectorURIs(Object deploymentId) throws NetworkConnectorTrackerException {
   43           Set<URI> locations;
   44           synchronized (idToLocations) {
   45               locations = idToLocations.get(deploymentId);
   46               if (null == locations) {
   47                   throw new NetworkConnectorTrackerException("[" + deploymentId + "] is not registered");
   48               }
   49               locations = new HashSet<URI>(locations);
   50           }
   51           return locations;
   52       }
   53   
   54       public void registerNetworkConnectorLocations(Object deploymentId, String nodeName, Set<URI> locations) {
   55           synchronized (idToLocations) {
   56               Set<URI> allLocations = idToLocations.get(deploymentId);
   57               if (null == allLocations) {
   58                   allLocations = new HashSet<URI>();
   59                   idToLocations.put(deploymentId, allLocations);
   60               }
   61               allLocations.addAll(locations);
   62               
   63               allLocations = nodeNameToLocations.get(nodeName);
   64               if (null == allLocations) {
   65                   allLocations = new HashSet<URI>();
   66                   nodeNameToLocations.put(nodeName, allLocations);
   67               }
   68               allLocations.addAll(locations);
   69           }
   70       }
   71   
   72       public void unregisterNetworkConnectorLocations(Object deploymentId, String nodeName, Set<URI> locations) {
   73           synchronized (idToLocations) {
   74               Set<URI> allLocations = idToLocations.get(deploymentId);
   75               if (null == allLocations) {
   76                   return;
   77               }
   78               allLocations.removeAll(locations);
   79               if (allLocations.isEmpty()) {
   80                   idToLocations.remove(deploymentId);
   81               }
   82               
   83               allLocations = nodeNameToLocations.get(nodeName);
   84               allLocations.removeAll(locations);
   85               if (allLocations.isEmpty()) {
   86                   nodeNameToLocations.remove(nodeName);
   87               }
   88           }
   89       }
   90   
   91       public void unregisterNetworkConnectorLocations(String nodeName) {
   92           synchronized (idToLocations) {
   93               Set<URI> locationsToRemove = nodeNameToLocations.remove(nodeName);
   94               if (null == locationsToRemove) {
   95                   return;
   96               }
   97               Map<Object, Set<URI>> clonedIdToLocations = new HashMap<Object, Set<URI>>(idToLocations);
   98               for (Map.Entry<Object, Set<URI>> entry : clonedIdToLocations.entrySet()) {
   99                   Set<URI> allLocations = entry.getValue();
  100                   allLocations.removeAll(locationsToRemove);
  101                   if (allLocations.isEmpty()) {
  102                       idToLocations.remove(entry.getKey());
  103                   }
  104               }
  105           }
  106       }
  107       
  108   }

Home » geronimo-2.2-source-release » org.apache.geronimo.openejb.cluster.infra » [javadoc | source]