1 11 package org.eclipse.osgi.framework.debug; 12 13 import java.io.*; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.Iterator ; 17 import java.util.Properties ; 18 19 public class DebugOptions implements org.eclipse.osgi.service.debug.DebugOptions { 20 Properties options = null; 21 22 private static DebugOptions singleton = null; 23 private static boolean debugEnabled = true; 24 private static final String OPTIONS = ".options"; 26 public static DebugOptions getDefault() { 27 if (singleton == null && debugEnabled) { 28 DebugOptions result = new DebugOptions(); 29 debugEnabled = result.isDebugEnabled(); 30 if (debugEnabled) 31 singleton = result; 32 } 33 return singleton; 34 } 35 36 public static URL buildURL(String spec, boolean trailingSlash) { 37 if (spec == null) 38 return null; 39 boolean isFile = spec.startsWith("file:"); try { 41 if (isFile) 42 return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash); 43 else 44 return new URL (spec); 45 } catch (MalformedURLException e) { 46 if (isFile) 49 return null; 50 try { 51 return adjustTrailingSlash(new File(spec).toURL(), trailingSlash); 52 } catch (MalformedURLException e1) { 53 return null; 54 } 55 } 56 } 57 58 private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException { 59 String file = url.getFile(); 60 if (trailingSlash == (file.endsWith("/"))) return url; 62 file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); return new URL (url.getProtocol(), url.getHost(), file); 64 } 65 66 private DebugOptions() { 67 super(); 68 loadOptions(); 69 } 70 71 public boolean getBooleanOption(String option, boolean defaultValue) { 72 String optionValue = getOption(option); 73 return (optionValue != null && optionValue.equalsIgnoreCase("true")) || defaultValue; } 75 76 public String getOption(String option) { 77 return options != null ? options.getProperty(option) : null; 78 } 79 80 public String getOption(String option, String defaultValue) { 81 return options != null ? options.getProperty(option, defaultValue) : defaultValue; 82 } 83 84 public int getIntegerOption(String option, int defaultValue) { 85 String value = getOption(option); 86 try { 87 return value == null ? defaultValue : Integer.parseInt(value); 88 } catch (NumberFormatException e) { 89 return defaultValue; 90 } 91 } 92 93 public void setOption(String option, String value) { 94 if (options != null) 95 options.put(option, value.trim()); 96 } 97 98 public boolean isDebugEnabled() { 99 return options != null; 100 } 101 102 private void loadOptions() { 103 String debugOptionsFilename = System.getProperty("osgi.debug"); if (debugOptionsFilename == null) 108 return; 109 options = new Properties (); 110 URL optionsFile; 111 if (debugOptionsFilename.length() == 0) { 112 String userDir = System.getProperty("user.dir").replace(File.separatorChar, '/'); if (!userDir.endsWith("/")) userDir += "/"; debugOptionsFilename = new File(userDir, OPTIONS).toString(); 119 } 120 optionsFile = buildURL(debugOptionsFilename, false); 121 if (optionsFile == null) { 122 System.out.println("Unable to construct URL for options file: " + debugOptionsFilename); return; 124 } 125 System.out.print("Debug options:\n " + optionsFile.toExternalForm()); try { 127 InputStream input = optionsFile.openStream(); 128 try { 129 options.load(input); 130 System.out.println(" loaded"); } finally { 132 input.close(); 133 } 134 } catch (FileNotFoundException e) { 135 System.out.println(" not found"); } catch (IOException e) { 137 System.out.println(" did not parse"); e.printStackTrace(System.out); 139 } 140 for (Iterator i = options.keySet().iterator(); i.hasNext();) { 142 Object key = i.next(); 143 options.put(key, ((String ) options.get(key)).trim()); 144 } 145 if (options.size() == 0) 146 options = null; 147 } 148 } | Popular Tags |