Java tutorial
/* ================================================================== * Created [2009-4-27 ?11:32:55] by Jon.King * ================================================================== * TSS * ================================================================== * mailTo:jinpujun@hotmail.com * Copyright (c) Jon.King, 2009-2012 * ================================================================== */ package com.jinhe.tss.core.common.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.core.util.FileHelper; import com.jinhe.tss.core.util.EasyUtils; /** * <p> LicenseManager.java </p> * * 1??????license * 2?licenses?? * 3????licenses * 4?licenses?JAVA Security API??????/??licenses?? * 5?license??????java??? * */ 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.startsWith("cpu") || !filename.endsWith(".license")) continue; try { License license = License.fromConfigFile(filename); Date expiresDate = license.getExpiresDate(); if (expiresDate != null && expiresDate.getTime() < System.currentTimeMillis()) { log.error("license \"" + file.getName() + "\" ?."); continue; } if (!validate(license)) { log.error("license \"" + file.getName() + "\" ??."); continue; } licenses.add(license); } catch (Exception e) { log.error(e); } } if (licenses.isEmpty()) { log.error("???license."); } return; } /** * ?licenses */ public void reloadLicenses() { if (licenses != null) licenses.clear(); licenses = null; loadLicenses(); } /** * ????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 (int i = 0; i < licenses.size(); i++) { License license = (License) licenses.get(i); int hasVersion = Integer.parseInt(license.getVersion().substring(0, 1)); if (license.getProduct().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 (int i = 0; i < licenses.size(); i++) { License license = (License) licenses.get(i); int hasVersion = Integer.parseInt(license.getVersion().substring(0, 1)); if (license.getProduct().equals(product) && hasVersion >= needsVersion) return license.getLicenseType().name; } } return null; } /** * ?license?? * ?Mac?????? * ??????? * * @param license * @return * @throws Exception */ boolean validate(License license) throws Exception { String macAddress = license.getMacAddress(); if (macAddress != null && macAddress.length() > 0) { String curMacAddress = MacAddressUtil.getMacAddress(); if (!macAddress.equals(curMacAddress)) return false; } String publicKey = FileHelper.readFile(new File(LicenseFactory.PUBLIC_KEY_FILE)).trim(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance("DSA"); sig.initVerify(pubKey); sig.update(license.getFingerprint()); return sig.verify(EasyUtils.decodeHex(license.getLicenseSignature())); } }