net.java.truelicense.core.BasicLicenseConsumerContext 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.io.File;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import net.java.truelicense.core.auth.*;
import net.java.truelicense.core.crypto.*;
import net.java.truelicense.core.io.*;
import static net.java.truelicense.core.util.Objects.requireNonNull;
import net.java.truelicense.core.util.CachePeriodProvider;
import net.java.truelicense.obfuscate.ObfuscatedString;
/**
* A basic context for license consumer applications.
*
* 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
final class BasicLicenseConsumerContext extends BasicLicenseApplicationContext
implements LicenseConsumerContext, CachePeriodProvider, LicenseProvider {
BasicLicenseConsumerContext(BasicLicenseManagementContext context) {
super(context);
}
@Override public long cachePeriodMillis() {
return context().cachePeriodMillis();
}
@Override public License license() { return context().license(); }
@Override public Authentication keyStore(
@CheckForNull Source source,
@CheckForNull String storeType,
ObfuscatedString storePassword,
String alias) {
return context().authentication(ksUnchecked(
source, storeType, storePassword, alias, null));
}
@Override public Authentication ftpKeyStore(
@CheckForNull Source source,
@CheckForNull String storeType,
ObfuscatedString storePassword,
String alias,
ObfuscatedString keyPassword) {
return context().authentication(ksUnchecked(
source, storeType, storePassword, alias, keyPassword));
}
@Override public Encryption pbe(
@CheckForNull String algorithm,
ObfuscatedString password) {
return context().encryption(pbeUnchecked(algorithm, password));
}
@Override public LicenseConsumerManager manager(
Authentication authentication,
Encryption encryption,
Store store) {
return manager(consumerParameters(authentication, encryption), store);
}
private LicenseConsumerManager manager(
final LicenseParameters lp,
final Store store) {
assert null != lp;
requireNonNull(store);
return new CachingManager() {
@Override public LicenseParameters parameters() { return lp; }
@Override public Store store() { return store; }
};
}
private abstract class CachingManager extends CachingLicenseConsumerManager
implements LicenseConsumerManager {
final BasicLicenseConsumerContext cc = BasicLicenseConsumerContext.this;
final String subject = cc.subject();
final long period = cc.cachePeriodMillis();
{ if (0 > period) throw new IllegalArgumentException(); }
@Override public String subject() { return subject; }
@Override public LicenseConsumerContext context() { return cc; }
@Override public long cachePeriodMillis() { return period; }
}
@Override public LicenseConsumerManager ftpManager(
LicenseConsumerManager parent,
Authentication authentication,
@CheckForNull Encryption encryption,
Store secret,
int days) {
return chainedManager(
parent,
ftpParameters(parent, authentication, encryption, days),
secret);
}
@Override public LicenseConsumerManager chainedManager(
LicenseConsumerManager parent,
Authentication authentication,
@CheckForNull Encryption encryption,
Store store) {
return chainedManager(
parent,
chainedParameters(parent, authentication, encryption),
store);
}
private LicenseConsumerManager chainedManager(
final LicenseConsumerManager parent,
final LicenseParameters lp,
final Store store) {
assert null != lp;
requireNonNull(parent);
requireNonNull(store);
return new ChainedManager() {
@Override public LicenseParameters parameters() { return lp; }
@Override public Store store() { return store; }
@Override LicenseConsumerManager parent() { return parent; }
};
}
private abstract class ChainedManager extends ChainedLicenseConsumerManager
implements LicenseConsumerManager {
final BasicLicenseConsumerContext cc = BasicLicenseConsumerContext.this;
final String subject = cc.subject();
final long period = cc.cachePeriodMillis();
{ if (0 > period) throw new IllegalArgumentException(); }
@Override public String subject() { return subject; }
@Override public LicenseConsumerContext context() { return cc; }
@Override public long cachePeriodMillis() { return period; }
@Override public License license() {
assert authentication().parameters().forSigning();
return cc.license();
}
}
@SuppressWarnings("PackageVisibleField")
@Override public ManagerBuilder manager() {
@NotThreadSafe
class MB implements ManagerBuilder {
final LicenseConsumerContext cc = BasicLicenseConsumerContext.this;
@Nullable LicenseConsumerManager parent;
int days;
@Nullable Authentication authentication;
@Nullable Encryption encryption;
@Nullable Store store;
@Override
public LicenseConsumerManager build() {
if (null == parent) return cc.manager(authentication, encryption, store);
return 0 != days
? cc.ftpManager(parent, authentication, encryption, store, days)
: cc.chainedManager(parent, authentication, encryption, store);
}
@Override
public ManagerBuilder inject() {
throw new UnsupportedOperationException();
}
@Override
public ManagerBuilder parent(final LicenseConsumerManager parent) {
this.parent = parent;
return this;
}
@Override
public ManagerBuilder parent() {
final MB target = this;
return new MB() {
@Override public ManagerBuilder inject() {
return target.parent(build());
}
};
}
@Override
public ManagerBuilder ftpDays(int days) {
this.days = days;
return this;
}
@Override
public ManagerBuilder authentication(final Authentication authentication) {
this.authentication = authentication;
return this;
}
@Override
public KsbaInjection keyStore() {
return new KsbaInjection() {
@Nullable String storeType, alias;
@Nullable Source source;
@Nullable ObfuscatedString storePassword, keyPassword;
@Override
public ManagerBuilder inject() {
return authentication(null != keyPassword
? cc.ftpKeyStore(source, storeType, storePassword, alias, keyPassword)
: cc.keyStore(source, storeType, storePassword, alias));
}
@Override
public KsbaInjection storeType(
final @CheckForNull String storeType) {
this.storeType = storeType;
return this;
}
@Override
public KsbaInjection loadFrom(final Source source) {
this.source = source;
return this;
}
@Override
public KsbaInjection loadFromResource(String name) {
return loadFrom(cc.resource(name));
}
@Override
public KsbaInjection storePassword(final ObfuscatedString storePassword) {
this.storePassword = storePassword;
return this;
}
@Override
public KsbaInjection alias(final String alias) {
this.alias = alias;
return this;
}
@Override
public KsbaInjection keyPassword(final ObfuscatedString keyPassword) {
this.keyPassword = keyPassword;
return this;
}
};
}
@Override
public ManagerBuilder encryption(final Encryption encryption) {
this.encryption = encryption;
return this;
}
@Override
public PbeInjection pbe() {
return new PbeInjection() {
@Nullable String algorithm;
@Nullable ObfuscatedString password;
@Override
public ManagerBuilder inject() {
return encryption(cc.pbe(algorithm, password));
}
@Override
public PbeInjection algorithm(
final @CheckForNull String algorithm) {
this.algorithm = algorithm;
return this;
}
@Override
public PbeInjection password(final ObfuscatedString password) {
this.password = password;
return this;
}
};
}
@Override
public ManagerBuilder storeIn(final Store store) {
this.store = store;
return this;
}
@Override
public ManagerBuilder storeInSystemNode(final Class> classInPackage) {
this.store = cc.systemNodeStore(classInPackage);
return this;
}
@Override
public ManagerBuilder storeInUserNode(final Class> classInPackage) {
this.store = cc.userNodeStore(classInPackage);
return this;
}
@Override
public ManagerBuilder storeInFile(final File file) {
this.store = cc.fileStore(file);
return this;
}
}
return new MB();
}
}