1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 import java.util.zip.ZipEntry ; 26 import java.util.zip.ZipFile ; 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 template; 35 36 public TracingOptionsManager() { 37 super(); 38 } 39 40 private void createTemplate() { 41 template = new Properties (); 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 modelOptions = getOptions(model); 50 if (modelOptions == null) 51 return; 52 for (Enumeration keys = modelOptions.keys(); keys.hasMoreElements();) { 53 String key = keys.nextElement().toString(); 54 String value = modelOptions.getProperty(key); 55 if (key != null && value != null) 56 template.setProperty(key, value); 57 } 58 } 59 60 public Hashtable getTemplateTable(String pluginId) { 61 if (template == null) 62 createTemplate(); 63 Hashtable defaults = new Hashtable (); 64 for (Enumeration keys = template.keys(); keys.hasMoreElements();) { 65 String 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 option, String pluginId) { 74 IPath path = new Path(option); 75 String firstSegment = path.segment(0); 76 return pluginId.equalsIgnoreCase(firstSegment); 77 } 78 79 public Properties getTracingOptions(Map storedOptions) { 80 Properties defaults = getTracingTemplateCopy(); 82 if (storedOptions != null) { 83 Iterator iter = storedOptions.keySet().iterator(); 85 while (iter.hasNext()) { 86 String key = iter.next().toString(); 87 if (defaults.containsKey(key)) { 88 defaults.setProperty(key, (String ) storedOptions.get(key)); 89 } 90 } 91 } 92 return defaults; 93 } 94 95 public Properties getTracingTemplateCopy() { 96 if (template == null) 97 createTemplate(); 98 return (Properties ) template.clone(); 99 } 100 101 public static boolean isTraceable(IPluginModelBase model) { 102 String location = model.getInstallLocation(); 103 if (location == null) 104 return false; 105 106 File pluginLocation = new File (location); 107 InputStream stream = null; 108 ZipFile jarFile = null; 109 try { 110 if (pluginLocation.isDirectory()) 111 return new File (pluginLocation, ".options").exists(); 113 jarFile = new ZipFile (pluginLocation, ZipFile.OPEN_READ); 114 ZipEntry manifestEntry = jarFile.getEntry(".options"); if (manifestEntry != null) { 116 stream = jarFile.getInputStream(manifestEntry); 117 } 118 } catch (FileNotFoundException e) { 119 } catch (IOException e) { 120 } finally { 121 try { 122 if (stream != null) 123 stream.close(); 124 if (jarFile != null) 125 jarFile.close(); 126 } catch (IOException e) { 127 } 128 } 129 return stream != null; 130 } 131 132 public void reset() { 133 template = null; 134 } 135 136 private void save(String fileName, Properties properties) { 137 try { 138 FileOutputStream stream = new FileOutputStream (fileName); 139 properties.store(stream, "Master Tracing Options"); stream.flush(); 141 stream.close(); 142 } catch (IOException e) { 143 PDECore.logException(e); 144 } 145 } 146 147 public void save(String filename, Map map, HashSet selected) { 148 Properties properties = getTracingOptions(map); 149 for (Enumeration keys = properties.keys(); keys.hasMoreElements();) { 150 String 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 filename, Map map) { 160 save(filename, getTracingOptions(map)); 161 } 162 163 private Properties getOptions(IPluginModelBase model) { 164 String location = model.getInstallLocation(); 165 if (location == null) 166 return null; 167 168 File pluginLocation = new File (location); 169 InputStream stream = null; 170 ZipFile jarFile = null; 171 try { 172 if (pluginLocation.isDirectory()) { 173 File file = new File (pluginLocation, ".options"); if (file.exists()) 175 stream = new FileInputStream (file); 176 } else { 177 jarFile = new ZipFile (pluginLocation, ZipFile.OPEN_READ); 178 ZipEntry manifestEntry = jarFile.getEntry(".options"); if (manifestEntry != null) { 180 stream = jarFile.getInputStream(manifestEntry); 181 } 182 } 183 if (stream != null) { 184 Properties modelOptions = new Properties (); 185 modelOptions.load(stream); 186 return modelOptions; 187 } 188 } catch (FileNotFoundException e) { 189 } catch (IOException e) { 190 } finally { 191 try { 192 if (stream != null) 193 stream.close(); 194 if (jarFile != null) 195 jarFile.close(); 196 } catch (IOException e) { 197 } 198 } 199 return null; 200 } 201 202 } 203 | Popular Tags |