001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.nio.ByteBuffer;
020import java.security.SecureRandom;
021import java.util.Random;
022import java.util.UUID;
023import java.util.concurrent.atomic.AtomicInteger;
024
025import org.apache.logging.log4j.Logger;
026import org.apache.logging.log4j.status.StatusLogger;
027import org.apache.logging.log4j.util.PropertiesUtil;
028
029/**
030 * Generates a unique ID. The generated UUID will be unique for approximately 8,925 years so long as
031 * less than 10,000 IDs are generated per millisecond on the same device (as identified by its MAC address).
032 */
033public final class UuidUtil {
034
035    private static final long[] EMPTY_LONG_ARRAY = {};
036
037    /**
038     * System property that may be used to seed the UUID generation with an integer value.
039     */
040    public static final String UUID_SEQUENCE = "org.apache.logging.log4j.uuidSequence";
041
042    private static final Logger LOGGER = StatusLogger.getLogger();
043
044    private static final String ASSIGNED_SEQUENCES = "org.apache.logging.log4j.assignedSequences";
045
046    private static final AtomicInteger COUNT = new AtomicInteger(0);
047    private static final long TYPE1 = 0x1000L;
048    private static final byte VARIANT = (byte) 0x80;
049    private static final int SEQUENCE_MASK = 0x3FFF;
050    private static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
051    private static final long INITIAL_UUID_SEQNO = PropertiesUtil.getProperties().getLongProperty(UUID_SEQUENCE, 0);
052
053    private static final long LOW_MASK = 0xffffffffL;
054    private static final long MID_MASK = 0xffff00000000L;
055    private static final long HIGH_MASK = 0xfff000000000000L;
056    private static final int NODE_SIZE = 8;
057    private static final int SHIFT_2 = 16;
058    private static final int SHIFT_4 = 32;
059    private static final int SHIFT_6 = 48;
060    private static final int HUNDRED_NANOS_PER_MILLI = 10000;
061
062    private static final long LEAST = initialize(NetUtils.getMacAddress());
063
064    /* This class cannot be instantiated */
065    private UuidUtil() {
066    }
067
068    /**
069     * Initializes this class
070     * 
071     * @param mac MAC address
072     * @return Least
073     */
074    static long initialize(byte[] mac) {
075        final Random randomGenerator = new SecureRandom();
076        if (mac == null || mac.length == 0) {
077            mac = new byte[6];
078            randomGenerator.nextBytes(mac);
079        }
080        final int length = mac.length >= 6 ? 6 : mac.length;
081        final int index = mac.length >= 6 ? mac.length - 6 : 0;
082        final byte[] node = new byte[NODE_SIZE];
083        node[0] = VARIANT;
084        node[1] = 0;
085        for (int i = 2; i < NODE_SIZE; ++i) {
086            node[i] = 0;
087        }
088        System.arraycopy(mac, index, node, 2, length);
089        final ByteBuffer buf = ByteBuffer.wrap(node);
090        long rand = INITIAL_UUID_SEQNO;
091        String assigned = PropertiesUtil.getProperties().getStringProperty(ASSIGNED_SEQUENCES);
092        long[] sequences;
093        if (assigned == null) {
094            sequences = EMPTY_LONG_ARRAY;
095        } else {
096            final String[] array = assigned.split(Patterns.COMMA_SEPARATOR);
097            sequences = new long[array.length];
098            int i = 0;
099            for (final String value : array) {
100                sequences[i] = Long.parseLong(value);
101                ++i;
102            }
103        }
104        if (rand == 0) {
105            rand = randomGenerator.nextLong();
106        }
107        rand &= SEQUENCE_MASK;
108        boolean duplicate;
109        do {
110            duplicate = false;
111            for (final long sequence : sequences) {
112                if (sequence == rand) {
113                    duplicate = true;
114                    break;
115                }
116            }
117            if (duplicate) {
118                rand = (rand + 1) & SEQUENCE_MASK;
119            }
120        } while (duplicate);
121        assigned = assigned == null ? Long.toString(rand) : assigned + ',' + Long.toString(rand);
122        System.setProperty(ASSIGNED_SEQUENCES, assigned);
123
124        return buf.getLong() | rand << SHIFT_6;
125    }
126
127    /**
128     * Generates Type 1 UUID. The time contains the number of 100NS intervals that have occurred
129     * since 00:00:00.00 UTC, 10 October 1582. Each UUID on a particular machine is unique to the 100NS interval
130     * until they rollover around 3400 A.D.
131     * <ol>
132     * <li>Digits 1-12 are the lower 48 bits of the number of 100 ns increments since the start of the UUID
133     * epoch.</li>
134     * <li>Digit 13 is the version (with a value of 1).</li>
135     * <li>Digits 14-16 are a sequence number that is incremented each time a UUID is generated.</li>
136     * <li>Digit 17 is the variant (with a value of binary 10) and 10 bits of the sequence number</li>
137     * <li>Digit 18 is final 16 bits of the sequence number.</li>
138     * <li>Digits 19-32 represent the system the application is running on.</li>
139     * </ol>
140     *
141     * @return universally unique identifiers (UUID)
142     */
143    public static UUID getTimeBasedUuid() {
144
145        final long time = ((System.currentTimeMillis() * HUNDRED_NANOS_PER_MILLI) +
146            NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) + (COUNT.incrementAndGet() % HUNDRED_NANOS_PER_MILLI);
147        final long timeLow = (time & LOW_MASK) << SHIFT_4;
148        final long timeMid = (time & MID_MASK) >> SHIFT_2;
149        final long timeHi = (time & HIGH_MASK) >> SHIFT_6;
150        final long most = timeLow | timeMid | TYPE1 | timeHi;
151        return new UUID(most, LEAST);
152    }
153}
154