KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > debug > DebugOptions


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.debug;
12
13 import java.io.*;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 public class DebugOptions implements org.eclipse.osgi.service.debug.DebugOptions {
20     Properties JavaDoc options = null;
21
22     private static DebugOptions singleton = null;
23     private static boolean debugEnabled = true;
24     private static final String JavaDoc OPTIONS = ".options"; //$NON-NLS-1$
25

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 JavaDoc buildURL(String JavaDoc spec, boolean trailingSlash) {
37         if (spec == null)
38             return null;
39         boolean isFile = spec.startsWith("file:"); //$NON-NLS-1$
40
try {
41             if (isFile)
42                 return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash);
43             else
44                 return new URL JavaDoc(spec);
45         } catch (MalformedURLException JavaDoc e) {
46             // if we failed and it is a file spec, there is nothing more we can do
47
// otherwise, try to make the spec into a file URL.
48
if (isFile)
49                 return null;
50             try {
51                 return adjustTrailingSlash(new File(spec).toURL(), trailingSlash);
52             } catch (MalformedURLException JavaDoc e1) {
53                 return null;
54             }
55         }
56     }
57
58     private static URL JavaDoc adjustTrailingSlash(URL JavaDoc url, boolean trailingSlash) throws MalformedURLException JavaDoc {
59         String JavaDoc file = url.getFile();
60         if (trailingSlash == (file.endsWith("/"))) //$NON-NLS-1$
61
return url;
62         file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); //$NON-NLS-1$
63
return new URL JavaDoc(url.getProtocol(), url.getHost(), file);
64     }
65
66     private DebugOptions() {
67         super();
68         loadOptions();
69     }
70
71     public boolean getBooleanOption(String JavaDoc option, boolean defaultValue) {
72         String JavaDoc optionValue = getOption(option);
73         return (optionValue != null && optionValue.equalsIgnoreCase("true")) || defaultValue; //$NON-NLS-1$
74
}
75
76     public String JavaDoc getOption(String JavaDoc option) {
77         return options != null ? options.getProperty(option) : null;
78     }
79
80     public String JavaDoc getOption(String JavaDoc option, String JavaDoc defaultValue) {
81         return options != null ? options.getProperty(option, defaultValue) : defaultValue;
82     }
83
84     public int getIntegerOption(String JavaDoc option, int defaultValue) {
85         String JavaDoc value = getOption(option);
86         try {
87             return value == null ? defaultValue : Integer.parseInt(value);
88         } catch (NumberFormatException JavaDoc e) {
89             return defaultValue;
90         }
91     }
92
93     public void setOption(String JavaDoc option, String JavaDoc 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         // if no debug option was specified, don't even bother to try.
104
// Must ensure that the options slot is null as this is the signal to the
105
// platform that debugging is not enabled.
106
String JavaDoc debugOptionsFilename = System.getProperty("osgi.debug"); //$NON-NLS-1$
107
if (debugOptionsFilename == null)
108             return;
109         options = new Properties JavaDoc();
110         URL JavaDoc optionsFile;
111         if (debugOptionsFilename.length() == 0) {
112             // default options location is user.dir (install location may be r/o so
113
// is not a good candidate for a trace options that need to be updatable by
114
// by the user)
115
String JavaDoc userDir = System.getProperty("user.dir").replace(File.separatorChar, '/'); //$NON-NLS-1$
116
if (!userDir.endsWith("/")) //$NON-NLS-1$
117
userDir += "/"; //$NON-NLS-1$
118
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); //$NON-NLS-1$
123
return;
124         }
125         System.out.print("Debug options:\n " + optionsFile.toExternalForm()); //$NON-NLS-1$
126
try {
127             InputStream input = optionsFile.openStream();
128             try {
129                 options.load(input);
130                 System.out.println(" loaded"); //$NON-NLS-1$
131
} finally {
132                 input.close();
133             }
134         } catch (FileNotFoundException e) {
135             System.out.println(" not found"); //$NON-NLS-1$
136
} catch (IOException e) {
137             System.out.println(" did not parse"); //$NON-NLS-1$
138
e.printStackTrace(System.out);
139         }
140         // trim off all the blanks since properties files don't do that.
141
for (Iterator JavaDoc i = options.keySet().iterator(); i.hasNext();) {
142             Object JavaDoc key = i.next();
143             options.put(key, ((String JavaDoc) options.get(key)).trim());
144         }
145         if (options.size() == 0)
146             options = null;
147     }
148 }
Popular Tags