net.java.truelicense.core.CachingLicenseConsumerManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelicense-core Show documentation
Show all versions of truelicense-core Show documentation
The TrueLicense Core module provides essential functionality for
license management.
/*
* Copyright (C) 2005-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.core;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.*;
import net.java.truelicense.core.auth.Artifactory;
import net.java.truelicense.core.io.*;
import net.java.truelicense.core.util.*;
/**
* A basic license consumer manager which caches some computed objects to speed
* up subsequent requests.
*
* @author Christian Schlichtherle
*/
@ThreadSafe
abstract class CachingLicenseConsumerManager
extends BasicLicenseManager implements CachePeriodProvider {
// These volatile fields get initialized by applying a pure function which
// takes the immutable value of the store() property as its single argument.
// So some concurrent threads may safely interleave when initializing these
// fields without creating a racing condition and thus it's not generally
// required to synchronize access to them.
private volatile CachedArtifactory
cachedArtifactory = new CachedArtifactory();
private volatile CachedLicense
cachedLicense = new CachedLicense();
@Override
public License install(final Source source)
throws LicenseManagementException {
final Store store = store();
synchronized (store) {
final License license = super.install(source);
// As a side effect of the license key installation, the cached
// artifactory and license get associated to the source unless this
// is a re-installation from an equal source or the cached objects
// have already been obsoleted by a time-out, that is, if the cache
// period is close to zero.
assert cachedArtifactory.matches(source) ||
cachedArtifactory.matches(store) ||
cachedArtifactory.isObsolete();
assert cachedLicense.matches(source) ||
cachedLicense.matches(store) ||
cachedLicense.isObsolete();
// Update the association of the cached artifactory to the store
// and invalidate the cached license because it gets shared with
// the caller.
this.cachedArtifactory = cachedArtifactory.source(store);
this.cachedLicense = new CachedLicense();
return license;
}
}
@Override
public void uninstall() throws LicenseManagementException {
final CachedArtifactory cachedArtifactory = new CachedArtifactory();
final CachedLicense cachedLicense = new CachedLicense();
synchronized (store()) {
super.uninstall();
this.cachedArtifactory = cachedArtifactory;
this.cachedLicense = cachedLicense;
}
}
@Override
License validate(final Source source) throws Exception {
License license = cachedLicense.map(source);
if (null == license)
this.cachedLicense = new CachedLicense(source,
license = decodeLicense(source),
cachePeriodMillis());
validation().validate(license);
return license;
}
@Override
Artifactory authenticate(final Source source) throws Exception {
Artifactory artifactory = cachedArtifactory.map(source);
if (null == artifactory)
this.cachedArtifactory = new CachedArtifactory(source,
artifactory = super.authenticate(source),
cachePeriodMillis());
return artifactory;
}
@Immutable
private static class CachedArtifactory
extends CacheEntry