1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.idgenerator;
18
19
20 import javax.servlet.ServletConfig;
21
22
23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25
26
27 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 /***
33 * Simple implementation of the IdGeneratorService.
34 *
35 * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
36 * @version $Id: JetspeedIdGeneratorService.java,v 1.5 2004/02/23 03:28:57 jford Exp $
37 */
38 public class JetspeedIdGeneratorService extends TurbineBaseService
39 implements IdGeneratorService
40 {
41 /***
42 * Static initialization of the logger for this class
43 */
44 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedIdGeneratorService.class.getName());
45
46
47 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
52 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
57 private static String peidPrefix = null;
58 private static String peidSuffix = null;
59
60 protected static long idCounter;
61
62 /***
63 * This is the early initialization method called by the
64 * Turbine <code>Service</code> framework
65 * @param conf The <code>ServletConfig</code>
66 * @exception throws a <code>InitializationException</code> if the service
67 * fails to initialize
68 */
69 public synchronized void init(ServletConfig conf) throws InitializationException {
70
71
72 if (getInit()) return;
73
74 initConfiguration();
75
76
77 setInit(true);
78
79 }
80 /***
81 * This is the lateinitialization method called by the
82 * Turbine <code>Service</code> framework
83 *
84 * @exception throws a <code>InitializationException</code> if the service
85 * fails to initialize
86 */
87 public void init() throws InitializationException {
88 logger.info( "Late init for JetspeedIdGeneratorService called" );
89 while( !getInit() ) {
90
91 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 /***
101 * This is the shutdown method called by the
102 * Turbine <code>Service</code> framework
103 */
104 public void shutdown()
105 {
106 logger.info( "Shutdown for JetspeedIdGeneratorService called. idCounter = "
107 + idCounter + " (" + Long.toHexString(idCounter) + ")" );
108 }
109
110 /***
111 * Loads the configuration parameters for this service from the
112 * JetspeedResources.properties file.
113 *
114 * @exception throws a <code>InitializationException</code> if the service
115 * fails to initialize
116 */
117 private void initConfiguration() throws InitializationException
118 {
119
120 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 /*** Creates new JetspeedIdGeneratorService */
132 public JetspeedIdGeneratorService() {
133 }
134
135 /***
136 * Generate a Unique PEID
137 * @return Unique PEID
138 */
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 }