1 11 package org.eclipse.swt.internal; 12 13 import java.io.*; 14 15 public class Library { 16 17 18 19 22 static int MAJOR_VERSION = 3; 23 24 27 static int MINOR_VERSION = 347; 28 29 32 static int REVISION = 0; 33 34 37 public static final int JAVA_VERSION, SWT_VERSION; 38 39 static final String SEPARATOR; 40 41 static { 42 SEPARATOR = System.getProperty("file.separator"); 43 JAVA_VERSION = parseVersion(System.getProperty("java.version")); 44 SWT_VERSION = SWT_VERSION(MAJOR_VERSION, MINOR_VERSION); 45 } 46 47 static int parseVersion(String version) { 48 if (version == null) return 0; 49 int major = 0, minor = 0, micro = 0; 50 int length = version.length(), index = 0, start = 0; 51 while (index < length && Character.isDigit(version.charAt(index))) index++; 52 try { 53 if (start < length) major = Integer.parseInt(version.substring(start, index)); 54 } catch (NumberFormatException e) {} 55 start = ++index; 56 while (index < length && Character.isDigit(version.charAt(index))) index++; 57 try { 58 if (start < length) minor = Integer.parseInt(version.substring(start, index)); 59 } catch (NumberFormatException e) {} 60 start = ++index; 61 while (index < length && Character.isDigit(version.charAt(index))) index++; 62 try { 63 if (start < length) micro = Integer.parseInt(version.substring(start, index)); 64 } catch (NumberFormatException e) {} 65 return JAVA_VERSION(major, minor, micro); 66 } 67 68 76 public static int JAVA_VERSION (int major, int minor, int micro) { 77 return (major << 16) + (minor << 8) + micro; 78 } 79 80 87 public static int SWT_VERSION (int major, int minor) { 88 return major * 1000 + minor; 89 } 90 91 static boolean extract (String fileName, String mappedName) { 92 FileOutputStream os = null; 93 InputStream is = null; 94 File file = new File(fileName); 95 try { 96 if (!file.exists ()) { 97 is = Library.class.getResourceAsStream ("/" + mappedName); if (is != null) { 99 int read; 100 byte [] buffer = new byte [4096]; 101 os = new FileOutputStream (fileName); 102 while ((read = is.read (buffer)) != -1) { 103 os.write(buffer, 0, read); 104 } 105 os.close (); 106 is.close (); 107 if (!Platform.PLATFORM.equals ("win32")) { try { 109 Runtime.getRuntime ().exec (new String []{"chmod", "755", fileName}).waitFor(); } catch (Throwable e) {} 111 } 112 if (load (fileName)) return true; 113 } 114 } 115 } catch (Throwable e) { 116 try { 117 if (os != null) os.close (); 118 } catch (IOException e1) {} 119 try { 120 if (is != null) is.close (); 121 } catch (IOException e1) {} 122 } 123 if (file.exists ()) file.delete (); 124 return false; 125 } 126 127 static boolean load (String libName) { 128 try { 129 if (libName.indexOf (SEPARATOR) != -1) { 130 System.load (libName); 131 } else { 132 System.loadLibrary (libName); 133 } 134 return true; 135 } catch (UnsatisfiedLinkError e) {} 136 return false; 137 } 138 139 150 public static void loadLibrary (String name) { 151 loadLibrary (name, true); 152 } 153 154 166 public static void loadLibrary (String name, boolean mapName) { 167 168 169 String libName1, libName2, mappedName1, mappedName2; 170 if (mapName) { 171 String version = System.getProperty ("swt.version"); if (version == null) { 173 version = "" + MAJOR_VERSION; 175 if (MINOR_VERSION < 10) { 176 version += "00"; } else { 178 if (MINOR_VERSION < 100) version += "0"; } 180 version += MINOR_VERSION; 181 182 if (REVISION > 0) version += "r" + REVISION; } 184 libName1 = name + "-" + Platform.PLATFORM + "-" + version; libName2 = name + "-" + Platform.PLATFORM; mappedName1 = System.mapLibraryName (libName1); 187 mappedName2 = System.mapLibraryName (libName2); 188 } else { 189 libName1 = libName2 = mappedName1 = mappedName2 = name; 190 } 191 192 193 String path = System.getProperty ("swt.library.path"); if (path != null) { 195 path = new File (path).getAbsolutePath (); 196 if (load (path + SEPARATOR + mappedName1)) return; 197 if (mapName && load (path + SEPARATOR + mappedName2)) return; 198 } 199 200 201 if (load (libName1)) return; 202 if (mapName && load (libName2)) return; 203 204 205 if (path == null) { 206 path = System.getProperty ("java.io.tmpdir"); path = new File (path).getAbsolutePath (); 208 if (load (path + SEPARATOR + mappedName1)) return; 209 if (mapName && load (path + SEPARATOR + mappedName2)) return; 210 } 211 212 213 if (path != null) { 214 if (extract (path + SEPARATOR + mappedName1, mappedName1)) return; 215 if (mapName && extract (path + SEPARATOR + mappedName2, mappedName2)) return; 216 } 217 218 219 throw new UnsatisfiedLinkError ("no " + libName1 + " or " + libName2 + " in swt.library.path, java.library.path or the jar file"); } 221 222 } 223 | Popular Tags |