| 1 |
package gnu.io.installer.util; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.io.FileOutputStream; |
| 6 |
import java.util.prefs.*; |
| 7 |
|
| 8 |
public class InstallUtil{ |
| 9 |
|
| 10 |
public static String getSharedFolderPath(){ |
| 11 |
return getFolderPath(kSharedUserDataFolder); |
| 12 |
} |
| 13 |
public static String getApplicationFolderPath(){ |
| 14 |
return getFolderPath(kApplicationFolder); |
| 15 |
} |
| 16 |
public static String getGlobalApplicationFolderPath(){ |
| 17 |
return getFolderPath(kSystemDomain,kApplicationFolder); |
| 18 |
} |
| 19 |
public static String getFolderPath(int folderKind){ |
| 20 |
try{ |
| 21 |
Class clazz = Class.forName("com.apple.eio.FileManager"); |
| 22 |
java.lang.reflect.Method m = clazz.getMethod("findFolder",new Class[]{int.class}); |
| 23 |
return (String)m.invoke(null,new Object []{new Integer(folderKind)}); |
| 24 |
}catch(Throwable t){} |
| 25 |
return null; |
| 26 |
} |
| 27 |
public static String getFolderPath(short domain,int folderKind){ |
| 28 |
try{ |
| 29 |
Class clazz = Class.forName("com.apple.eio.FileManager"); |
| 30 |
java.lang.reflect.Method m = clazz.getMethod("findFolder",new Class[]{short.class,int.class}); |
| 31 |
return (String)m.invoke(null,new Object []{new Short(domain),new Integer(folderKind)}); |
| 32 |
}catch(Throwable t){ |
| 33 |
System.out.println("getFolderPath Throwable "+t); |
| 34 |
} |
| 35 |
return null; |
| 36 |
} |
| 37 |
public static void copyResourceToFile(String absResPath,java.io.File file){ |
| 38 |
try{ |
| 39 |
InputStream is = InstallUtil.class.getResourceAsStream(absResPath); |
| 40 |
byte []buffer = new byte[4096]; |
| 41 |
int rb = 0; |
| 42 |
if(is != null){ |
| 43 |
FileOutputStream fos = new FileOutputStream(file); |
| 44 |
while((rb = is.read(buffer,0,buffer.length)) > 0){ |
| 45 |
fos.write(buffer,0,rb); |
| 46 |
} |
| 47 |
fos.close(); |
| 48 |
} |
| 49 |
}catch(Throwable t){ |
| 50 |
t.printStackTrace(); |
| 51 |
} |
| 52 |
} |
| 53 |
public static void installPart(Preferences sysPref,Preferences checkPref,String resPathName,File fileToInstall){ |
| 54 |
if(fileToInstall == null || resPathName == null) return; |
| 55 |
boolean needInstall = (checkPref == null); |
| 56 |
char separator = File.separatorChar; |
| 57 |
long prevLength = 0; |
| 58 |
File fooFile = new File(resPathName); |
| 59 |
String resName = fooFile.getName(); |
| 60 |
String resParentName = fooFile.getParent(); |
| 61 |
if(separator != '/') resParentName = resParentName.replace(separator,'/'); |
| 62 |
Preferences installPref = sysPref.node(resParentName); |
| 63 |
if(!needInstall){ |
| 64 |
try{ |
| 65 |
prevLength = fileToInstall.length(); |
| 66 |
}catch(Throwable t){} |
| 67 |
} |
| 68 |
needInstall = (prevLength == 0); |
| 69 |
if(!needInstall){ |
| 70 |
String checkResParentName = resParentName.substring(1); |
| 71 |
Preferences checkInstallPref = null; |
| 72 |
try{ |
| 73 |
checkInstallPref = (checkPref.nodeExists(checkResParentName))?checkPref.node(checkResParentName):null; |
| 74 |
}catch(Throwable t){ |
| 75 |
checkInstallPref = null; |
| 76 |
} |
| 77 |
long installValueLength = installPref.getLong(resName,0); |
| 78 |
long checkValueLength = (checkInstallPref == null)?0:checkInstallPref.getLong(resName,0); |
| 79 |
needInstall = (checkValueLength != 0) && |
| 80 |
((installValueLength != prevLength) || (checkValueLength != prevLength) || (checkValueLength != installValueLength)); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
if(!needInstall){ |
| 85 |
System.out.println("File "+fileToInstall+" is uptodate"); |
| 86 |
}else{ |
| 87 |
System.out.println("copy file "+fileToInstall); |
| 88 |
InstallUtil.copyResourceToFile(resPathName,fileToInstall); |
| 89 |
try{ |
| 90 |
long newLength = fileToInstall.length(); |
| 91 |
installPref.putLong(resName,newLength); |
| 92 |
installPref.flush(); |
| 93 |
}catch(Throwable t){} |
| 94 |
} |
| 95 |
} |
| 96 |
public static final short kSystemDomain = -32766; |
| 97 |
public static final short kLocalDomain = -32765; |
| 98 |
public static final short kNetworkDomain = -32764; |
| 99 |
public static final short kUserDomain = -32763; |
| 100 |
public static final short kClassicDomain = -32762; |
| 101 |
|
| 102 |
public static final int kPreferenceFolder = 0x70726566; |
| 103 |
public static final int kApplicationSupportFolder = 0x61737570; |
| 104 |
public static final int kSharedUserDataFolder = 0x73646174; |
| 105 |
public static final int kApplicationFolder = 0x61707073; |
| 106 |
public static final int kExtensionFolder = 0x6578746E; |
| 107 |
public static final int kDomainLibraryFolder = 0x646C6962; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|