1 20 21 package org.jdesktop.jdic.init; 22 23 import java.io.File ; 24 import java.net.URL ; 25 import java.lang.reflect.Field ; 26 27 import org.jdesktop.jdic.browser.internal.WebBrowserUtil; 28 29 48 public class JdicManager { 49 private boolean isShareNativeInitialized = false; 50 private boolean isBrowserNativeInitialized = false; 51 52 53 boolean isWindows = 54 (System.getProperty("os.name").indexOf("Windows") >= 0) ? 55 true : false; 56 57 58 String libPathEnv = isWindows ? "PATH" : "LD_LIBRARY_PATH"; 59 60 61 String binaryPath = null; 62 63 64 private static JdicManager sSingleton = null; 65 66 69 private JdicManager() { 70 } 71 72 75 public static synchronized JdicManager getManager() { 76 if (sSingleton == null) { 77 sSingleton = new JdicManager(); 78 } 79 return sSingleton; 80 } 81 82 90 public void initShareNative() throws JdicInitException { 91 if (isShareNativeInitialized) { 94 return; 95 } 96 97 try { 98 binaryPath = (new URL (JdicManager.class.getProtectionDomain() 100 .getCodeSource().getLocation(), ".")).openConnection() 101 .getPermission().getName(); 102 binaryPath = (new File (binaryPath)).getCanonicalPath(); 103 if (System.getProperty("javawebstart.version") != null) { 104 String cacheDirName = "RN" + "jdic-native.jar" + "/"; 109 binaryPath += File.separator + cacheDirName; 110 } 111 112 String newLibPath = binaryPath + File.pathSeparator + 116 System.getProperty("java.library.path"); 117 System.setProperty("java.library.path", newLibPath); 118 Field fieldSysPath = ClassLoader .class.getDeclaredField("sys_paths"); 119 fieldSysPath.setAccessible(true); 120 if (fieldSysPath != null) { 121 fieldSysPath.set(System .class.getClassLoader(), null); 122 } 123 124 } catch (Throwable e) { 125 throw new JdicInitException(e); 126 } 127 128 isShareNativeInitialized = true; 129 } 130 131 140 public void initBrowserNative() throws JdicInitException { 141 if (isBrowserNativeInitialized) { 145 return; 146 } 147 148 try { 149 InitUtility.preAppendEnv(libPathEnv, binaryPath); 152 153 String browserPath = WebBrowserUtil.getBrowserPath(); 154 if (browserPath == null) { 155 throw new JdicInitException( 156 "Can't locate the native browser path!"); 157 } 158 159 if (WebBrowserUtil.isDefaultBrowserMozilla()) { 160 String envMFH = InitUtility.getEnv("MOZILLA_FIVE_HOME"); 164 if (envMFH == null) { 165 File browserFile = new File (browserPath); 166 if (browserFile.isDirectory()) { 167 envMFH = browserFile.getCanonicalPath(); 168 } else { 169 envMFH = browserFile.getCanonicalFile().getParent(); 170 } 171 } 172 173 if (!isWindows) { 174 InitUtility.preAppendEnv("PATH", binaryPath); 176 String embedBinary = WebBrowserUtil.getEmbedBinaryName(); 179 Runtime.getRuntime().exec("chmod a+x "+ 180 binaryPath + File.separator + embedBinary); 181 } else { 182 String xpcomPath = envMFH + File.separator + "xpcom.dll"; 188 if (!(new File (xpcomPath).isFile())) { 189 String mozGreHome 192 = WebBrowserUtil.getMozillaGreHome(); 193 if (mozGreHome == null) { 194 throw new JdicInitException( 195 "Can't locate the GRE directory of the " + 196 "installed Mozilla binary: " + envMFH); 197 } 198 envMFH = mozGreHome; 199 } 200 } 201 202 InitUtility.setEnv("MOZILLA_FIVE_HOME", envMFH); 203 InitUtility.preAppendEnv(libPathEnv, envMFH); 204 } } catch (Throwable e) { 206 throw new JdicInitException(e); 207 } 208 209 isBrowserNativeInitialized = true; 210 } 211 public String getBinaryPath(){ 212 return binaryPath; 213 } 214 } 215 | Popular Tags |