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 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 19 import org.eclipse.osgi.service.debug.DebugOptions; 20 21 25 public class FrameworkDebugOptions implements DebugOptions { 26 private Properties options = null; 27 private static FrameworkDebugOptions singleton = null; 28 private static boolean debugEnabled = true; 29 private static final String OPTIONS = ".options"; 31 36 public static FrameworkDebugOptions getDefault() { 37 if (singleton == null && debugEnabled) { 38 FrameworkDebugOptions result = new FrameworkDebugOptions(); 39 debugEnabled = result.isDebugEnabled(); 40 if (debugEnabled) 41 singleton = result; 42 } 43 return singleton; 44 } 45 46 private static URL buildURL(String spec, boolean trailingSlash) { 47 if (spec == null) 48 return null; 49 boolean isFile = spec.startsWith("file:"); try { 51 if (isFile) 52 return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash); 53 return new URL (spec); 54 } catch (MalformedURLException e) { 55 if (isFile) 58 return null; 59 try { 60 return adjustTrailingSlash(new File(spec).toURL(), trailingSlash); 61 } catch (MalformedURLException e1) { 62 return null; 63 } 64 } 65 } 66 67 private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException { 68 String file = url.getFile(); 69 if (trailingSlash == (file.endsWith("/"))) return url; 71 file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); return new URL (url.getProtocol(), url.getHost(), file); 73 } 74 75 private FrameworkDebugOptions() { 76 super(); 77 loadOptions(); 78 } 79 80 83 public boolean getBooleanOption(String option, boolean defaultValue) { 84 String optionValue = getOption(option); 85 return (optionValue != null && optionValue.equalsIgnoreCase("true")) || defaultValue; } 87 88 91 public String getOption(String option) { 92 return options != null ? options.getProperty(option) : null; 93 } 94 95 98 public String getOption(String option, String defaultValue) { 99 return options != null ? options.getProperty(option, defaultValue) : defaultValue; 100 } 101 102 105 public int getIntegerOption(String option, int defaultValue) { 106 String value = getOption(option); 107 try { 108 return value == null ? defaultValue : Integer.parseInt(value); 109 } catch (NumberFormatException e) { 110 return defaultValue; 111 } 112 } 113 114 117 public void setOption(String option, String value) { 118 if (options != null) 119 options.put(option, value.trim()); 120 } 121 122 private boolean isDebugEnabled() { 123 return options != null; 124 } 125 126 private void loadOptions() { 127 String debugOptionsFilename = FrameworkProperties.getProperty("osgi.debug"); if (debugOptionsFilename == null) 132 return; 133 options = new Properties (); 134 URL optionsFile; 135 if (debugOptionsFilename.length() == 0) { 136 String userDir = FrameworkProperties.getProperty("user.dir").replace(File.separatorChar, '/'); if (!userDir.endsWith("/")) userDir += "/"; debugOptionsFilename = new File(userDir, OPTIONS).toString(); 143 } 144 optionsFile = buildURL(debugOptionsFilename, false); 145 if (optionsFile == null) { 146 System.out.println("Unable to construct URL for options file: " + debugOptionsFilename); return; 148 } 149 System.out.print("Debug options:\n " + optionsFile.toExternalForm()); try { 151 InputStream input = optionsFile.openStream(); 152 try { 153 options.load(input); 154 System.out.println(" loaded"); } finally { 156 input.close(); 157 } 158 } catch (FileNotFoundException e) { 159 System.out.println(" not found"); } catch (IOException e) { 161 System.out.println(" did not parse"); e.printStackTrace(System.out); 163 } 164 for (Iterator i = options.keySet().iterator(); i.hasNext();) { 166 Object key = i.next(); 167 options.put(key, ((String ) options.get(key)).trim()); 168 } 169 if (options.size() == 0) 170 options = null; 171 } 172 } 173 | Popular Tags |