001: /*******************************************************************************
002: * Copyright (c) 2000, 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.pde.internal.core;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.util.Enumeration;
019: import java.util.HashSet;
020: import java.util.Hashtable;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.zip.ZipEntry;
025: import java.util.zip.ZipFile;
026:
027: import org.eclipse.core.runtime.IPath;
028: import org.eclipse.core.runtime.Path;
029: import org.eclipse.pde.core.plugin.IPluginModelBase;
030: import org.eclipse.pde.core.plugin.PluginRegistry;
031:
032: public class TracingOptionsManager {
033: private Properties template;
034:
035: public TracingOptionsManager() {
036: super ();
037: }
038:
039: private void createTemplate() {
040: template = new Properties();
041: IPluginModelBase[] models = PluginRegistry.getAllModels();
042: for (int i = 0; i < models.length; i++) {
043: addToTemplate(models[i]);
044: }
045: }
046:
047: private void addToTemplate(IPluginModelBase model) {
048: Properties modelOptions = getOptions(model);
049: if (modelOptions == null)
050: return;
051: for (Enumeration keys = modelOptions.keys(); keys
052: .hasMoreElements();) {
053: String key = keys.nextElement().toString();
054: String value = modelOptions.getProperty(key);
055: if (key != null && value != null)
056: template.setProperty(key, value);
057: }
058: }
059:
060: public Hashtable getTemplateTable(String pluginId) {
061: if (template == null)
062: createTemplate();
063: Hashtable defaults = new Hashtable();
064: for (Enumeration keys = template.keys(); keys.hasMoreElements();) {
065: String key = keys.nextElement().toString();
066: if (belongsTo(key, pluginId)) {
067: defaults.put(key, template.get(key));
068: }
069: }
070: return defaults;
071: }
072:
073: private boolean belongsTo(String option, String pluginId) {
074: IPath path = new Path(option);
075: String firstSegment = path.segment(0);
076: return pluginId.equalsIgnoreCase(firstSegment);
077: }
078:
079: public Properties getTracingOptions(Map storedOptions) {
080: // Start with the fresh template from plugins
081: Properties defaults = getTracingTemplateCopy();
082: if (storedOptions != null) {
083: // Load stored values, but only for existing keys
084: Iterator iter = storedOptions.keySet().iterator();
085: while (iter.hasNext()) {
086: String key = iter.next().toString();
087: if (defaults.containsKey(key)) {
088: defaults.setProperty(key, (String) storedOptions
089: .get(key));
090: }
091: }
092: }
093: return defaults;
094: }
095:
096: public Properties getTracingTemplateCopy() {
097: if (template == null)
098: createTemplate();
099: return (Properties) template.clone();
100: }
101:
102: public static boolean isTraceable(IPluginModelBase model) {
103: String location = model.getInstallLocation();
104: if (location == null)
105: return false;
106:
107: File pluginLocation = new File(location);
108: InputStream stream = null;
109: ZipFile jarFile = null;
110: try {
111: if (pluginLocation.isDirectory())
112: return new File(pluginLocation, ".options").exists(); //$NON-NLS-1$
113:
114: jarFile = new ZipFile(pluginLocation, ZipFile.OPEN_READ);
115: ZipEntry manifestEntry = jarFile.getEntry(".options"); //$NON-NLS-1$
116: if (manifestEntry != null) {
117: stream = jarFile.getInputStream(manifestEntry);
118: }
119: } catch (FileNotFoundException e) {
120: } catch (IOException e) {
121: } finally {
122: try {
123: if (stream != null)
124: stream.close();
125: if (jarFile != null)
126: jarFile.close();
127: } catch (IOException e) {
128: }
129: }
130: return stream != null;
131: }
132:
133: public void reset() {
134: template = null;
135: }
136:
137: private void save(String fileName, Properties properties) {
138: try {
139: FileOutputStream stream = new FileOutputStream(fileName);
140: properties.store(stream, "Master Tracing Options"); //$NON-NLS-1$
141: stream.flush();
142: stream.close();
143: } catch (IOException e) {
144: PDECore.logException(e);
145: }
146: }
147:
148: public void save(String filename, Map map, HashSet selected) {
149: Properties properties = getTracingOptions(map);
150: for (Enumeration keys = properties.keys(); keys
151: .hasMoreElements();) {
152: String key = keys.nextElement().toString();
153: Path path = new Path(key);
154: if (path.segmentCount() < 1
155: || !selected.contains(path.segment(0).toString())) {
156: properties.remove(key);
157: }
158: }
159: save(filename, properties);
160: }
161:
162: public void save(String filename, Map map) {
163: save(filename, getTracingOptions(map));
164: }
165:
166: private Properties getOptions(IPluginModelBase model) {
167: String location = model.getInstallLocation();
168: if (location == null)
169: return null;
170:
171: File pluginLocation = new File(location);
172: InputStream stream = null;
173: ZipFile jarFile = null;
174: try {
175: if (pluginLocation.isDirectory()) {
176: File file = new File(pluginLocation, ".options"); //$NON-NLS-1$
177: if (file.exists())
178: stream = new FileInputStream(file);
179: } else {
180: jarFile = new ZipFile(pluginLocation, ZipFile.OPEN_READ);
181: ZipEntry manifestEntry = jarFile.getEntry(".options"); //$NON-NLS-1$
182: if (manifestEntry != null) {
183: stream = jarFile.getInputStream(manifestEntry);
184: }
185: }
186: if (stream != null) {
187: Properties modelOptions = new Properties();
188: modelOptions.load(stream);
189: return modelOptions;
190: }
191: } catch (FileNotFoundException e) {
192: } catch (IOException e) {
193: } finally {
194: try {
195: if (stream != null)
196: stream.close();
197: if (jarFile != null)
198: jarFile.close();
199: } catch (IOException e) {
200: }
201: }
202: return null;
203: }
204:
205: }
|