TrueLicense Core 1.33

de.schlichtherle.license
Class LicenseManager

java.lang.Object
  extended by de.schlichtherle.license.LicenseManager
All Implemented Interfaces:
LicenseCreator, LicenseVerifier, Remote
Direct Known Subclasses:
LicenseManager

public class LicenseManager
extends Object
implements LicenseCreator, LicenseVerifier

This is the top level class which manages all licensing aspects like for instance the creation, installation and verification of license keys. The license manager knows how to install, verify and uninstall full and trial licenses for a given subject and ensures the privacy of the license content in its persistent form (i.e. the license key). For signing, verifying and validating licenses, this class cooperates with a LicenseNotary.

This class is thread-safe.

Author:
Christian Schlichtherle

Field Summary
private  GenericCertificate certificate
          The cached certificate of the current license key.
private  long certificateTimeout
          The time when the certificate was last set.
protected static String CN
          => "CN="
private static String CN_USER
          => localized string for resource key "user"
private static String EXC_CONSUMER_AMOUNT_IS_NOT_ONE
          => "exc.consumerAmountIsNotOne"
private static String EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE
          => "exc.consumerAmountIsNotPositive"
private static String EXC_CONSUMER_TYPE_IS_NOT_USER
          => "exc.consumerTypeIsNotUser"
private static String EXC_CONSUMER_TYPE_IS_NULL
          => "exc.consumerTypeIsNull"
private static String EXC_HOLDER_IS_NULL
          => "exc.holderIsNull"
private static String EXC_INVALID_SUBJECT
          => "exc.invalidSubject"
private static String EXC_ISSUED_IS_NULL
          => "exc.issuedIsNull"
private static String EXC_ISSUER_IS_NULL
          => "exc.issuerIsNull"
private static String EXC_LICENSE_HAS_EXPIRED
          => "exc.licenseHasExpired"
private static String EXC_LICENSE_IS_NOT_YET_VALID
          => "exc.licenseIsNotYetValid"
private static String FILE_FILTER_DESCRIPTION
          => "fileFilter.description"
private static String FILE_FILTER_SUFFIX
          => " (*.lic)"
private  FileFilter fileFilter
          A suitable file filter for the subject of this license manager.
private  PrivacyGuard guard
           
static String LICENSE_SUFFIX
          The suffix for files which hold license certificates. => ".lic" - must be lowercase!
private  LicenseNotary notary
           
private  LicenseParam param
           
private static String PREFERENCES_KEY
          The key in the preferences used to store the license key. => "license"
private static String SYSTEM
          => "System"
private static long TIMEOUT
          The timeout for the license content cache.
private static String USER
          => "User"
 
Constructor Summary
protected LicenseManager()
          Constructs a License Manager.
  LicenseManager(LicenseParam param)
          Constructs a License Manager.
 
Method Summary
 byte[] create(LicenseContent content)
          Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and returns it as a license key.
protected  byte[] create(LicenseContent content, LicenseNotary notary)
          Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and returns it as a license key.
protected  GenericCertificate getCertificate()
          Returns the license certificate cached from the last installation/verification of a license key or null if there wasn't an installation/verification or a timeout has occured.
 FileFilter getFileFilter()
          Returns a suitable file filter for the subject of this license manager.
protected  byte[] getLicenseKey()
          Returns the current license key.
protected  LicenseNotary getLicenseNotary()
          Returns a license notary configured to use the keystore parameters contained in the current license parameters - null is never returned.
 LicenseParam getLicenseParam()
          Returns the license configuration parameters.
protected  PrivacyGuard getPrivacyGuard()
          Returns a privacy guard configured to use the cipher parameters contained in the current license parameters - null is never returned.
protected  void initialize(LicenseContent content)
          Initializes the given license content with some reasonable defaults unless the respective properties have already been set.
protected  LicenseContent install(byte[] key, LicenseNotary notary)
          Decrypts, decompresses, decodes and verifies the license key in key, validates its license content and installs it as the current license key.
 LicenseContent install(File keyFile)
          Loads, decrypts, decompresses, decodes and verifies the license key in keyFile, validates its license content and installs it as the current license key.
protected  LicenseContent install(File keyFile, LicenseNotary notary)
          Loads, decrypts, decompresses, decodes and verifies the license key in keyFile, validates its license content and installs it as the current license key.
protected static byte[] loadLicenseKey(File keyFile)
          Loads and returns the first megabyte of content from keyFile as license key in a newly created byte array.
protected static Date midnight()
          Returns midnight local time today.
protected  void setCertificate(GenericCertificate certificate)
          Sets the given license certificate as installed or verified.
private  void setCertificate0(GenericCertificate certificate)
           
protected  void setLicenseKey(byte[] key)
          Installs the given license key as the current license key.
 void setLicenseParam(LicenseParam param)
          Sets the license configuration parameters.
private  void setLicenseParam0(LicenseParam param)
           
 void store(LicenseContent content, File keyFile)
          Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and stores it to the given file as a license key.
protected  void store(LicenseContent content, LicenseNotary notary, File keyFile)
          Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and stores it to the given file as a license key.
protected static void storeLicenseKey(byte[] key, File keyFile)
          Stores the given license key to the given file.
 void uninstall()
          Uninstalls the current license key.
protected  void validate(LicenseContent content)
          Validates the license content.
 LicenseContent verify()
          Decrypts, decompresses, decodes and verifies the current license key, validates its license content and returns it.
 LicenseContent verify(byte[] key)
          Decrypts, decompresses, decodes and verifies the given license key, validates its license content and returns it.
protected  LicenseContent verify(byte[] key, LicenseNotary notary)
          Decrypts, decompresses, decodes and verifies the given license key, validates its license content and returns it.
protected  LicenseContent verify(LicenseNotary notary)
          Decrypts, decompresses, decodes and verifies the current license key, validates its license content and returns it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

certificate

private GenericCertificate certificate
The cached certificate of the current license key.


certificateTimeout

private long certificateTimeout
The time when the certificate was last set.


CN

protected static final String CN
=> "CN="


CN_USER

private static final String CN_USER
=> localized string for resource key "user"


EXC_CONSUMER_AMOUNT_IS_NOT_ONE

private static final String EXC_CONSUMER_AMOUNT_IS_NOT_ONE
=> "exc.consumerAmountIsNotOne"


EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE

private static final String EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE
=> "exc.consumerAmountIsNotPositive"


EXC_CONSUMER_TYPE_IS_NOT_USER

private static final String EXC_CONSUMER_TYPE_IS_NOT_USER
=> "exc.consumerTypeIsNotUser"


EXC_CONSUMER_TYPE_IS_NULL

private static final String EXC_CONSUMER_TYPE_IS_NULL
=> "exc.consumerTypeIsNull"


EXC_HOLDER_IS_NULL

private static final String EXC_HOLDER_IS_NULL
=> "exc.holderIsNull"


EXC_INVALID_SUBJECT

private static final String EXC_INVALID_SUBJECT
=> "exc.invalidSubject"


EXC_ISSUED_IS_NULL

private static final String EXC_ISSUED_IS_NULL
=> "exc.issuedIsNull"


EXC_ISSUER_IS_NULL

private static final String EXC_ISSUER_IS_NULL
=> "exc.issuerIsNull"


EXC_LICENSE_HAS_EXPIRED

private static final String EXC_LICENSE_HAS_EXPIRED
=> "exc.licenseHasExpired"


EXC_LICENSE_IS_NOT_YET_VALID

private static final String EXC_LICENSE_IS_NOT_YET_VALID
=> "exc.licenseIsNotYetValid"


FILE_FILTER_DESCRIPTION

private static final String FILE_FILTER_DESCRIPTION
=> "fileFilter.description"


FILE_FILTER_SUFFIX

private static final String FILE_FILTER_SUFFIX
=> " (*.lic)"


fileFilter

private FileFilter fileFilter
A suitable file filter for the subject of this license manager.


guard

private PrivacyGuard guard

LICENSE_SUFFIX

public static final String LICENSE_SUFFIX
The suffix for files which hold license certificates. => ".lic" - must be lowercase!


notary

private LicenseNotary notary

param

private LicenseParam param

PREFERENCES_KEY

private static final String PREFERENCES_KEY
The key in the preferences used to store the license key. => "license"


SYSTEM

private static final String SYSTEM
=> "System"


TIMEOUT

private static final long TIMEOUT
The timeout for the license content cache.

See Also:
Constant Field Values

USER

private static final String USER
=> "User"

Constructor Detail

LicenseManager

protected LicenseManager()
Constructs a License Manager.

Warning: The manager created by this constructor is not valid and cannot be used unless setLicenseParam(LicenseParam) is called!


LicenseManager

public LicenseManager(LicenseParam param)
Constructs a License Manager.

Parameters:
param - the license configuration parameters - may not be null.
Throws:
NullPointerException - If the given parameter object does not obey the contract of its interface due to a null pointer.
IllegalPasswordException - If any password in the parameter object does not comply to the current policy.
Method Detail

create

public final byte[] create(LicenseContent content)
                    throws Exception
Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and returns it as a license key.

As a side effect, the given license content is initialized with some reasonable defaults unless the respective properties have already been set.

Specified by:
create in interface LicenseCreator
Parameters:
content - the license content - may not be null.
Returns:
The license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
create(LicenseContent, LicenseNotary), initialize(LicenseContent), validate(LicenseContent)

create

protected byte[] create(LicenseContent content,
                        LicenseNotary notary)
                 throws Exception
Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and returns it as a license key.

As a side effect, the given license content is initialized with some reasonable defaults unless the respective properties have already been set.

Parameters:
content - the license content - may not be null.
notary - the license notary used to sign the license key - may not be null.
Returns:
The license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
initialize(LicenseContent), validate(LicenseContent)

getCertificate

protected GenericCertificate getCertificate()
Returns the license certificate cached from the last installation/verification of a license key or null if there wasn't an installation/verification or a timeout has occured.


getFileFilter

public FileFilter getFileFilter()
Returns a suitable file filter for the subject of this license manager. On Windows systems, the case of the suffix is ignored when browsing directories.

Returns:
A valid FileFilter.

getLicenseKey

protected byte[] getLicenseKey()
Returns the current license key.


getLicenseNotary

protected LicenseNotary getLicenseNotary()
Returns a license notary configured to use the keystore parameters contained in the current license parameters - null is never returned.


getLicenseParam

public LicenseParam getLicenseParam()
Returns the license configuration parameters.


getPrivacyGuard

protected PrivacyGuard getPrivacyGuard()
Returns a privacy guard configured to use the cipher parameters contained in the current license parameters - null is never returned.


initialize

protected void initialize(LicenseContent content)
Initializes the given license content with some reasonable defaults unless the respective properties have already been set.

See Also:
validate(LicenseContent)

install

protected LicenseContent install(byte[] key,
                                 LicenseNotary notary)
                          throws Exception
Decrypts, decompresses, decodes and verifies the license key in key, validates its license content and installs it as the current license key.

Parameters:
key - the license key - may not be null.
notary - the license notary used to verify the license key - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

install

public final LicenseContent install(File keyFile)
                             throws Exception
Loads, decrypts, decompresses, decodes and verifies the license key in keyFile, validates its license content and installs it as the current license key.

Parameters:
keyFile - the file to load the license key from - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
install(File, LicenseNotary), validate(LicenseContent)

install

protected LicenseContent install(File keyFile,
                                 LicenseNotary notary)
                          throws Exception
Loads, decrypts, decompresses, decodes and verifies the license key in keyFile, validates its license content and installs it as the current license key.

Parameters:
keyFile - The file to load the license key from - may not be null.
notary - The license notary used to verify the license key - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

loadLicenseKey

protected static byte[] loadLicenseKey(File keyFile)
                                throws IOException
Loads and returns the first megabyte of content from keyFile as license key in a newly created byte array.

Parameters:
keyFile - the file holding the license key - may not be null.
Throws:
IOException

midnight

protected static Date midnight()
Returns midnight local time today.


setCertificate

protected void setCertificate(GenericCertificate certificate)
Sets the given license certificate as installed or verified.

Parameters:
certificate - the license certificate - may be null to clear.

setCertificate0

private void setCertificate0(GenericCertificate certificate)

setLicenseKey

protected void setLicenseKey(byte[] key)
Installs the given license key as the current license key. If key is null, the current license key gets uninstalled (but the cached license certificate is not cleared).


setLicenseParam

public void setLicenseParam(LicenseParam param)
Sets the license configuration parameters. Calling this method resets the manager as if it had been newly created. Some plausibility checks are applied to the given parameter object to ensure that it adheres to the contract of the parameter interfaces.

Parameters:
param - the license configuration parameters - may not be null.
Throws:
NullPointerException - If the given parameter object does not obey the contract of its interface due to a null pointer.
IllegalPasswordException - If any password in the parameter object does not comply to the current policy.

setLicenseParam0

private void setLicenseParam0(LicenseParam param)

store

public final void store(LicenseContent content,
                        File keyFile)
                 throws Exception
Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and stores it to the given file as a license key.

As a side effect, the given license content is initialized with some reasonable defaults unless the respective properties have already been set.

Parameters:
content - the license content - may not be null.
keyFile - the file to save the license key to - may not be null. This should have a LICENSE_SUFFIX.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
store(LicenseContent, LicenseNotary, File), initialize(LicenseContent), validate(LicenseContent)

store

protected void store(LicenseContent content,
                     LicenseNotary notary,
                     File keyFile)
              throws Exception
Initializes and validates the license content, creates a new signed license certificate for it and compresses, encrypts and stores it to the given file as a license key.

As a side effect, the given license content is initialized with some reasonable defaults unless the respective properties have already been set.

Parameters:
content - the license content - may not be null.
notary - the license notary used to sign the license key - may not be null.
keyFile - the file to save the license key to - may not be null. This should have a LICENSE_SUFFIX.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
initialize(LicenseContent), validate(LicenseContent)

storeLicenseKey

protected static void storeLicenseKey(byte[] key,
                                      File keyFile)
                               throws IOException
Stores the given license key to the given file.

Parameters:
key - the license key - may not be null.
keyFile - the file to save the license key to - may not be null. This should have a LICENSE_SUFFIX.
Throws:
IOException

uninstall

public void uninstall()
               throws Exception
Uninstalls the current license key.

Throws:
Exception - An instance of a subclass of this class for various reasons.

validate

protected void validate(LicenseContent content)
                 throws LicenseContentException
Validates the license content. This method is called whenever a license certificate is created, installed or verified.

Validation consists of the following plausability checks for the properties of this class:

If you need more or less rigid restrictions, you should override this method in a subclass.

Parameters:
content - the license content - may not be null.
Throws:
NullPointerException - if content is null.
LicenseContentException - if any validation test fails. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
initialize(LicenseContent)

verify

public final LicenseContent verify()
                            throws Exception
Decrypts, decompresses, decodes and verifies the current license key, validates its license content and returns it.

Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
NoLicenseInstalledException - if no license key is installed.
Exception - for any other reason. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

verify

public final LicenseContent verify(byte[] key)
                            throws Exception
Decrypts, decompresses, decodes and verifies the given license key, validates its license content and returns it.

Specified by:
verify in interface LicenseVerifier
Parameters:
key - the license key - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
Exception - an instance of a subclass of this class for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

verify

protected LicenseContent verify(byte[] key,
                                LicenseNotary notary)
                         throws Exception
Decrypts, decompresses, decodes and verifies the given license key, validates its license content and returns it.

Parameters:
key - the license key - may not be null.
notary - the license notary used to verify the license key - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
Exception - for various reasons. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

verify

protected LicenseContent verify(LicenseNotary notary)
                         throws Exception
Decrypts, decompresses, decodes and verifies the current license key, validates its license content and returns it.

Parameters:
notary - the license notary used to verify the current license key - may not be null.
Returns:
A clone of the verified and validated content of the license key - null is never returned.
Throws:
NoLicenseInstalledException - if no license key is installed.
Exception - for any other reason. Note that you should always use Throwable.getLocalizedMessage() to get a (possibly localized) meaningful detail message.
See Also:
validate(LicenseContent)

TrueLicense Core 1.33

Copyright © 2005-2015 Schlichtherle IT Services. All Rights Reserved.