1 package org.apache.turbine.services.uniqueid; 2 3 18 19 import java.security.MessageDigest ; 20 21 import org.apache.commons.codec.base64.Base64; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.apache.turbine.Turbine; 27 import org.apache.turbine.services.InitializationException; 28 import org.apache.turbine.services.TurbineBaseService; 29 import org.apache.turbine.util.GenerateUniqueId; 30 import org.apache.turbine.util.RunData; 31 32 39 public class TurbineUniqueIdService 40 extends TurbineBaseService 41 implements UniqueIdService 42 { 43 44 private static Log log = LogFactory.getLog(TurbineUniqueIdService.class); 45 46 47 protected static String turbineId = "UNKNOWN"; 48 49 protected static String turbineURL = "UNKNOWN"; 50 51 protected static int counter; 52 53 56 public void init(RunData data) 57 throws InitializationException 58 { 59 init(); 60 } 61 62 66 public void init() 67 throws InitializationException 68 { 69 try 70 { 71 String url = Turbine.getDefaultServerData().toString(); 78 79 MessageDigest md = MessageDigest.getInstance("MD5"); 80 byte [] bytesId = md.digest(url.getBytes("UTF-8")); 81 turbineId = new String (Base64.encode(bytesId)); 82 83 log.info("This is Turbine instance running at: " + url); 84 log.info("The instance id is #" + turbineId); 85 setInit(true); 86 } 87 catch (Exception e) 88 { 89 throw new InitializationException( 90 "Could not initialize TurbineUniqueId Service", e); 91 } 92 } 93 94 97 public void shutdown() 98 { 99 log.info("Turbine instance running at " + turbineURL + " shutting down."); 100 } 101 102 112 public String getInstanceId() 113 { 114 return turbineId; 115 } 116 117 124 public String getUniqueId() 125 { 126 int current; 127 synchronized (TurbineUniqueIdService.class) 128 { 129 current = counter++; 130 } 131 String id = Integer.toString(current); 132 133 if (current < 100000000) 136 { 137 id = ("00000000" + id).substring(id.length()); 138 } 139 return id; 140 } 141 142 147 public String getPseudorandomId() 148 { 149 return GenerateUniqueId.getIdentifier(); 150 } 151 } 152 | Popular Tags |