Tools.java Source code

Java tutorial

Introduction

Here is the source code for Tools.java

Source

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import sun.misc.BASE64Decoder;

import com.justep.filesystem.FileSystemWrapper;
import com.justep.license.EncryptModel;

public class Tools {

    private static byte[] loadCert(Document cert) {
        if (null == cert)
            throw new RuntimeException("??");
        Element root = cert.getRootElement();
        if (null == root)
            throw new RuntimeException("???");
        String content = root.elementTextTrim("content");
        if (null == content)
            throw new RuntimeException("???");
        return content.getBytes();
    }

    public static void encryptApp(Object cert, String password, String sourceModelPath, String targetModelPath,
            String app, String isCompile) {
        if (null == password || "".equals(password))
            throw new RuntimeException("???");
        if (null == sourceModelPath || "".equals(sourceModelPath))
            throw new RuntimeException("");
        if (null == targetModelPath || "".equals(targetModelPath))
            throw new RuntimeException("?");
        if (null == app || "".equals(app))
            throw new RuntimeException("??");
        if (sourceModelPath.equalsIgnoreCase(targetModelPath))
            throw new RuntimeException(
                    "?????");
        try {
            password = java.net.URLDecoder.decode(password, "UTF8");
        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException("???", e1);
        }
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read((InputStream) cert);
        } catch (DocumentException e) {
            throw new RuntimeException("???", e);
        }
        byte[] bCert = loadCert(doc);
        EncryptModel eapp = new EncryptModel(bCert, sourceModelPath, targetModelPath);
        try {
            String[] appList = app.split(";");
            for (int i = 0; i < appList.length; i++)
                eapp.encrypt(appList[i], password, !"false".equalsIgnoreCase(isCompile));
        } catch (IOException e) {
            throw new RuntimeException("", e);
        }
    }

    public static Map<String, String> regAppLicense(String appLicense) {
        if (null == appLicense || "".equals(appLicense))
            throw new RuntimeException("License");
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            Map<String, String> ret = new HashMap<String, String>();
            SAXReader reader = new SAXReader();
            reader.setEncoding("UTF-8");
            Document doc = reader.read(new ByteArrayInputStream(decoder.decodeBuffer(appLicense)));
            Element root = doc.getRootElement();
            String app = root.elementTextTrim("models");
            ret.put("app", app);
            String content = root.elementTextTrim("content");
            String appPath = FileSystemWrapper.instance().getRealPath(app);
            File fApp = new File(appPath);
            if (!fApp.exists())
                throw new RuntimeException("?\"" + app + "\"?");
            File fAppLicenseDir = new File(appPath + "/license");
            fAppLicenseDir.mkdirs();
            FileOutputStream fo = null;
            try {
                fo = new FileOutputStream(appPath + "/license/license");
                fo.write(content.getBytes());
                fo.flush();
            } finally {
                if (null != fo)
                    fo.close();
            }
            String developer = root.elementTextTrim("developer");
            ret.put("developer", developer);
            String validDate = root.elementTextTrim("valid-date");
            ret.put("valid-date", !"0".equals(validDate) ? validDate : "??");
            String userCount = root.elementTextTrim("user-count");
            ret.put("user-count", !"0".equals(userCount) ? userCount : "??");

            return ret;
        } catch (IOException e) {
            throw new RuntimeException("License", e);
        } catch (DocumentException e) {
            throw new RuntimeException("License", e);
        }
    }

}