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
079: + "-"
080: + Long.toHexString(ss.getLocalPort()
081: ^ System.currentTimeMillis()) + "-";
082: Thread.sleep(100);
083: ss.close();
084: } catch (Exception ioe) {
085: LOG.warn("Could not generate unique stub", ioe);
086: }
087: } else {
088: hostName = "localhost";
089: stub = hostName
090: + Long.toHexString(System.currentTimeMillis())
091: + "-";
092: }
093: UNIQUE_STUB = stub;
094: }
095:
096: /**
097: * As we have to find the hostname as a side-affect of generating a unique
098: * stub, we allow it's easy retrevial here
099: *
100: * @return the local host name
101: */
102:
103: public static String getHostName() {
104: return hostName;
105: }
106:
107: /**
108: * Generate a unqiue id
109: *
110: * @return a unique id
111: */
112:
113: public synchronized String generateId() {
114: return this .seed + (this .sequence++);
115: }
116:
117: /**
118: * Generate a unique ID - that is friendly for a URL or file system
119: *
120: * @return a unique id
121: */
122: public String generateSanitizedId() {
123: String result = generateId();
124: result = result.replace(':', '-');
125: result = result.replace('_', '-');
126: result = result.replace('.', '-');
127: return result;
128: }
129:
130: }
|