KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > internal > impl > GnomeUtility


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.desktop.internal.impl;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.StreamTokenizer JavaDoc;
32
33 /**
34  * Utility class shared by classes on Gnome.
35  */

36 public class GnomeUtility {
37     static {
38         System.loadLibrary("jdic");
39     }
40
41     /**
42      * Suppress default constructor.
43      */

44     private GnomeUtility() {
45     }
46
47     /**
48      * Checks if there is already a running Mozilla instance.
49      *
50      * @param mozillaPath the given absolute path for mozilla executable.
51      * @throws IOException if running Mozilla commandline fails.
52      * @return true if there is already a mozilla instance running.
53      */

54     public static boolean isMozillaRunning(String JavaDoc mozillaPath) throws IOException JavaDoc {
55         // Check running mozilla instance using: mozilla -remote ping().
56
// If there is no running Mozilla instance. The complete output is: 'No running
57
// window found.'
58
String JavaDoc MOZILLA_OUTPUT_NO_RUNNING = "No running window";
59
60         InputStream JavaDoc stderr = null;
61         InputStreamReader JavaDoc isr = null;
62         BufferedReader JavaDoc br = null;
63
64         try {
65             Process JavaDoc proc =
66                 Runtime.getRuntime().exec(
67                     new String JavaDoc[] { mozillaPath, "-remote", "ping()" });
68
69             stderr = proc.getErrorStream();
70             isr = new InputStreamReader JavaDoc(stderr);
71             br = new BufferedReader JavaDoc(isr);
72             String JavaDoc line = null;
73             while ((line = br.readLine()) != null) {
74                 if (line.indexOf(MOZILLA_OUTPUT_NO_RUNNING) != -1) {
75                     br.close();
76                     return false;
77                 }
78             }
79             br.close();
80         } catch (IOException JavaDoc e) {
81             throw e;
82         }
83
84         return true;
85     }
86
87     /**
88      * Returns the Mozilla version number.
89      *
90      * @param mozillaPath the given absolute path for mozilla executable.
91      * @return the Mozilla version number.
92      */

93     public static String JavaDoc getMozillaVersionNumber(String JavaDoc mozillaPath) {
94         String JavaDoc MOZILLA_VERSION_PREFIX = "Mozilla ";
95
96         InputStream JavaDoc stdin = null;
97         InputStreamReader JavaDoc isr = null;
98         BufferedReader JavaDoc br = null;
99         Runtime JavaDoc rt = Runtime.getRuntime();
100         Process JavaDoc proc = null;
101         String JavaDoc verNum = null;
102
103         try {
104             // Run "mozilla -version" to get the version info.
105
proc = rt.exec(new String JavaDoc[] { mozillaPath, "-version" });
106
107             stdin = proc.getInputStream();
108             isr = new InputStreamReader JavaDoc(stdin);
109             br = new BufferedReader JavaDoc(isr);
110
111             String JavaDoc line = null;
112             if ((line = br.readLine()) != null) {
113                 // Parse the version info to get the version number.
114
// The returned version info would be like:
115
// "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020830, build 2002083014"
116
// or: "Mozilla 1.4, Copyright (c) 2003 mozilla.org, build 2003052912"
117
// Since the patch for supporting this feature(targeting frame) is checked into Mozilla source tree
118
// in revision after 1.4, so it's only necessary to parse the second version info style.
119
if (line.indexOf(MOZILLA_VERSION_PREFIX) == 0) {
120                     verNum =
121                         line.substring(
122                             MOZILLA_VERSION_PREFIX.length(),
123                             line.indexOf(','));
124                 }
125             }
126             br.close();
127         } catch (IOException JavaDoc e) {
128             return null;
129         }
130
131         return verNum;
132     }
133     
134     /**
135      * Returns the Gnome default system mailer path.
136      *
137      * @return path of the default system mailer.
138      */

139     public static String JavaDoc getDefaultMailerPath()
140         throws UnsupportedOperationException JavaDoc {
141         String JavaDoc DEFAULT_MAILER_PROPERTY_FILE = "defmailer.properties";
142         // Find the system mailer (default "mailto" protocol handler) path by GConf settings first.
143
String JavaDoc defMailerPath = nativeGetDefaultMailerPath();
144         
145         // If no default mailer setting in GConf, check the property file defining the default mailer.
146
if (defMailerPath != null) {
147             return defMailerPath;
148         } else {
149             Properties JavaDoc mailerProp = new Properties JavaDoc();
150             String JavaDoc propFilePath = getPropFilePath(DEFAULT_MAILER_PROPERTY_FILE);
151
152             if (propFilePath == null) {
153                 throw new UnsupportedOperationException JavaDoc("No default mailer is set in GConf, and the property file defining default mailer" +
154                     " is not found: " + DEFAULT_MAILER_PROPERTY_FILE);
155             } else {
156                 // Found the default mailer property file.
157
try {
158                     mailerProp.load(new FileInputStream JavaDoc(propFilePath));
159                     defMailerPath = mailerProp.getProperty("MAILER");
160             
161                     if (defMailerPath == null) {
162                         throw new UnsupportedOperationException JavaDoc("The default mailer path is not set in the property file: " +
163                             propFilePath);
164                     }
165
166                     return defMailerPath;
167                 } catch (IOException JavaDoc e) {
168                     throw new UnsupportedOperationException JavaDoc("Failed to get default mailer path from property file: " + propFilePath);
169                 }
170             }
171         }
172     }
173
174     /**
175      * Returns the path of the property file defining the system default mailer.
176      *
177      * @param propFileName the name of the property file defining the system default mailer.
178      * @return path of the property file.
179      */

180     private static String JavaDoc getPropFilePath(String JavaDoc propFileName) {
181         String JavaDoc classpath = System.getProperty("java.class.path");
182         StreamTokenizer JavaDoc classpath_st = new StreamTokenizer JavaDoc(new StringReader JavaDoc(classpath));
183
184         classpath_st.whitespaceChars(File.pathSeparatorChar, File.pathSeparatorChar);
185         classpath_st.wordChars(File.separatorChar, File.separatorChar);
186
187         classpath_st.ordinaryChar('.');
188         classpath_st.wordChars('.', '.');
189         classpath_st.ordinaryChar(' ');
190         classpath_st.wordChars(' ', ' ');
191         classpath_st.wordChars('_', '_');
192
193         try {
194             while (classpath_st.nextToken() != StreamTokenizer.TT_EOF) {
195                 int jarIndex = -1;
196
197                 if ((classpath_st.ttype == StreamTokenizer.TT_WORD) &&
198                     ((jarIndex = classpath_st.sval.indexOf("jdic.jar")) != -1)) {
199                     String JavaDoc propPath = classpath_st.sval.substring(0, jarIndex);
200                     if (propPath != null) {
201                         propPath = propPath + File.separator + propFileName;
202                     } else {
203                         propPath = "." + File.separator + propFileName;
204                     }
205
206                     File JavaDoc tmpFile = new File JavaDoc(propPath);
207                     if (tmpFile.exists()) {
208                         return tmpFile.getAbsolutePath();
209                     }
210                 }
211             }
212         } catch (IOException JavaDoc ioe) {
213         }
214
215         return null;
216     }
217        
218     private native static String JavaDoc nativeGetDefaultMailerPath();
219 }
220
Popular Tags