1 9 package org.jboss.remoting.ident; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.io.Serializable ; 17 import java.net.InetAddress ; 18 import java.rmi.dgc.VMID ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Random ; 22 import java.util.WeakHashMap ; 23 import javax.management.MBeanServer ; 24 import javax.management.ObjectName ; 25 import org.jboss.remoting.network.NetworkRegistry; 26 27 33 public class Identity implements Serializable 34 { 35 static final long serialVersionUID = -2788084303665751253L; 36 37 private static transient Random random = new Random (System.currentTimeMillis()); 38 public static transient String DEFAULT_DOMAIN = "JBOSS"; 39 private static transient String _domain = System.getProperty("jboss.identity.domain", DEFAULT_DOMAIN); 40 private static transient Map identities = new WeakHashMap (2); 41 42 private final String instanceid; 43 private final InetAddress ip; 44 private final String serverid; 45 private String domain; 46 private int hashCode; 47 48 private Identity(InetAddress addr, String instanceid, String serverid) 49 { 50 this.ip = addr; 51 this.instanceid = instanceid; 52 this.serverid = serverid; 53 this.domain = ((_domain == null || _domain.equals("")) ? DEFAULT_DOMAIN : _domain); 54 calcHashCode(); 55 } 56 57 private void calcHashCode() 58 { 59 this.hashCode = (ip.hashCode() + instanceid.hashCode() + serverid.hashCode() - domain.hashCode()); 60 } 61 62 65 public static void setDomain(String domain) 66 { 67 Iterator iter = identities.keySet().iterator(); 68 while(iter.hasNext()) 69 { 70 Identity ident = (Identity) identities.get(iter.next()); 71 if(ident != null) 72 { 73 ident.domain = domain; 74 } 75 ident.calcHashCode(); 76 } 77 System.setProperty("jboss.identity.domain", domain); 78 _domain = domain; 79 NetworkRegistry.getInstance().changeDomain(domain); 80 } 81 82 83 public int hashCode() 84 { 85 return hashCode; 86 } 87 88 89 public String toString() 90 { 91 return "JBOSS Identity [address:" + ip + ",instanceid:" + instanceid + ",JMX id:" + serverid + ",domain:" + domain + "]"; 92 } 93 94 99 public final String getDomain() 100 { 101 return domain; 102 } 103 104 110 public String getInstanceId() 111 { 112 return this.instanceid; 113 } 114 115 120 public InetAddress getAddress() 121 { 122 return this.ip; 123 } 124 125 131 public String getJMXId() 132 { 133 return this.serverid; 134 } 135 136 142 public boolean isSameJVM(Identity identity) 143 { 144 return identity.equals(this); 145 } 146 147 155 public boolean isSameInstance(Identity identity) 156 { 157 return identity.getInstanceId().equals(instanceid); 158 } 159 160 168 public boolean isSameMachine(Identity identity) 169 { 170 return identity.getAddress().equals(ip); 171 } 172 173 public boolean equals(Object obj) 174 { 175 if(obj instanceof Identity) 176 { 177 return hashCode == obj.hashCode(); 178 } 179 return false; 180 } 181 182 public static synchronized final Identity get(MBeanServer server) 183 { 184 if(identities.containsKey(server)) 185 { 186 return (Identity) identities.get(server); 187 } 188 try 189 { 190 String serverid = (String ) server.getAttribute(new ObjectName ("JMImplementation:type=MBeanServerDelegate"), "MBeanServerId"); 191 Identity identity = new Identity(InetAddress.getLocalHost(), createId(server), serverid); 192 identities.put(server, identity); 193 return identity; 194 } 195 catch(Exception ex) 196 { 197 throw new RuntimeException ("Exception creating identity: " + ex.getMessage()); 198 } 199 } 200 201 private static final synchronized String createId(MBeanServer server) 202 { 203 String myid = System.getProperty("jboss.identity"); 205 if(myid != null) 206 { 207 return myid; 208 } 209 String id = null; 210 File file = null; 211 try 212 { 213 ObjectName obj = new ObjectName ("jboss.system:type=ServerConfig"); 215 File dir = (File ) server.getAttribute(obj, "ServerDataDir"); 216 if(dir != null) 217 { 218 if(dir.exists() == false) 219 { 220 dir.mkdirs(); 221 } 222 file = new File (dir, "jboss.identity"); 223 } 224 } 225 catch(Exception ex) 226 { 227 } 228 if(file == null) 229 { 230 String fl = System.getProperty("jboss.identity.dir", "."); 232 File dir = new File (fl); 233 if(dir.exists() == false) 234 { 235 dir.mkdirs(); 236 } 237 file = new File (dir, "jboss.identity"); 238 } 239 if(file.exists() && file.canRead()) 240 { 241 InputStream is = null; 242 try 243 { 244 is = new FileInputStream (file); 245 byte buf[] = new byte[800]; 246 int c = is.read(buf); 247 id = new String (buf, 0, c); 248 } 249 catch(Exception ex) 250 { 251 throw new RuntimeException ("Error loading jboss.identity: " + ex.toString()); 252 } 253 finally 254 { 255 if(is != null) 256 { 257 try 258 { 259 is.close(); 260 } 261 catch(Exception ig) 262 { 263 } 264 } 265 } 266 } 267 else 268 { 269 OutputStream out = null; 270 try 271 { 272 id = createUniqueID(); 273 if(file.exists() == false) 274 { 275 file.createNewFile(); 276 } 277 out = new FileOutputStream (file); 278 out.write(id.getBytes()); 279 } 280 catch(Exception ex) 281 { 282 throw new RuntimeException ("Error creating Instance ID: " + ex.toString()); 283 } 284 finally 285 { 286 if(out != null) 287 { 288 try 289 { 290 out.flush(); 291 out.close(); 292 } 293 catch(Exception ig) 294 { 295 } 296 } 297 } 298 } 299 System.setProperty("jboss.identity", id); 300 return id; 301 } 302 303 public static final String createUniqueID() 304 { 305 String id = new VMID ().toString(); 306 return id.replace(':', 'x') + random.nextInt(1000); 308 } 309 } 310 | Popular Tags |