CS結構軟體自動升級實現(四)

來源:互聯網
上載者:User

接CS結構軟體自動升級實現(三) :

Config.java處理設定檔:

 1/** *//********************************************************************
  2 * 項目名稱        :rochoc<p>
  3 * 包名稱         :com.rochoc.autoupdate<p>
  4 * 檔案名稱        :Config.java<p>
  5 * 編寫者         :kfzx-luoc<p>
  6 * 編寫日期        :2008-12-22<p>
  7 * 程式功能(類)描述  :<p>
  8 * 用於更新的設定檔讀取對像
  9 * 程式變更日期      :
10 * 變更作者        :
11 * 變更說明        :
12********************************************************************/
13package com.rochoc.autoupdate;
14
15import java.io.File;
16import java.text.SimpleDateFormat;
17import java.util.Date;
18import java.util.List;
19
20import org.dom4j.Document;
21import org.dom4j.Element;
22import org.dom4j.io.SAXReader;
23
24
25/** *//**
26 * @author kfzx-luoc
27 *
28 * TODO To change the template for this generated type comment go to
29 * Window - Preferences - Java - Code Style - Code Templates
30 */
31public class Config
32{
33  public static String cfgFile = ".\\config\\autoupdate.xml";
34  private static Config config = null;
35  /** *//** xml的document*/
36  private Document doc = null;
37  public static Config getInstance()
38  {
39    if(config==null)
40    {
41      config = new Config();
42    }
43    return config;
44  }
45  
46  private Config()
47  {
48    try
49    {
50      SAXReader reader = new SAXReader();
51      doc = reader.read(cfgFile);
52    }catch(Exception e)
53    {
54      e.printStackTrace();
55    }
56  }
57  public void refresh()
58  {
59    config = new Config();
60  }
61  public String getVerstion()
62  {
63    if(config==null)
64    {
65      return "";
66    }
67    List lst = doc.selectNodes("Info/Version");
68    Element el = (Element)lst.get(0);
69    return el.getText();
70  }
71  public String getServerIp()
72  {
73    if(config==null)
74    {
75      return "";
76    }
77    List lst = doc.selectNodes("Info/UpdateServer/Ip");
78    Element el = (Element)lst.get(0);
79    return el.getText();
80  }
81  public UpdFile [] getFiles()
82  {
83    if(config==null)
84    {
85      return null;
86    }
87    List file = doc.selectNodes("Info/Files");
88    List lst = ((Element)file.get(0)).elements();
89    if(lst.size()==0)
90    {
91      return null;
92    }
93    UpdFile files[] = new UpdFile[lst.size()];
94    for(int i=0;i<lst.size();i++)
95    {
96      Element el = (Element)lst.get(i);
97      List childs = el.elements();
98      Element name = (Element)childs.get(0);//Name
99      Element path = (Element)childs.get(1);//Path
100      Element ver = (Element)childs.get(2);//Version
101      files[i] = new UpdFile(name.getText());
102      if("File".equals(el.getName()))
103      {
104        files[i].setType(0);//檔案
105      }else
106      {
107        files[i].setType(1);//目錄
108      }
109      files[i].setPath(path.getText());
110      files[i].setVersion(ver.getText());
111    }
112    return files;
113  }
114  public String getServerPort()
115  {
116    if(config==null)
117    {
118      return "";
119    }
120    List lst = doc.selectNodes("Info/UpdateServer/Port");
121    Element el = (Element)lst.get(0);
122    return el.getText();
123  }
124  public static void print(String msg)
125  {
126    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss->>" );
127    String str = sdf.format( new Date());
128    System.out.println(str+msg);
129  }
130  public static void main(String args[])
131  {
132    Config cfg = Config.getInstance();
133    UpdFile files[] = cfg.getFiles();
134    for(int i=0;i<files.length;i++)
135    {
136      System.out.println(files[i]);
137    }
138    Config.print("test");
139  }
140  /** *//**
141   * 格式化路徑,增加其尾部的目錄分隔字元
142   *
143   * @param p 要格式化的目錄字元串
144   */
145  public static String formatPath(String p)
146  {
147    if (!p.endsWith(File.separator))
148      return (p + File.separator);
149    return p;
150  }
151
152  /** *//**
153   * 格式化路徑,去除其尾部的目錄分隔字元
154   *
155   * @param p 要格式化的目錄字元串
156   */
157  public static String unformatPath(String p)
158  {
159    if (p.endsWith(File.separator))
160      return (p.substring(0, p.length()-1));
161    return p;
162  }
163  public static byte[] getCmd(String cmd)
164  {
165    //第一位用於標識是命令,後面8位為命令名
166    byte cmdb [] = new byte[9];
167    cmdb[0] = AUPD.CMD_DATA_SECT;
168    byte [] tmp = cmd.getBytes();
169    if(tmp.length!=8)
170    {
171      Config.print("命令有誤:"+cmd+"<<");
172      return null;
173    }
174    for(int i=0;i<8;i++)
175    {
176      cmdb[i+1] = tmp[i];
177    }
178    return cmdb;
179  }
180  public static String parseCmd(byte cmd[])
181  {
182    return new String(cmd,0,8);
183  }
184  public static byte [] getLen(int len)
185  {
186    String slen = String.valueOf(len);
187    while(slen.length()<4)
188    {
189      slen = "0"+slen;
190    }
191    return slen.getBytes();
192  }
193  public static void copyArray(byte objary[], byte souary[], int o_begin,
194      int s_begin, int len)
195  {
196    for (int i = 0; i < len; i++)
197    {
198      objary[o_begin + i] = souary[s_begin + i];
199    }
200  }
201}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.