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

net.java.truelicense.core.ChainedLicenseConsumerManager 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.concurrent.ThreadSafe;
import net.java.truelicense.core.io.*;

/**
 * A caching license consumer manager which establishes a Chain Of
 * Responsibility with its parent license consumer manager.
 *
 * @author Christian Schlichtherle
 */
@ThreadSafe
abstract class ChainedLicenseConsumerManager
extends CachingLicenseConsumerManager implements LicenseProvider {

    /** The parent license consumer manager. */
    abstract LicenseConsumerManager parent();

    private boolean ftp() {
        return authentication().parameters().forSigning();
    }

    @Override
    public License install(Source source) throws LicenseManagementException {
        try {
            return parent().install(source);
        } catch (final LicenseManagementException primary) {
            if (ftp()) throw primary;
            return super.install(source);
        }
    }

    @Override public License view() throws LicenseManagementException {
        try {
            return parent().view();
        } catch (final LicenseManagementException primary) {
            try {
                return super.view(); // uses store()
            } catch (final LicenseManagementException secondary) {
                synchronized (store()) {
                    try {
                        return super.view(); // repeat
                    } catch (final LicenseManagementException ternary) {
                        return createIffNewFtp(ternary); // uses store(), too
                    }
                }
            }
        }
    }

    @Override public void verify() throws LicenseManagementException {
        try {
            parent().verify();
        } catch (final LicenseManagementException primary) {
            try {
                super.verify(); // uses store()
            } catch (final LicenseManagementException secondary) {
                synchronized (store()) {
                    try {
                        super.verify(); // repeat
                    } catch (final LicenseManagementException ternary) {
                        createIffNewFtp(ternary); // uses store(), too
                    }
                }
            }
        }
    }

    @Override public void uninstall() throws LicenseManagementException {
        final LicenseConsumerManager parent = parent();
        try {
            parent.uninstall();
        } catch (final LicenseManagementException primary) {
            if (ftp()) throw primary;
            super.uninstall();
        }
    }

    private License createIffNewFtp(final LicenseManagementException ex)
    throws LicenseManagementException {
        if (!ftp()) throw ex;
        final Store store = store();
        if (store.exists()) throw ex;
        return super.create(license(), store);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy