KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > IdGenerator


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

18 package org.apache.activemq.util;
19 import java.net.InetAddress JavaDoc;
20 import java.net.ServerSocket JavaDoc;
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 /**
25  * Generator for Globally unique Strings.
26  */

27
28 public class IdGenerator{
29
30     private static final Logger JavaDoc log = Logger.getLogger(IdGenerator.class.getName());
31     private static final String JavaDoc UNIQUE_STUB;
32     private static int instanceCount;
33     private static String JavaDoc hostName;
34     private String JavaDoc seed;
35     private long sequence;
36     
37     static {
38         String JavaDoc stub = "";
39         boolean canAccessSystemProps = true;
40         try{
41             SecurityManager JavaDoc sm = System.getSecurityManager();
42             if(sm != null){
43                 sm.checkPropertiesAccess();
44             }
45         }catch(SecurityException JavaDoc se){
46             canAccessSystemProps = false;
47         }
48         
49         if ( canAccessSystemProps) {
50             try {
51                 hostName = InetAddress.getLocalHost().getHostName();
52                 ServerSocket JavaDoc ss = new ServerSocket JavaDoc(0);
53                 stub="-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-";
54                 Thread.sleep(100);
55                 ss.close();
56             }catch(Exception JavaDoc ioe){
57                 log.log(Level.WARNING, "could not generate unique stub",ioe);
58             }
59         }else{
60             hostName="localhost";
61             stub = "-1-" +System.currentTimeMillis() +"-";
62         }
63         UNIQUE_STUB = stub;
64     }
65     
66     /**
67      * As we have to find the hostname as a side-affect of generating
68      * a unique stub, we allow it's easy retrevial here
69      * @return the local host name
70      */

71     
72     public static String JavaDoc getHostName(){
73         return hostName;
74     }
75     
76     /**
77      * Construct an IdGenerator
78      *
79      */

80     
81     public IdGenerator(String JavaDoc prefix){
82         synchronized(UNIQUE_STUB){
83             this.seed = prefix + UNIQUE_STUB +(instanceCount++) +":";
84         }
85     }
86     
87     public IdGenerator(){
88         this("ID:" + hostName);
89     }
90     
91     /**
92      * Generate a unqiue id
93      * @return a unique id
94      */

95     
96     public synchronized String JavaDoc generateId(){
97         return this.seed + (this.sequence++);
98     }
99     
100     /**
101      * Generate a unique ID - that is friendly for a URL or file system
102      * @return a unique id
103      */

104     public String JavaDoc generateSanitizedId(){
105         String JavaDoc result = generateId();
106         result = result.replace(':', '-');
107         result = result.replace('_', '-');
108         result = result.replace('.', '-');
109         return result;
110     }
111
112 }
113
Popular Tags