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