KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > id > IdGenerator


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.servicemix.id;
18
19 /*
20  * Copied from http://svn.apache.org/repos/asf/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/IdGenerator.java
21  */

22
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.ServerSocket JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * Generator for Globally unique Strings.
31  */

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

76     
77     public static String JavaDoc getHostName(){
78         return hostName;
79     }
80     
81     /**
82      * Construct an IdGenerator
83      *
84      */

85     
86     public IdGenerator(String JavaDoc prefix){
87         synchronized(UNIQUE_STUB){
88             this.seed = prefix + UNIQUE_STUB +(instanceCount++) +":";
89         }
90     }
91     
92     public IdGenerator(){
93         this("ID:");
94     }
95     
96     /**
97      * Generate a unqiue id
98      * @return a unique id
99      */

100     
101     public synchronized String JavaDoc generateId(){
102         return this.seed + (this.sequence++);
103     }
104     
105     /**
106      * Generate a unique ID - that is friendly for a URL or file system
107      * @return a unique id
108      */

109     public String JavaDoc generateSanitizedId(){
110         String JavaDoc result = generateId();
111         result = result.replace(':', '-');
112         result = result.replace('_', '-');
113         result = result.replace('.', '-');
114         return result;
115     }
116
117 }
118
Popular Tags