KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > Config


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.net.InetAddress JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 public class Config {
27     private static boolean configured = false;
28     private static String JavaDoc multicastAddress = null;
29     private static String JavaDoc multicastItf = null;
30     private static int multicastPort = -1;
31     private static String JavaDoc multicastGroupName = null;
32     //private static Vector membersIp = null;
33
private static String JavaDoc 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 JavaDoc MULTICAST_ADDRESS_PROPERTY =
39         "carol.cmi.multicast.address";
40     public static final String JavaDoc MULTICAST_ITF_PROPERTY =
41         "carol.cmi.multicast.itf";
42     public static final String JavaDoc MULTICAST_GROUPNAME_PROPERTY =
43         "carol.cmi.multicast.groupname";
44     public static final String JavaDoc RR_FACTOR_PROPERTY = "carol.cmi.rr.factor";
45     public static final String JavaDoc STUB_DEBUG_PROPERTY = "carol.cmi.stub.debug";
46
47     /**
48      * Set properties
49      * @param pr
50      */

51     public static synchronized void setProperties(Properties JavaDoc pr)
52         throws Exception JavaDoc {
53         if (configured) {
54             throw new Exception JavaDoc("Cmi already configured");
55         }
56         Iterator JavaDoc i = pr.entrySet().iterator();
57         while (i.hasNext()) {
58             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
59             String JavaDoc s = (String JavaDoc) e.getValue();
60             Object JavaDoc 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 JavaDoc a =
67                         InetAddress
68                             .getByName(s.substring(0, l))
69                             .getHostAddress();
70                     int p = new Integer JavaDoc(s.substring(l + 1)).intValue();
71                     multicastAddress = a;
72                     multicastPort = p;
73                 } catch (Exception JavaDoc ex) {
74                     throw new Exception JavaDoc(
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 JavaDoc(s.trim()).intValue();
83             } else if (k.equals(STUB_DEBUG_PROPERTY)) {
84                 stubDebug = new Boolean JavaDoc(s.trim()).booleanValue();
85             }
86         }
87         configured = true;
88     }
89
90     public static String JavaDoc 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 JavaDoc 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 JavaDoc 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