net.java.truelicense.core.BasicLicenseInitialization Maven / Gradle / Ivy
Show all versions of truelicense-core Show documentation
/*
* Copyright (C) 2005-2013 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.core;
import java.util.Date;
import javax.annotation.concurrent.Immutable;
import javax.security.auth.x500.X500Principal;
import static net.java.truelicense.core.Messages.*;
import net.java.truelicense.core.util.*;
import net.java.truelicense.obfuscate.Obfuscate;
/**
* A basic license initialization.
*
* Note that this class is immutable.
* Unless stated otherwise, all no-argument methods need to return consistent
* objects so that caching them is not required.
* A returned object is considered to be consistent if it compares
* {@linkplain Object#equals(Object) equal} or at least behaves identical to
* any previously returned object.
*
* @author Christian Schlichtherle
*/
@Immutable
final class BasicLicenseInitialization
implements LicenseInitialization, LicenseSubjectProvider, Clock {
@Obfuscate private static final String DEFAULT_CONSUMER_TYPE = "User";
/** The canonical name prefix for X.500 principals. */
@Obfuscate private static final String CN_PREFIX = "CN=";
/** The canonical name of an anonymous user. */
@Obfuscate static final String ANONYMOUS = "anonymous";
private final BasicLicenseManagementContext context;
BasicLicenseInitialization(final BasicLicenseManagementContext context) {
assert null != context;
this.context = context;
}
/** Returns the basic license management context. */
BasicLicenseManagementContext context() { return context; }
@Override public String subject() { return context().subject(); }
@Override public Date now() { return context().now(); }
/**
* Initializes undefined properties of the given license with default
* values.
*
* @param license the license to initialize.
*/
@Override public void initialize(final License license) {
if (null == license.getConsumerType())
license.setConsumerType(DEFAULT_CONSUMER_TYPE);
if (null == license.getHolder())
license.setHolder(new X500Principal(CN_PREFIX + message(ANONYMOUS)));
if (null == license.getIssued())
license.setIssued(now()); // don't trust the system clock!
if (null == license.getIssuer())
license.setIssuer(new X500Principal(CN_PREFIX + subject()));
if (null == license.getSubject())
license.setSubject(subject());
}
}