View Javadoc

1   /*$Id: GloballyUniqueIdGen.java,v 1.2 2004/07/01 11:16:22 nw Exp $
2    * Created on 16-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.manager.idgen;
12  
13  import org.astrogrid.component.descriptor.ComponentDescriptor;
14  
15  import java.net.InetAddress;
16  import java.util.Random;
17  
18  import junit.framework.Test;
19  
20  /*** Implementation of idgen that should produce globally-unique urn-style identifiers.
21   * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
22   *
23   */
24  public class GloballyUniqueIdGen implements IdGen, ComponentDescriptor {
25      /*** Construct a new GloballyUniqueIdGen
26       * 
27       */
28      public GloballyUniqueIdGen() {
29          super();
30      }
31  
32      
33      /*** stuff for generating a unique job urn */
34      protected static String hostname;
35      static {
36          hostname = null;
37          try {
38              hostname = InetAddress.getLocalHost().toString();
39          } catch (Exception e) {
40              hostname="unavailable";
41          }
42      }
43      private static Random rand = new Random();
44  
45      /***
46       * @see org.astrogrid.applications.manager.idgen.IdGen#getNewID()
47       */
48      public synchronized String getNewID() {
49  
50          StringBuffer
51              buffer = new StringBuffer(128);
52          //Account acc = job.getCredentials().getAccount();
53          buffer
54              .append("cea:")
55              .append(hostname) // should ensure its globally unique
56              /* if I passed in user / account info , could add all this..
57              .append('/')         
58             .append( acc.getName() )
59             .append( '@' )
60             .append( acc.getCommunity() )
61             */
62             .append( '/' )          // should ensure its system-unique - we'd need to have 2 simultaneous (in same milli) concurrent requests, that both generate the same random number. 
63             .append( System.currentTimeMillis()) 
64             .append( ':' )
65             .append(Math.abs(rand.nextInt()));
66          return buffer.toString();
67          
68      }
69  
70  
71      /***
72       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
73       */
74      public String getName() {
75          return "Globally-Unique ID generator";
76      }
77  
78  
79      /***
80       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
81       */
82      public String getDescription() {
83          return "generates identifiers such as " + getNewID();
84      }
85  
86  
87      /***
88       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
89       */
90      public Test getInstallationTest() {
91          return null;
92      }    
93  }
94  
95  
96  /* 
97  $Log: GloballyUniqueIdGen.java,v $
98  Revision 1.2  2004/07/01 11:16:22  nw
99  merged in branch
100 nww-itn06-componentization
101 
102 Revision 1.1.2.2  2004/07/01 01:42:47  nw
103 final version, before merge
104 
105 Revision 1.1.2.1  2004/06/17 09:21:23  nw
106 finished all major functionality additions to core
107  
108 */