/*--------------------------------------------------------------------------*/ private InputStream openInputStream(String name, NativeLibraryClient client) throws Exception { Class clientClass = client.getClass(); // ---------------------------------------------------- // try to open an input stream, assuming the library // is located with the client // ---------------------------------------------------- InputStream input = clientClass.getResourceAsStream(name + extension); // ---------------------------------------------------- // if this is not successful, try to load from the // location where all native libraries are supposed // to be located. // ---------------------------------------------------- if (input == null) { input = clientClass.getResourceAsStream('/' + nativeDirectory + '/' + name + extension); } // ---------------------------------------------------- // if this fails as well, throw an exception // ---------------------------------------------------- if (input == null) { throw (new Exception("can't locate library")); } else { return (input); } }
/*--------------------------------------------------------------------------*/ private String getNativePath(String name, NativeLibraryClient client) { ProtectionDomain domain = client.getClass().getProtectionDomain(); CodeSource codeSource = domain.getCodeSource(); URL url = codeSource.getLocation(); String path = url.getPath(); path = path + nativeDirectory + '/' + name + extension; path = path.replace('/', File.separatorChar); // Revise the URI-path to a file path; needed in uninstaller because it // writes the jar contents into a sandbox; may be with blanks in the // path. path = revisePath(path); return (path); }
/*--------------------------------------------------------------------------*/ public synchronized void loadLibrary(String name, NativeLibraryClient client) throws Exception { String libraryName = strip(name); String tempFileName = ""; // ---------------------------------------------------- // Return if the library is already loaded // ---------------------------------------------------- if (loaded(libraryName)) { return; } if (System.getProperty("DLL_PATH") != null) { String path = System.getProperty("DLL_PATH") + "/" + name + extension; path = path.replace('/', File.separatorChar); Debug.trace("Try to load library " + path); System.load(path); return; } // ---------------------------------------------------- // First try a straight load // ---------------------------------------------------- try { System.loadLibrary(libraryName); return; } catch (UnsatisfiedLinkError exception) { } catch (SecurityException exception) { } // ---------------------------------------------------- // Next, try to get the protocol for loading the resource. // ---------------------------------------------------- Class clientClass = client.getClass(); String resourceName = clientClass.getName(); int nameStart = resourceName.lastIndexOf('.') + 1; resourceName = resourceName.substring(nameStart, resourceName.length()) + CLIENT_EXTENSION; URL url = clientClass.getResource(resourceName); if (url == null) { throw (new Exception("can't identify load protocol for " + libraryName + extension)); } String protocol = url.getProtocol(); // ---------------------------------------------------- // If it's a local file, load it from the current location // ---------------------------------------------------- if (protocol.equalsIgnoreCase(FILE_PROTOCOL)) { try { System.load(getClientPath(name, url)); } catch (Throwable exception) { try { System.load(getNativePath(name, client)); } catch (Throwable exception2) { throw (new Exception("error loading library")); } } } // ---------------------------------------------------- // If it is in a *.jar file, extract it to 'java.io.tmpdir' // ---------------------------------------------------- else if (protocol.equalsIgnoreCase(JAR_PROTOCOL)) { tempFileName = getTempFileName(libraryName); try { extractFromJar(libraryName, tempFileName, client); clients.add(client); temporaryFileNames.add(tempFileName); libraryNames.add( tempFileName.substring( (tempFileName.lastIndexOf(File.separatorChar) + 1), tempFileName.length())); // -------------------------------------------------- // Try loading the temporary file from 'java.io.tmpdir'. // -------------------------------------------------- System.load(tempFileName); } catch (Throwable exception) { throw (new Exception("error loading library\n" + exception.toString())); } } }