Coverage Report - org.apache.camel.util.UuidGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
UuidGenerator
62% 
N/A 
1
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.util;
 19  
 import java.net.InetAddress;
 20  
 import java.net.ServerSocket;
 21  
 import java.util.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 
 24  
 /**
 25  
  * Generator for Globally unique Strings.
 26  
  */
 27  
 
 28  
 public class UuidGenerator {
 29  
 
 30  1
     private static final Logger log = Logger.getLogger(UuidGenerator.class.getName());
 31  
     private static final String UNIQUE_STUB;
 32  
     private static int instanceCount;
 33  
     private static String hostName;
 34  
     private String seed;
 35  
     private long sequence;
 36  
     
 37  
     static {
 38  1
         String stub = "";
 39  1
         boolean canAccessSystemProps = true;
 40  
         try{
 41  1
             SecurityManager sm = System.getSecurityManager();
 42  1
             if(sm != null){
 43  0
                 sm.checkPropertiesAccess();
 44  
             }
 45  0
         }catch(SecurityException se){
 46  0
             canAccessSystemProps = false;
 47  1
         }
 48  
         
 49  1
         if ( canAccessSystemProps) {
 50  
             try {
 51  1
                 hostName = InetAddress.getLocalHost().getHostName();
 52  1
                 ServerSocket ss = new ServerSocket(0);
 53  1
                 stub="/" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "/";
 54  1
                 Thread.sleep(100);
 55  1
                 ss.close();
 56  0
             }catch(Exception ioe){
 57  0
                 log.log(Level.WARNING, "could not generate unique stub",ioe);
 58  1
             }
 59  0
         }else{
 60  0
             hostName="localhost";
 61  0
             stub = "-1-" +System.currentTimeMillis() +"-";
 62  
         }
 63  1
         UNIQUE_STUB = stub;
 64  1
     }
 65  
     
 66  
     /**
 67  
      * As we have to find the hostname as a side-affect of generating
 68  
      * a unique stub, we allow it's easy retrevial here
 69  
      * @return the local host name
 70  
      */
 71  
     
 72  
     public static String getHostName(){
 73  0
         return hostName;
 74  
     }
 75  
     
 76  
     /**
 77  
      * Construct an IdGenerator
 78  
      *
 79  
      */
 80  
     
 81  2
     public UuidGenerator(String prefix){
 82  2
         synchronized(UNIQUE_STUB){
 83  2
             this.seed = prefix + UNIQUE_STUB +(instanceCount++) +"-";
 84  2
         }
 85  2
     }
 86  
     
 87  
     public UuidGenerator(){
 88  2
         this("ID-" + hostName);
 89  2
     }
 90  
     
 91  
     /**
 92  
      * Generate a unqiue id
 93  
      * @return a unique id
 94  
      */
 95  
     
 96  
     public synchronized String generateId(){
 97  1604
         return this.seed + (this.sequence++);
 98  
     }
 99  
     
 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  0
         String result = generateId();
 106  0
         result = result.replace(':', '-');
 107  0
         result = result.replace('_', '-');
 108  0
         result = result.replace('.', '-');
 109  0
         return result;
 110  
     }
 111  
 
 112  
 }