01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.idgenerator;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21:
22: /**
23: * Simple implementation of the IdGeneratorService.
24: *
25: * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
26: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
27: * @version $Id: JetspeedIdGenerator.java 516448 2007-03-09 16:25:47Z ate $
28: */
29: public class JetspeedIdGenerator implements IdGenerator {
30: private final static Log log = LogFactory
31: .getLog(JetspeedIdGenerator.class);
32:
33: // default configuration values
34: private final static long DEFAULT_CONFIG_COUNTER_START = 0x10000;
35: private final static String DEFAULT_CONFIG_PEID_PREFIX = "P-";
36: private final static String DEFAULT_CONFIG_PEID_SUFFIX = "";
37:
38: // configuration parameters
39: private String peidPrefix = null;
40: private String peidSuffix = null;
41: protected long idCounter;
42:
43: public JetspeedIdGenerator() {
44: this .idCounter = DEFAULT_CONFIG_COUNTER_START;
45: this .peidPrefix = DEFAULT_CONFIG_PEID_PREFIX;
46: this .peidSuffix = DEFAULT_CONFIG_PEID_SUFFIX;
47: }
48:
49: public JetspeedIdGenerator(long counterStart) {
50: this .idCounter = counterStart;
51: this .peidPrefix = DEFAULT_CONFIG_PEID_PREFIX;
52: this .peidSuffix = DEFAULT_CONFIG_PEID_SUFFIX;
53: }
54:
55: public JetspeedIdGenerator(long counterStart, String prefix,
56: String suffix) {
57: this .idCounter = counterStart;
58: this .peidPrefix = prefix;
59: this .peidSuffix = suffix;
60: }
61:
62: public void start() {
63: log.info("Start JetspeedIdGenerator");
64: }
65:
66: public void stop() {
67: log
68: .info("Shutdown for JetspeedIdGenerator called. idCounter = "
69: + idCounter
70: + " ("
71: + Long.toHexString(idCounter) + ")");
72: }
73:
74: /**
75: * Generate a Unique PEID
76: * @return Unique PEID
77: */
78: public String getNextPeid() {
79: long newid;
80:
81: synchronized (JetspeedIdGenerator.class) {
82: newid = idCounter++;
83: }
84:
85: return peidPrefix
86: + Long.toHexString(System.currentTimeMillis()) + "-"
87: + Long.toHexString(newid) + peidSuffix;
88: }
89:
90: }
|