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.camel.util;
018    
019    import java.net.InetAddress;
020    import java.net.ServerSocket;
021    import java.util.logging.Level;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    
026    /**
027     * Generator for Globally unique Strings.
028     */
029    public class UuidGenerator {
030    
031        private static final transient Log LOG = LogFactory.getLog(UuidGenerator.class); 
032        private static final String UNIQUE_STUB;
033        private static int instanceCount;
034        private static String hostName;
035        private String seed;
036        private long sequence;
037    
038        static {
039            String stub = "";
040            boolean canAccessSystemProps = true;
041            try {
042                SecurityManager sm = System.getSecurityManager();
043                if (sm != null) {
044                    sm.checkPropertiesAccess();
045                }
046            } catch (SecurityException se) {
047                canAccessSystemProps = false;
048            }
049    
050            if (canAccessSystemProps) {
051                try {
052                    hostName = InetAddress.getLocalHost().getHostName();
053                    ServerSocket ss = new ServerSocket(0);
054                    stub = "/" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "/";
055                    Thread.sleep(100);
056                    ss.close();
057                } catch (Exception ioe) {
058                    LOG.warn("Could not generate unique stub", ioe);
059                }
060            } else {
061                hostName = "localhost";
062                stub = "-1-" + System.currentTimeMillis() + "-";
063            }
064            UNIQUE_STUB = stub;
065        }
066    
067        public UuidGenerator(String prefix) {
068            synchronized (UNIQUE_STUB) {
069                this.seed = prefix + UNIQUE_STUB + (instanceCount++) + "-";
070            }
071        }
072    
073        public UuidGenerator() {
074            this("ID-" + hostName);
075        }
076    
077        /**
078         * As we have to find the hostname as a side-affect of generating a unique
079         * stub, we allow it's easy retrevial here
080         * 
081         * @return the local host name
082         */
083        public static String getHostName() {
084            return hostName;
085        }
086    
087        /**
088         * Generate a unqiue id
089         */
090        public synchronized String generateId() {
091            return this.seed + (this.sequence++);
092        }
093    
094        /**
095         * Generate a unique ID - that is friendly for a URL or file system
096         * 
097         * @return a unique id
098         */
099        public String generateSanitizedId() {
100            return generateSanitizedId(generateId());
101        }
102    
103        /**
104         * Ensures that the id is friendly for a URL or file system
105         *
106         * @param id the unique id
107         * @return the id as file friendly id
108         */
109        public static String generateSanitizedId(String id) {
110            id = id.replace(':', '-');
111            id = id.replace('_', '-');
112            id = id.replace('.', '-');
113            return id;
114        }
115    
116    }