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