001: /*******************************************************************************
002: * Copyright (c) 2003, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.about;
011:
012: import java.io.BufferedReader;
013: import java.io.ByteArrayInputStream;
014: import java.io.ByteArrayOutputStream;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017: import java.io.PrintWriter;
018: import java.util.Comparator;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.Properties;
022: import java.util.SortedSet;
023: import java.util.TreeSet;
024:
025: import org.eclipse.core.runtime.CoreException;
026: import org.eclipse.core.runtime.IBundleGroup;
027: import org.eclipse.core.runtime.IBundleGroupProvider;
028: import org.eclipse.core.runtime.Platform;
029: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
030: import org.eclipse.core.runtime.preferences.IPreferencesService;
031: import org.eclipse.osgi.util.NLS;
032: import org.eclipse.ui.about.ISystemSummarySection;
033: import org.eclipse.ui.internal.WorkbenchMessages;
034: import org.eclipse.ui.internal.WorkbenchPlugin;
035: import org.eclipse.ui.internal.util.Util;
036: import org.osgi.framework.Bundle;
037:
038: /**
039: * This class puts basic platform information into the system summary log. This
040: * includes sections for the java properties, the ids of all installed features
041: * and plugins, as well as a the current contents of the preferences service.
042: *
043: * @since 3.0
044: */
045: public class ConfigurationLogDefaultSection implements
046: ISystemSummarySection {
047:
048: private static final String ECLIPSE_PROPERTY_PREFIX = "eclipse."; //$NON-NLS-1$
049:
050: /* (non-Javadoc)
051: * @see org.eclipse.ui.about.ISystemSummarySection#write(java.io.PrintWriter)
052: */
053: public void write(PrintWriter writer) {
054: appendProperties(writer);
055: appendFeatures(writer);
056: appendRegistry(writer);
057: appendUserPreferences(writer);
058: }
059:
060: /**
061: * Appends the <code>System</code> properties.
062: */
063: private void appendProperties(PrintWriter writer) {
064: writer.println();
065: writer
066: .println(WorkbenchMessages.SystemSummary_systemProperties);
067: Properties properties = System.getProperties();
068: SortedSet set = new TreeSet(new Comparator() {
069: public int compare(Object o1, Object o2) {
070: String s1 = (String) o1;
071: String s2 = (String) o2;
072: return s1.compareTo(s2);
073: }
074: });
075: set.addAll(properties.keySet());
076: Iterator i = set.iterator();
077: while (i.hasNext()) {
078: String key = (String) i.next();
079: String value = properties.getProperty(key);
080:
081: writer.print(key);
082: writer.print('=');
083:
084: // some types of properties have special characters embedded
085: if (key.startsWith(ECLIPSE_PROPERTY_PREFIX)) {
086: printEclipseProperty(writer, value);
087: } else if (key.toUpperCase().indexOf("PASSWORD") != -1) { //$NON-NLS-1$
088: // We should obscure any property that may be a password
089: for (int j = 0; j < value.length(); j++) {
090: writer.print('*');
091: }
092: writer.println();
093: } else {
094: writer.println(value);
095: }
096: }
097: }
098:
099: private static void printEclipseProperty(PrintWriter writer,
100: String value) {
101: String[] lines = Util.getArrayFromList(value, "\n"); //$NON-NLS-1$
102: for (int i = 0; i < lines.length; ++i) {
103: writer.println(lines[i]);
104: }
105: }
106:
107: /**
108: * Appends the installed and configured features.
109: */
110: private void appendFeatures(PrintWriter writer) {
111: writer.println();
112: writer.println(WorkbenchMessages.SystemSummary_features);
113:
114: IBundleGroupProvider[] providers = Platform
115: .getBundleGroupProviders();
116: LinkedList groups = new LinkedList();
117: if (providers != null) {
118: for (int i = 0; i < providers.length; ++i) {
119: IBundleGroup[] bundleGroups = providers[i]
120: .getBundleGroups();
121: for (int j = 0; j < bundleGroups.length; ++j) {
122: groups
123: .add(new AboutBundleGroupData(
124: bundleGroups[j]));
125: }
126: }
127: }
128: AboutBundleGroupData[] bundleGroupInfos = (AboutBundleGroupData[]) groups
129: .toArray(new AboutBundleGroupData[0]);
130:
131: AboutData.sortById(false, bundleGroupInfos);
132:
133: for (int i = 0; i < bundleGroupInfos.length; ++i) {
134: AboutBundleGroupData info = bundleGroupInfos[i];
135: String[] args = new String[] { info.getId(),
136: info.getVersion(), info.getName() };
137: writer.println(NLS.bind(
138: WorkbenchMessages.SystemSummary_featureVersion,
139: args));
140: }
141: }
142:
143: /**
144: * Appends the contents of the Plugin Registry.
145: */
146: private void appendRegistry(PrintWriter writer) {
147: writer.println();
148: writer.println(WorkbenchMessages.SystemSummary_pluginRegistry);
149:
150: Bundle[] bundles = WorkbenchPlugin.getDefault().getBundles();
151: AboutBundleData[] bundleInfos = new AboutBundleData[bundles.length];
152:
153: for (int i = 0; i < bundles.length; ++i) {
154: bundleInfos[i] = new AboutBundleData(bundles[i]);
155: }
156:
157: AboutData.sortById(false, bundleInfos);
158:
159: for (int i = 0; i < bundleInfos.length; ++i) {
160: AboutBundleData info = bundleInfos[i];
161: String[] args = new String[] { info.getId(),
162: info.getVersion(), info.getName(),
163: info.getStateName() };
164: writer
165: .println(NLS
166: .bind(
167: WorkbenchMessages.SystemSummary_descriptorIdVersionState,
168: args));
169: }
170: }
171:
172: /**
173: * Appends the preferences
174: */
175: private void appendUserPreferences(PrintWriter writer) {
176: // write the prefs to a byte array
177: IPreferencesService service = Platform.getPreferencesService();
178: IEclipsePreferences node = service.getRootNode();
179: ByteArrayOutputStream stm = new ByteArrayOutputStream();
180: try {
181: service.exportPreferences(node, stm, null);
182: } catch (CoreException e) {
183: writer.println("Error reading preferences " + e.toString());//$NON-NLS-1$
184: }
185:
186: // copy the prefs from the byte array to the writer
187: writer.println();
188: writer.println(WorkbenchMessages.SystemSummary_userPreferences);
189:
190: BufferedReader reader = null;
191: try {
192: ByteArrayInputStream in = new ByteArrayInputStream(stm
193: .toByteArray());
194: reader = new BufferedReader(new InputStreamReader(in,
195: "8859_1")); //$NON-NLS-1$
196: char[] chars = new char[8192];
197:
198: while (true) {
199: int read = reader.read(chars);
200: if (read <= 0) {
201: break;
202: }
203: writer.write(chars, 0, read);
204: }
205: } catch (IOException e) {
206: writer.println("Error reading preferences " + e.toString());//$NON-NLS-1$
207: }
208:
209: // ByteArray streams don't need to be closed
210: }
211: }
|