net.java.truelicense.core.V2XmlLicenseManagementContext 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 javax.annotation.concurrent.Immutable;
import javax.xml.bind.*;
import net.java.truelicense.core.auth.BasicRepository;
import net.java.truelicense.core.codec.JaxbCodec;
import net.java.truelicense.obfuscate.Obfuscate;
/**
* The root context for the management of Version-2-with-XML (V2/XML) format
* license keys.
* Note that there is no interoperability between different format license keys.
*
* Use this context to create a {@link LicenseVendorContext} or a
* {@link LicenseConsumerContext}.
* Here's an example for verifying the installed license key in a consumer
* application:
*
* LicenseManagementContext mc = new V2XmlLicenseManagementContext("MyApp 1");
* LicenseConsumerContext cc = mc.consumer();
* LicenseConsumerManager cm = cc.manager(...);
* cm.verify();
*
*
* Or in a fluent API programming style:
*
* new V2XmlLicenseManagementContext("MyApp 1").consumer().manager()...verify();
*
*
* Do not copy-paste this code!
* Instead, use the TrueLicense Maven Archetype to generate a sample project
* for you.
*
* Where required, this class should get subclassed to customize license
* {@linkplain #license creation},
* {@linkplain #initialization initialization}
* or {@linkplain #validation validation},
* {@linkplain #codec encoding} or to implement an authoritative
* {@linkplain #now clock} or to provide an alternative
* {@linkplain #classLoader class loader} for loading key stores.
*
* 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
public class V2XmlLicenseManagementContext
extends BasicV2LicenseManagementContext {
private volatile JAXBContext context;
/**
* Constructs a V2/XML license management context.
* The provided string should get computed on demand from an obfuscated
* form, e.g. by annotating a constant string value with the
* {@literal @}{@link Obfuscate} annotation and processing it with the
* TrueLicense Maven Plugin.
*
* @param subject the licensing subject, i.e. a product name with an
* optional version range, e.g. {@code MyApp 1}.
*/
public V2XmlLicenseManagementContext(String subject) { super(subject); }
/**
* {@inheritDoc}
*
* The implementation in the class {@link V2XmlLicenseManagementContext}
* returns a {@link JaxbCodec}.
*/
@Override public JaxbCodec codec() {
try {
return new JaxbCodec(context());
} catch (final JAXBException ex) {
throw new IllegalStateException(ex);
}
}
/**
* Returns the JAXB context to use for {@link #codec}.
*
* The implementation in the class {@link V2XmlLicenseManagementContext}
* lazily resolves this property by calling {@link #newContext}.
*/
public JAXBContext context() {
final JAXBContext c = context;
return null != c ? c : (context = newContext());
}
/**
* Returns a new JAXB context for use with {@linkplain #license licenses}
* and {@linkplain #repository repositories}.
* This method is normally only called once.
* In a multi-threaded environment, it may get called more than once, but
* then each invocation must return an object which behaves equivalent to
* any previously returned object.
*/
protected JAXBContext newContext() {
try {
return JAXBContext.newInstance(License.class, BasicRepository.class);
} catch (final JAXBException ex) {
throw new AssertionError(ex);
}
}
}