fi.csc.mobileauth.comm.CommonsEventIdGenerator.java Source code

Java tutorial

Introduction

Here is the source code for fi.csc.mobileauth.comm.CommonsEventIdGenerator.java

Source

/*
 * Copyright (c) 2014-2015 CSC - IT Center for Science, http://www.csc.fi
 *
 * CSC licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fi.csc.mobileauth.comm;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommonsEventIdGenerator implements EventIdGenerator {

    /** Class logger. */
    private final Logger log = LoggerFactory.getLogger(CommonsEventIdGenerator.class);

    private int idLength;

    public CommonsEventIdGenerator() {
        this(8);
    }

    public CommonsEventIdGenerator(int length) {
        idLength = length;
        log.info("Commons eventId generator initialized with id length {}", idLength);
    }

    public String generateEventId(String prefix) {
        String eventId = prefix.toUpperCase();
        String randomPart = RandomStringUtils.randomAlphanumeric(idLength - prefix.length()).toUpperCase();
        return eventId.concat(randomPart);
    }
}