net.java.truelicense.core.ChainedLicenseConsumerManager 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.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);
}
}