Updater.java
  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  * Class responsible for automatic updates of application.
 19  * @author Qbix
 20  */
 21 public abstract class Updater {
 22     private static int revision = 21;   //current version (for me it's revision number, but you can automatize it by build no.
 23     private static URI address;         //address, where you have latest version
 24     private static String logAddress = System.getProperty("user.dir");  //address, where do you keep appliaction log, needed for my exception handler
 25     private static String userDir = System.getProperty("user.dir");     //determine, where you have to copy files
 26     private static Hashtable<String, String> actionsList;               //list of determined actions to update
 27 
 28     /*
 29      *Specification of update file
 30      *      version number (must be integer)
 31      *      modifications list
 32      *example:
 33      *      22
 34      *      +filename.fv
 35      *      +File.txt
 36      *      -ff.sst
 37      *      21
 38      *      +filen.sf
 39      *
 40      *  Higher numbers always at the top
 41      *  Before filename you always have a sign:
 42      *  "+" - file is added or modified in this version
 43      *  "-" - file is removed in this version
 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             //if latest version is higher than current revision, there is update available
 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");//updates.kojot is file containing modifiaction data
 76                 fr = new FileReader(updateList);
 77                 BufferedReader br = new BufferedReader(fr);
 78                 //while there are more lines in file
 79                 while (br.ready()){
 80                     String line = br.readLine();
 81                     /* if currently checked version is current revision, or older, break checking to avoid
 82                      * not necessary operations.
 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      * Method parsing line of file
113      * @param s String to parse
114      * @return -1, if it was position to modify, else the number of starting revision
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);   //address to copy updated files
132             File sourceFile = new File(address + fileAddress);              //source of updated files
133             if(!destinationFile.getParentFile().exists())destinationFile.getParentFile().mkdirs();//make dirs if needed
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