KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > about > ConfigurationLogDefaultSection


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.ui.internal.about;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.SortedSet JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IBundleGroup;
28 import org.eclipse.core.runtime.IBundleGroupProvider;
29 import org.eclipse.core.runtime.Platform;
30 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
31 import org.eclipse.core.runtime.preferences.IPreferencesService;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.ui.about.ISystemSummarySection;
34 import org.eclipse.ui.internal.WorkbenchMessages;
35 import org.eclipse.ui.internal.WorkbenchPlugin;
36 import org.eclipse.ui.internal.util.Util;
37 import org.osgi.framework.Bundle;
38
39 /**
40  * This class puts basic platform information into the system summary log. This
41  * includes sections for the java properties, the ids of all installed features
42  * and plugins, as well as a the current contents of the preferences service.
43  *
44  * @since 3.0
45  */

46 public class ConfigurationLogDefaultSection implements ISystemSummarySection {
47
48     private static final String JavaDoc ECLIPSE_PROPERTY_PREFIX = "eclipse."; //$NON-NLS-1$
49

50     /* (non-Javadoc)
51      * @see org.eclipse.ui.about.ISystemSummarySection#write(java.io.PrintWriter)
52      */

53     public void write(PrintWriter JavaDoc writer) {
54         appendProperties(writer);
55         appendFeatures(writer);
56         appendRegistry(writer);
57         appendUserPreferences(writer);
58     }
59
60     /**
61      * Appends the <code>System</code> properties.
62      */

63     private void appendProperties(PrintWriter JavaDoc writer) {
64         writer.println();
65         writer.println(WorkbenchMessages.SystemSummary_systemProperties);
66         Properties JavaDoc properties = System.getProperties();
67         SortedSet JavaDoc set = new TreeSet JavaDoc(new Comparator JavaDoc() {
68             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
69                 String JavaDoc s1 = (String JavaDoc) o1;
70                 String JavaDoc s2 = (String JavaDoc) o2;
71                 return s1.compareTo(s2);
72             }
73         });
74         set.addAll(properties.keySet());
75         Iterator JavaDoc i = set.iterator();
76         while (i.hasNext()) {
77             String JavaDoc key = (String JavaDoc)i.next();
78             String JavaDoc value = properties.getProperty(key);
79
80             writer.print(key);
81             writer.print('=');
82
83             // some types of properties have special characters embedded
84
if (key.startsWith(ECLIPSE_PROPERTY_PREFIX)) {
85                 printEclipseProperty(writer, value);
86             } else if (key.toUpperCase().indexOf("PASSWORD") != -1) { //$NON-NLS-1$
87
// We should obscure any property that may be a password
88
for (int j = 0; j < value.length(); j++) {
89                     writer.print('*');
90                 }
91                 writer.println();
92             } else {
93                 writer.println(value);
94             }
95         }
96     }
97
98     private static void printEclipseProperty(PrintWriter JavaDoc writer, String JavaDoc value) {
99         String JavaDoc[] lines = Util.getArrayFromList(value, "\n"); //$NON-NLS-1$
100
for (int i = 0; i < lines.length; ++i) {
101             writer.println(lines[i]);
102         }
103     }
104
105     /**
106      * Appends the installed and configured features.
107      */

108     private void appendFeatures(PrintWriter JavaDoc writer) {
109         writer.println();
110         writer.println(WorkbenchMessages.SystemSummary_features);
111
112         IBundleGroupProvider[] providers = Platform.getBundleGroupProviders();
113         LinkedList JavaDoc groups = new LinkedList JavaDoc();
114         if (providers != null) {
115             for (int i = 0; i < providers.length; ++i) {
116                 IBundleGroup[] bundleGroups = providers[i].getBundleGroups();
117                 for (int j = 0; j < bundleGroups.length; ++j) {
118                     groups.add(new AboutBundleGroupData(bundleGroups[j]));
119                 }
120             }
121         }
122         AboutBundleGroupData[] bundleGroupInfos = (AboutBundleGroupData[]) groups
123                 .toArray(new AboutBundleGroupData[0]);
124
125         AboutData.sortById(false, bundleGroupInfos);
126
127         for (int i = 0; i < bundleGroupInfos.length; ++i) {
128             AboutBundleGroupData info = bundleGroupInfos[i];
129             String JavaDoc[] args = new String JavaDoc[] { info.getId(), info.getVersion(),
130                     info.getName() };
131             writer.println(NLS.bind(WorkbenchMessages.SystemSummary_featureVersion, args));
132         }
133     }
134
135     /**
136      * Appends the contents of the Plugin Registry.
137      */

138     private void appendRegistry(PrintWriter JavaDoc writer) {
139         writer.println();
140         writer.println(WorkbenchMessages.SystemSummary_pluginRegistry);
141
142         Bundle[] bundles = WorkbenchPlugin.getDefault().getBundles();
143         AboutBundleData[] bundleInfos = new AboutBundleData[bundles.length];
144
145         for (int i = 0; i < bundles.length; ++i) {
146             bundleInfos[i] = new AboutBundleData(bundles[i]);
147         }
148
149         AboutData.sortById(false, bundleInfos);
150
151         for (int i = 0; i < bundleInfos.length; ++i) {
152             AboutBundleData info = bundleInfos[i];
153             String JavaDoc[] args = new String JavaDoc[] { info.getId(), info.getVersion(),
154                     info.getName(), info.getStateName() };
155             writer.println(NLS.bind(WorkbenchMessages.SystemSummary_descriptorIdVersionState, args));
156         }
157     }
158
159     /**
160      * Appends the preferences
161      */

162     private void appendUserPreferences(PrintWriter JavaDoc writer) {
163         // write the prefs to a byte array
164
IPreferencesService service = Platform.getPreferencesService();
165         IEclipsePreferences node = service.getRootNode();
166         ByteArrayOutputStream JavaDoc stm = new ByteArrayOutputStream JavaDoc();
167         try {
168             service.exportPreferences(node, stm, null);
169         } catch (CoreException e) {
170             writer.println("Error reading preferences " + e.toString());//$NON-NLS-1$
171
}
172
173         // copy the prefs from the byte array to the writer
174
writer.println();
175         writer.println(WorkbenchMessages.SystemSummary_userPreferences);
176
177         BufferedReader JavaDoc reader = null;
178         try {
179             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(stm
180                     .toByteArray());
181             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in, "8859_1")); //$NON-NLS-1$
182
char[] chars = new char[8192];
183
184             while (true) {
185                 int read = reader.read(chars);
186                 if (read <= 0) {
187                     break;
188                 }
189                 writer.write(chars, 0, read);
190             }
191         } catch (IOException JavaDoc e) {
192             writer.println("Error reading preferences " + e.toString());//$NON-NLS-1$
193
}
194
195         // ByteArray streams don't need to be closed
196
}
197 }
198
Popular Tags