KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.UnknownHostException JavaDoc;
23
24 /**
25  * The aim of this class is to generate unique IDs to identify an instance of a
26  * clustered server. The IDs generated by this class are guaranteed to be
27  * unique on the whole network. Several methods may be provided.
28  * To generate an ID, time may be needed, because they can be generated by
29  * locking a well know resource for a period of time.
30  * @author Simon Nieuviarts
31  */

32 public class ClusterIdFactory {
33     private static byte localIdArray[];
34     private static ClusterId localId;
35     private static long date;
36     private static java.net.ServerSocket JavaDoc ss;
37
38     private ClusterIdFactory() {
39     }
40
41     public static synchronized void start() throws ClusterException {
42         startTypeIp();
43     }
44
45     /**
46      * Generate a cluster id based on a IP address.
47      * @param a ip address
48      */

49     private static synchronized void startTypeIp() throws ClusterException {
50         if (localIdArray != null) {
51             return;
52         }
53         InetAddress JavaDoc a;
54         try {
55             a = InetAddress.getLocalHost();
56         } catch (UnknownHostException JavaDoc e) {
57             throw new ClusterException("Error in getLocalHost() : " + e.toString());
58         }
59         byte aa[] = a.getAddress();
60         if (aa.length != 4) {
61             throw new ClusterException(
62                 "IP Address of size " + aa.length + " not supported by ClusterIdFactory");
63         }
64         if (aa[0] == 127) {
65             throw new ClusterException(
66                 "Loopback IP address not allowed in ClusterIdFactory : fix /etc/hosts (or equivalent) so that java.net.InetAddress.getLocalHost() does not return 127.x.x.x");
67         }
68         try {
69             ss = new java.net.ServerSocket JavaDoc(0, 1, a);
70         } catch (Exception JavaDoc e) {
71             throw new ClusterException(e.toString());
72         }
73         int p = ss.getLocalPort();
74         if ((p & 0xffff) != p) {
75             throw new ClusterException(
76                 "Invalid port number (more than 16 bits) in ClusterIdFactory");
77         }
78         date = System.currentTimeMillis() / 1000;
79         if (date >= 128L << 24) {
80             throw new ClusterException("Invalid date (too high) in ClusterIdFactory");
81         }
82         localIdArray = new byte[10];
83         localIdArray[0] = (byte) (p & 0xff);
84         localIdArray[1] = (byte) (p >> 8);
85         localIdArray[2] = aa[3];
86         localIdArray[3] = aa[2];
87         localIdArray[4] = aa[1];
88         localIdArray[5] = aa[0];
89         int n = 4;
90         long d = date;
91         while (n > 0) {
92             n--;
93             localIdArray[9 - n] = (byte) (d & 0xff);
94             d >>= 8;
95         }
96         localId = ClusterId.toClusterId(localIdArray);
97         SecureRandom.setSeed(localIdArray);
98     }
99
100     /**
101      * Return the local cluster Id. Is <code>null</code> if generate()
102      * has not been called before.
103      * @return the cluster Id
104      */

105     public static synchronized ClusterId getLocalId() throws ClusterException {
106         if (localIdArray == null) {
107             start();
108         }
109         if (ss == null) {
110             return localId;
111         }
112         while (true) {
113             long d = System.currentTimeMillis();
114             // d / 1000 should not be lower than date
115
if ((d / 1000) > date) {
116                 try {
117                     ss.close();
118                 } catch (java.io.IOException JavaDoc e) {
119                     // Can not close. Nothing to do...
120
}
121                 ss = null;
122                 return localId;
123             }
124             try {
125                 synchronized (localIdArray) {
126                     localIdArray.wait(1001 - (d % 1000));
127                 }
128             } catch (InterruptedException JavaDoc e) {
129                 // Nothing to do
130
}
131             d = System.currentTimeMillis();
132         }
133     }
134 }
135
Popular Tags