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-2013 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.ThreadSafe;
import net.java.truelicense.core.auth.Artifactory;
import net.java.truelicense.core.io.Source;
import net.java.truelicense.core.util.CacheEntry;
import net.java.truelicense.core.util.CachePeriodProvider;
/**
* 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 are volatile functions of the source.
// So two threads may work with different objects, but because they are
// function results, it doesn't matter as long as the source compares equal.
// Note that the source is the same because it's the store() property.
private volatile CachedArtifactory ca = new CachedArtifactory();
private volatile CachedLicense cl = new CachedLicense();
@Override
public License install(final Source source)
throws LicenseManagementException {
synchronized (source) {
final License l = super.install(source);
assert ca.matches(source) || 0 == cachePeriodMillis();
assert cl.matches(source) || 0 == cachePeriodMillis();
this.ca = ca.source(store()); // update cached source to store
// Note that the license MUST NOT get cached yet because it gets
// shared with the caller here!
return l;
}
}
@Override
public void uninstall() throws LicenseManagementException {
final CachedArtifactory ca = new CachedArtifactory();
final CachedLicense cl = new CachedLicense();
synchronized (store()) {
super.uninstall();
this.ca = ca;
this.cl = cl;
}
}
@Override
License validate(Source source) throws Exception {
License l = cl.map(source);
if (null == l)
this.cl = new CachedLicense(source,
l = decodeLicense(source),
cachePeriodMillis());
validation().validate(l);
return l;
}
@Override
Artifactory authenticate(final Source source) throws Exception {
Artifactory a = ca.map(source);
if (null == a)
this.ca = new CachedArtifactory(source,
a = super.authenticate(source),
cachePeriodMillis());
return a;
}
private static class CachedArtifactory
extends CacheEntry