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.hc.client5.http.impl.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  import java.util.concurrent.locks.ReentrantLock;
36  
37  /**
38   * Should produce reasonably unique tokens.
39   */
40  class BasicIdGenerator {
41  
42      private final String hostname;
43      private final SecureRandom rnd;
44  
45      private long count;
46  
47      private final ReentrantLock lock;
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          this.lock = new ReentrantLock();
65      }
66  
67      public void generate(final StringBuilder buffer) {
68          lock.lock();
69          try {
70              this.count++;
71              final int rndnum = this.rnd.nextInt();
72              buffer.append(System.currentTimeMillis());
73              buffer.append('.');
74              try (Formatter formatter = new Formatter(buffer, Locale.ROOT)) {
75                  formatter.format("%1$016x-%2$08x", this.count, rndnum);
76              }
77              buffer.append('.');
78              buffer.append(this.hostname);
79          } finally {
80              lock.unlock();
81          }
82      }
83  
84      public String generate() {
85          final StringBuilder buffer = new StringBuilder();
86          generate(buffer);
87          return buffer.toString();
88      }
89  
90  }