KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > AbstractInstall


1 /*
2  * $RCSfile: AbstractInstall.java,v $
3  * @modification $Date: 2005/05/12 22:13:02 $
4  * @version $Id: AbstractInstall.java,v 1.18 2005/05/12 22:13:02 deniger Exp $
5  *
6  */

7 /**
8  * This is an abstract base class that defines some methods an
9  * installer/uninstaller need to implement. It has some concrete helper methods.
10  *
11  * Both the Setup and the Uninstall class is a kind of installation.
12  *
13  * This first version have some methods copied from the Setup class that should
14  * not be in this class. The goal is to create a more clean class.
15  *
16  * @see com.memoire.vainstall.Setup
17  * @see com.memoire.vainstall.Uninstall
18  *
19  * @author Henrik Falk
20  * @version $Id: AbstractInstall.java,v 1.18 2005/05/12 22:13:02 deniger Exp $
21  */

22 package com.memoire.vainstall;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.LineNumberReader JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 public abstract class AbstractInstall {
34     public static final int CANCEL = 0x01;
35
36     public static final int BACK = 0x02;
37
38     public static final int NEXT = 0x04;
39
40     public static final int FINISH = 0x08;
41
42     public static final boolean IS_WIN = System.getProperty("os.name")
43             .startsWith("Win");
44
45     //MAC_OS X
46
public static final boolean IS_MAC_OS_X = System.getProperty("os.name")
47             .startsWith("Mac OS X");
48
49     public static final boolean IS_MAC = System.getProperty("os.name")
50             .startsWith("Mac")
51             && !IS_MAC_OS_X;
52
53     public static final boolean IS_UNIX = !IS_WIN && !IS_MAC || IS_MAC_OS_X;
54
55     public static final boolean IS_ROOT = (IS_UNIX && "root".equals(System
56             .getProperty("user.name")))
57             || (IS_WIN && new File JavaDoc("C:\\").canWrite());
58
59     protected VAStep step_;
60
61     protected boolean dirty_;
62
63     /**
64      *
65      */

66     protected VAStepFactory ui_;
67
68     /**
69      *
70      */

71     protected VAStats stats_;
72
73     protected static final int LANGUAGE = 0;
74
75     protected static final int START = 1;
76
77     protected static final int WELCOME = 2;
78
79     protected static final int LICENSE = 3;
80
81     protected static final int README = 4;
82
83     protected static final int LICENSE_KEY = 5;
84
85     protected static final int UPGRADE = 6;
86
87     protected static final int DIRECTORY = 7;
88
89     protected static final int INSTALL = 8;
90
91     protected static final int SHORTCUTS = 9;
92
93     protected static final int END = 10;
94
95     protected int state_;
96
97     protected int actionEnabled_;
98
99     protected File JavaDoc sharedDir_;
100
101     protected File JavaDoc classloaderTempDir_;
102
103     protected UpgradeInfo uInfo_;
104
105     /**
106      * The current language that is choosen
107      */

108     protected String JavaDoc language;
109
110     public AbstractInstall() {
111         super();
112         if (IS_WIN) {
113             extractJniDll("/JNIWinShortcut.dll",true);
114             extractJniDll("/ICE_JNIRegistry.dll",false);
115         }
116         actionEnabled_ = 0;
117         stats_ = new VAStats();
118         sharedDir_ = null;
119         uInfo_ = null;
120         dirty_ = false;
121         state_ = START;
122     }
123
124     /**
125      * This method deletes all files in a directory and updates failures to the
126      * 'stats_' variable This method should moved to a support class and thats
127      * why I made it public.
128      *
129      * @author Henrik Falk
130      * @param dir
131      * File Directory to delete
132      * @return void
133      */

134     public void deleteDirRecursive(File JavaDoc dir) {
135         // verify parameter
136
if (dir == null || dir.exists() == false) {
137             return;
138         }
139         // get list of files in directory if any
140
File JavaDoc[] filelist = dir.listFiles();
141         for (int i = 0; i < filelist.length; i++) {
142             boolean isdir = filelist[i].isDirectory();
143             if (isdir == true) {
144                 // delete everything in the directory
145
deleteDirRecursive(filelist[i]);
146             } else {
147                 // delete the file
148
if (filelist[i].delete() == false) {
149                     stats_.addFile(filelist[i], VAStats.FAILURE);
150                     filelist[i].deleteOnExit();
151                     VAGlobals.printDebug("F " + filelist[i] + " not deleted");
152                 } // endif
153
} // endif
154
} // endfor
155
// delete the directory
156
if (dir.delete() == false) {
157             dir.deleteOnExit();
158             stats_.addDirectory(dir, VAStats.FAILURE);
159             VAGlobals.printDebug("D " + dir + " not deleted");
160         } // endif
161
}
162
163     public boolean cleanShortcuts(File JavaDoc shortcutLogDir) throws IOException JavaDoc {
164         boolean value;
165         File JavaDoc logFile;
166         Vector JavaDoc directories;
167         logFile = new File JavaDoc(shortcutLogDir, "shortcuts.vai");
168         value = false;
169         if (logFile.exists()) {
170             value = true;
171             directories = deleteFiles(logFile);
172             deleteDirectories(directories);
173         }
174         return value;
175     }
176
177     protected Vector JavaDoc deleteFiles(File JavaDoc log) throws IOException JavaDoc {
178         Vector JavaDoc dirs = new Vector JavaDoc();
179         LineNumberReader JavaDoc logReader = new LineNumberReader JavaDoc(new FileReader JavaDoc(log));
180         String JavaDoc line = logReader.readLine();
181         while (line != null) {
182             File JavaDoc del = new File JavaDoc(line);
183             if (del.isDirectory()) {
184
185                 dirs.add(del);
186             } else if (del.delete()) {
187                 stats_.addFile(del, VAStats.SUCCESS);
188             } else {
189                 del.deleteOnExit();
190                 stats_.addFile(del, VAStats.FAILURE);
191                 VAGlobals.printDebug("F " + del + " not deleted");
192             }
193             line = logReader.readLine();
194         }
195         logReader.close();
196         return dirs;
197     }
198
199     protected void deleteDirectories(Vector JavaDoc directories) throws IOException JavaDoc {
200         Object JavaDoc[] dirs = directories.toArray();
201         Arrays.sort(dirs);
202         for (int i = dirs.length - 1; i >= 0; i--) {
203             File JavaDoc del = (File JavaDoc) dirs[i];
204             if (del.delete()) {
205                 stats_.addDirectory(del, VAStats.SUCCESS);
206             } else {
207                 del.deleteOnExit();
208                 stats_.addDirectory(del, VAStats.FAILURE);
209                 VAGlobals.printDebug("D " + del + " not deleted");
210             }
211         }
212     }
213
214     private void restoreLastVersion(File JavaDoc sharedDir, UpgradeInfo uInfo) {
215         File JavaDoc destPath = new File JavaDoc(sharedDir, "vai_" + VAGlobals.APP_NAME + "_"
216                 + VAGlobals.APP_VERSION);
217         // eliminate new install info
218
if (destPath.exists())
219             deleteDirRecursive(destPath);
220         if ((uInfo == null) || (!uInfo.upgrade))
221             return;
222         File JavaDoc lastVerPath = new File JavaDoc(sharedDir, "vai_" + VAGlobals.APP_NAME
223                 + "_" + uInfo.lastVersion());
224         // restore old install info if versions are the same
225
if (destPath.equals(lastVerPath)) {
226             lastVerPath = new File JavaDoc(destPath.getAbsolutePath() + ".bak");
227             lastVerPath.renameTo(destPath);
228         }
229     }
230
231     public void setActionEnabled(int a) {
232         actionEnabled_ = a;
233         ui_.setActionEnabled(a);
234     }
235
236     public void cancel() {
237         cleanInstall(sharedDir_, uInfo_);
238         quit();
239     }
240
241     protected void cleanInstall(File JavaDoc sharedDir, UpgradeInfo uInfo) {
242         if ((!dirty_) || (sharedDir == null) || (!sharedDir.exists())) {
243             VAGlobals.printDebug("No cleaning needed");
244             return;
245         }
246         File JavaDoc uninstDir = new File JavaDoc(sharedDir.getAbsolutePath() + File.separator
247                 + "vai_" + VAGlobals.APP_NAME + "_" + VAGlobals.APP_VERSION);
248         if (!uninstDir.exists()) {
249             VAGlobals.printDebug("No cleaning needed");
250             return;
251         }
252         VAGlobals.printDebug("Deleting uninstall files...");
253         if ((uInfo != null) && (uInfo.upgrade)) {
254             restoreLastVersion(sharedDir, uInfo);
255         } else {
256             // delete all in the uninstall directory
257
deleteDirRecursive(uninstDir);
258         }
259         VAGlobals.printDebug("All cleaned OK");
260     }
261
262     protected void checkUpgrade(File JavaDoc sharedDir, UpgradeInfo uInfo) {
263         VAUpgradeStep step = (VAUpgradeStep) step_;
264         step.status(VAGlobals.i18n("Setup_LookPreviousVersions"));
265         ui_.uiSleep(2000);
266         checkVersions(sharedDir, uInfo);
267         if (uInfo.upgrade) {
268             VAGlobals.printDebug("Previous version found : "
269                     + uInfo.lastVersion());
270             step.status(VAGlobals.i18n("Setup_PreviousVersionFound") + " "
271                     + uInfo.lastVersion());
272             step.version(uInfo.lastVersion());
273             step.directory(uInfo.lastPath().getAbsolutePath());
274         } else {
275             VAGlobals.printDebug("No previous version found");
276             step.status(VAGlobals.i18n("Setup_NoPreviousVersionFound"));
277             step.version(VAGlobals.i18n("Setup_None"));
278             step.directory(VAGlobals.i18n("Setup_None"));
279         }
280     }
281
282     private void checkVersions(File JavaDoc sharedDir, UpgradeInfo uInfo) {
283         String JavaDoc[] ls = sharedDir.list(new SetupFileFilter("vai_"
284                 + VAGlobals.APP_NAME + "_", SetupFileFilter.STARTS_WITH,
285                 SetupFileFilter.FILTER));
286         if ((ls != null) && (ls.length > 0)) {
287             Arrays.sort(ls);
288             uInfo.versions = new String JavaDoc[ls.length];
289             uInfo.paths = new File JavaDoc[ls.length];
290             for (int i = 0; i < ls.length; i++) {
291                 uInfo.versions[i] = ls[i].substring(ls[i].lastIndexOf('_') + 1);
292                 try {
293                     LineNumberReader JavaDoc log = new LineNumberReader JavaDoc(new FileReader JavaDoc(
294                             new File JavaDoc(new File JavaDoc(sharedDir, ls[i]),
295                                     "uninstall.vai")));
296                     String JavaDoc f = log.readLine();
297                     if (f != null)
298                         uInfo.paths[i] = new File JavaDoc(f);
299                     log.close();
300                 } catch (IOException JavaDoc ex) {
301                     System.err.println(ex.getMessage());
302                     uInfo.paths[i] = null;
303                 }
304             }
305         }
306         uInfo.upgrade = (uInfo.lastVersion() != null)
307                 && (uInfo.lastPath() != null);
308     }
309
310     /**
311      * @param entryName
312      * the name of the dll file
313      * @param isWinShortcut
314      * true if it's the dll for the JNIWindowsShortcut
315      */

316     private void extractJniDll(String JavaDoc entryName, boolean isWinShortcut) {
317         try {
318             InputStream JavaDoc dll = getClass().getResourceAsStream(entryName);
319             File JavaDoc dllFile = new File JavaDoc(System.getProperty("java.io.tmpdir")
320                     + File.separator
321                     + entryName.substring(entryName.lastIndexOf('/') + 1));
322             VAGlobals.printDebug("Extracting library " + dllFile);
323             System.out.println(dllFile.getAbsolutePath());
324             FileOutputStream JavaDoc dllout = new FileOutputStream JavaDoc(dllFile);
325             byte[] data = new byte[512];
326             int read = dll.read(data, 0, data.length);
327             while (read > 0) {
328                 dllout.write(data, 0, read);
329                 read = dll.read(data, 0, data.length);
330             }
331             dll.close();
332             dllout.close();
333             VAGlobals.printDebug(" library extracted");
334             if (isWinShortcut) {
335                 JNIWindowsShortcut.LIB_NAME = dllFile.getAbsolutePath();;
336             } else
337                 VAGlobals.JNI_DLL_FILE = dllFile.getAbsolutePath();
338             dllFile.deleteOnExit();
339         } catch (IOException JavaDoc e) {
340             exitOnError(e);
341         }
342     }
343
344     protected void exitOnError(Throwable JavaDoc e) {
345         if (ui_ != null)
346             ui_.showFatalError(e);
347         else
348             e.printStackTrace();
349         deleteTmpFile();
350         System.exit(1);
351     }
352     
353     private void deleteTmpFile(){
354         if (classloaderTempDir_ != null) {
355             VAGlobals.printDebug("Temporary directory deleted: "
356                     + classloaderTempDir_.getAbsolutePath());
357             deleteDirRecursive(classloaderTempDir_);
358         }
359     }
360
361     protected void quit() {
362         // delete classloader
363
//File classloaderDir = new
364
// File(System.getProperty("user.dir")+File.separator+"com");
365
deleteTmpFile();
366         ui_ = null;
367         VAGlobals.printDebug("Exiting");
368         System.exit(0);
369     }
370
371     /**
372      * The next step in an install/uninstall
373      *
374      * @return void
375      */

376     public abstract void nextStep();
377
378     /**
379      * A previous step in an install/uninstall
380      *
381      * @return void
382      */

383     public abstract void previousStep();
384
385     /**
386      * A redo step in an install/uninstall This is conceptually an 'again' step.
387      * Only 'tui' uses the redo feature at the moment.
388      *
389      * @return void
390      */

391     public abstract void redoStep();
392 }
Popular Tags