1 18 package org.apache.activemq.transport.discovery.rendezvous; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import javax.jmdns.JmDNS; 25 import java.util.concurrent.atomic.AtomicInteger ; 26 27 public class JmDNSFactory { 28 29 static Map registry = new HashMap (); 30 static class UsageTracker { 31 AtomicInteger count = new AtomicInteger (0); 32 JmDNS jmDNS; 33 } 34 35 static synchronized JmDNS create(final InetAddress address) throws IOException { 36 UsageTracker tracker = (UsageTracker)registry.get(address); 37 if( tracker == null ) { 38 tracker = new UsageTracker(); 39 tracker.jmDNS = new JmDNS(address) { 40 public void close() { 41 if( onClose(address) ) { 42 super.close(); 43 } 44 } 45 }; 46 registry.put(address, tracker); 47 } 48 tracker.count.incrementAndGet(); 49 return tracker.jmDNS; 50 } 51 52 static synchronized boolean onClose(InetAddress address){ 53 UsageTracker tracker=(UsageTracker) registry.get(address); 54 if(tracker!=null){ 55 if(tracker.count.decrementAndGet()==0){ 56 registry.remove(address); 57 return true; 58 } 59 } 60 return false; 61 } 62 } 63 | Popular Tags |