KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > idgenerator > JetspeedIdGeneratorService


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.services.idgenerator;
18
19 // Java
20
import javax.servlet.ServletConfig JavaDoc;
21
22 // Jetspeed
23
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24 import org.apache.jetspeed.services.logging.JetspeedLogger;
25
26 // Turbine
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     // configuration keys
47
private final static String JavaDoc CONFIG_COUNTER_START = "counter.start";
48     private final static String JavaDoc CONFIG_PEID_PREFIX = "peid.prefix";
49     private final static String JavaDoc CONFIG_PEID_SUFFIX = "peid.suffix";
50
51     // default configuration values
52
private final static long DEFAULT_CONFIG_COUNTER_START = 0x10000;
53     private final static String JavaDoc DEFAULT_CONFIG_PEID_PREFIX = "P-";
54     private final static String JavaDoc DEFAULT_CONFIG_PEID_SUFFIX = "";
55
56     // configuration parameters
57
private static String JavaDoc peidPrefix = null;
58     private static String JavaDoc 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 JavaDoc conf) throws InitializationException {
70
71         // already initialized
72
if (getInit()) return;
73
74         initConfiguration();
75
76         // initialization done
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             //Not yet...
91
try {
92                 Thread.sleep( 100 );
93                 logger.info( "Waiting for init of JetspeedIdGeneratorService..." );
94             } catch (InterruptedException JavaDoc 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         // get configuration parameters from Jetspeed Resources
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 JavaDoc 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