FileDocCategorySizeDatePackage
FindAsResource.javaAPI DocFobs4JMF API 0.4.13002Tue Dec 19 14:37:16 GMT 2006com.moesol.bindings

FindAsResource

public class FindAsResource extends Object implements NativeLibraryFinderStrategy

Fields Summary
private static final Logger
s_logger
Constructors Summary
Methods Summary
static java.io.FilecomputeOutputFile(java.io.File outputDirectory, java.lang.String mappedName)
Compute an unique name for us to copy the library to. The file returned will not be in use by any other process.

param
mappedName
return
a unique filename
throws
IOException

		for (int i = 0; i < 1024; i++) {
			File check = new File(outputDirectory, mappedName + "." + i);
			if (check.createNewFile()) {
				return check;
			}
		}
		throw new RuntimeException("More that 1024 processes are running that are using or have failed to clean: " + outputDirectory + "/" + mappedName);
	
private static voidcopyStream(java.io.InputStream is, java.io.File outputFile)

		OutputStream os = null;
		try {
			os = new BufferedOutputStream(new FileOutputStream(outputFile));
			int c;
			while ((c = is.read()) != -1) {
				os.write(c);
			}
		} finally {
			safeClose(os);
		}
	
public voidfindAndLoad(java.lang.Class classInJar, java.lang.String library)


	      
		String mappedName = System.mapLibraryName(library);
		InputStream is = findLibraryResource(classInJar, mappedName);
		
		try {
			NativeLibraryFinder.getLibraryDirectory().mkdirs();
			File outputFile = computeOutputFile(NativeLibraryFinder.getLibraryDirectory(), mappedName);
			outputFile.deleteOnExit(); // delete on exit 
			copyStream(is, outputFile);
			System.load(outputFile.getAbsolutePath());
		} catch (IOException e) {
			UnsatisfiedLinkError le = new UnsatisfiedLinkError("Failed to extract native library");
			le.initCause(e);
			throw le;
		} finally {
			safeClose(is);
		}
	
java.io.InputStreamfindLibraryResource(java.lang.Class classInJar, java.lang.String mappedName)

		for (int i = 0; i < NativeLibraryFinder.OS_PREFIXES.length; i++) {
			String osPrefix = NativeLibraryFinder.OS_PREFIXES[i];
			String resource = osPrefix + mappedName;
			s_logger.log(Level.FINE, "trying {0}", resource);
			InputStream is = classInJar.getResourceAsStream(resource);
			if (is != null) {
				return is;
			}
		}
		throw new UnsatisfiedLinkError("Failed to find: " + mappedName);
	
private static voidsafeClose(java.io.InputStream closable)

		if (closable == null) {
			return;
		}
		try {
			closable.close();
		} catch (IOException e) {
			s_logger.log(Level.INFO, "Failed to close", e);
		}
	
private static voidsafeClose(java.io.OutputStream closable)

		if (closable == null) {
			return;
		}
		try {
			closable.close();
		} catch (IOException e) {
			s_logger.log(Level.INFO, "Failed to close", e);
		}