com.littcore.license.LicenseManager.java Source code

Java tutorial

Introduction

Here is the source code for com.littcore.license.LicenseManager.java

Source

package com.littcore.license;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.littcore.common.Utility;
import com.littcore.format.FormatDateTime;
import com.littcore.security.DecryptFailedException;
import com.littcore.security.DigitalSignatureTool;
import com.littcore.security.ISecurity;
import com.littcore.security.algorithm.Algorithm;
import com.littcore.security.algorithm.DESTool;
import com.littcore.util.XmlUtils;
import com.littcore.version.Version;

/**
 * 
 * <b></b> ?.
 * <pre><b>??</b>
 *    ???(??)
 *    
 *    ?????
 * </pre>
 * 
 * <pre><b></b>
 *    2012-06-13
 *                1?????
 *    2010-02-02 
 *                1??license????
 *                2?license??
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">?</a>
 * @since 2008-09-12, 2010-02-02
 * @version 1.0, 1.1
 * 
 */
public final class LicenseManager {
    public static final LicenseManager licenseManager = new LicenseManager();

    /**
     * license.
     */
    private License license;

    /** . */
    private PublicKey pubKey;

    /**
     * ??.
     */
    private LicenseManager() {
    }

    /**
     * Gets the single instance of LicenseManager.
     * 
     * @return single instance of LicenseManager
     */
    public static final LicenseManager getInstance() {
        return licenseManager;
    }

    /**
     * ?(??).
     * 
     * @param license the license
     * 
     * @return License 
     * 
     */
    public static final void setDefaultLicense(License license) {
        getInstance().license = license;
    }

    /**
     * ??(??).
     * 
     * @return License 
     * 
     * @throws LicenseException ??
     */
    public static final License getDefaultLicense() throws LicenseException {
        if (getInstance().license != null)
            return getInstance().license;
        else
            throw new LicenseException();
    }

    /**
     * ?(??).
     * 
     * @return License 
     * 
     * @throws LicenseException ?
     */
    public static final PublicKey getDefaultKey() throws LicenseException {
        if (getInstance().pubKey != null)
            return getInstance().pubKey;
        else
            throw new LicenseException();
    }

    /**
     * ?license.
     * 
     * @param licenseFile ?
     * 
     * @return License 
     * 
     * @throws LicenseException ?
     */
    public static final License getLicense(File licenseFile) throws LicenseException {
        try {
            if (getInstance().license != null)
                return getInstance().license;
            else
                return loadLicense(licenseFile);
        } catch (Exception e) {
            throw new LicenseException(e);
        }
    }

    /**
     * ?license.
     * 
     * @param licenseFilePath ?
     * 
     * @return License 
     * 
     * @throws LicenseException ?
     */
    public static final License getLicense(String licenseFilePath) throws LicenseException {
        try {
            if (getInstance().license != null)
                return getInstance().license;
            else
                return loadLicense(licenseFilePath);
        } catch (Exception e) {
            throw new LicenseException(e);
        }
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKey 
     * 
     * @throws LicenseException ??
     */
    public static void validateLicense() throws LicenseException {
        LicenseManager instance = LicenseManager.getInstance();
        validateLicense(instance.license, instance.pubKey);
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKeyFile 
     * 
     * @throws LicenseException ??
     */
    public static void validateLicense(License license, String pubKeyFilePath) throws LicenseException {
        File pubKeyFile = new File(pubKeyFilePath);
        validateLicense(license, pubKeyFile);
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKeyFile 
     * 
     * @throws LicenseException ??
     */
    public static void validateLicense(File licenseFile, File pubKeyFile) throws LicenseException {
        License license = getLicense(licenseFile);
        validateLicense(license, pubKeyFile);
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKey 
     * 
     * @throws LicenseException ??
     */
    public static void validateLicense(License license, PublicKey pubKey) throws LicenseException {
        try {
            DigitalSignatureTool tool = new DigitalSignatureTool(pubKey);
            validateLicense(license, tool);
        } catch (FileNotFoundException e) {
            throw new LicenseException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new LicenseException(e);
        } catch (DecryptFailedException e) {
            throw new LicenseException(e);
        }
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKeyFile 
     * 
     * @throws LicenseException ??
     */
    public static void validateLicense(License license, File pubKeyFile) throws LicenseException {
        try {
            DigitalSignatureTool tools = new DigitalSignatureTool(pubKeyFile);
            validateLicense(license, tools);
        } catch (FileNotFoundException e) {
            throw new LicenseException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new LicenseException(e);
        } catch (DecryptFailedException e) {
            throw new LicenseException(e);
        }
    }

    /**
     * license.
     * 
     * @param license ?
     * @param pubKeyFile 
     * 
     * @throws LicenseException ??
     */
    private static void validateLicense(License license, DigitalSignatureTool tools) throws LicenseException {
        if (license == null)
            throw new LicenseException("License is not exist!");
        license.validate();
        try {
            String signedData = license.getSignature();
            tools.validateSign(license.toString(), signedData);
        } catch (DecryptFailedException e) {
            throw new LicenseException(e);
        }
        //???      
        //??      
        //      if(license.isExpired())
        //      {
        //         throw new LicenseException("??");         
        //      }      
    }

    /**
     * license.
     * 
     * @param licenseFilePath ?
     * @param pubKeyFilePath 
     * 
     * @throws LicenseException ??
     */
    public static final void validateLicense(String licenseFilePath, String pubKeyFilePath)
            throws LicenseException {
        //Note, the software license expressely forbids tampering with this check. 
        License license = getLicense(licenseFilePath);
        validateLicense(license, pubKeyFilePath);
    }

    /**
     * ??.
     * 
     * @param licenseFilePath ?
     * 
     * @return ?
     * 
     * @throws FileNotFoundException 
     * @throws DocumentException ?
     */
    private static License loadLicense(String licenseFilePath) throws FileNotFoundException, DocumentException {
        File licenseFile = new File(licenseFilePath);
        return loadLicense(licenseFile);
    }

    /**
     * ??.
     * 
     * @param licenseFile ?
     * 
     * @return ?
     * 
     * @throws FileNotFoundException 
     * @throws DocumentException ?
     */
    public static License loadLicense(File licenseFile) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(licenseFile);
        return loadLicense(document);
    }

    public static License loadLicenseFromContent(String xmlContent) throws LicenseException {
        try {
            Document document = XmlUtils.readXml(xmlContent);
            return loadLicense(document);
        } catch (Exception e) {
            throw new LicenseException(e);
        }
    }

    /**
     * @param document
     * @return
     */
    private static License loadLicense(Document document) {
        Element rootElement = document.getRootElement(); // ?rootElement      
        License license = new License();
        license.setLicenseId(rootElement.element("licenseId").getTextTrim());
        license.setLicenseType(rootElement.element("licenseType").getTextTrim());
        license.setProductName(rootElement.element("productName").getTextTrim());
        license.setCompanyName(rootElement.element("companyName").getTextTrim());
        license.setCustomerName(rootElement.element("customerName").getTextTrim());
        license.setVersion(Version.parseVersion(rootElement.element("version").getTextTrim()));
        license.setCreateDate(Utility.parseDate(rootElement.element("createDate").getTextTrim()));
        license.setExpiredDate(Utility.parseDate(rootElement.element("expiredDate").getTextTrim()));
        license.setSignature(rootElement.element("signature").getTextTrim());
        return license;
    }

    /**
     * ?License???
     * 
     * @param licenseId
     * @param content
     * @return
     * @throws LicenseException
     */
    public static String decryptLicense(String licenseId, String content) throws LicenseException {
        //DES
        try {
            ISecurity security = new DESTool(licenseId, Algorithm.BLOWFISH);
            return security.decrypt(content);
        } catch (NoSuchAlgorithmException e) {
            throw new LicenseException(e);
        } catch (DecryptFailedException e) {
            throw new LicenseException(e);
        }
    }

    /**
     * ??.
     * 
     * @param license the license
     * @param targetFile 
     */
    public static void writeLicense(License license, File targetFile) throws IOException {
        Document document = DocumentHelper.createDocument();
        document.addElement("license");
        Element root = document.getRootElement();

        //
        root.addElement("licenseId").setText(license.getLicenseId());
        root.addElement("licenseType").setText(license.getLicenseType());
        root.addElement("productName").setText(license.getProductName());
        root.addElement("companyName").setText(license.getCompanyName());
        root.addElement("customerName").setText(license.getCustomerName());
        root.addElement("version").setText(license.getVersion().toString());
        root.addElement("createDate").setText(FormatDateTime.formatDate(license.getCreateDate()));
        root.addElement("expiredDate").setText(FormatDateTime.formatDate(license.getExpiredDate()));
        root.addElement("signature").setText(license.getSignature());
        XmlUtils.writeXml(targetFile, document);
    }

    /**
     * ??.
     * 
     * @param licenseFilePath ?
     * @param pubKeyFilePath 
        
     * @throws LicenseException ?
     */
    public static void reload(String licenseFilePath, String pubKeyFilePath) throws LicenseException {
        try {
            License license = loadLicense(licenseFilePath);
            //?
            validateLicense(license, pubKeyFilePath);
            //???       
            getInstance().license = license; //
            getInstance().pubKey = DigitalSignatureTool.readPubKey(new File(pubKeyFilePath));
        } catch (FileNotFoundException e) {
            throw new LicenseException("??", e);
        } catch (DocumentException e) {
            throw new LicenseException("??", e);
        } catch (DecryptFailedException e) {
            throw new LicenseException("??", e);
        }
    }

    public static void main(String[] args) throws Exception {
        LicenseManager.validateLicense("D:\\license.xml", "D:\\teamwork.dat");
    }

}