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  
36  /**
37   * Should produce reasonably unique tokens.
38   */
39  class BasicIdGenerator {
40  
41      private final String hostname;
42      private final SecureRandom rnd;
43  
44      private long count;
45  
46      public BasicIdGenerator() {
47          super();
48          String hostname;
49          try {
50              hostname = InetAddress.getLocalHost().getHostName();
51          } catch (final UnknownHostException ex) {
52              hostname = "localhost";
53          }
54          this.hostname = hostname;
55          try {
56              this.rnd = SecureRandom.getInstance("SHA1PRNG");
57          } catch (final NoSuchAlgorithmException ex) {
58              throw new Error(ex);
59          }
60          this.rnd.setSeed(System.currentTimeMillis());
61      }
62  
63      public synchronized void generate(final StringBuilder buffer) {
64          this.count++;
65          final int rndnum = this.rnd.nextInt();
66          buffer.append(System.currentTimeMillis());
67          buffer.append('.');
68          try (Formatter formatter = new Formatter(buffer, Locale.ROOT)) {
69              formatter.format("%1$016x-%2$08x", this.count, rndnum);
70          }
71          buffer.append('.');
72          buffer.append(this.hostname);
73      }
74  
75      public String generate() {
76          final StringBuilder buffer = new StringBuilder();
77          generate(buffer);
78          return buffer.toString();
79      }
80  
81  }