KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > Library


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.internal;
12
13 import java.io.*;
14
15 public class Library {
16
17     /* SWT Version - Mmmm (M=major, mmm=minor) */
18     
19     /**
20      * SWT Major version number (must be >= 0)
21      */

22     static int MAJOR_VERSION = 3;
23     
24     /**
25      * SWT Minor version number (must be in the range 0..999)
26      */

27     static int MINOR_VERSION = 347;
28     
29     /**
30      * SWT revision number (must be >= 0)
31      */

32     static int REVISION = 0;
33     
34     /**
35      * The JAVA and SWT versions
36      */

37     public static final int JAVA_VERSION, SWT_VERSION;
38
39     static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {}
65     return JAVA_VERSION(major, minor, micro);
66 }
67
68 /**
69  * Returns the Java version number as an integer.
70  *
71  * @param major
72  * @param minor
73  * @param micro
74  * @return the version
75  */

76 public static int JAVA_VERSION (int major, int minor, int micro) {
77     return (major << 16) + (minor << 8) + micro;
78 }
79
80 /**
81  * Returns the SWT version number as an integer.
82  *
83  * @param major
84  * @param minor
85  * @return the version
86  */

87 public static int SWT_VERSION (int major, int minor) {
88     return major * 1000 + minor;
89 }
90
91 static boolean extract (String JavaDoc fileName, String JavaDoc 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); //$NON-NLS-1$
98
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")) { //$NON-NLS-1$
108
try {
109                         Runtime.getRuntime ().exec (new String JavaDoc []{"chmod", "755", fileName}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
110
} catch (Throwable JavaDoc e) {}
111                 }
112                 if (load (fileName)) return true;
113             }
114         }
115     } catch (Throwable JavaDoc 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 JavaDoc 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 JavaDoc e) {}
136     return false;
137 }
138
139 /**
140  * Loads the shared library that matches the version of the
141  * Java code which is currently running. SWT shared libraries
142  * follow an encoding scheme where the major, minor and revision
143  * numbers are embedded in the library name and this along with
144  * <code>name</code> is used to load the library. If this fails,
145  * <code>name</code> is used in another attempt to load the library,
146  * this time ignoring the SWT version encoding scheme.
147  *
148  * @param name the name of the library to load
149  */

150 public static void loadLibrary (String JavaDoc name) {
151     loadLibrary (name, true);
152 }
153
154 /**
155  * Loads the shared library that matches the version of the
156  * Java code which is currently running. SWT shared libraries
157  * follow an encoding scheme where the major, minor and revision
158  * numbers are embedded in the library name and this along with
159  * <code>name</code> is used to load the library. If this fails,
160  * <code>name</code> is used in another attempt to load the library,
161  * this time ignoring the SWT version encoding scheme.
162  *
163  * @param name the name of the library to load
164  * @param mapName true if the name should be mapped, false otherwise
165  */

166 public static void loadLibrary (String JavaDoc name, boolean mapName) {
167     
168     /* Compute the library name and mapped name */
169     String JavaDoc libName1, libName2, mappedName1, mappedName2;
170     if (mapName) {
171         String JavaDoc version = System.getProperty ("swt.version"); //$NON-NLS-1$
172
if (version == null) {
173             version = "" + MAJOR_VERSION; //$NON-NLS-1$
174
/* Force 3 digits in minor version number */
175             if (MINOR_VERSION < 10) {
176                 version += "00"; //$NON-NLS-1$
177
} else {
178                 if (MINOR_VERSION < 100) version += "0"; //$NON-NLS-1$
179
}
180             version += MINOR_VERSION;
181             /* No "r" until first revision */
182             if (REVISION > 0) version += "r" + REVISION; //$NON-NLS-1$
183
}
184         libName1 = name + "-" + Platform.PLATFORM + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$
185
libName2 = name + "-" + Platform.PLATFORM; //$NON-NLS-1$
186
mappedName1 = System.mapLibraryName (libName1);
187         mappedName2 = System.mapLibraryName (libName2);
188     } else {
189         libName1 = libName2 = mappedName1 = mappedName2 = name;
190     }
191
192     /* Try loading library from swt library path */
193     String JavaDoc path = System.getProperty ("swt.library.path"); //$NON-NLS-1$
194
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     /* Try loading library from java library path */
201     if (load (libName1)) return;
202     if (mapName && load (libName2)) return;
203     
204     /* Try loading library from the tmp directory if swt library path is not specified */
205     if (path == null) {
206         path = System.getProperty ("java.io.tmpdir"); //$NON-NLS-1$
207
path = new File (path).getAbsolutePath ();
208         if (load (path + SEPARATOR + mappedName1)) return;
209         if (mapName && load (path + SEPARATOR + mappedName2)) return;
210     }
211         
212     /* Try extracting and loading library from jar */
213     if (path != null) {
214         if (extract (path + SEPARATOR + mappedName1, mappedName1)) return;
215         if (mapName && extract (path + SEPARATOR + mappedName2, mappedName2)) return;
216     }
217     
218     /* Failed to find the library */
219     throw new UnsatisfiedLinkError JavaDoc ("no " + libName1 + " or " + libName2 + " in swt.library.path, java.library.path or the jar file"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
220
}
221
222 }
223
Popular Tags