View Javadoc

1   package org.lsst.ccs.utilities.tracers;
2   
3   import java.net.InetAddress;
4   import java.net.UnknownHostException;
5   import java.util.GregorianCalendar;
6   
7   /**
8    * @author bamade
9    */
10  // Date: 30/08/13
11  
12  public class Names {
13      public static final long LSST_EPOCH = new GregorianCalendar(2014,1,1).getTimeInMillis();
14      /**
15       * generates a (nearly) unique name for a given machine.
16       * This can be used if one wants something more "readable" than a UUID
17       * <B>But</B> since it uses a timestamp then calling this method in rapid succession
18       * will generate the same name based on the same timestamp.
19       *  this duplication could also happen if this method is called by two different process
20       *  on the same machine.
21       * So it is ok only to generate a name called once on a machine
22       * @param seed any string including <TT>null</TT>
23       * @return a concatenation of the seed, the machine name and a base 36 representation of the time
24       */
25      public static String almostUniqueAgentName(String seed) {
26          String startName = (seed!= null)? seed + "_" : "" ;
27          //concatenate system name
28          String systemName;
29          try {
30             systemName= InetAddress.getLocalHost().getHostName();
31          } catch (UnknownHostException e) {
32              //TODO : getMachAddress
33              systemName= "?" ;
34          }
35          // then time
36          //String timeString = Long.toString(simpleTimestamp() , Character.MAX_RADIX) ;
37          String timeString = toBase64String(simpleTimestamp()) ;
38          return String.format("%s%s_%s",startName, systemName,timeString) ;
39      }
40  
41      /**
42       * same as <TT>uniqueAgentName(null)</TT>
43       * @return
44       */
45      public static String almostUniqueAgentName() {
46          return almostUniqueAgentName(null) ;
47      }
48  
49      /**
50       * can used to generate simple names based on a seed and a timestamp.
51       * The string is not guaranteed unique if generated in rapid succession or in concurrent processes
52       * @param seed any String (including <TT>null</TT>)
53       * @return
54       */
55      public static String almostUniqueLocalName(String seed) {
56          String startName = (seed!= null)? seed + "_" : "" ;
57          //String timeString = Long.toString(simpleTimestamp() , Character.MAX_RADIX) ;
58          String timeString = toBase64String(simpleTimestamp()) ;
59          return String.format("%s%s",startName,timeString) ;
60      }
61  
62  
63      /**
64       * returns a short timestamp starting from LSST_EPOCH
65       * @return
66       */
67      public static long simpleTimestamp() {
68          long timeStamp = System.currentTimeMillis() ;
69          return timeStamp - LSST_EPOCH ;
70      }
71      private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
72              + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "_=";
73  
74      /**
75       * transforms a long value in an (unconventional) base64 String
76       * (it is not the standard base64 encoding but contains character '=' instead of '/')
77       * @param val
78       * @return
79       */
80      public static String toBase64String(long val) {
81          StringBuffer stb = new StringBuffer() ;
82          boolean negative = val < 0 ;
83          if(negative) {
84              val = - val ;
85          }
86          while(val > 0) {
87              int modulo = (int) (val % 64) ;
88              stb.insert(0,base64code.charAt(modulo)) ;
89              val = val / 64 ;
90          }
91          return negative? "-" + stb.toString(): stb.toString() ;
92      }
93  }