使用ServiceLoader进行前后检查的动态库加载

时间:2012-06-26 14:12:22

标签: java dll serviceloader

这个问题是解决attach library dynamic loading problem的下一步。我有一些这样的:

public static void loadAttachProvider(String binPath) throws Exception {
    /*
     * If AttachProvider is already available now then propably we are in JDK
     * or path to attach.dll was specified as java.library.path on startup
     */
    if (isAttachProviderAvailable()) return;

    /*
     * In normall-use code I have generated binPath appropriate to platform (VM)
     * family and bitness (e.g. win32 or win64)
     */
    System.setProperty("java.library.path", binPath);

    // Reload library path hack
    Field sys = ClassLoader.class.getDeclared("sys_paths");
    sys.setAccessible(true);
    sys.set(null, null);

    // Here I want to be sure that AttachProvider is available
    //System.loadLibrary("attach"); <- I'm try this too, but it's not suffiecient
    if (!isAttachProviderAvailable()) {
        throw new IOException("AttachProvider not available");
    }
}

检查AttachProvider可用性如下:

public static boolean isAttachProviderAvailable() {
    // This line is a substance of AttachProvider.providers() method
    ServiceLoader<AttachProvider> providers = ServiceLoader.load(AttachProvider.class, AttachProvider.class.getClassLoader());
    try {
        for (Iterator<AttachProvider> it = providers.iterator(); it.hasNext();) {
            /*
             * In normall-use code the "windows" constant is replaced
             * by something like platform-specific PLATFORM_FAMILY
             */
            if (it.next().type().equals("windows")) return true;
        }
    } catch (ServiceConfigurationError ex) {}
    return false;
}

如果使用JDK的私有JRE运行它,那么一切都会好的,因为JDK在默认库路径中有attach.dll

问题

这项工作只有在我删除第一个isAttachProviderAvailable时才会有用,因为如果我尝试在AttachProvider可用之前对其进行实例化,那么我将无法在加载attach.dll时将其实例化。 ServiceLoader未根据新库路径刷新。怎么解决这个?如何在不实例化的情况下检查AttachProvider可用性或如何刷新ServiceLoader

1 个答案:

答案 0 :(得分:0)

解决了第一次附加检查替换为这样的事情:

try {
    System.loadLibrary("attach");
    if (isAttachProviderAvailable()) return;
    throw new IOException("AttachProvider not available");
} catch (UnsatisfiedLinkError ex) {
   /*
    * dll can't be loaded, so we need to specify special binpath
    * and reload library path as post above
    */
}

但是我在等待更好的解决方案。