001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.id;
018
019 /*
020 * Copied from
021 * http://svn.apache.org/repos/asf/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/IdGenerator.java
022 */
023
024 import java.net.InetAddress;
025 import java.net.ServerSocket;
026
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030 /**
031 * Generator for Globally unique Strings.
032 */
033
034 public class IdGenerator {
035
036 private static final Log LOG = LogFactory.getLog(IdGenerator.class);
037
038 private static final String UNIQUE_STUB;
039
040 private static int instanceCount;
041
042 private static String hostName;
043
044 private String seed;
045
046 private long sequence;
047
048 public IdGenerator() {
049 this("ID:");
050 }
051
052 /**
053 * Construct an IdGenerator
054 *
055 */
056 public IdGenerator(String prefix) {
057 synchronized (UNIQUE_STUB) {
058 this.seed = prefix + UNIQUE_STUB + (instanceCount++) + ":";
059 }
060 }
061
062 static {
063 String stub = "";
064 boolean canAccessSystemProps = true;
065 try {
066 SecurityManager sm = System.getSecurityManager();
067 if (sm != null) {
068 sm.checkPropertiesAccess();
069 }
070 } catch (SecurityException se) {
071 canAccessSystemProps = false;
072 }
073
074 if (canAccessSystemProps) {
075 try {
076 hostName = InetAddress.getLocalHost().getHostAddress();
077 ServerSocket ss = new ServerSocket(0);
078 stub = hostName + "-" + Long.toHexString(ss.getLocalPort() ^ System.currentTimeMillis()) + "-";
079 Thread.sleep(100);
080 ss.close();
081 } catch (Exception ioe) {
082 LOG.warn("Could not generate unique stub", ioe);
083 }
084 } else {
085 hostName = "localhost";
086 stub = hostName + Long.toHexString(System.currentTimeMillis()) + "-";
087 }
088 UNIQUE_STUB = stub;
089 }
090
091 /**
092 * As we have to find the hostname as a side-affect of generating a unique
093 * stub, we allow it's easy retrevial here
094 *
095 * @return the local host name
096 */
097
098 public static String getHostName() {
099 return hostName;
100 }
101
102 /**
103 * Generate a unqiue id
104 *
105 * @return a unique id
106 */
107
108 public synchronized String generateId() {
109 return this.seed + (this.sequence++);
110 }
111
112 /**
113 * Generate a unique ID - that is friendly for a URL or file system
114 *
115 * @return a unique id
116 */
117 public String generateSanitizedId() {
118 String result = generateId();
119 result = result.replace(':', '-');
120 result = result.replace('_', '-');
121 result = result.replace('.', '-');
122 return result;
123 }
124
125 }