com.jp.protection.pub
Class LicenseReader

java.lang.Object
  extended by com.jp.protection.pub.LicenseReader
Direct Known Subclasses:
BasicLauncherConfigStorage.EntryLicenseReader, BasicLicenseStorage.EntryLicenseReader, LicenseUpgradeChecker.BackupLicenseReader, LicenseUpgradeChecker.FileLicenseReader, ProductLicenseReader

public class LicenseReader
extends java.lang.Object

This class is intended to read a license, decode it, hold the already read license and notify interested listeners about reading process and its results.

See Also:
LicenseReaderListener

Nested Class Summary
static class LicenseReader.FileStorage
           
static class LicenseReader.InMemoryStorage
           
static interface LicenseReader.Storage
           
 
Field Summary
protected  java.lang.Class fClassForLoadLicenseResource
           
protected  byte[] fDecryptKeyBytes
           
protected  License fLicense
           
protected  java.lang.String fLicenseFileName
           
protected  java.lang.String fLicenseFolder
           
protected  boolean fLicenseRead
           
protected  LicenseReaderIssueResolver fLicenseReaderIssueResolver
           
protected  java.lang.String fLicenseResourceFolder
           
protected  boolean fPerformLicenseBackups
           
protected  boolean fSearchLicenseInFile
           
protected  java.lang.String fSecurityAlgorithm
           
protected  SecurityProvider fSecurityProvider
           
protected  boolean fSkipEncryption
           
protected  LicenseReader.Storage fStorage
           
protected  boolean fUserHomeRelative
           
protected  boolean fVerbose
           
 
Constructor Summary
LicenseReader()
           
 
Method Summary
 void addLicenseReaderListener(LicenseReaderListener l)
          Adds license reader listener
 boolean backupLicense()
          Performs backup of current license
 boolean canRestoreLicenseFromBackup()
          Checks whether a license can be restored from backup
protected  void checkLicenseFile()
           
protected  byte[] decodeLicense(byte[] aLicenseBytes)
           
protected  void error(java.lang.String aMessage)
           
protected  void error(java.lang.Throwable ex)
           
protected  void fireLicenseAboutToRead(LicenseReader e, java.lang.String aProductID)
           
protected  void fireLicenseAvailable(LicenseReader e, java.lang.String aLicenseLocation)
           
protected  void fireLicenseCorrupted(LicenseReader e, java.lang.String aLicenseLocation)
           
protected  void fireLicenseMissing(LicenseReader e, java.lang.String aLicenseLocation)
           
protected  void fireLicenseRemoved(LicenseReader e, java.lang.String aLicenseLocation)
           
protected  void fireLicenseUpdated(LicenseReader e, java.lang.String aLicenseLocation)
           
 java.io.InputStream getBackupLicenseInputStream()
          Returns input stream for a backed up license
 java.lang.Class getClassForLoadLicenseResource()
          Returns class used for loading license from the application resource.
 byte[] getDecryptKeyBytes()
          Returns bytes array that specifies key used to decrypt the license.
 LicenseReaderIssueResolver getIssueResolver()
          Returns issue resolver used to fix issues occured during license locating and reading.
 License getLicense()
          Returns already read license.
 License getLicense(boolean aForceRead)
          Returns already read license.
 byte[] getLicenseBytes()
          Reads a license file and returns it as an array of bytes
protected  byte[] getLicenseBytes(java.io.InputStream aLicenseInputStream)
           
 java.io.File getLicenseFile()
          Constructs and returns file of the license according to the licenseFileName, licenseFolder and licenseReader attributes.
 java.io.InputStream getLicenseFileInputStream()
          Returns input stream to read license from file
 java.lang.String getLicenseFileLocation()
          Returns path of license file
 java.lang.String getLicenseFileName()
          Returns the license file name, which should be read by the reader.
 java.lang.String getLicenseFolder()
          Returns path to the folder outside of application archive, which contains the license to be read.
 java.io.InputStream getLicenseInputStream()
          Returns input stream to read license from
 java.io.OutputStream getLicenseOutputStream()
          Returns output stream to write license to.
 java.lang.String getLicenseProduct(java.io.InputStream aLicenseInputStream)
          Reads and returns license product from the specified stream.
 java.lang.String getLicenseResourceFolder()
          Returns the path to the folder within application archive, which contains the license file.
 java.io.InputStream getLicenseResourceInputStream()
          Returns input stream to read license from the application resource
 java.lang.String getLicenseResourceLocation()
          Constructs and returns location of the license resource within the application archive
 byte[] getPrivateKeyBytes()
          Deprecated. use getDecryptKeyBytes() instead
 java.lang.String getSecurityAlgorithm()
          Returns identifier of security algorithm used to decode licenses.
protected  SecurityProvider getSecurityProvider()
           
 LicenseReader.Storage getStorage()
           
static java.io.File getUserHome()
           
 boolean isLicenseRead()
          Checks whether license is already read.
 boolean isPerformLicenseBackups()
          Returns whether license backups should be performed when applying a new license
 boolean isSearchLicenseInFile()
          Returns whether license should be searched in file first
 boolean isSkipEncryption()
          Checks whether license encryption should be skipped.
 boolean isUserHomeRelative()
          Checks whether the reader should treat license folder as relative to the user home.
 boolean isVerbose()
          Checks whether reader runs in verbose mode.
protected  void readLicense()
           
protected  void readLicense(byte[] aLicenseBytes)
           
 void readLicense(java.io.InputStream aLicenseInputStream, java.lang.String aLicenseLocation)
          Reads the license from the specified stream.
 boolean removeLicense()
          Removes license from local storage.
 void removeLicenseReaderListener(LicenseReaderListener l)
          Removes license reader listener.
 boolean restoreLicenseFromBackup()
          Restores a license from backup
 void setClassForLoadLicenseResource(java.lang.Class aClassForLoadLicenseResource)
          Specifies class should be used for loading license from the application resource.
 void setDecryptKeyBytes(byte[] aDecryptKeyBytes)
          Specifies bytes array representing key required to decrypt the license.
 void setDecryptKeyBytes(java.lang.String aDecryptKeyBytes)
          Specifies bytes array used to restore the key required to decode the license.
 void setIssueResolver(LicenseReaderIssueResolver aLicenseReaderIssueResolver)
          Specifies issue resolver should be used to fix issues occured during license locating and reading.
 void setLicenseFileName(java.lang.String aLicenseFileName)
          Specifies the name of the license file
 void setLicenseFolder(java.lang.String aLicenseFolder)
          Specifies path to the folder, which contains the license to be read.
 void setLicenseResourceFolder(java.lang.String aLicenseResourceFolder)
          Specifies the path to the resource folder, which contains the license file.
 void setPerformLicenseBackups(boolean aValue)
          Specifies whether license backups should be performed when applying a new license
 void setPrivateKeyBytes(byte[] aPrivateKeyBytes)
          Deprecated. use setDecryptKeyBytes() instead
 void setPrivateKeyBytes(java.lang.String aPrivateKeyBytes)
          Deprecated. use setDecryptKeyBytes() instead
 void setSearchLicenseInFile(boolean aSearchLicenseInFile)
          Specifies that license should be searched in file first.
 void setSecurityAlgorithm(java.lang.String aSecurityAlgorithm)
          Specifies identifier of security algorithm should be used to decode licenses.
 void setSkipEncryption(boolean aSkipEncryption)
          Specifies that license encryption should be skipped.
 void setStorage(LicenseReader.Storage aStorage)
           
 void setUserHomeRelative(boolean aUserHomeRelative)
          Specifies that the reader should treat license folder as relative to the user home.
 void setVerbose(boolean aVerbose)
          Specifies that reader should be run in verbose mode.
protected  byte[] smartDecodeLicense(byte[] aLicenseBytes)
           
protected  byte[] smartGetLicenseBytes(java.io.InputStream aLicenseInputStream)
           
protected  void smartReadLicense(byte[] aLicenseBytes)
           
 boolean updateLicense(byte[] aLicenseBytes)
          Updates license in the local storage.
 boolean updateLicense(java.io.InputStream aNewLicenseInputStream)
          Replaces current license with new one optionally performing backup
 boolean updateLicense(java.io.InputStream aNewLicenseInputStream, boolean isMakeBackup)
          Replaces current license with new one explicitly specifying whether backup need to be done
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fLicense

protected License fLicense

fLicenseFileName

protected java.lang.String fLicenseFileName

fUserHomeRelative

protected boolean fUserHomeRelative

fLicenseFolder

protected java.lang.String fLicenseFolder

fLicenseResourceFolder

protected java.lang.String fLicenseResourceFolder

fLicenseRead

protected boolean fLicenseRead

fVerbose

protected boolean fVerbose

fSkipEncryption

protected boolean fSkipEncryption

fDecryptKeyBytes

protected byte[] fDecryptKeyBytes

fLicenseReaderIssueResolver

protected LicenseReaderIssueResolver fLicenseReaderIssueResolver

fSearchLicenseInFile

protected boolean fSearchLicenseInFile

fPerformLicenseBackups

protected boolean fPerformLicenseBackups

fStorage

protected LicenseReader.Storage fStorage

fSecurityProvider

protected SecurityProvider fSecurityProvider

fSecurityAlgorithm

protected java.lang.String fSecurityAlgorithm

fClassForLoadLicenseResource

protected java.lang.Class fClassForLoadLicenseResource
Constructor Detail

LicenseReader

public LicenseReader()
Method Detail

getLicenseResourceFolder

public java.lang.String getLicenseResourceFolder()
Returns the path to the folder within application archive, which contains the license file. If this path is null and therefore is not specified the reader will not try to find the license in the application resources.

Returns:
path to the resource folder.

setLicenseResourceFolder

public void setLicenseResourceFolder(java.lang.String aLicenseResourceFolder)
Specifies the path to the resource folder, which contains the license file.

Parameters:
aLicenseResourceFolder - path to the resource folder

getDecryptKeyBytes

public byte[] getDecryptKeyBytes()
Returns bytes array that specifies key used to decrypt the license.

Returns:
decryption key bytes

setDecryptKeyBytes

public void setDecryptKeyBytes(byte[] aDecryptKeyBytes)
Specifies bytes array representing key required to decrypt the license.

Parameters:
aDecryptKeyBytes - decryption key bytes

setDecryptKeyBytes

public void setDecryptKeyBytes(java.lang.String aDecryptKeyBytes)
Specifies bytes array used to restore the key required to decode the license.

Parameters:
aDecryptKeyBytes - private key bytes as String e.g. "1, 23, 45, 23, -1"

getPrivateKeyBytes

public byte[] getPrivateKeyBytes()
Deprecated. use getDecryptKeyBytes() instead

Returns bytes array that specifies private key used to decode the license.

Returns:
private key bytes

setPrivateKeyBytes

public void setPrivateKeyBytes(byte[] aPrivateKeyBytes)
Deprecated. use setDecryptKeyBytes() instead

Specifies bytes array used to restore the private key required to decode the license.

Parameters:
aPrivateKeyBytes - private key bytes

setPrivateKeyBytes

public void setPrivateKeyBytes(java.lang.String aPrivateKeyBytes)
Deprecated. use setDecryptKeyBytes() instead

Specifies bytes array used to restore the private key required to decode the license.

Parameters:
aPrivateKeyBytes - private key bytes as String e.g. "1, 23, 45, 23, -1"

isLicenseRead

public boolean isLicenseRead()
Checks whether license is already read.

Returns:
true if license is already read, false otherwise.

isSkipEncryption

public boolean isSkipEncryption()
Checks whether license encryption should be skipped.

Returns:
true license encryption should be skipped, false otherwise.

setSkipEncryption

public void setSkipEncryption(boolean aSkipEncryption)
Specifies that license encryption should be skipped. Note: skipping can be useful for debugging purposes only.

Parameters:
aSkipEncryption - true if license encryption should be skipped, false otherwise.

isVerbose

public boolean isVerbose()
Checks whether reader runs in verbose mode.

Returns:
true if reader runs in verbose mode, false otherwise.

setVerbose

public void setVerbose(boolean aVerbose)
Specifies that reader should be run in verbose mode. If reader run in verbose mode all exceptions stack traces and error messages, if any, will be printed to stderr. Otherwise there is no output in order to prevent the code flow analysis.

Parameters:
aVerbose - true if reader should run in verbose mode, false otherwise.

error

protected void error(java.lang.String aMessage)

error

protected void error(java.lang.Throwable ex)

isUserHomeRelative

public boolean isUserHomeRelative()
Checks whether the reader should treat license folder as relative to the user home.

Returns:
true if reader should treat license folder as relative to the user home, false otherwise.

setUserHomeRelative

public void setUserHomeRelative(boolean aUserHomeRelative)
Specifies that the reader should treat license folder as relative to the user home.

Parameters:
aUserHomeRelative - true if reader should treat license folder as relative to the user home, false otherwise.

getLicenseFileName

public java.lang.String getLicenseFileName()
Returns the license file name, which should be read by the reader.

Returns:
name of license file.

setLicenseFileName

public void setLicenseFileName(java.lang.String aLicenseFileName)
Specifies the name of the license file

Parameters:
aLicenseFileName - name of the license file

getLicenseFolder

public java.lang.String getLicenseFolder()
Returns path to the folder outside of application archive, which contains the license to be read.

Returns:
path to the folder.

setLicenseFolder

public void setLicenseFolder(java.lang.String aLicenseFolder)
Specifies path to the folder, which contains the license to be read.

Parameters:
aLicenseFolder - path to the folder

getLicense

public License getLicense()
Returns already read license. If license is not yet read, reads it first and then returns a license.

Returns:
instance of the License, null if license can't be read.

getLicense

public License getLicense(boolean aForceRead)
Returns already read license. If license is not yet read, reads it first and then returns a license.

Parameters:
aForceRead - specifies whether license should be re-read if it is already read.
Returns:
instance of the License, null if license can not be read.

readLicense

protected void readLicense()

isSearchLicenseInFile

public boolean isSearchLicenseInFile()
Returns whether license should be searched in file first

Returns:
true if license should be searched in file first

setSearchLicenseInFile

public void setSearchLicenseInFile(boolean aSearchLicenseInFile)
Specifies that license should be searched in file first.

Parameters:
aSearchLicenseInFile - true if license should be searched in file first

getLicenseInputStream

public java.io.InputStream getLicenseInputStream()
Returns input stream to read license from

Returns:
input stream to read license from

getLicenseOutputStream

public java.io.OutputStream getLicenseOutputStream()
Returns output stream to write license to.

Returns:
output stream to write license to.

isPerformLicenseBackups

public boolean isPerformLicenseBackups()
Returns whether license backups should be performed when applying a new license

Returns:
true if license backups should be performed when applying a new license; false otherwise
Since:
v5.0

setPerformLicenseBackups

public void setPerformLicenseBackups(boolean aValue)
Specifies whether license backups should be performed when applying a new license

Parameters:
aValue - true if license backups should be performed when applying a new license; false otherwise
Since:
v5.0

backupLicense

public boolean backupLicense()
Performs backup of current license

Returns:
true if the license has been backed up; false otherwise
Since:
v5.0

canRestoreLicenseFromBackup

public boolean canRestoreLicenseFromBackup()
Checks whether a license can be restored from backup

Returns:
true if a license can be restored from backup; false otherwise
Since:
v5.0

restoreLicenseFromBackup

public boolean restoreLicenseFromBackup()
Restores a license from backup

Returns:
true if license has been restored from backup; false otherwise
Since:
v5.0

getBackupLicenseInputStream

public java.io.InputStream getBackupLicenseInputStream()
Returns input stream for a backed up license

Returns:
input stream
Since:
v5.0

updateLicense

public boolean updateLicense(java.io.InputStream aNewLicenseInputStream)
                      throws java.io.IOException
Replaces current license with new one optionally performing backup

Parameters:
aNewLicenseInputStream - input stream for reading a new license
Returns:
true if current license has been replaced with new one; false otherwise
Throws:
java.io.IOException - for any exception occured during reading/writing of licenses
Since:
v5.0

updateLicense

public boolean updateLicense(java.io.InputStream aNewLicenseInputStream,
                             boolean isMakeBackup)
                      throws java.io.IOException
Replaces current license with new one explicitly specifying whether backup need to be done

Parameters:
aNewLicenseInputStream - input stream for reading a new license
isMakeBackup - true if backup need to be done; false otherwise
Returns:
true if current license has been replaced with new one; false otherwise
Throws:
java.io.IOException - for any exception occured during reading/writing of licenses
Since:
v5.0

checkLicenseFile

protected void checkLicenseFile()
                         throws java.lang.IllegalStateException
Throws:
java.lang.IllegalStateException

getStorage

public LicenseReader.Storage getStorage()

setStorage

public void setStorage(LicenseReader.Storage aStorage)

readLicense

public void readLicense(java.io.InputStream aLicenseInputStream,
                        java.lang.String aLicenseLocation)
                 throws java.io.IOException
Reads the license from the specified stream.

Parameters:
aLicenseInputStream - stream to read license from
aLicenseLocation - optional license location information that will be passed to License Reader events.
Throws:
java.io.IOException - for any errors occured during license reading.

smartDecodeLicense

protected byte[] smartDecodeLicense(byte[] aLicenseBytes)

decodeLicense

protected byte[] decodeLicense(byte[] aLicenseBytes)

getSecurityAlgorithm

public java.lang.String getSecurityAlgorithm()
Returns identifier of security algorithm used to decode licenses.

Returns:
identifier of security algorithm used to decode licenses.

setSecurityAlgorithm

public void setSecurityAlgorithm(java.lang.String aSecurityAlgorithm)
Specifies identifier of security algorithm should be used to decode licenses.

Parameters:
aSecurityAlgorithm - identifier of security algorithm.

getSecurityProvider

protected SecurityProvider getSecurityProvider()

smartReadLicense

protected void smartReadLicense(byte[] aLicenseBytes)

readLicense

protected void readLicense(byte[] aLicenseBytes)

getLicenseProduct

public java.lang.String getLicenseProduct(java.io.InputStream aLicenseInputStream)
Reads and returns license product from the specified stream.

Parameters:
aLicenseInputStream - stream to read license product from.
Returns:
license product

smartGetLicenseBytes

protected byte[] smartGetLicenseBytes(java.io.InputStream aLicenseInputStream)

getLicenseBytes

protected byte[] getLicenseBytes(java.io.InputStream aLicenseInputStream)

getLicenseFileLocation

public java.lang.String getLicenseFileLocation()
Returns path of license file

Returns:
path of license file

getLicenseResourceLocation

public java.lang.String getLicenseResourceLocation()
Constructs and returns location of the license resource within the application archive

Returns:
location of license resource

getLicenseFileInputStream

public java.io.InputStream getLicenseFileInputStream()
Returns input stream to read license from file

Returns:
input stream to read license from file

getLicenseResourceInputStream

public java.io.InputStream getLicenseResourceInputStream()
Returns input stream to read license from the application resource

Returns:
input stream to read license from the application resource

getClassForLoadLicenseResource

public java.lang.Class getClassForLoadLicenseResource()
Returns class used for loading license from the application resource.

Returns:
class used for loading license from the application resource.

setClassForLoadLicenseResource

public void setClassForLoadLicenseResource(java.lang.Class aClassForLoadLicenseResource)
Specifies class should be used for loading license from the application resource. If explicit value is not specified the LicenseReader class is used.

Parameters:
aClassForLoadLicenseResource - class should be used for loading license from the application resource.

getUserHome

public static java.io.File getUserHome()

getLicenseFile

public java.io.File getLicenseFile()
Constructs and returns file of the license according to the licenseFileName, licenseFolder and licenseReader attributes.

Returns:
file of the license

addLicenseReaderListener

public void addLicenseReaderListener(LicenseReaderListener l)
Adds license reader listener

Parameters:
l - license listener instance.

removeLicenseReaderListener

public void removeLicenseReaderListener(LicenseReaderListener l)
Removes license reader listener.

Parameters:
l - license listener instance.

fireLicenseMissing

protected void fireLicenseMissing(LicenseReader e,
                                  java.lang.String aLicenseLocation)

fireLicenseCorrupted

protected void fireLicenseCorrupted(LicenseReader e,
                                    java.lang.String aLicenseLocation)

fireLicenseAvailable

protected void fireLicenseAvailable(LicenseReader e,
                                    java.lang.String aLicenseLocation)

fireLicenseAboutToRead

protected void fireLicenseAboutToRead(LicenseReader e,
                                      java.lang.String aProductID)

getIssueResolver

public LicenseReaderIssueResolver getIssueResolver()
Returns issue resolver used to fix issues occured during license locating and reading.

Returns:
issue resolver.

setIssueResolver

public void setIssueResolver(LicenseReaderIssueResolver aLicenseReaderIssueResolver)
Specifies issue resolver should be used to fix issues occured during license locating and reading.

Parameters:
aLicenseReaderIssueResolver - issue resolver

getLicenseBytes

public byte[] getLicenseBytes()
Reads a license file and returns it as an array of bytes

Returns:
array of bytes represents license file

fireLicenseRemoved

protected void fireLicenseRemoved(LicenseReader e,
                                  java.lang.String aLicenseLocation)

fireLicenseUpdated

protected void fireLicenseUpdated(LicenseReader e,
                                  java.lang.String aLicenseLocation)

removeLicense

public boolean removeLicense()
Removes license from local storage. It can be called when license was revoked or is not available anymore. Default implementation deletes local license file. Note: license will not be reloaded/cleared after calling of that method; getLicense(true) must be called explicitly to apply that change.

Returns:
true if license was removed; false otherwise.
Since:
v4.8

updateLicense

public boolean updateLicense(byte[] aLicenseBytes)
                      throws java.io.IOException
Updates license in the local storage. It can be called when a new up-to-date version of the license was obtained from a remote service like from Licensing Server. Default implementation writes license bytes to the local license file. Note: license will not be reloaded after calling of that method; getLicense(true) must be called explicitly to apply that change.

Parameters:
aLicenseBytes - byte[] license bytes
Throws:
java.io.IOException - if writing failed
Since:
v4.8