KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > init > JdicManager


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.init;
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26
27 import org.jdesktop.jdic.browser.internal.WebBrowserUtil;
28
29 /**
30  * Initialization manager for JDIC to set the environment variables or initialize
31  * the set up for native libraries and executable files.
32  * <p>
33  * There are 3 modes of operation: WebStart, file system, and .jar file.
34  * <p>
35  * When using WebStart, please specify a .jar file(jdic-native.jar) with the
36  * native libraries for your platform to be loaded by WebStart in your JNPL.
37  * This class will find the unjared native libraries and executables, and use
38  * them directly.
39  * <p>
40  * If not in WebStart, the system will expect the native libraries to be located
41  * in directory at the root of the classpath or .jar containing this class.
42  *
43  * @author Michael Samblanet
44  * Paul Huang
45  * George Zhang
46  * @created July 29, 2004
47  */

48 public class JdicManager {
49     private boolean isShareNativeInitialized = false;
50     private boolean isBrowserNativeInitialized = false;
51
52     /** If the current platform is Windows */
53     boolean isWindows =
54         (System.getProperty("os.name").indexOf("Windows") >= 0) ?
55         true : false;
56     
57     /** The environment variable for library path setting */
58     String JavaDoc libPathEnv = isWindows ? "PATH" : "LD_LIBRARY_PATH";
59     
60     /** The path for the JDIC native files (jdic.dll/libjdic.so, etc) */
61     String JavaDoc binaryPath = null;
62
63     /** Singleton instance of this class */
64     private static JdicManager sSingleton = null;
65
66     /**
67      * Private constructor to prevent public construction.
68      */

69     private JdicManager() {
70     }
71
72     /**
73      * Returns a singleton instance of <code>JdicManager</code>.
74      */

75     public static synchronized JdicManager getManager() {
76         if (sSingleton == null) {
77             sSingleton = new JdicManager();
78         }
79         return sSingleton;
80     }
81
82     /**
83      * Initializes the shared native file settings for all the JDIC components/
84      * packages. Set necessary environment variables for the shared native
85      * library and executable files, including *.dll files on Windows, and *.so
86      * files on Unix.
87      *
88      * @exception JdicInitException Generic initialization exception
89      */

90     public void initShareNative() throws JdicInitException {
91         // If the shared native file setting was already initialized,
92
// just return.
93
if (isShareNativeInitialized) {
94             return;
95         }
96
97         try {
98             // Find the root path of this class.
99
binaryPath = (new URL JavaDoc(JdicManager.class.getProtectionDomain()
100                 .getCodeSource().getLocation(), ".")).openConnection()
101                 .getPermission().getName();
102             binaryPath = (new File JavaDoc(binaryPath)).getCanonicalPath();
103             if (System.getProperty("javawebstart.version") != null) {
104                 // We are running under WebStart.
105
// NOTE: for a WebStart application, the jar file including
106
// the native libraries/executables must use the name
107
// "jdic-native.jar".
108
String JavaDoc cacheDirName = "RN" + "jdic-native.jar" + "/";
109                 binaryPath += File.separator + cacheDirName;
110             }
111     
112             // Add the binary path (including jdic.dll or libjdic.so) to
113
// "java.library.path", since we need to use the native methods in
114
// class InitUtility.
115
String JavaDoc newLibPath = binaryPath + File.pathSeparator +
116                                 System.getProperty("java.library.path");
117             System.setProperty("java.library.path", newLibPath);
118             Field JavaDoc fieldSysPath = ClassLoader JavaDoc.class.getDeclaredField("sys_paths");
119             fieldSysPath.setAccessible(true);
120             if (fieldSysPath != null) {
121                 fieldSysPath.set(System JavaDoc.class.getClassLoader(), null);
122             }
123     
124         } catch (Throwable JavaDoc e) {
125             throw new JdicInitException(e);
126         }
127         
128         isShareNativeInitialized = true;
129     }
130
131     /**
132      * Initializes the native file settings for the JDIC Browser component
133      * (package <code>org.jdecktop.jdic.browser</code>). Set necessary
134      * environment variables for the Browser specific native library and
135      * executable files, including *.exe files on Windows, and mozembed-<os>-gtk*
136      * files on Unix.
137      *
138      * @exception JdicInitException Generic initialization exception
139      */

140     public void initBrowserNative() throws JdicInitException {
141         // The Browser component is used.
142
// If the Browser specific native file setting was already initialized,
143
// just return.
144
if (isBrowserNativeInitialized) {
145             return;
146         }
147
148         try {
149             // Pre-append the JDIC binary path to PATH(on Windows) or
150
// LD_LIBRARY_PATH(on Unix).
151
InitUtility.preAppendEnv(libPathEnv, binaryPath);
152
153             String JavaDoc 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                 // Mozilla is the default/embedded browser.
161
// Use the user defined value or the mozilla binary
162
// path as the value of MOZILLA_FIVE_HOME env variable.
163
String JavaDoc envMFH = InitUtility.getEnv("MOZILLA_FIVE_HOME");
164                 if (envMFH == null) {
165                     File JavaDoc browserFile = new File JavaDoc(browserPath);
166                     if (browserFile.isDirectory()) {
167                         envMFH = browserFile.getCanonicalPath();
168                     } else {
169                         envMFH = browserFile.getCanonicalFile().getParent();
170                     }
171                 }
172                 
173                 if (!isWindows) {
174                     // On Unix, add the binary path to PATH.
175
InitUtility.preAppendEnv("PATH", binaryPath);
176                     // When running on webstart, the browser binary will lose
177
// "x" permission after extracted from .jar file.
178
String JavaDoc embedBinary = WebBrowserUtil.getEmbedBinaryName();
179                     Runtime.getRuntime().exec("chmod a+x "+
180                             binaryPath + File.separator + embedBinary);
181                 } else {
182                     // Mozilla on Windows, reset MOZILLA_FIVE_HOME to the GRE
183
// directory path:
184
// [Common Files]\mozilla.org\GRE\1.x_BUILDID,
185
// if Mozilla installs from a .exe package.
186
//
187
String JavaDoc xpcomPath = envMFH + File.separator + "xpcom.dll";
188                     if (!(new File JavaDoc(xpcomPath).isFile())) {
189                         // Mozilla installs from a .exe package. Check the
190
// installed GRE directory.
191
String JavaDoc 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             } // end - Mozilla is the default/embedded browser.
205
} catch (Throwable JavaDoc e) {
206             throw new JdicInitException(e);
207         }
208         
209         isBrowserNativeInitialized = true;
210     }
211     public String JavaDoc getBinaryPath(){
212         return binaryPath;
213     }
214 }
215
Popular Tags