KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > TracingOptionsManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.pde.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import java.util.zip.ZipFile JavaDoc;
27
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.core.plugin.PluginRegistry;
32
33 public class TracingOptionsManager {
34     private Properties JavaDoc template;
35
36     public TracingOptionsManager() {
37         super();
38     }
39
40     private void createTemplate() {
41         template = new Properties JavaDoc();
42         IPluginModelBase[] models = PluginRegistry.getAllModels();
43         for (int i = 0; i < models.length; i++) {
44             addToTemplate(models[i]);
45         }
46     }
47     
48     private void addToTemplate(IPluginModelBase model) {
49         Properties JavaDoc modelOptions = getOptions(model);
50         if (modelOptions == null)
51             return;
52         for (Enumeration JavaDoc keys = modelOptions.keys(); keys.hasMoreElements();) {
53             String JavaDoc key = keys.nextElement().toString();
54             String JavaDoc value = modelOptions.getProperty(key);
55             if (key != null && value != null)
56                 template.setProperty(key, value);
57         }
58     }
59     
60     public Hashtable JavaDoc getTemplateTable(String JavaDoc pluginId) {
61         if (template == null)
62             createTemplate();
63         Hashtable JavaDoc defaults = new Hashtable JavaDoc();
64         for (Enumeration JavaDoc keys = template.keys(); keys.hasMoreElements();) {
65             String JavaDoc key = keys.nextElement().toString();
66             if (belongsTo(key, pluginId)) {
67                 defaults.put(key, template.get(key));
68             }
69         }
70         return defaults;
71     }
72
73     private boolean belongsTo(String JavaDoc option, String JavaDoc pluginId) {
74         IPath path = new Path(option);
75         String JavaDoc firstSegment = path.segment(0);
76         return pluginId.equalsIgnoreCase(firstSegment);
77     }
78
79     public Properties JavaDoc getTracingOptions(Map JavaDoc storedOptions) {
80         // Start with the fresh template from plugins
81
Properties JavaDoc defaults = getTracingTemplateCopy();
82         if (storedOptions != null) {
83             // Load stored values, but only for existing keys
84
Iterator JavaDoc iter = storedOptions.keySet().iterator();
85             while (iter.hasNext()) {
86                 String JavaDoc key = iter.next().toString();
87                 if (defaults.containsKey(key)) {
88                     defaults.setProperty(key, (String JavaDoc) storedOptions.get(key));
89                 }
90             }
91         }
92         return defaults;
93     }
94
95     public Properties JavaDoc getTracingTemplateCopy() {
96         if (template == null)
97             createTemplate();
98         return (Properties JavaDoc) template.clone();
99     }
100
101     public static boolean isTraceable(IPluginModelBase model) {
102         String JavaDoc location = model.getInstallLocation();
103         if (location == null)
104             return false;
105         
106         File JavaDoc pluginLocation = new File JavaDoc(location);
107         InputStream JavaDoc stream = null;
108         ZipFile JavaDoc jarFile = null;
109         try {
110             if (pluginLocation.isDirectory())
111                 return new File JavaDoc(pluginLocation, ".options").exists(); //$NON-NLS-1$
112

113             jarFile = new ZipFile JavaDoc(pluginLocation, ZipFile.OPEN_READ);
114             ZipEntry JavaDoc manifestEntry = jarFile.getEntry(".options"); //$NON-NLS-1$
115
if (manifestEntry != null) {
116                 stream = jarFile.getInputStream(manifestEntry);
117             }
118         } catch (FileNotFoundException JavaDoc e) {
119         } catch (IOException JavaDoc e) {
120         } finally {
121             try {
122                 if (stream != null)
123                     stream.close();
124                 if (jarFile != null)
125                     jarFile.close();
126             } catch (IOException JavaDoc e) {
127             }
128         }
129         return stream != null;
130     }
131     
132     public void reset() {
133         template = null;
134     }
135
136     private void save(String JavaDoc fileName, Properties JavaDoc properties) {
137         try {
138             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(fileName);
139             properties.store(stream, "Master Tracing Options"); //$NON-NLS-1$
140
stream.flush();
141             stream.close();
142         } catch (IOException JavaDoc e) {
143             PDECore.logException(e);
144         }
145     }
146
147     public void save(String JavaDoc filename, Map JavaDoc map, HashSet JavaDoc selected) {
148         Properties JavaDoc properties = getTracingOptions(map);
149         for (Enumeration JavaDoc keys = properties.keys(); keys.hasMoreElements();) {
150             String JavaDoc key = keys.nextElement().toString();
151             Path path = new Path(key);
152             if (path.segmentCount() < 1 || !selected.contains(path.segment(0).toString())) {
153                 properties.remove(key);
154             }
155         }
156         save(filename, properties);
157     }
158
159     public void save(String JavaDoc filename, Map JavaDoc map) {
160         save(filename, getTracingOptions(map));
161     }
162     
163     private Properties JavaDoc getOptions(IPluginModelBase model) {
164         String JavaDoc location = model.getInstallLocation();
165         if (location == null)
166             return null;
167         
168         File JavaDoc pluginLocation = new File JavaDoc(location);
169         InputStream JavaDoc stream = null;
170         ZipFile JavaDoc jarFile = null;
171         try {
172             if (pluginLocation.isDirectory()) {
173                 File JavaDoc file = new File JavaDoc(pluginLocation, ".options"); //$NON-NLS-1$
174
if (file.exists())
175                     stream = new FileInputStream JavaDoc(file);
176             } else {
177                     jarFile = new ZipFile JavaDoc(pluginLocation, ZipFile.OPEN_READ);
178                     ZipEntry JavaDoc manifestEntry = jarFile.getEntry(".options"); //$NON-NLS-1$
179
if (manifestEntry != null) {
180                         stream = jarFile.getInputStream(manifestEntry);
181                     }
182             }
183             if (stream != null) {
184                 Properties JavaDoc modelOptions = new Properties JavaDoc();
185                 modelOptions.load(stream);
186                 return modelOptions;
187             }
188         } catch (FileNotFoundException JavaDoc e) {
189         } catch (IOException JavaDoc e) {
190         } finally {
191             try {
192                 if (stream != null)
193                     stream.close();
194                 if (jarFile != null)
195                     jarFile.close();
196             } catch (IOException JavaDoc e) {
197             }
198         }
199         return null;
200     }
201
202 }
203
Popular Tags