001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal;
011:
012: import java.io.*;
013:
014: public class Library {
015:
016: /* SWT Version - Mmmm (M=major, mmm=minor) */
017:
018: /**
019: * SWT Major version number (must be >= 0)
020: */
021: static int MAJOR_VERSION = 3;
022:
023: /**
024: * SWT Minor version number (must be in the range 0..999)
025: */
026: static int MINOR_VERSION = 409;
027:
028: /**
029: * SWT revision number (must be >= 0)
030: */
031: static int REVISION = 0;
032:
033: /**
034: * The JAVA and SWT versions
035: */
036: public static final int JAVA_VERSION, SWT_VERSION;
037:
038: static final String SEPARATOR;
039:
040: static {
041: SEPARATOR = System.getProperty("file.separator");
042: JAVA_VERSION = parseVersion(System.getProperty("java.version"));
043: SWT_VERSION = SWT_VERSION(MAJOR_VERSION, MINOR_VERSION);
044: }
045:
046: static int parseVersion(String version) {
047: if (version == null)
048: return 0;
049: int major = 0, minor = 0, micro = 0;
050: int length = version.length(), index = 0, start = 0;
051: while (index < length
052: && Character.isDigit(version.charAt(index)))
053: index++;
054: try {
055: if (start < length)
056: major = Integer.parseInt(version
057: .substring(start, index));
058: } catch (NumberFormatException e) {
059: }
060: start = ++index;
061: while (index < length
062: && Character.isDigit(version.charAt(index)))
063: index++;
064: try {
065: if (start < length)
066: minor = Integer.parseInt(version
067: .substring(start, index));
068: } catch (NumberFormatException e) {
069: }
070: start = ++index;
071: while (index < length
072: && Character.isDigit(version.charAt(index)))
073: index++;
074: try {
075: if (start < length)
076: micro = Integer.parseInt(version
077: .substring(start, index));
078: } catch (NumberFormatException e) {
079: }
080: return JAVA_VERSION(major, minor, micro);
081: }
082:
083: /**
084: * Returns the Java version number as an integer.
085: *
086: * @param major
087: * @param minor
088: * @param micro
089: * @return the version
090: */
091: public static int JAVA_VERSION(int major, int minor, int micro) {
092: return (major << 16) + (minor << 8) + micro;
093: }
094:
095: /**
096: * Returns the SWT version number as an integer.
097: *
098: * @param major
099: * @param minor
100: * @return the version
101: */
102: public static int SWT_VERSION(int major, int minor) {
103: return major * 1000 + minor;
104: }
105:
106: static boolean extract(String fileName, String mappedName) {
107: FileOutputStream os = null;
108: InputStream is = null;
109: File file = new File(fileName);
110: try {
111: if (!file.exists()) {
112: is = Library.class
113: .getResourceAsStream("/" + mappedName); //$NON-NLS-1$
114: if (is != null) {
115: int read;
116: byte[] buffer = new byte[4096];
117: os = new FileOutputStream(fileName);
118: while ((read = is.read(buffer)) != -1) {
119: os.write(buffer, 0, read);
120: }
121: os.close();
122: is.close();
123: if (!Platform.PLATFORM.equals("win32")) { //$NON-NLS-1$
124: try {
125: Runtime
126: .getRuntime()
127: .exec(
128: new String[] {
129: "chmod", "755", fileName }).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
130: } catch (Throwable e) {
131: }
132: }
133: if (load(fileName))
134: return true;
135: }
136: }
137: } catch (Throwable e) {
138: try {
139: if (os != null)
140: os.close();
141: } catch (IOException e1) {
142: }
143: try {
144: if (is != null)
145: is.close();
146: } catch (IOException e1) {
147: }
148: }
149: if (file.exists())
150: file.delete();
151: return false;
152: }
153:
154: static boolean load(String libName) {
155: try {
156: if (libName.indexOf(SEPARATOR) != -1) {
157: System.load(libName);
158: } else {
159: System.loadLibrary(libName);
160: }
161: return true;
162: } catch (UnsatisfiedLinkError e) {
163: }
164: return false;
165: }
166:
167: /**
168: * Loads the shared library that matches the version of the
169: * Java code which is currently running. SWT shared libraries
170: * follow an encoding scheme where the major, minor and revision
171: * numbers are embedded in the library name and this along with
172: * <code>name</code> is used to load the library. If this fails,
173: * <code>name</code> is used in another attempt to load the library,
174: * this time ignoring the SWT version encoding scheme.
175: *
176: * @param name the name of the library to load
177: */
178: public static void loadLibrary(String name) {
179: loadLibrary(name, true);
180: }
181:
182: /**
183: * Loads the shared library that matches the version of the
184: * Java code which is currently running. SWT shared libraries
185: * follow an encoding scheme where the major, minor and revision
186: * numbers are embedded in the library name and this along with
187: * <code>name</code> is used to load the library. If this fails,
188: * <code>name</code> is used in another attempt to load the library,
189: * this time ignoring the SWT version encoding scheme.
190: *
191: * @param name the name of the library to load
192: * @param mapName true if the name should be mapped, false otherwise
193: */
194: public static void loadLibrary(String name, boolean mapName) {
195:
196: /* Compute the library name and mapped name */
197: String libName1, libName2, mappedName1, mappedName2;
198: if (mapName) {
199: String version = System.getProperty("swt.version"); //$NON-NLS-1$
200: if (version == null) {
201: version = "" + MAJOR_VERSION; //$NON-NLS-1$
202: /* Force 3 digits in minor version number */
203: if (MINOR_VERSION < 10) {
204: version += "00"; //$NON-NLS-1$
205: } else {
206: if (MINOR_VERSION < 100)
207: version += "0"; //$NON-NLS-1$
208: }
209: version += MINOR_VERSION;
210: /* No "r" until first revision */
211: if (REVISION > 0)
212: version += "r" + REVISION; //$NON-NLS-1$
213: }
214: libName1 = name + "-" + Platform.PLATFORM + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$
215: libName2 = name + "-" + Platform.PLATFORM; //$NON-NLS-1$
216: mappedName1 = System.mapLibraryName(libName1);
217: mappedName2 = System.mapLibraryName(libName2);
218: } else {
219: libName1 = libName2 = mappedName1 = mappedName2 = name;
220: }
221:
222: /* Try loading library from swt library path */
223: String path = System.getProperty("swt.library.path"); //$NON-NLS-1$
224: if (path != null) {
225: path = new File(path).getAbsolutePath();
226: if (load(path + SEPARATOR + mappedName1))
227: return;
228: if (mapName && load(path + SEPARATOR + mappedName2))
229: return;
230: }
231:
232: /* Try loading library from java library path */
233: if (load(libName1))
234: return;
235: if (mapName && load(libName2))
236: return;
237:
238: /* Try loading library from the tmp directory if swt library path is not specified */
239: if (path == null) {
240: path = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
241: path = new File(path).getAbsolutePath();
242: if (load(path + SEPARATOR + mappedName1))
243: return;
244: if (mapName && load(path + SEPARATOR + mappedName2))
245: return;
246: }
247:
248: /* Try extracting and loading library from jar */
249: if (path != null) {
250: if (extract(path + SEPARATOR + mappedName1, mappedName1))
251: return;
252: if (mapName
253: && extract(path + SEPARATOR + mappedName2,
254: mappedName2))
255: return;
256: }
257:
258: /* Failed to find the library */
259: throw new UnsatisfiedLinkError(
260: "no " + libName1 + " or " + libName2 + " in swt.library.path, java.library.path or the jar file"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
261: }
262:
263: }
|