KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
19 import org.eclipse.osgi.service.debug.DebugOptions;
20
21 /**
22  * The DebugOptions class used by the framework to get debug options from.
23  * @since 3.1
24  */

25 public class FrameworkDebugOptions implements DebugOptions {
26     private Properties JavaDoc options = null;
27     private static FrameworkDebugOptions singleton = null;
28     private static boolean debugEnabled = true;
29     private static final String JavaDoc OPTIONS = ".options"; //$NON-NLS-1$
30

31     /**
32      * Returns the singleton instance of <code>FrameworkDebugOptions</code>. If
33      * debug is not enabled then <code>null</code> is returned.
34      * @return the instance of <code>FrameworkDebugOptions</code>
35      */

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 JavaDoc buildURL(String JavaDoc spec, boolean trailingSlash) {
47         if (spec == null)
48             return null;
49         boolean isFile = spec.startsWith("file:"); //$NON-NLS-1$
50
try {
51             if (isFile)
52                 return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash);
53             return new URL JavaDoc(spec);
54         } catch (MalformedURLException JavaDoc e) {
55             // if we failed and it is a file spec, there is nothing more we can do
56
// otherwise, try to make the spec into a file URL.
57
if (isFile)
58                 return null;
59             try {
60                 return adjustTrailingSlash(new File(spec).toURL(), trailingSlash);
61             } catch (MalformedURLException JavaDoc e1) {
62                 return null;
63             }
64         }
65     }
66
67     private static URL JavaDoc adjustTrailingSlash(URL JavaDoc url, boolean trailingSlash) throws MalformedURLException JavaDoc {
68         String JavaDoc file = url.getFile();
69         if (trailingSlash == (file.endsWith("/"))) //$NON-NLS-1$
70
return url;
71         file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); //$NON-NLS-1$
72
return new URL JavaDoc(url.getProtocol(), url.getHost(), file);
73     }
74
75     private FrameworkDebugOptions() {
76         super();
77         loadOptions();
78     }
79
80     /**
81      * @see DebugOptions#getBooleanOption(String, boolean)
82      */

83     public boolean getBooleanOption(String JavaDoc option, boolean defaultValue) {
84         String JavaDoc optionValue = getOption(option);
85         return (optionValue != null && optionValue.equalsIgnoreCase("true")) || defaultValue; //$NON-NLS-1$
86
}
87
88     /**
89      * @see DebugOptions#getOption(String)
90      */

91     public String JavaDoc getOption(String JavaDoc option) {
92         return options != null ? options.getProperty(option) : null;
93     }
94
95     /**
96      * @see DebugOptions#getOption(String, String)
97      */

98     public String JavaDoc getOption(String JavaDoc option, String JavaDoc defaultValue) {
99         return options != null ? options.getProperty(option, defaultValue) : defaultValue;
100     }
101
102     /**
103      * @see DebugOptions#getIntegerOption(String, int)
104      */

105     public int getIntegerOption(String JavaDoc option, int defaultValue) {
106         String JavaDoc value = getOption(option);
107         try {
108             return value == null ? defaultValue : Integer.parseInt(value);
109         } catch (NumberFormatException JavaDoc e) {
110             return defaultValue;
111         }
112     }
113
114     /**
115      * @see DebugOptions#setOption(String, String)
116      */

117     public void setOption(String JavaDoc option, String JavaDoc 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         // if no debug option was specified, don't even bother to try.
128
// Must ensure that the options slot is null as this is the signal to the
129
// platform that debugging is not enabled.
130
String JavaDoc debugOptionsFilename = FrameworkProperties.getProperty("osgi.debug"); //$NON-NLS-1$
131
if (debugOptionsFilename == null)
132             return;
133         options = new Properties JavaDoc();
134         URL JavaDoc optionsFile;
135         if (debugOptionsFilename.length() == 0) {
136             // default options location is user.dir (install location may be r/o so
137
// is not a good candidate for a trace options that need to be updatable by
138
// by the user)
139
String JavaDoc userDir = FrameworkProperties.getProperty("user.dir").replace(File.separatorChar, '/'); //$NON-NLS-1$
140
if (!userDir.endsWith("/")) //$NON-NLS-1$
141
userDir += "/"; //$NON-NLS-1$
142
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); //$NON-NLS-1$
147
return;
148         }
149         System.out.print("Debug options:\n " + optionsFile.toExternalForm()); //$NON-NLS-1$
150
try {
151             InputStream input = optionsFile.openStream();
152             try {
153                 options.load(input);
154                 System.out.println(" loaded"); //$NON-NLS-1$
155
} finally {
156                 input.close();
157             }
158         } catch (FileNotFoundException e) {
159             System.out.println(" not found"); //$NON-NLS-1$
160
} catch (IOException e) {
161             System.out.println(" did not parse"); //$NON-NLS-1$
162
e.printStackTrace(System.out);
163         }
164         // trim off all the blanks since properties files don't do that.
165
for (Iterator JavaDoc i = options.keySet().iterator(); i.hasNext();) {
166             Object JavaDoc key = i.next();
167             options.put(key, ((String JavaDoc) options.get(key)).trim());
168         }
169         if (options.size() == 0)
170             options = null;
171     }
172 }
173
Popular Tags