Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* $Id: ContextIDGenerator.java,v 1.31 2010/10/25 20:36:51 agoubard Exp $
*
* See the COPYRIGHT file for redistribution and use restrictions.
*/
package org.xins.server;
import java.util.Map;
import java.util.Random;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.collections.InvalidPropertyValueException;
import org.xins.common.collections.MissingRequiredPropertyException;
import org.xins.common.manageable.Manageable;
import org.xins.common.manageable.InitializationException;
import org.xins.common.net.IPAddressUtils;
import org.xins.common.text.DateConverter;
import org.xins.common.text.TextUtils;
/**
* Generator for diagnostic context identifiers. Generated context
* identifiers will be in the format:
*
*
app@host:time:rnd
*
* where:
*
*
*
app is the name of the deployed application, e.g.
* "sso";
*
*
host is the hostname of the computer running this
* engine, e.g. "freddy.bravo.com";
*
*
time is the current date and time in the format
* yyMMdd-HHmmssNNN, e.g. "050806-171522358";
*
*
rnd is a 5 hex-digits randomly generated number, e.g.
* "2f4e6".
*
*
* @version $Revision: 1.31 $ $Date: 2010/10/25 20:36:51 $
* @author Ernst de Haan
*/
final class ContextIDGenerator extends Manageable {
/**
* The hexadecimal digits.
*/
private static final char[] HEX_DIGITS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
/**
* The name of the runtime property that hostname for the server
* running the API.
*/
private static final String HOSTNAME_PROPERTY = "org.xins.server.hostname";
/**
* The name of the API. Never null.
*/
private final String _apiName;
/**
* The name for the local host. Never null.
*/
private String _hostname;
/**
* The fixed prefix for generated context identifiers, as a character
* buffer. Never null when this instance is initialized.
*/
private char[] _prefixBuffer;
/**
* The length of the prefix.
*/
private int _prefixLength;
/**
* A date converter. Never null. Needs to be locked before
* usage.
*/
private final DateConverter _dateConverter;
/**
* A pseudo-random number generator. Never null
*/
private final Random _random;
/**
* Constructs a new ContextIDGenerator.
*
* @param apiName
* the name of the API, cannot be null.
*
* @throws IllegalArgumentException
* if apiName == null.
*/
ContextIDGenerator(String apiName)
throws IllegalArgumentException {
// Check preconditions
MandatoryArgumentChecker.check("apiName", apiName);
// Store API name and determine host name
_apiName = apiName;
_hostname = IPAddressUtils.getLocalHost();
// Create a DateConverter that will not prepend the century
_dateConverter = new DateConverter(false);
// Initialize a pseudo-random number generator
_random = new Random();
}
/**
* Performs the initialization procedure (actual implementation). When this
* method is called from {@link #init(Map<String, String>)}, the state and the
* argument will have been checked and the state will have been set to
* {@link #INITIALIZING}.
*
* @param properties
* the initialization properties, not null.
*
* @throws MissingRequiredPropertyException
* if a required property is not given.
*
* @throws InvalidPropertyValueException
* if the value of a certain property is invalid.
*
* @throws InitializationException
* if the initialization failed, for any other reason.
*/
protected void initImpl(Map properties)
throws MissingRequiredPropertyException,
InvalidPropertyValueException,
InitializationException {
// Determine if the hostname has changed
String hostname = properties.get(HOSTNAME_PROPERTY);
if (!TextUtils.isEmpty(hostname) && !hostname.equals(_hostname)) {
Log.log_3310(_hostname, hostname);
_hostname = hostname;
}
// Determine prefix and total context ID length
String prefix = _apiName + '@' + _hostname + ':';
_prefixBuffer = prefix.toCharArray();
_prefixLength = prefix.length();
}
/**
* Generates a diagnostic context identifier.
*
* @return
* the generated diagnostic context identifier, never null.
*
* @throws IllegalStateException
* if this object is currently not usable, i.e. in the
* {@link #USABLE} state.
*/
String generate() throws IllegalStateException {
// Check preconditions
assertUsable();
// Construct a new string buffer with the exact needed capacity
int prefixLength = _prefixLength;
int length = prefixLength + 22;
char[] buffer = new char[length];
// Copy the template into the buffer
System.arraycopy(_prefixBuffer, 0, buffer, 0, prefixLength);
// Determine the current time and append the timestamp
long date = System.currentTimeMillis();
synchronized (_dateConverter) {
_dateConverter.format(date, buffer, prefixLength);
}
// Append 5 pseudo-random hex digits
int random = _random.nextInt() & 0x0fffffff;
int pos = prefixLength + 16;
buffer[pos++] = ':';
buffer[pos++] = HEX_DIGITS[ random & 15];
buffer[pos++] = HEX_DIGITS[(random >> 4) & 15];
buffer[pos++] = HEX_DIGITS[(random >> 8) & 15];
buffer[pos++] = HEX_DIGITS[(random >> 12) & 15];
buffer[pos ] = HEX_DIGITS[(random >> 16) & 15];
// Log and return the context ID
return new String(buffer);
}
}