/**
 * 注册表网注册表写授权信息
 */
package com.encryption;
import java.io.*;
import java.util.prefs.*;
import java.util.Calendar;
import java.util.Date;
public class PrefsProduct {

    public String getLocalPrefsByNodeAndKey(String node, String key){
        /* 建立一个位于user root下的/com/sunway/spc节点参数项*/
        Preferences prefsdemo = Preferences.userRoot().node(node);
        String values = "";
        try
        {
            values = prefsdemo.get(key, "");
        } catch (Exception e)
        {
            System.err.println("Cannot export nodes: " + e);
            try {
                throw new Exception("SQL: \"" + "\" 执行出错，原因是：" + e.getMessage());
            }catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception,ioexception);
                throw illegalargumentexception;
            }
        }
        return values;
    }

    public void putLocalPrefsByNodeAndKey(String node, String keys[],String values[]){
        //String keys[] =
        //	{"sunway","copyright","author"};
        //String values[] =
        //	{"sunway technology company","copyright 2002","turbochen@163.com"} ;

        /* 建立一个位于user root下的/com/sunway/spc节点参数项*/
        //Preferences prefsdemo = Preferences.userRoot().node("/com/sunway/spc");
        Preferences prefsdemo = Preferences.userRoot().node(node);
        /* 储存参数项*/
        for (int i=0 ; i < keys.length; i++)
        {
            prefsdemo.put(keys[i], values[i]);
            System.out.println("keys[i] =" + keys[i] + ", values[i] =" + values[i]);
        }

        /* 导出到XML文件 */
        try
        {
            FileOutputStream fos = new FileOutputStream("prefsdemo.xml");
            prefsdemo.exportNode(fos);
        } catch (Exception e)
        {
            System.err.println("Cannot export nodes: " + e);
        }

    }
    public boolean AuthorizationIs(String node, String key){
        boolean authorizationIs = true;
        try
        {
            LocalInfo localinfo = new LocalInfo();
            PrefsProduct prefsproduct = new PrefsProduct();
            System.out.println("prefsproduct = "+ prefsproduct.getLocalPrefsByNodeAndKey(node,key) + ",node = " + node +",key = " + key);
            System.err.println("processoridbyenc: " + prefsproduct.getLocalPrefsByNodeAndKey(node,key));
            String idstr = localinfo.getLocalProcessorIdByEnc(prefsproduct.getLocalPrefsByNodeAndKey(node,key));
            idstr = (idstr == null)?"":idstr.trim();
            if(idstr.length() == 0) {
                return false;
            }
            String processoridbyenc = com.coolservlets.beans.method.genMethod.readValueStr(idstr, "@@", 0);
            processoridbyenc = (processoridbyenc == null)?"":processoridbyenc.trim();
            //System.err.println("processoridbyenc: " + processoridbyenc);
            System.out.println("processoridbyenc = "+ processoridbyenc);
            if(processoridbyenc.length() == 0) {
                System.err.println("authorizationIs: processoridbyenc.length() =" + authorizationIs);
                return false;
            }
            String processoridby = localinfo.getLocalProcessorId();
            System.err.println("processoridby =" + processoridby);
            System.out.println("processoridby = "+ processoridby);
            if(!processoridby.equalsIgnoreCase(processoridbyenc)){
                System.err.println("!processoridby.equalsIgnoreCase(processoridbyenc) ");
                return false;
            }
            String validity = com.coolservlets.beans.method.genMethod.readValueStr(idstr, "@@", 1);
            validity = (validity == null)?"":validity.trim();
            System.out.println("validity = "+ validity);
            if(validity.length() == 0) {
                System.err.println("validity.length() == 0");
                return false;
            }
            validity = com.coolservlets.beans.method.genMethod.readValueStr(validity, " ", 0);
            System.out.println("validity 1= "+ validity);
            Calendar c = Calendar.getInstance();
            int iYear = Integer.parseInt(com.coolservlets.beans.method.genMethod.readValueStr(validity, "-", 0));
            int iMonth = Integer.parseInt(com.coolservlets.beans.method.genMethod.readValueStr(validity, "-", 1));
            int iDay = Integer.parseInt(com.coolservlets.beans.method.genMethod.readValueStr(validity, "-", 2));
            c.set(iYear,iMonth,iDay);
            System.out.println("iYear = "+iYear + ",iMonth" + iMonth + ",iDay=" +iDay);
            Date before =c.getTime();
            Date date = new Date();
            //String sDate = Integer.toString(date.getYear() + 1900) + "-" + Integer.toString(date.getMonth() + 1) + "-" + Integer.toString(date.getDate());
            Calendar c1 = Calendar.getInstance();
            c1.set(date.getYear() + 1900,date.getMonth() + 1,date.getDate());
            Date now=c1.getTime();
            int compareToBefore=before.compareTo(now);
            //System.out.println("compareToBefore = "+compareToBefore);
            if(compareToBefore < 0){
                System.err.println("compareToBefore < 0");
                return false;
            }
            //System.out.println("compareToBefore = "+compareToBefore);
            //System.err.println("validity: " + validity);
        } catch (Exception e)
        {
            System.err.println("Cannot export nodes: " + e);
            try {
                throw new Exception("SQL: \"" + "\" 执行出错，原因是：" + e.getMessage());
            }catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception,ioexception);
                throw illegalargumentexception;
            }
        }
        //System.err.println("authorizationIs: " + authorizationIs);
        //return authorizationIs;
        return true;
    }
    public static void main(String args[])
    {
        /* 导出到XML文件 */
        try
        {

            //PrefsProduct prefsproduct = new PrefsProduct();
            //boolean authis = prefsproduct.AuthorizationIs("/com/sunway/spc","mg{u");
            //System.err.println("prefsproduct: " + authis);
        } catch (Exception e)
        {
            System.err.println("Cannot export nodes: " + e);
            try {
                throw new Exception("SQL: \"" + "\" 执行出错，原因是：" + e.getMessage());
            }catch (Exception ioexception) {
                IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
                Fake(illegalargumentexception,ioexception);
                throw illegalargumentexception;
            }
        }

        /* 去掉注释可以清除注册表中的参数项*/
		/*    try
      {
        prefsdemo.removeNode();
      } catch (BackingStoreException e)
      {
      } */
    }
    public static Throwable Fake(Throwable throwable, Throwable throwable1) {
        try {
            throwable.getClass().getMethod("initCause", new Class[] {
                    java.lang.Throwable.class
            }).invoke(throwable, new Object[] {
                    throwable1
            });
        }
        catch (Exception ioexception) {
            IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
            Fake(illegalargumentexception,ioexception);
            throw illegalargumentexception;
        }
        return throwable;
    }
}