| 1 | package gnu.io.installer; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.util.Enumeration; |
| 5 | import java.util.prefs.*; |
| 6 | |
| 7 | import gnu.io.installer.util.*; |
| 8 | |
| 9 | |
| 10 | public abstract class RXTXInstaller { |
| 11 | |
| 12 | static Preferences sysPref = Preferences.systemRoot(); |
| 13 | Preferences checkPref = null; |
| 14 | protected File jarFolder,libFolder; |
| 15 | InstallInstruction inst = new InstallInstruction(); |
| 16 | |
| 17 | public abstract void runPreProcess(); |
| 18 | public abstract void runPostProcess(); |
| 19 | |
| 20 | |
| 21 | public void install(){ |
| 22 | initInstallation(RXTXInstaller.class); |
| 23 | runPreProcess(); |
| 24 | if(inst != null){ |
| 25 | for(Enumeration e = inst.getKeys();e.hasMoreElements();){ |
| 26 | File f = (File)e.nextElement(); |
| 27 | InstallUtil.installPart(sysPref,checkPref,inst.getPath(f),f); |
| 28 | } |
| 29 | } |
| 30 | runPostProcess(); |
| 31 | finishInstallation(); |
| 32 | } |
| 33 | |
| 34 | protected void finishInstallation(){ |
| 35 | if(checkPref == null) return; |
| 36 | try{ |
| 37 | checkPref.removeNode(); |
| 38 | }catch(Throwable t){} |
| 39 | } |
| 40 | |
| 41 | public InstallInstruction getInstallInstruction(){ |
| 42 | return inst; |
| 43 | } |
| 44 | |
| 45 | public static RXTXInstaller getInstance() { |
| 46 | String osName = System.getProperty("os.name").toLowerCase(); |
| 47 | if(osName.startsWith("mac")){ |
| 48 | return new gnu.io.installer.macosx.MACOSXRXTXInstaller(); |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | protected void initInstallation(Class rootClass){ |
| 54 | checkPref = null; |
| 55 | if(rootClass == null) return; |
| 56 | try{ |
| 57 | Preferences.importPreferences(rootClass.getResourceAsStream("installer.pref")); |
| 58 | String checkPrefName = "/check"; |
| 59 | checkPref = (sysPref.nodeExists(checkPrefName))?sysPref.node(checkPrefName):null; |
| 60 | }catch(Throwable t){ |
| 61 | checkPref = null; |
| 62 | System.out.println("Throwable "+t); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public void addLibResource(String path,String resource){ |
| 67 | if(libFolder == null || path == null || resource == null) return; |
| 68 | File libFile = new File(libFolder,path); |
| 69 | addResource(libFile,resource); |
| 70 | } |
| 71 | |
| 72 | public void addJarResource(String path,String resource){ |
| 73 | if(jarFolder == null || path == null || resource == null) return; |
| 74 | File jarFile = new File(jarFolder,path); |
| 75 | addResource(jarFile,resource); |
| 76 | } |
| 77 | |
| 78 | public void addResource(File path,String resource){ |
| 79 | if(inst == null) inst = new InstallInstruction(); |
| 80 | inst.addResource(path,resource); |
| 81 | } |
| 82 | |
| 83 | public static void main(String args[]) { |
| 84 | RXTXInstaller installer = RXTXInstaller.getInstance(); |
| 85 | if(installer != null) installer.install(); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 |