1 11 12 package org.eclipse.core.internal.registry; 13 14 import java.io.*; 15 import java.util.HashMap ; 16 import org.eclipse.core.internal.runtime.*; 17 import org.eclipse.core.runtime.*; 18 19 public class RegistryCacheWriter { 20 22 protected HashMap objectTable = null; 27 protected MultiStatus problems = null; 28 protected File cacheFile; 29 30 public RegistryCacheWriter(File cacheFile) { 31 super(); 32 this.cacheFile = cacheFile; 33 } 34 35 private int addToObjectTable(Object object) { 36 if (objectTable == null) { 37 objectTable = new HashMap (); 38 } 39 objectTable.put(object, new Integer (objectTable.size())); 40 return (objectTable.size() - 1); 42 } 43 44 private int getFromObjectTable(Object object) { 45 if (objectTable != null) { 46 Object objectResult = objectTable.get(object); 47 if (objectResult != null) { 48 return ((Integer ) objectResult).intValue(); 49 } 50 } 51 return -1; 52 } 53 54 public void writeConfigurationElement(ConfigurationElement object, DataOutputStream out) { 55 try { 56 writeCachedStringOrNull(object.getName(), out); 57 writeStringOrNull(object.getValue(), out); 58 59 ConfigurationProperty[] properties = object.getProperties(); 60 int length = (properties == null) ? 0 : properties.length; 61 out.writeInt(length); 62 for (int i = 0; i < length; i++) 63 writeConfigurationProperty(properties[i], out); 64 65 IConfigurationElement[] elements = object.getChildren(); 66 length = (elements == null) ? 0 : elements.length; 67 out.writeInt(length); 68 for (int i = 0; i < length; i++) 69 writeConfigurationElement((ConfigurationElement) elements[i], out); 70 } catch (IOException ioe) { 71 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "ConfigruationElement"), ioe)); } 73 } 74 75 public void writeConfigurationProperty(ConfigurationProperty object, DataOutputStream out) { 76 try { 77 writeCachedStringOrNull(object.getName(), out); 78 writeStringOrNull(object.getValue(), out); 79 } catch (IOException ioe) { 80 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "ConfigurationProperty"), ioe)); } 82 } 83 84 public void writeExtension(Extension object, DataOutputStream out) { 85 try { 86 if (writeIndex(object, out)) 87 return; 88 89 addToObjectTable(object); 91 out.writeByte(RegistryCacheReader.OBJECT); 92 writeStringOrNull(object.getSimpleIdentifier(), out); 93 writeBundleModel((Namespace) object.getParent(), out); 94 writeStringOrNull(object.getName(), out); 95 writeCachedStringOrNull(object.getExtensionPointIdentifier(), out); 96 writeSubElements(object, out); 97 } catch (IOException ioe) { 98 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "Extension"), ioe)); } 100 } 101 102 private void writeCachedStringOrNull(String string, DataOutputStream out) throws IOException { 103 if (string == null) 104 out.writeByte(RegistryCacheReader.NULL); 105 else { 106 int index = getFromObjectTable(string); 107 if (index == -1) { 108 addToObjectTable(string); 109 out.writeByte(RegistryCacheReader.OBJECT); 110 out.writeUTF(string); 111 } else { 112 out.writeByte(RegistryCacheReader.INDEX); 113 out.writeInt(index); 114 } 115 } 116 } 117 118 public void writeSubElements(Extension object, DataOutputStream out) throws IOException { 119 IConfigurationElement[] subElements = object.getConfigurationElements(); 120 if (subElements == null) { 121 out.writeByte(RegistryCacheReader.NULL); 122 return; 123 } 124 out.writeByte(RegistryCacheReader.OBJECT); 125 out.writeInt(out.size()); 127 out.writeInt(subElements.length); 128 for (int i = 0; i < subElements.length; i++) 129 writeConfigurationElement((ConfigurationElement) subElements[i], out); 130 } 131 132 public void writeExtensionPoint(ExtensionPoint object, DataOutputStream out) { 133 try { 134 if (writeIndex(object, out)) 135 return; 136 addToObjectTable(object); 138 out.writeByte(RegistryCacheReader.OBJECT); 139 writeStringOrNull(object.getSimpleIdentifier(), out); 140 writeStringOrNull(object.getName(), out); 141 writeStringOrNull(object.getSchema(), out); 142 143 IExtension[] extensions = object.getExtensions(); 145 int length = extensions == null ? 0 : extensions.length; 146 out.writeInt(length); 147 for (int i = 0; i < length; i++) 148 writeExtension((Extension) extensions[i], out); 149 } catch (IOException ioe) { 150 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "ExtensionPoint"), ioe)); } 152 } 153 154 public void writeHeaderInformation(long registryStamp, DataOutputStream out) { 155 try { 156 out.writeInt(RegistryCacheReader.REGISTRY_CACHE_VERSION); 157 out.writeLong(InternalPlatform.getDefault().getStateTimeStamp()); 158 out.writeLong(registryStamp); 159 InternalPlatform info = InternalPlatform.getDefault(); 160 out.writeUTF(info.getOS()); 161 out.writeUTF(info.getWS()); 162 out.writeUTF(info.getNL()); 163 } catch (IOException ioe) { 164 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "HeaderInformation"), ioe)); } 166 } 167 168 public void writeBundleModel(Namespace object, DataOutputStream out) { 169 try { 170 if (writeIndex(object, out)) 171 return; 172 173 addToObjectTable(object); 175 out.writeByte(RegistryCacheReader.OBJECT); 176 writeCachedStringOrNull(object.getUniqueIdentifier(), out); 177 out.writeLong(object.getId()); 178 writeRegistry((ExtensionRegistry) object.getParent(), out); 179 writeCachedStringOrNull(object.getHostIdentifier(), out); 180 181 IExtensionPoint[] extensionPoints = object.getExtensionPoints(); 184 int length = (extensionPoints == null) ? 0 : extensionPoints.length; 185 out.writeInt(length); 186 for (int i = 0; i < length; i++) 187 writeExtensionPoint((ExtensionPoint) extensionPoints[i], out); 188 189 IExtension[] extensions = object.getExtensions(); 191 length = (extensions == null) ? 0 : extensions.length; 192 out.writeInt(length); 193 for (int i = 0; i < length; i++) 194 writeExtension((Extension) extensions[i], out); 195 } catch (IOException ioe) { 196 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "Bundle"), ioe)); } 198 } 199 200 public void writeCache(ExtensionRegistry object, long registryStamp, DataOutputStream out) { 201 if (problems == null) 202 problems = new MultiStatus(Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.registryCacheWriteProblems"), null); writeHeaderInformation(registryStamp, out); 204 writeRegistry(object, out); 205 } 206 207 public void writeRegistry(ExtensionRegistry object, DataOutputStream out) { 208 try { 209 if (writeIndex(object, out)) 210 return; 211 addToObjectTable(object); 213 out.writeByte(RegistryCacheReader.OBJECT); 214 String [] ids = object.basicGetNamespaces(); 215 out.writeInt(ids.length); 216 for (int i = 0; i < ids.length; i++) 217 writeBundleModel(object.basicGetNamespace(ids[i]), out); 218 } catch (IOException ioe) { 219 problems.add(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, Policy.bind("meta.regCacheIOExceptionWriting", "ExtensionRegisry"), ioe)); } 221 } 222 223 private void writeStringOrNull(String string, DataOutputStream out) throws IOException { 224 if (string == null) 225 out.writeByte(RegistryCacheReader.NULL); 226 else { 227 out.writeByte(RegistryCacheReader.OBJECT); 228 out.writeUTF(string); 229 } 230 } 231 232 private boolean writeIndex(Object object, DataOutputStream out) throws IOException { 233 if (object == null) { 234 out.writeByte(RegistryCacheReader.NULL); 235 return true; 236 } 237 int index = getFromObjectTable(object); 238 if (index == -1) 239 return false; 240 out.writeByte(RegistryCacheReader.INDEX); 241 out.writeInt(index); 242 return true; 243 } 244 245 public void saveCache(ExtensionRegistry registry, long registryStamp) { 246 registry.enterRead(); 247 SafeFileOutputStream safeOut = null; 248 try { 249 safeOut = new SafeFileOutputStream(cacheFile); 250 DataOutputStream out = new DataOutputStream(safeOut); 251 try { 252 writeCache(registry, registryStamp, out); 253 } finally { 254 RegistryCacheReader reader = registry.getCacheReader(); 259 if (reader != null && reader.hasFailed()) 260 safeOut.close(true); 261 else 262 out.close(); 264 } 265 } catch (IOException e) { 266 if (InternalPlatform.DEBUG_REGISTRY) 269 e.printStackTrace(); 270 } finally { 271 registry.exitRead(); 272 } 273 } 274 275 public void saveCache(ExtensionRegistry registry) { 276 this.saveCache(registry, 0); 277 } 278 279 } | Popular Tags |