View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /***
25   * Simple implementation of the IdGeneratorService.
26   *
27   * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
28   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
29   * @version $Id: JetspeedIdGenerator.java 516448 2007-03-09 16:25:47Z ate $
30   */
31  public class JetspeedIdGenerator implements IdGenerator
32  {
33      private final static Log log = LogFactory.getLog(JetspeedIdGenerator.class);
34  
35      // default configuration values
36      private final static long DEFAULT_CONFIG_COUNTER_START = 0x10000;
37      private final static String DEFAULT_CONFIG_PEID_PREFIX = "P-";
38      private final static String DEFAULT_CONFIG_PEID_SUFFIX = "";
39  
40      // configuration parameters
41      private String peidPrefix = null;
42      private String peidSuffix = null;
43      protected long idCounter;
44  
45      public JetspeedIdGenerator()
46      {
47          this.idCounter = DEFAULT_CONFIG_COUNTER_START;
48          this.peidPrefix = DEFAULT_CONFIG_PEID_PREFIX;
49          this.peidSuffix = DEFAULT_CONFIG_PEID_SUFFIX;         
50      }
51  
52      public JetspeedIdGenerator(long counterStart)
53      {
54          this.idCounter = counterStart;
55          this.peidPrefix = DEFAULT_CONFIG_PEID_PREFIX;
56          this.peidSuffix = DEFAULT_CONFIG_PEID_SUFFIX; 
57      }
58  
59      public JetspeedIdGenerator(long counterStart, String prefix, String suffix)
60      {
61          this.idCounter = counterStart;
62          this.peidPrefix = prefix;
63          this.peidSuffix = suffix; 
64      }
65      
66      public void start() 
67      {
68          log.info( "Start JetspeedIdGenerator");        
69       }
70  
71      public void stop() 
72      {
73          log.info( "Shutdown for JetspeedIdGenerator called. idCounter = "
74               + idCounter + " (" + Long.toHexString(idCounter) + ")" ); 
75      }
76  
77      /***
78       * Generate a Unique PEID
79       * @return Unique PEID
80       */
81      public String getNextPeid()
82      {
83          long newid;
84  
85          synchronized(JetspeedIdGenerator.class)
86          {
87              newid = idCounter++;
88          }
89          
90          return peidPrefix + Long.toHexString(System.currentTimeMillis()) + "-" 
91                 + Long.toHexString(newid) + peidSuffix;
92      }
93      
94  }