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

net.customware.license.support.simple.SimpleLicenseParam Maven / Gradle / Ivy

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

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.prefs.Preferences;

import net.customware.license.support.Exemption;
import net.customware.license.support.Restriction;
import net.customware.license.support.ValidatingLicenseParam;
import net.customware.license.support.store.ByteArrayStoreProvider;
import net.customware.license.support.store.StoreProvider;
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.KeyStoreParam;

public abstract class SimpleLicenseParam implements ValidatingLicenseParam {
    private String subject = "";

    private String keyAlias;

    private String publicPassword;

    private String privatePassword;

    private KeyStoreParam keyStoreParam;

    private CipherParam cipherParam;

    private StoreProvider storeProvider;

    private Preferences preferences;

    private List mRestrictions;

    private List uRestrictions;

    private List mExemptions;

    private List uExemptions;

    private boolean locked;

    private Object context;

    public SimpleLicenseParam() {
        this( true );
    }
    
    public SimpleLicenseParam( boolean lockable ) {
        keyStoreParam = new KeyStoreParam() {

            public String getAlias() {
                return keyAlias;
            }

            public String getKeyPwd() {
                return privatePassword;
            }

            public String getStorePwd() {
                return publicPassword;
            }

            public InputStream getStream() throws IOException {
                return storeProvider.createInputStream();
            }

        };

        cipherParam = new CipherParam() {
            public String getKeyPwd() {
                return publicPassword;
            }

        };

        preInit();
        init();
        postInit();
        locked = lockable;
    }

    /**
     * Subclasses can override this to add standard initialisation code which
     * occurs after the {@link #init()} method call, but before the class is
     * locked.
     */
    protected void postInit() {
        // Do nothing...
    }

    /**
     * Subclasses can override this to add standard initialisation code which
     * occurs before the {@link #init()} method call.
     */
    protected void preInit() {
        // Do nothing...
    }

    /**
     * This method is called when the class is being constructed, before it is
     * locked. Override this method to apply any settings fo the parameters.
     */
    protected abstract void init();

    public boolean isLocked() {
        return locked;
    }

    /**
     * Checks if the class is locked. If so, a {@link IllegalStateException} is
     * thrown.
     */
    protected void checkLock() {
        if ( locked )
            throw new IllegalStateException( "No further changes can be made to this class." );
    }

    public CipherParam getCipherParam() {
        return cipherParam;
    }

    public KeyStoreParam getKeyStoreParam() {
        return keyStoreParam;
    }

    public Preferences getPreferences() {
        return preferences;
    }

    protected void setPreferences( Preferences preferences ) {
        checkLock();
        this.preferences = preferences;
    }

    public String getSubject() {
        return subject;
    }

    public String getCipherPassword() {
        return publicPassword;
    }

    public String getKeyAlias() {
        return keyAlias;
    }

    protected void setKeyAlias( String keyAlias ) {
        checkLock();
        this.keyAlias = keyAlias;
    }

    protected void setPrivatePassword( String privatePassword ) {
        checkLock();
        this.privatePassword = privatePassword;
    }

    protected void setPublicPassword( String publicPassword ) {
        checkLock();
        this.publicPassword = publicPassword;
    }

    protected void setSubject( String subject ) {
        checkLock();
        this.subject = subject;
    }

    protected void setStoreProvider( StoreProvider provider ) {
        checkLock();
        this.storeProvider = provider;
    }

    protected void addRestriction( Restriction restriction ) {
        checkLock();
        if ( mRestrictions == null ) {
            mRestrictions = new java.util.ArrayList();
            uRestrictions = Collections.unmodifiableList( mRestrictions );
        }
        mRestrictions.add( restriction );
    }

    public Collection getRestrictions() {
        if ( mRestrictions == null )
            return Collections.EMPTY_LIST;

        return uRestrictions;
    }

    /**
     * This method sets the the specified byte array as the key store.
     * 
     * @param storeBytes
     *            The store bytes.
     */
    protected void setStoreBytes( byte[] storeBytes ) {
        setStoreProvider( new ByteArrayStoreProvider( storeBytes ) );
    }

    protected void addExemption( Exemption exemption ) {
        checkLock();
        if ( mExemptions == null ) {
            mExemptions = new java.util.ArrayList();
            uExemptions = Collections.unmodifiableList( mExemptions );
        }
        mExemptions.add( exemption );
    }

    public Collection getExemptions() {
        if ( mExemptions == null )
            return Collections.EMPTY_LIST;

        return uExemptions;
    }

    protected void setContext( Object context ) {
        checkLock();
        this.context = context;
    }

    public Object getContext() {
        return context;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy