![JAR search and dependency download from the Maven repository](/logo.png)
net.java.truelicense.core.BasicLicenseValidation 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 static net.java.truelicense.core.Messages.*;
import net.java.truelicense.core.util.*;
import net.java.truelicense.obfuscate.Obfuscate;
/**
* A basic license validation.
*
* 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 BasicLicenseValidation
implements LicenseValidation, LicenseSubjectProvider, Clock {
@Obfuscate
static final String INVALID_SUBJECT = "invalidSubject";
@Obfuscate
static final String HOLDER_IS_NULL = "holderIsNull";
@Obfuscate
static final String ISSUER_IS_NULL = "issuerIsNull";
@Obfuscate
static final String ISSUED_IS_NULL = "issuedIsNull";
@Obfuscate
static final String LICENSE_IS_NOT_YET_VALID = "licenseIsNotYetValid";
@Obfuscate
static final String LICENSE_HAS_EXPIRED = "licenseHasExpired";
@Obfuscate
static final String CONSUMER_TYPE_IS_NULL = "consumerTypeIsNull";
@Obfuscate
static final String CONSUMER_AMOUNT_IS_NOT_POSITIVE = "consumerAmountIsNotPositive";
private final BasicLicenseManagementContext context;
BasicLicenseValidation(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(); }
@Override public void validate(final License license)
throws LicenseValidationException {
if (0 >= license.getConsumerAmount())
throw new LicenseValidationException(message(CONSUMER_AMOUNT_IS_NOT_POSITIVE, license.getConsumerAmount()));
if (null == license.getConsumerType())
throw new LicenseValidationException(message(CONSUMER_TYPE_IS_NULL));
if (null == license.getHolder())
throw new LicenseValidationException(message(HOLDER_IS_NULL));
if (null == license.getIssued())
throw new LicenseValidationException(message(ISSUED_IS_NULL));
if (null == license.getIssuer())
throw new LicenseValidationException(message(ISSUER_IS_NULL));
final Date now = now(); // don't trust the system clock!
final Date notAfter = license.getNotAfter();
if (null != notAfter && now.after(notAfter))
throw new LicenseValidationException(message(LICENSE_HAS_EXPIRED, notAfter));
final Date notBefore = license.getNotBefore();
if (null != notBefore && now.before(notBefore))
throw new LicenseValidationException(message(LICENSE_IS_NOT_YET_VALID, notBefore));
if (!subject().equals(license.getSubject()))
throw new LicenseValidationException(message(INVALID_SUBJECT, license.getSubject(), subject()));
}
}