All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pyx4j.jni.Common Maven / Gradle / Ivy

/*
 * Pyx4j framework
 * Copyright (C) 2006 pyx4.com.
 *
 * @author vlads
 * @version $Id: Common.java 32 2007-03-14 05:57:09Z vlads $
 */
package com.pyx4j.jni;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

//import org.apache.log4j.Logger;

public class Common {

    public static final String NATIVE_LIB = "libPyx4jNative";

    private static boolean triedToLoadAlredy;

    private static boolean libraryAvailable;

    private static final boolean debug = false;

    private static boolean windows;
    
    private static String tempDirPrefix = "pyx4j_";
    
    public static boolean isAvailable() {
        if (triedToLoadAlredy) {
            return libraryAvailable;
        }
        String libFileName = NATIVE_LIB;
        String sysName = System.getProperty("os.name");

        if (sysName.toLowerCase(Locale.ENGLISH).indexOf("windows") != -1)  {
            libFileName = libFileName + ".dll";
            windows = true;
        } else if (sysName.toLowerCase(Locale.ENGLISH).indexOf("linux") != -1) {
            libFileName = libFileName + ".so";
        } else {
            triedToLoadAlredy = true;
            libraryAvailable = false;
            return libraryAvailable;
        }

        libraryAvailable = tryload(NATIVE_LIB);

        if (!libraryAvailable) {
            libraryAvailable = loadAsSystemResource(libFileName);
        }

        if (!libraryAvailable) {
           // Logger log = Logger.getLogger(Common.class.getClass());
            System.err.println("Native Library " + NATIVE_LIB + " not avalable on " + sysName);
            //log.error("Native Library " + JOUR_LIB + " not avalable");
            //log.info("java.library.path=" + System.getProperty ("java.library.path"));
        }
        triedToLoadAlredy = true;
        return libraryAvailable;
    }

    public static boolean isWindows() {
        if (!triedToLoadAlredy) {
            isAvailable();
        }
        return windows;
    }

    private static boolean tryload(String name) {
        try {
            System.loadLibrary(name);
        } catch (Throwable e) {
            if (debug) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    private static boolean loadAsSystemResource(String libFileName) {
        InputStream is = null;
        try {
            ClassLoader clo = Common.class.getClassLoader();
            if (clo == null) {
                is = ClassLoader.getSystemResourceAsStream(libFileName);
            } else {
                is = clo.getResourceAsStream(libFileName);
            }
        } catch (Throwable e) {
            throw new Error("Native Library " + libFileName + " is not a ressource !");
        }
        if (is == null) {
            throw new Error("Native Library " + libFileName + " is not a ressource !");
        }
        File fd = makeTempName(libFileName);
        try {
            if (!copy2File(is, fd)) {
                return false;
            }
        } finally {
            try {
                is.close();
            } catch (IOException ignore) {
                is = null;
            }
        }
        fd.deleteOnExit();
//        deleteOnExit(fd);
        try {
            System.load(fd.getAbsolutePath());
            if (debug) {
               System.out.println("Use Native Library " + fd.getAbsolutePath());
            }
        } catch (Throwable e) {
            if (debug) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    private static boolean copy2File(InputStream is, File fd) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fd);
            byte b[] = new byte[1000];
            int len;
            while ((len = is.read(b)) >= 0) {
                fos.write(b, 0, len);
            }
            return true;
        } catch (Throwable e) {
            System.err.println("Can't create temporary file" + fd.getAbsolutePath());
            if (debug) {
                e.printStackTrace();
            }
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ignore) {
                    fos = null;
                }
            }
        }
    }

    private static File makeTempName(String libFileName) {
        String tmpDir = System.getProperty("java.io.tmpdir");
        String uname = System.getProperty("user.name");
        int count = 0;
        File fd = null;
        File dir = null;
        while (true) {
            if (count > 10) {
            	if (debug) System.out.println("Can't create temporary dir " + dir.getAbsolutePath());
            	return new File(tmpDir, libFileName);
            }
            dir = new File(tmpDir, tempDirPrefix + uname + "_"+ (count ++));
            fd = new File(dir, libFileName);
            if ((fd.exists()) && (!fd.delete())) {
                continue;
            }
            if ((!dir.exists()) && (!dir.mkdirs())) {
            	if (debug) System.out.println("Can't create temporary dir " + dir.getAbsolutePath());
                continue;
            }
            dir.deleteOnExit();
            try {
				if (!fd.createNewFile()) {
					if (debug) System.out.println("Can't create file in temporary dir " + fd.getAbsolutePath());
				    continue;
				}
			} catch (IOException e) {
				if (debug) System.out.println("Can't create file in temporary dir " + fd.getAbsolutePath());
				continue;
			}
            break;
        }
        return fd;
    }
    
//    private static void deleteOnExit(final File fd) {
//        Runnable r = new Runnable() {
//            public void run() {
//                if (!fd.delete()) {
//                    System.err.println("Can't remove Native Library " + fd);
//                }
//            }
//        };
//        Runtime.getRuntime().addShutdownHook(new Thread(r));
//    }

    static {
        isAvailable();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy