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

net.customware.license.support.BundledLicenseManager Maven / Gradle / Ivy

The newest version!
package net.customware.license.support;

import java.io.File;

import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseNotary;
import de.schlichtherle.license.LicenseParam;

/**
 * This is a simple extension of {@link LicenseManager} which enables it to be
 * used in a bundled library - that is, one which does not use the same
 * classloader as the default system.
 * 
 * @author David Peterson
 */
public class BundledLicenseManager extends LicenseManager {

    private ClassLoader bundledClassLoader;

    /**
     * Constructs a BundledLicenseManager, using its own class loader to restore
     * the LicenseContent.
     */
    public BundledLicenseManager() {
    }

    public BundledLicenseManager( LicenseParam licenseParam ) {
        super( licenseParam );
    }

    public BundledLicenseManager( ClassLoader classLoader ) {
        bundledClassLoader = classLoader;
    }

    /**
     * Constructs a BundledLicenseManager, using the specified class loader to
     * restore the LicenseContent.
     * 
     * @param classLoader
     *            The class loader to use.
     */
    public BundledLicenseManager( LicenseParam licenseParam, ClassLoader classLoader ) {
        super( licenseParam );
        bundledClassLoader = classLoader;
    }

    @SuppressWarnings("deprecation")
    @Override protected synchronized LicenseContent install( byte[] license, LicenseNotary notary ) throws Exception {
        ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( getClassLoader() );
        try {
            LicenseContent content = super.install( license, notary );
            return content;
        } finally {
            Thread.currentThread().setContextClassLoader( ctxClassLoader );
        }
    }

    @SuppressWarnings("deprecation")
    @Override protected synchronized LicenseContent verify( byte[] license, LicenseNotary notary ) throws Exception {
        ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( getClassLoader() );
        try {
            LicenseContent content = super.verify( license, notary );
            return content;
        } finally {
            Thread.currentThread().setContextClassLoader( ctxClassLoader );
        }
    }

    @SuppressWarnings("deprecation")
    @Override protected synchronized LicenseContent verify( LicenseNotary notary ) throws Exception {
        ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( getClassLoader() );
        try {
            LicenseContent content = super.verify( notary );
            return content;
        } finally {
            Thread.currentThread().setContextClassLoader( ctxClassLoader );
        }
    }

    @SuppressWarnings("deprecation")
    @Override protected synchronized void store( LicenseContent licenseContent, LicenseNotary notary, File file )
            throws Exception {
        ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader( getClassLoader() );
        try {
            super.store( licenseContent, notary, file );
        } finally {
            Thread.currentThread().setContextClassLoader( ctxClassLoader );
        }
    }

    private ClassLoader getClassLoader() {
        if ( bundledClassLoader == null )
            bundledClassLoader = getClass().getClassLoader();
        return bundledClassLoader;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy