~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/ident/Identity.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
package org.jboss.remoting.ident;
23
 
 
24
 
import org.jboss.remoting.network.NetworkRegistry;
25
 
import org.jboss.remoting.util.SecurityUtility;
26
 
 
27
 
import javax.management.MBeanServer;
28
 
import javax.management.ObjectName;
29
 
import java.io.File;
30
 
import java.io.FileInputStream;
31
 
import java.io.FileNotFoundException;
32
 
import java.io.FileOutputStream;
33
 
import java.io.IOException;
34
 
import java.io.InputStream;
35
 
import java.io.OutputStream;
36
 
import java.io.Serializable;
37
 
import java.net.InetAddress;
38
 
import java.net.UnknownHostException;
39
 
import java.rmi.dgc.VMID;
40
 
import java.security.AccessController;
41
 
import java.security.PrivilegedAction;
42
 
import java.security.PrivilegedActionException;
43
 
import java.security.PrivilegedExceptionAction;
44
 
import java.util.Iterator;
45
 
import java.util.Map;
46
 
import java.util.Random;
47
 
import java.util.WeakHashMap;
48
 
 
49
 
/**
50
 
 * Identity is used to uniquely identify a JBoss server instance on the network.
51
 
 *
52
 
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
53
 
 * @version $Revision: 5002 $
54
 
 */
55
 
public class Identity implements Serializable
56
 
{
57
 
   static final long serialVersionUID = -2788084303665751253L;
58
 
 
59
 
   private static transient Random random = new Random(System.currentTimeMillis());
60
 
   public static transient String DEFAULT_DOMAIN = "JBOSS";
61
 
   private static transient String _domain = DEFAULT_DOMAIN;
62
 
   
63
 
   static
64
 
   {
65
 
      _domain = getSystemProperty("jboss.identity.domain", DEFAULT_DOMAIN);
66
 
   }
67
 
   
68
 
   private static transient Map identities = new WeakHashMap(2);
69
 
 
70
 
   private final String instanceid;
71
 
   private final InetAddress ip;
72
 
   private final String serverid;
73
 
   private String domain;
74
 
   private int hashCode;
75
 
 
76
 
   private Identity(InetAddress addr, String instanceid, String serverid)
77
 
   {
78
 
      this.ip = addr;
79
 
      this.instanceid = instanceid;
80
 
      this.serverid = serverid;
81
 
      this.domain = ((_domain == null || _domain.equals("")) ? DEFAULT_DOMAIN : _domain);
82
 
      calcHashCode();
83
 
   }
84
 
 
85
 
   private void calcHashCode()
86
 
   {
87
 
      this.hashCode = (ip.hashCode() + instanceid.hashCode() + serverid.hashCode() - domain.hashCode());
88
 
   }
89
 
 
90
 
   /**
91
 
    * set the domain for all active mbean servers
92
 
    */
93
 
   public static void setDomain(String domain)
94
 
   {
95
 
      Iterator iter = identities.keySet().iterator();
96
 
      while(iter.hasNext())
97
 
      {
98
 
         Identity ident = (Identity) identities.get(iter.next());
99
 
         if(ident != null)
100
 
         {
101
 
            ident.domain = domain;
102
 
         }
103
 
         ident.calcHashCode();
104
 
      }
105
 
      setSystemProperty("jboss.identity.domain", domain);
106
 
      _domain = domain;
107
 
      NetworkRegistry.getInstance().changeDomain(domain);
108
 
   }
109
 
 
110
 
 
111
 
   public int hashCode()
112
 
   {
113
 
      return hashCode;
114
 
   }
115
 
 
116
 
 
117
 
   public String toString()
118
 
   {
119
 
      return "JBOSS Identity\n\taddress:" + ip + "\n\tinstanceid:" + instanceid + "\n\tJMX id:" + serverid + "\n\tdomain:" + domain + "\n";
120
 
   }
121
 
 
122
 
   /**
123
 
    * return the domain for the server
124
 
    *
125
 
    * @return
126
 
    */
127
 
   public final String getDomain()
128
 
   {
129
 
      return domain;
130
 
   }
131
 
 
132
 
   /**
133
 
    * return the JBOSS Instance ID, which is the same between reboots of the same
134
 
    * JBOSS server instance.
135
 
    *
136
 
    * @return
137
 
    */
138
 
   public String getInstanceId()
139
 
   {
140
 
      return this.instanceid;
141
 
   }
142
 
 
143
 
   /**
144
 
    * return the JBOSS IP Address of the instance
145
 
    *
146
 
    * @return
147
 
    */
148
 
   public InetAddress getAddress()
149
 
   {
150
 
      return this.ip;
151
 
   }
152
 
 
153
 
   /**
154
 
    * return the JMX server ID for the JBoss instance, which is different between
155
 
    * reboots of the same JBOSS server instance.
156
 
    *
157
 
    * @return
158
 
    */
159
 
   public String getJMXId()
160
 
   {
161
 
      return this.serverid;
162
 
   }
163
 
 
164
 
   /**
165
 
    * returns true if the identity represents the same JVM
166
 
    *
167
 
    * @param identity
168
 
    * @return
169
 
    */
170
 
   public boolean isSameJVM(Identity identity)
171
 
   {
172
 
      return identity.equals(this);
173
 
   }
174
 
 
175
 
   /**
176
 
    * returns true if the identity represents the same JBOSS Instance, although
177
 
    * may not be the exact same process space since the JVM process space might be
178
 
    * different (such as a reboot).
179
 
    *
180
 
    * @param identity
181
 
    * @return
182
 
    */
183
 
   public boolean isSameInstance(Identity identity)
184
 
   {
185
 
      return identity.getInstanceId().equals(instanceid);
186
 
   }
187
 
 
188
 
   /**
189
 
    * returns true if the identity is on the same JBOSS machine, represented by the
190
 
    * same IP address (this may not work in the case several physically different
191
 
    * machines have the same IP Address).
192
 
    *
193
 
    * @param identity
194
 
    * @return
195
 
    */
196
 
   public boolean isSameMachine(Identity identity)
197
 
   {
198
 
      return identity.getAddress().equals(ip);
199
 
   }
200
 
 
201
 
   public boolean equals(Object obj)
202
 
   {
203
 
      if(obj instanceof Identity)
204
 
      {
205
 
         return hashCode == obj.hashCode();
206
 
      }
207
 
      return false;
208
 
   }
209
 
 
210
 
   public static synchronized final Identity get(final MBeanServer server)
211
 
   {
212
 
      if(identities.containsKey(server))
213
 
      {
214
 
         return (Identity) identities.get(server);
215
 
      }
216
 
      try
217
 
      {
218
 
         InetAddress localHost = getLocalHost();
219
 
         ObjectName objectName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
220
 
         String serverid = (String) getMBeanAttribute(server, objectName, "MBeanServerId");
221
 
         Identity identity = new Identity(localHost, createId(server), serverid);
222
 
         identities.put(server, identity);
223
 
         return identity;
224
 
      }
225
 
      catch(Exception ex)
226
 
      {
227
 
         String type = ex.getClass().getName();
228
 
         final RuntimeException rex = new RuntimeException("Exception creating identity: " + type + ": " + ex.getMessage());
229
 
         rex.setStackTrace(ex.getStackTrace());
230
 
         throw rex;
231
 
      }
232
 
   }
233
 
 
234
 
   private static final synchronized String createId(final MBeanServer server)
235
 
   {
236
 
      // we can set as a system property
237
 
      String myid = getSystemProperty("jboss.identity");
238
 
      if(myid != null)
239
 
      {
240
 
         return myid;
241
 
      }
242
 
      String id = null;
243
 
      File file = null;
244
 
      try
245
 
      {
246
 
         // FIRST TRY THE JBOSS guy to determine our data directory
247
 
         final ObjectName obj = new ObjectName("jboss.system:type=ServerConfig");
248
 
         File dir = (File) getMBeanAttribute(server, obj, "ServerDataDir");
249
 
         if(dir != null)
250
 
         {
251
 
            if(fileExists(dir) == false)
252
 
            {
253
 
               mkdirs(dir);
254
 
            }
255
 
            file = new File(dir, "jboss.identity");
256
 
         }
257
 
      }
258
 
      catch(Exception ex)
259
 
      {
260
 
      }
261
 
      if(file == null)
262
 
      {
263
 
         // we may not have that mbean, which is OK
264
 
         String fl = getSystemProperty("jboss.identity.dir", ".");
265
 
         
266
 
         File dir = new File(fl);
267
 
         if(fileExists(dir) == false)
268
 
         {
269
 
            mkdirs(dir);
270
 
         }
271
 
         file = new File(dir, "jboss.identity");
272
 
      }
273
 
 
274
 
      boolean canRead = canRead(file);
275
 
      if(fileExists(file) && canRead)
276
 
      {
277
 
         InputStream is = null;
278
 
         try
279
 
         {
280
 
            is = getFileInputStream(file);
281
 
            byte buf[] = new byte[800];
282
 
            int c = is.read(buf);
283
 
            id = new String(buf, 0, c);
284
 
         }
285
 
         catch(Exception ex)
286
 
         {
287
 
            throw new RuntimeException("Error loading jboss.identity: " + ex.toString());
288
 
         }
289
 
         finally
290
 
         {
291
 
            if(is != null)
292
 
            {
293
 
               try
294
 
               {
295
 
                  is.close();
296
 
               }
297
 
               catch(Exception ig)
298
 
               {
299
 
               }
300
 
            }
301
 
         }
302
 
      }
303
 
      else
304
 
      {
305
 
         OutputStream out = null;
306
 
         try
307
 
         {
308
 
            id = createUniqueID();
309
 
            if(fileExists(file) == false)
310
 
            {
311
 
               createNewFile(file);
312
 
            }
313
 
 
314
 
            out = getFileOutputStream(file);
315
 
            out.write(id.getBytes());
316
 
         }
317
 
         catch(Exception ex)
318
 
         {
319
 
            throw new RuntimeException("Error creating Instance ID: " + ex.toString());
320
 
         }
321
 
         finally
322
 
         {
323
 
            if(out != null)
324
 
            {
325
 
               try
326
 
               {
327
 
                  out.flush();
328
 
                  out.close();
329
 
               }
330
 
               catch(Exception ig)
331
 
               {
332
 
               }
333
 
            }
334
 
         }
335
 
      }
336
 
      
337
 
      setSystemProperty("jboss.identity", id);
338
 
      
339
 
      return id;
340
 
   }
341
 
 
342
 
   public static final String createUniqueID()
343
 
   {
344
 
      String id = new VMID().toString();
345
 
      // colons don't work in JMX
346
 
      return id.replace(':', 'x') + random.nextInt(1000);
347
 
   }
348
 
   
349
 
   static private boolean fileExists(final File file)
350
 
   {
351
 
      if (file == null)
352
 
         return false;
353
 
      
354
 
      if (SecurityUtility.skipAccessControl())
355
 
      {
356
 
         return file.exists();
357
 
      }
358
 
 
359
 
      return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
360
 
      {
361
 
         public Object run()
362
 
         {
363
 
            return new Boolean(file.exists());
364
 
         }
365
 
      })).booleanValue();
366
 
   }
367
 
   
368
 
   static private boolean mkdirs(final File dir)
369
 
   {
370
 
      if (SecurityUtility.skipAccessControl())
371
 
      {
372
 
         return dir.mkdirs();
373
 
      }
374
 
      
375
 
      return ((Boolean) AccessController.doPrivileged( new PrivilegedAction()
376
 
      {
377
 
         public Object run()
378
 
         {
379
 
            return new Boolean(dir.mkdirs());
380
 
         }
381
 
      })).booleanValue();
382
 
   }
383
 
   
384
 
   static private FileInputStream getFileInputStream(final File file) throws FileNotFoundException
385
 
   {
386
 
      if (SecurityUtility.skipAccessControl())
387
 
      {
388
 
         return new FileInputStream(file);
389
 
      }
390
 
      
391
 
      try
392
 
      {
393
 
         return (FileInputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
394
 
         {
395
 
            public Object run() throws FileNotFoundException
396
 
            {
397
 
               return new FileInputStream(file);
398
 
            }
399
 
         });
400
 
      }
401
 
      catch (PrivilegedActionException e)
402
 
      {
403
 
         throw (FileNotFoundException) e.getCause();
404
 
      }
405
 
   }
406
 
   
407
 
   static private FileOutputStream getFileOutputStream(final File file)
408
 
   throws FileNotFoundException
409
 
   {
410
 
      if (SecurityUtility.skipAccessControl())
411
 
      {
412
 
         return new FileOutputStream(file);
413
 
      }
414
 
      
415
 
      try
416
 
      {
417
 
         return (FileOutputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
418
 
         {
419
 
            public Object run() throws FileNotFoundException
420
 
            {
421
 
               return new FileOutputStream(file);
422
 
            }
423
 
         });
424
 
      }
425
 
      catch (PrivilegedActionException e)
426
 
      {
427
 
         throw (FileNotFoundException) e.getCause();
428
 
      }
429
 
   }
430
 
   
431
 
   static private boolean canRead(final File file)
432
 
   {
433
 
      if (SecurityUtility.skipAccessControl())
434
 
      {
435
 
         return file.canRead();
436
 
      }
437
 
      
438
 
      return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
439
 
      {
440
 
         public Object run()
441
 
         {
442
 
            return new Boolean(file.canRead());
443
 
         }
444
 
      })).booleanValue();
445
 
   }
446
 
   
447
 
   static private boolean createNewFile(final File file) throws IOException
448
 
   {
449
 
      if (SecurityUtility.skipAccessControl())
450
 
      {
451
 
         return file.createNewFile();
452
 
      }
453
 
      
454
 
      try
455
 
      {
456
 
         return ((Boolean)AccessController.doPrivileged( new PrivilegedExceptionAction()
457
 
         {
458
 
            public Object run() throws Exception
459
 
            {
460
 
               return new Boolean(file.createNewFile());
461
 
            }
462
 
         })).booleanValue();
463
 
      }
464
 
      catch (Exception e)
465
 
      {
466
 
         throw (IOException) e.getCause();
467
 
      }
468
 
   }
469
 
   
470
 
   static private Object getMBeanAttribute(final MBeanServer server, final ObjectName objectName, final String attribute)
471
 
   throws Exception
472
 
   {
473
 
      if (SecurityUtility.skipAccessControl())
474
 
      {
475
 
         return server.getAttribute(objectName, attribute);
476
 
      }
477
 
      
478
 
      try
479
 
      {
480
 
         return AccessController.doPrivileged( new PrivilegedExceptionAction()
481
 
         {
482
 
            public Object run() throws Exception
483
 
            {
484
 
               return server.getAttribute(objectName, attribute);
485
 
            }
486
 
         });
487
 
      }
488
 
      catch (PrivilegedActionException e)
489
 
      {
490
 
         throw (Exception) e.getCause();
491
 
      }  
492
 
   }
493
 
   
494
 
   static private String getSystemProperty(final String name, final String defaultValue)
495
 
   {
496
 
      if (SecurityUtility.skipAccessControl())
497
 
         return System.getProperty(name, defaultValue);
498
 
         
499
 
      String value = null;
500
 
      try
501
 
      {
502
 
         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
503
 
         {
504
 
            public Object run() throws Exception
505
 
            {
506
 
               return System.getProperty(name, defaultValue);
507
 
            }
508
 
         });
509
 
      }
510
 
      catch (PrivilegedActionException e)
511
 
      {
512
 
         throw (RuntimeException) e.getCause();
513
 
      }
514
 
      
515
 
      return value;
516
 
   }
517
 
   
518
 
   static private String getSystemProperty(final String name)
519
 
   {
520
 
      if (SecurityUtility.skipAccessControl())
521
 
         return System.getProperty(name);
522
 
      
523
 
      String value = null;
524
 
      try
525
 
      {
526
 
         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
527
 
         {
528
 
            public Object run() throws Exception
529
 
            {
530
 
               return System.getProperty(name);
531
 
            }
532
 
         });
533
 
      }
534
 
      catch (PrivilegedActionException e)
535
 
      {
536
 
         throw (RuntimeException) e.getCause();
537
 
      }
538
 
      
539
 
      return value;
540
 
   }
541
 
   
542
 
   static private void setSystemProperty(final String name, final String value)
543
 
   {
544
 
      if (SecurityUtility.skipAccessControl())
545
 
      {
546
 
         System.setProperty(name, value);
547
 
         return;
548
 
      }
549
 
      
550
 
      try
551
 
      {
552
 
         AccessController.doPrivileged( new PrivilegedExceptionAction()
553
 
         {
554
 
            public Object run() throws Exception
555
 
            {
556
 
               return System.setProperty(name, value);
557
 
            }
558
 
         });
559
 
      }
560
 
      catch (PrivilegedActionException e)
561
 
      {
562
 
         throw (RuntimeException) e.getCause();
563
 
      }
564
 
   }
565
 
   
566
 
   static private InetAddress getLocalHost() throws UnknownHostException
567
 
   {
568
 
      if (SecurityUtility.skipAccessControl())
569
 
      {
570
 
         try
571
 
         {
572
 
            return InetAddress.getLocalHost();
573
 
         }
574
 
         catch (IOException e)
575
 
         {
576
 
            return InetAddress.getByName("127.0.0.1");
577
 
         }
578
 
      }
579
 
 
580
 
      try
581
 
      {
582
 
         return (InetAddress) AccessController.doPrivileged( new PrivilegedExceptionAction()
583
 
         {
584
 
            public Object run() throws IOException
585
 
            {
586
 
               try
587
 
               {
588
 
                  return InetAddress.getLocalHost();
589
 
               }
590
 
               catch (IOException e)
591
 
               {
592
 
                  return InetAddress.getByName("127.0.0.1");
593
 
               }
594
 
            }
595
 
         });
596
 
      }
597
 
      catch (PrivilegedActionException e)
598
 
      {
599
 
         throw (UnknownHostException) e.getCause();
600
 
      }
601
 
   }
602
 
}