1 package Maintenance;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.net.URI;
13 import java.util.Hashtable;
14 import java.util.Iterator;
15 import java.util.Set;
16
17
18
19 @author
20
21 public abstract class Updater {
22 private static int revision = 21;
23 private static URI address;
24 private static String logAddress = System.getProperty("user.dir");
25 private static String userDir = System.getProperty("user.dir");
26 private static Hashtable<String, String> actionsList;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public static boolean checkForUpdates(){
47 try{
48 address = new URI("D:/Kojot/");
49 File addr = new File(address + "updates.kojot");
50 FileReader fr = new FileReader(addr);
51 BufferedReader br = new BufferedReader(fr);
52 int rev = Integer.parseInt(br.readLine());
53
54 if (rev > revision){
55 br.close();
56 fr.close();
57 return true;
58 }
59 }
60 catch(FileNotFoundException e){
61 new ExceptionHandler(e, logAddress);
62 return false;
63 }
64 catch(Exception e){
65 new ExceptionHandler(e, logAddress);
66 return false;
67 }
68 return false;
69 }
70 public static void update(){
71 if(Updater.checkForUpdates()){
72 actionsList = new Hashtable();
73 FileReader fr = null;
74 try {
75 File updateList = new File(address + "updates.kojot");
76 fr = new FileReader(updateList);
77 BufferedReader br = new BufferedReader(fr);
78
79 while (br.ready()){
80 String line = br.readLine();
81
82
83
84 if(parseLine(line)<=revision && parseLine(line)!=-1) break;
85 }
86 Set keys = actionsList.keySet();
87 Iterator it = keys.iterator();
88 while(it.hasNext()){
89 String addr = (String)it.next();
90 String action = (String)actionsList.get(addr);
91 if(action.equals("+")) copyFile(addr);
92 if(action.equals("-")) deleteFile(addr);
93 }
94
95
96 } catch (FileNotFoundException ex) {
97 new ExceptionHandler(ex, logAddress);
98 }
99 catch (Exception e){
100 new ExceptionHandler(e, logAddress);
101 }finally {
102 try {
103 fr.close();
104 } catch (IOException ex) {
105 new ExceptionHandler(ex, logAddress);
106 }
107 }
108
109 }
110 }
111
112
113 @param
114 @return
115
116 private static int parseLine(String s){
117 char action = s.charAt(0);
118 switch(action){
119 case '+':
120 if(!actionsList.containsKey(s.substring(1))) actionsList.put(s.substring(1), "+");
121 return -1;
122 case '-':
123 if(!actionsList.containsKey(s.substring(1))) actionsList.put(s.substring(1), "-");
124 return -1;
125 default:
126 return Integer.parseInt(s);
127 }
128 }
129 public static boolean copyFile(String fileAddress){
130 try {
131 File destinationFile = new File(userDir + "/" + fileAddress);
132 File sourceFile = new File(address + fileAddress);
133 if(!destinationFile.getParentFile().exists())destinationFile.getParentFile().mkdirs();
134 InputStream in = new FileInputStream(sourceFile);
135 OutputStream out = new FileOutputStream(destinationFile, false);
136 byte[] buf = new byte[1024];
137 int len;
138 while ((len = in.read(buf)) > 0){
139 out.write(buf, 0, len);
140 }
141 in.close();
142 out.close();
143 return true;
144 } catch (FileNotFoundException e) {
145 new ExceptionHandler(e, logAddress);
146 }catch (Exception e) {
147 new ExceptionHandler(e, logAddress);
148 }
149 return false;
150 }
151 public static boolean deleteFile(String fileAddress){
152 File removeFile = new File(userDir + "/" + fileAddress);
153 return removeFile.delete();
154 }
155 public static void main(String[] args){
156 Updater.checkForUpdates();
157 Updater.update();
158 }
159 }
160
161