View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache;
28  
29  import java.net.InetAddress;
30  import java.net.UnknownHostException;
31  import java.security.NoSuchAlgorithmException;
32  import java.security.SecureRandom;
33  import java.util.Formatter;
34  import java.util.Locale;
35  
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  
39  /**
40   * Should produce reasonably unique tokens.
41   */
42  @Contract(threading = ThreadingBehavior.SAFE)
43  class BasicIdGenerator {
44  
45      private final String hostname;
46      private final SecureRandom rnd;
47      private long count;
48  
49      public BasicIdGenerator() {
50          super();
51          String hostname;
52          try {
53              hostname = InetAddress.getLocalHost().getHostName();
54          } catch (final UnknownHostException ex) {
55              hostname = "localhost";
56          }
57          this.hostname = hostname;
58          try {
59              this.rnd = SecureRandom.getInstance("SHA1PRNG");
60          } catch (final NoSuchAlgorithmException ex) {
61              throw new Error(ex);
62          }
63          this.rnd.setSeed(System.currentTimeMillis());
64      }
65  
66      public synchronized void generate(final StringBuilder buffer) {
67          this.count++;
68          final int rndnum = this.rnd.nextInt();
69          buffer.append(System.currentTimeMillis());
70          buffer.append('.');
71          final Formatter formatter = new Formatter(buffer, Locale.US);
72          formatter.format("%1$016x-%2$08x", Long.valueOf(this.count), Integer.valueOf(rndnum));
73          formatter.close();
74          buffer.append('.');
75          buffer.append(this.hostname);
76      }
77  
78      public String generate() {
79          final StringBuilder buffer = new StringBuilder();
80          generate(buffer);
81          return buffer.toString();
82      }
83  
84  }