All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.truelicense.core.CachingLicenseConsumerManager Maven / Gradle / Ivy

Go to download

The TrueLicense Core module provides essential functionality for license management.

There is a newer version: 2.6.6
Show newest version
/*
 * 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 {

        private static final long serialVersionUID = 1L;

        CachedArtifactory() { this(null, null, 0); } // => isObsolete() == true

        CachedArtifactory(
                @CheckForNull Source source,
                @CheckForNull Artifactory artifactory,
                long timeoutPeriodMillis) {
            super(source, artifactory, timeoutPeriodMillis);
        }

        CachedArtifactory source(@CheckForNull Source param) {
            return new CachedArtifactory(param, getValue(), getCachePeriodMillis());
        }
    } // CachedArtifactory

    private static class CachedLicense
    extends CacheEntry {

        private static final long serialVersionUID = 1L;

        CachedLicense() { this(null, null, 0); } // => isObsolete() == true

        CachedLicense(
                @CheckForNull Source source,
                @CheckForNull License license,
                long timeoutPeriodMillis) {
            super(source, license, timeoutPeriodMillis);
        }
    } // CachedLicense
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy