001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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: public class Library {
013:
014: /* SWT Version - Mmmm (M=major, mmm=minor) */
015:
016: /**
017: * SWT Major version number (must be >= 0)
018: */
019: static int MAJOR_VERSION = 3;
020:
021: /**
022: * SWT Minor version number (must be in the range 0..999)
023: */
024: static int MINOR_VERSION = 235;
025:
026: /**
027: * SWT revision number (must be >= 0)
028: */
029: static int REVISION = 0;
030:
031: /**
032: * The JAVA and SWT versions
033: */
034: public static final int JAVA_VERSION, SWT_VERSION;
035:
036: static {
037: JAVA_VERSION = parseVersion(System.getProperty("java.version"));
038: SWT_VERSION = SWT_VERSION(MAJOR_VERSION, MINOR_VERSION);
039: }
040:
041: static int parseVersion(String version) {
042: if (version == null)
043: return 0;
044: int major = 0, minor = 0, micro = 0;
045: int length = version.length(), index = 0, start = 0;
046: while (index < length
047: && Character.isDigit(version.charAt(index)))
048: index++;
049: try {
050: if (start < length)
051: major = Integer.parseInt(version
052: .substring(start, index));
053: } catch (NumberFormatException e) {
054: }
055: start = ++index;
056: while (index < length
057: && Character.isDigit(version.charAt(index)))
058: index++;
059: try {
060: if (start < length)
061: minor = Integer.parseInt(version
062: .substring(start, index));
063: } catch (NumberFormatException e) {
064: }
065: start = ++index;
066: while (index < length
067: && Character.isDigit(version.charAt(index)))
068: index++;
069: try {
070: if (start < length)
071: micro = Integer.parseInt(version
072: .substring(start, index));
073: } catch (NumberFormatException e) {
074: }
075: return JAVA_VERSION(major, minor, micro);
076: }
077:
078: /**
079: * Returns the Java version number as an integer.
080: *
081: * @param major
082: * @param minor
083: * @param micro
084: * @return the version
085: */
086: public static int JAVA_VERSION(int major, int minor, int micro) {
087: return (major << 16) + (minor << 8) + micro;
088: }
089:
090: /**
091: * Returns the SWT version number as an integer.
092: *
093: * @param major
094: * @param minor
095: * @return the version
096: */
097: public static int SWT_VERSION(int major, int minor) {
098: return major * 1000 + minor;
099: }
100:
101: /**
102: * Loads the shared library that matches the version of the
103: * Java code which is currently running. SWT shared libraries
104: * follow an encoding scheme where the major, minor and revision
105: * numbers are embedded in the library name and this along with
106: * <code>name</code> is used to load the library. If this fails,
107: * <code>name</code> is used in another attempt to load the library,
108: * this time ignoring the SWT version encoding scheme.
109: *
110: * @param name the name of the library to load
111: */
112: public static void loadLibrary(String name) {
113: /*
114: * Include platform name to support different windowing systems
115: * on same operating system.
116: */
117: String platform = Platform.PLATFORM;
118:
119: /*
120: * Get version qualifier.
121: */
122: String version = System.getProperty("swt.version"); //$NON-NLS-1$
123: if (version == null) {
124: version = "" + MAJOR_VERSION; //$NON-NLS-1$
125: /* Force 3 digits in minor version number */
126: if (MINOR_VERSION < 10) {
127: version += "00"; //$NON-NLS-1$
128: } else {
129: if (MINOR_VERSION < 100)
130: version += "0"; //$NON-NLS-1$
131: }
132: version += MINOR_VERSION;
133: /* No "r" until first revision */
134: if (REVISION > 0)
135: version += "r" + REVISION; //$NON-NLS-1$
136: }
137:
138: /*
139: * GOOGLE: Since we're bundling our own version of SWT, we need to be
140: * able to tell SWT where its dynamic libraries live. Otherwise we'd
141: * have to force our users to always specify a -Djava.library.path
142: * on the command line.
143: */
144: String swtLibraryPath = System.getProperty("swt.library.path");
145: try {
146: String newName = name + "-" + platform + "-" + version; //$NON-NLS-1$ //$NON-NLS-2$
147: if (swtLibraryPath != null)
148: System.load(swtLibraryPath
149: + System.mapLibraryName(newName));
150: else
151: System.loadLibrary(newName);
152: return;
153: } catch (UnsatisfiedLinkError e1) {
154: try {
155: String newName = name + "-" + platform; //$NON-NLS-1$
156: if (swtLibraryPath != null)
157: System.load(swtLibraryPath
158: + System.mapLibraryName(newName));
159: else
160: System.loadLibrary(newName);
161: return;
162: } catch (UnsatisfiedLinkError e2) {
163: throw e1;
164: }
165: }
166: }
167:
168: }
|