1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.Properties ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.StringReader ; 31 import java.io.StreamTokenizer ; 32 33 36 public class GnomeUtility { 37 static { 38 System.loadLibrary("jdic"); 39 } 40 41 44 private GnomeUtility() { 45 } 46 47 54 public static boolean isMozillaRunning(String mozillaPath) throws IOException { 55 String MOZILLA_OUTPUT_NO_RUNNING = "No running window"; 59 60 InputStream stderr = null; 61 InputStreamReader isr = null; 62 BufferedReader br = null; 63 64 try { 65 Process proc = 66 Runtime.getRuntime().exec( 67 new String [] { mozillaPath, "-remote", "ping()" }); 68 69 stderr = proc.getErrorStream(); 70 isr = new InputStreamReader (stderr); 71 br = new BufferedReader (isr); 72 String 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 e) { 81 throw e; 82 } 83 84 return true; 85 } 86 87 93 public static String getMozillaVersionNumber(String mozillaPath) { 94 String MOZILLA_VERSION_PREFIX = "Mozilla "; 95 96 InputStream stdin = null; 97 InputStreamReader isr = null; 98 BufferedReader br = null; 99 Runtime rt = Runtime.getRuntime(); 100 Process proc = null; 101 String verNum = null; 102 103 try { 104 proc = rt.exec(new String [] { mozillaPath, "-version" }); 106 107 stdin = proc.getInputStream(); 108 isr = new InputStreamReader (stdin); 109 br = new BufferedReader (isr); 110 111 String line = null; 112 if ((line = br.readLine()) != null) { 113 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 e) { 128 return null; 129 } 130 131 return verNum; 132 } 133 134 139 public static String getDefaultMailerPath() 140 throws UnsupportedOperationException { 141 String DEFAULT_MAILER_PROPERTY_FILE = "defmailer.properties"; 142 String defMailerPath = nativeGetDefaultMailerPath(); 144 145 if (defMailerPath != null) { 147 return defMailerPath; 148 } else { 149 Properties mailerProp = new Properties (); 150 String propFilePath = getPropFilePath(DEFAULT_MAILER_PROPERTY_FILE); 151 152 if (propFilePath == null) { 153 throw new UnsupportedOperationException ("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 try { 158 mailerProp.load(new FileInputStream (propFilePath)); 159 defMailerPath = mailerProp.getProperty("MAILER"); 160 161 if (defMailerPath == null) { 162 throw new UnsupportedOperationException ("The default mailer path is not set in the property file: " + 163 propFilePath); 164 } 165 166 return defMailerPath; 167 } catch (IOException e) { 168 throw new UnsupportedOperationException ("Failed to get default mailer path from property file: " + propFilePath); 169 } 170 } 171 } 172 } 173 174 180 private static String getPropFilePath(String propFileName) { 181 String classpath = System.getProperty("java.class.path"); 182 StreamTokenizer classpath_st = new StreamTokenizer (new StringReader (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 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 tmpFile = new File (propPath); 207 if (tmpFile.exists()) { 208 return tmpFile.getAbsolutePath(); 209 } 210 } 211 } 212 } catch (IOException ioe) { 213 } 214 215 return null; 216 } 217 218 private native static String nativeGetDefaultMailerPath(); 219 } 220 | Popular Tags |