1 package org.sapia.ubik.rmi.server; 2 3 import java.io.IOException ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.sapia.ubik.mcast.EventChannel; 9 import org.sapia.ubik.rmi.Consts; 10 import org.sapia.ubik.rmi.RemoteRuntimeException; 11 12 13 24 class EventChannelSingleton { 25 private static Map _channels = new HashMap (); 26 private static String _mcastAddress = Consts.DEFAULT_MCAST_ADDR; 27 private static int _mcastPort = Consts.DEFAULT_MCAST_PORT; 28 29 static { 30 try { 31 if (System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_PORT_KEY) != null) { 32 _mcastPort = Integer.parseInt(System.getProperty( 33 org.sapia.ubik.rmi.Consts.MCAST_PORT_KEY)); 34 } 35 } catch (NumberFormatException e) { 36 } 38 39 if (System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_ADDR_KEY) != null) { 40 _mcastAddress = System.getProperty(org.sapia.ubik.rmi.Consts.MCAST_ADDR_KEY); 41 } 42 } 43 44 51 static synchronized EventChannel getEventChannelFor(String domain) 52 throws RemoteRuntimeException { 53 return getEventChannelFor(domain, _mcastAddress, _mcastPort); 54 } 55 56 63 static synchronized EventChannel getEventChannelFor(String domain, 64 String mcastAddress, int mcastPort) throws RemoteRuntimeException { 65 EventChannel channel; 66 67 String key = domain + ":" + mcastAddress + ":" + mcastPort; 68 69 if ((channel = (EventChannel) _channels.get(key)) == null) { 70 try { 71 channel = new EventChannel(domain, mcastAddress, mcastPort); 72 channel.start(); 73 } catch (IOException e) { 74 throw new RemoteRuntimeException( 75 "Could not create event channel for domain: " + domain, e); 76 } 77 78 _channels.put(key, channel); 79 } 80 81 return channel; 82 } 83 84 87 static synchronized void shutdown() { 88 EventChannel channel; 89 90 for (Iterator iter = _channels.values().iterator(); iter.hasNext();) { 91 channel = (EventChannel) iter.next(); 92 channel.close(); 93 } 94 95 _channels.clear(); 96 } 97 } 98 | Popular Tags |