View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.concurrent;
33  
34  import com.google.common.io.BaseEncoding;
35  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36  import java.net.NetworkInterface;
37  import java.net.SocketException;
38  import java.nio.ByteBuffer;
39  import java.security.NoSuchAlgorithmException;
40  import java.security.SecureRandom;
41  import java.util.Enumeration;
42  import java.util.function.Supplier;
43  import java.util.logging.Level;
44  import java.util.logging.Logger;
45  import javax.annotation.ParametersAreNonnullByDefault;
46  import org.spf4j.base.AppendableUtils;
47  import org.spf4j.os.ProcessUtil;
48  
49  /**
50   * Unique ID Generator Based on the assumptions: 1. host MAC address is used. (each network interface has a Unique ID)
51   * (encoded with provided encoder) 2. process id is used + current epoch seconds. it is assumed the PID is not recycled
52   * within a second. 3. A process sequence is used. UIDs will cycle after Long.MaxValue is reached.
53   *
54   * @author zoly
55   */
56  @ParametersAreNonnullByDefault
57  public final class UIDGenerator implements Supplier<CharSequence> {
58  
59    private final Sequence sequence;
60  
61    private final StringBuilder base;
62  
63    private final int maxSize;
64  
65    public UIDGenerator(final Sequence sequence) {
66      this(sequence, 0);
67    }
68  
69    public UIDGenerator(final Sequence sequence, final String prefix) {
70      this(sequence, BaseEncoding.base64Url(), 0, '.', prefix);
71    }
72  
73    public UIDGenerator(final Sequence sequence, final long customEpoch) {
74      this(sequence, BaseEncoding.base64Url(), customEpoch, '.', "");
75    }
76  
77    public UIDGenerator(final Sequence sequence, final String prefix, final long customEpoch) {
78      this(sequence, BaseEncoding.base64Url(), customEpoch, '.', prefix);
79    }
80  
81    /**
82     * Construct a UID Generator
83     *
84     * @param sequence
85     * @param baseEncoding - if null MAC address based ID will not be included.
86     */
87    @SuppressFBWarnings("STT_TOSTRING_STORED_IN_FIELD")
88    public UIDGenerator(final Sequence sequence, final BaseEncoding baseEncoding,
89            final long customEpoch, final char separator, final String prefix) {
90      this.sequence = sequence;
91      StringBuilder sb = generateIdBase(prefix, baseEncoding, separator, customEpoch);
92      base = sb;
93      maxSize = base.length() + 16;
94    }
95  
96    public static StringBuilder generateIdBase(final String prefix,
97            final char separator) {
98      return generateIdBase(prefix, BaseEncoding.base64Url(), separator, 1509741164184L);
99    }
100 
101   public static StringBuilder generateIdBase(final String prefix,
102           final char separator,
103           final long customEpoch) {
104     return generateIdBase(prefix, BaseEncoding.base64Url(), separator, customEpoch);
105   }
106 
107   @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
108   public static StringBuilder generateIdBase(final String prefix,
109           final BaseEncoding baseEncoding, final char separator,
110           final long customEpoch) {
111     StringBuilder sb = new StringBuilder(16 + prefix.length());
112     sb.append(prefix);
113 
114     byte[] intfMac;
115     try {
116       Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
117       if (networkInterfaces != null && networkInterfaces.hasMoreElements()) {
118         do {
119           intfMac = networkInterfaces.nextElement().getHardwareAddress();
120         } while ((intfMac == null || intfMac.length == 0) && networkInterfaces.hasMoreElements());
121         if (intfMac == null) {
122           Logger.getLogger(UIDGenerator.class.getName()).warning(
123                   "Unable to get interface MAC address for ID generation");
124           try {
125             intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array();
126           } catch (NoSuchAlgorithmException ex) {
127             throw new IllegalStateException(ex);
128           }
129         }
130       } else {
131         Logger.getLogger(UIDGenerator.class.getName()).warning(
132                 "Unable to get interface MAC address for ID generation");
133         try {
134           intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array();
135         } catch (NoSuchAlgorithmException ex) {
136           throw new IllegalStateException(ex);
137         }
138       }
139     } catch (SocketException ex) {
140       Logger.getLogger(UIDGenerator.class.getName()).log(Level.WARNING,
141               "Unable to get interface MAC address for ID generation", ex);
142       try {
143         intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array();
144       } catch (NoSuchAlgorithmException ex2) {
145         ex2.addSuppressed(ex);
146         throw new IllegalStateException(ex2);
147       }
148     }
149     sb.append(baseEncoding.encode(intfMac)).append(separator);
150 
151     AppendableUtils.appendUnsignedString(sb, ProcessUtil.getPid(), 5);
152     sb.append(separator);
153     AppendableUtils.appendUnsignedString(sb, (System.currentTimeMillis() - customEpoch) / 1000, 5);
154     sb.append(separator);
155     return sb;
156   }
157 
158   public int getMaxSize() {
159     return maxSize;
160   }
161 
162   public CharSequence next() {
163     StringBuilder result = new StringBuilder(maxSize);
164     result.append(base);
165     AppendableUtils.appendUnsignedString(result, sequence.next(), 5);
166     return result;
167   }
168 
169   @Override
170   public String toString() {
171     return "UIDGenerator{" + "sequence=" + sequence + ", base=" + base + ", maxSize=" + maxSize + '}';
172   }
173 
174   @Override
175   public CharSequence get() {
176     return next();
177   }
178 
179 }