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

net.java.truelicense.core.BasicLicenseManager 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 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 input, final Sink sink) throws LicenseManagementException { return wrap(new Callable() { @Override public License call() throws Exception { authorization().clearCreate(parameters()); return encrypt(input, 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 input, final Sink sink) throws Exception { return compress(input, encryption().apply(sink)); } License compress(final License input, final Sink sink) throws Exception { return encodeRepository(input, compression().apply(sink)); } License encodeRepository(final License input, final Sink sink) throws Exception { final Result result = encodeAndSign(input); final Repository repository = result.repository(); codec().encode(sink, repository); return result.license(); } Result encodeAndSign(final License input) throws Exception { final License output = validate(input); final Repository repository = repository(); authentication().sign(codec(), repository, output); return new Result() { @Override public License license() { return output; } @Override public Repository repository() { return repository; } }; } License validate(final License input) throws Exception { final License output = initialize(input); validation().validate(output); return output; } License initialize(final License input) throws Exception { final License output = duplicate(input); initialization().initialize(output); return output; } @SuppressWarnings("unchecked") 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 l = decodeLicense(source); validation().validate(l); return l; } License decodeLicense(Source source) throws Exception { return authenticate(source).decode(License.class); } @SuppressWarnings("unchecked") 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(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy