net.java.truelicense.core.BasicLicenseManager Maven / Gradle / Ivy
Show all versions of truelicense-core Show documentation
/*
* Copyright (C) 2005-2013 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.core;
import java.util.concurrent.Callable;
import javax.annotation.concurrent.Immutable;
import net.java.truelicense.core.auth.*;
import net.java.truelicense.core.codec.*;
import net.java.truelicense.core.crypto.*;
import net.java.truelicense.core.io.*;
/**
* A basic license manager.
*
* Note that this class is immutable.
* Unless stated otherwise, all no-argument methods need to return consistent
* objects so that caching them is not required.
* A returned object is considered to be consistent if it compares
* {@linkplain Object#equals(Object) equal} or at least behaves identical to
* any previously returned object.
*
* @author Christian Schlichtherle
*/
@Immutable
abstract class BasicLicenseManager
implements LicenseParametersProvider, StoreProvider {
public License create(final License bean, final Sink sink)
throws LicenseManagementException {
return wrap(new Callable() {
@Override public License call() throws Exception {
authorization().clearCreate(parameters());
return encrypt(bean, sink);
}
});
}
public License install(final Source source)
throws LicenseManagementException {
return wrap(new Callable() {
@Override public License call() throws Exception {
authorization().clearInstall(parameters());
final License license = validate(source);
Copy.copy(source, store());
return license;
}
});
}
public License view() throws LicenseManagementException {
return wrap(new Callable() {
@Override public License call() throws Exception {
authorization().clearView(parameters());
return decodeLicense(store());
}
});
}
public void verify() throws LicenseManagementException {
wrap(new Callable() {
@Override public Void call() throws Exception {
authorization().clearVerify(parameters());
validate(store());
return null;
}
});
}
public void uninstall() throws LicenseManagementException {
wrap(new Callable() {
@Override public Void call() throws Exception {
authorization().clearUninstall(parameters());
final Store store = store();
//authenticate(store);
store.delete();
return null;
}
});
}
//
// Utility functions:
//
/**
* Executes the given {@code task} and wraps any
* non-{@link RuntimeException} and non-{@link LicenseManagementException} in a new
* {@code LicenseManagementException}.
*
* @param task the task to {@link Callable#call}.
* @return the result of calling the task.
* @throws RuntimeException at the discretion of the task.
* @throws LicenseManagementException on any other {@link Exception} thrown by the
* task.
*/
private static V wrap(final Callable task) throws LicenseManagementException {
try { return task.call(); }
catch (final RuntimeException ex) { throw ex; }
catch (final LicenseManagementException ex) { throw ex; }
catch (final Exception ex) { throw new LicenseManagementException(ex); }
}
//
// License vendor functions:
//
License encrypt(final License bean, final Sink sink) throws Exception {
return compress(bean, encryption().apply(sink));
}
License compress(final License bean, final Sink sink) throws Exception {
return encodeRepository(bean, compression().apply(sink));
}
License encodeRepository(final License bean, final Sink sink)
throws Exception {
final Result result = encodeAndSign(bean);
final Repository repository = result.repository();
codec().encode(sink, repository);
return result.license();
}
Result encodeAndSign(final License bean)
throws Exception {
final License duplicate = validate(bean);
final Repository repository = repository();
authentication().sign(codec(), repository, duplicate);
return new Result() {
@Override public License license() { return duplicate; }
@Override public Repository repository() { return repository; }
};
}
License validate(final License bean) throws Exception {
final License duplicate = initialize(bean);
validation().validate(duplicate);
return duplicate;
}
License initialize(final License bean) throws Exception {
final License duplicate = duplicate(bean);
initialization().initialize(duplicate);
return duplicate;
}
V duplicate(final V object) throws Exception {
final Codec codec = codec();
final Store store = new MemoryStore();
codec.encode(store, object);
return codec.decode(store, object.getClass());
}
/** License And Repository Provider. */
private interface Result
extends LicenseProvider, RepositoryProvider { }
//
// License consumer functions:
//
License validate(final Source source) throws Exception {
final License license = decodeLicense(source);
validation().validate(license);
return license;
}
License decodeLicense(Source source) throws Exception {
return authenticate(source).decode(License.class);
}
Artifactory authenticate(Source source) throws Exception {
return authentication().verify(codec(), decodeRepository(source));
}
Repository decodeRepository(Source source) throws Exception {
return codec().decode(decompress(source), Repository.class);
}
Source decompress(Source source) {
return compression().unapply(decrypt(source));
}
Source decrypt(Source source) { return encryption().unapply(source); }
//
// Property/factory functions:
//
final LicenseAuthorization authorization() {
return parameters().authorization();
}
final LicenseInitialization initialization() {
return parameters().initialization();
}
final LicenseValidation validation() { return parameters().validation(); }
final Repository repository() { return parameters().repository(); }
final Authentication authentication() {
return parameters().authentication();
}
final Codec codec() { return parameters().codec(); }
final Transformation compression() { return parameters().compression(); }
final Encryption encryption() { return parameters().encryption(); }
/**
* Returns the store for the license key (optional operation).
*
* @throws UnsupportedOperationException If this method is called on a
* {@link LicenseVendorManager}.
*/
@Override public abstract Store store();
}