1 16 17 package org.apache.jetspeed.services.idgenerator; 18 19 import javax.servlet.ServletConfig ; 21 22 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 24 import org.apache.jetspeed.services.logging.JetspeedLogger; 25 26 import org.apache.turbine.services.InitializationException; 28 import org.apache.turbine.services.resources.ResourceService; 29 import org.apache.turbine.services.TurbineBaseService; 30 import org.apache.turbine.services.TurbineServices; 31 32 38 public class JetspeedIdGeneratorService extends TurbineBaseService 39 implements IdGeneratorService 40 { 41 44 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedIdGeneratorService.class.getName()); 45 46 private final static String CONFIG_COUNTER_START = "counter.start"; 48 private final static String CONFIG_PEID_PREFIX = "peid.prefix"; 49 private final static String CONFIG_PEID_SUFFIX = "peid.suffix"; 50 51 private final static long DEFAULT_CONFIG_COUNTER_START = 0x10000; 53 private final static String DEFAULT_CONFIG_PEID_PREFIX = "P-"; 54 private final static String DEFAULT_CONFIG_PEID_SUFFIX = ""; 55 56 private static String peidPrefix = null; 58 private static String peidSuffix = null; 59 60 protected static long idCounter; 61 62 69 public synchronized void init(ServletConfig conf) throws InitializationException { 70 71 if (getInit()) return; 73 74 initConfiguration(); 75 76 setInit(true); 78 79 } 80 87 public void init() throws InitializationException { 88 logger.info( "Late init for JetspeedIdGeneratorService called" ); 89 while( !getInit() ) { 90 try { 92 Thread.sleep( 100 ); 93 logger.info( "Waiting for init of JetspeedIdGeneratorService..." ); 94 } catch (InterruptedException ie ) { 95 logger.error("Exception", ie); 96 } 97 } 98 } 99 100 104 public void shutdown() 105 { 106 logger.info( "Shutdown for JetspeedIdGeneratorService called. idCounter = " 107 + idCounter + " (" + Long.toHexString(idCounter) + ")" ); 108 } 109 110 117 private void initConfiguration() throws InitializationException 118 { 119 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) 121 .getResources(IdGeneratorService.SERVICE_NAME); 122 123 peidPrefix = serviceConf.getString( CONFIG_PEID_PREFIX, DEFAULT_CONFIG_PEID_PREFIX ); 124 peidSuffix = serviceConf.getString( CONFIG_PEID_SUFFIX, DEFAULT_CONFIG_PEID_SUFFIX ); 125 synchronized(JetspeedIdGeneratorService.class) 126 { 127 idCounter = serviceConf.getLong( CONFIG_COUNTER_START, DEFAULT_CONFIG_COUNTER_START ); 128 } 129 130 } 131 132 public JetspeedIdGeneratorService() { 133 } 134 135 139 public String getNextPeid() 140 { 141 long newid; 142 143 synchronized(JetspeedIdGeneratorService.class) 144 { 145 newid = idCounter++; 146 } 147 148 return peidPrefix + Long.toHexString(System.currentTimeMillis()) + "-" 149 + Long.toHexString(newid) + peidSuffix; 150 } 151 152 } 153 | Popular Tags |