com.jinhe.tss.framework.license.LicenseManager.java Source code

Java tutorial

Introduction

Here is the source code for com.jinhe.tss.framework.license.LicenseManager.java

Source

package com.jinhe.tss.framework.license;

import java.io.File;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jinhe.tss.util.EasyUtils;
import com.jinhe.tss.util.FileHelper;

/** 
 * <pre> 
 * 1??????license
 * 2?licenses??
 * 3????licenses
 * 
 * 4?licenses?JAVA Security API?????
 *    ?/??licenses??
 * 
 * 5?license?????
 *    ?java???
 * </pre>
 */
public final class LicenseManager {

    private final Log log = LogFactory.getLog(getClass());

    private static LicenseManager instance = new LicenseManager();

    private LicenseManager() {
    }

    public static synchronized LicenseManager getInstance() {
        return instance;
    }

    private List<License> licenses;

    public void loadLicenses() {
        if (licenses != null)
            return;

        licenses = new ArrayList<License>();
        String files[] = new File(LicenseFactory.LICENSE_DIR).list();
        for (int i = 0; i < files.length; i++) {
            String filename = files[i];
            File file = new File(LicenseFactory.LICENSE_DIR, filename);
            if (file.isDirectory() || !filename.endsWith(".license")) {
                continue;
            }

            try {
                License license = License.fromConfigFile(filename);

                Date expiresDate = license.expiresDate;
                if (expiresDate != null && expiresDate.before(new Date())) {
                    log.error("license \"" + file.getName() + "\" ?.");
                    continue;
                }
                if (!validate(license)) {
                    log.error("license \"" + file.getName() + "\" ??.");
                    continue;
                }

                licenses.add(license);
            } catch (Exception e) {
                log.error("?license" + filename, e);
            }
        }

        if (licenses.isEmpty()) {
            log.error("???license.");
        }
        return;
    }

    /**
     * ????license??
     * @param product
     * @param version
     * @return
     */
    public boolean validateLicense(String product, String version) {
        loadLicenses();
        if (!licenses.isEmpty()) {
            int needsVersion = Integer.parseInt(version.substring(0, 1));
            for (License license : licenses) {
                int hasVersion = Integer.parseInt(license.version.substring(0, 1));
                if (license.product.equals(product) && hasVersion >= needsVersion) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * ?license
     * 
     * @param product
     * @param version
     * @return
     */
    public String getLicenseType(String product, String version) {
        loadLicenses();
        if (!licenses.isEmpty()) {
            int needsVersion = Integer.parseInt(version.substring(0, 1));
            for (License license : licenses) {
                int hasVersion = Integer.parseInt(license.version.substring(0, 1));
                if (license.product.equals(product) && hasVersion >= needsVersion) {
                    return license.licenseType;
                }
            }
        }
        return null;
    }

    /**
     * <pre>
     * ?license??
     * ?Mac??????
     * ???????
     * </pre>
     * @param license
     * @return
     * @throws Exception
     */
    boolean validate(License license) throws Exception {
        String macAddress = license.macAddress;
        if (!EasyUtils.isNullOrEmpty(macAddress)) {
            String curMacAddress = MacAddress.getMacAddress();
            if (!macAddress.equals(curMacAddress)) {
                return false;
            }
        }

        File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE);
        String publicKey = FileHelper.readFile(keyFile).trim();

        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
        KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM);
        java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

        Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM);
        sig.initVerify(pubKey);
        sig.update(license.getFingerprint());
        return sig.verify(EasyUtils.decodeHex(license.licenseSignature));
    }
}