1 19 package org.objectweb.carol.cmi; 20 21 import java.net.InetAddress ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 public class Config { 27 private static boolean configured = false; 28 private static String multicastAddress = null; 29 private static String multicastItf = null; 30 private static int multicastPort = -1; 31 private static String multicastGroupName = null; 32 private static String localHost = null; 34 public final static int DEFAULT_RR_FACTOR = 100; 35 private static int loadFactor = DEFAULT_RR_FACTOR; 36 private static boolean stubDebug = false; 37 38 public static final String MULTICAST_ADDRESS_PROPERTY = 39 "carol.cmi.multicast.address"; 40 public static final String MULTICAST_ITF_PROPERTY = 41 "carol.cmi.multicast.itf"; 42 public static final String MULTICAST_GROUPNAME_PROPERTY = 43 "carol.cmi.multicast.groupname"; 44 public static final String RR_FACTOR_PROPERTY = "carol.cmi.rr.factor"; 45 public static final String STUB_DEBUG_PROPERTY = "carol.cmi.stub.debug"; 46 47 51 public static synchronized void setProperties(Properties pr) 52 throws Exception { 53 if (configured) { 54 throw new Exception ("Cmi already configured"); 55 } 56 Iterator i = pr.entrySet().iterator(); 57 while (i.hasNext()) { 58 Map.Entry e = (Map.Entry ) i.next(); 59 String s = (String ) e.getValue(); 60 Object k = e.getKey(); 61 if (k.equals(MULTICAST_ADDRESS_PROPERTY)) { 62 s = s.trim(); 63 try { 64 int l = s.indexOf(':'); 65 s.substring(0, l); 66 String a = 67 InetAddress 68 .getByName(s.substring(0, l)) 69 .getHostAddress(); 70 int p = new Integer (s.substring(l + 1)).intValue(); 71 multicastAddress = a; 72 multicastPort = p; 73 } catch (Exception ex) { 74 throw new Exception ( 75 "Invalid multicast address (" + s + ")", ex); 76 } 77 } else if (k.equals(MULTICAST_GROUPNAME_PROPERTY)) { 78 multicastGroupName = s.trim(); 79 } else if (k.equals(MULTICAST_ITF_PROPERTY)) { 80 multicastItf = s.trim(); 81 } else if (k.equals(RR_FACTOR_PROPERTY)) { 82 loadFactor = new Integer (s.trim()).intValue(); 83 } else if (k.equals(STUB_DEBUG_PROPERTY)) { 84 stubDebug = new Boolean (s.trim()).booleanValue(); 85 } 86 } 87 configured = true; 88 } 89 90 public static String getMulticastGroupName() throws ConfigException { 91 if (multicastGroupName == null) 92 throw new ConfigException( 93 "Property " + MULTICAST_GROUPNAME_PROPERTY + " not defined"); 94 return multicastGroupName; 95 } 96 97 public static String getMulticastAddress() throws ConfigException { 98 if (multicastAddress == null) 99 throw new ConfigException( 100 "Property " + MULTICAST_ADDRESS_PROPERTY + " not defined"); 101 return multicastAddress; 102 } 103 104 public static int getMulticastPort() { 105 return multicastPort; 106 } 107 108 public static String getMulticastItf() { 109 return multicastItf; 110 } 111 112 public static int getLoadFactor() { 113 return loadFactor; 114 } 115 116 public static boolean isStubDebug() { 117 return stubDebug; 118 } 119 } 120 | Popular Tags |