KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > RegistryCacheWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.internal.registry;
13
14 import java.io.*;
15 import java.util.HashMap JavaDoc;
16 import org.eclipse.core.internal.runtime.*;
17 import org.eclipse.core.runtime.*;
18
19 public class RegistryCacheWriter {
20     // See RegistryCacheReader for constants commonly used here too.
21

22     // objectTable will be a hashmap of objects. The objects will be things
23
// like a plugin descriptor, extension, extension point, etc. The integer
24
// index value will be used in the cache to allow cross-references in the
25
// cached registry.
26
protected HashMap JavaDoc 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 JavaDoc object) {
36         if (objectTable == null) {
37             objectTable = new HashMap JavaDoc();
38         }
39         objectTable.put(object, new Integer JavaDoc(objectTable.size()));
40         // return the index of the object just added (i.e. size - 1)
41
return (objectTable.size() - 1);
42     }
43
44     private int getFromObjectTable(Object JavaDoc object) {
45         if (objectTable != null) {
46             Object JavaDoc objectResult = objectTable.get(object);
47             if (objectResult != null) {
48                 return ((Integer JavaDoc) 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)); //$NON-NLS-1$ //$NON-NLS-2$
72
}
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)); //$NON-NLS-1$ //$NON-NLS-2$
81
}
82     }
83
84     public void writeExtension(Extension object, DataOutputStream out) {
85         try {
86             if (writeIndex(object, out))
87                 return;
88
89             // add this object to the object table first
90
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)); //$NON-NLS-1$ //$NON-NLS-2$
99
}
100     }
101
102     private void writeCachedStringOrNull(String JavaDoc 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         // write the offset for sub-elements data
126
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             // add this object to the object table first
137
addToObjectTable(object);
138             out.writeByte(RegistryCacheReader.OBJECT);
139             writeStringOrNull(object.getSimpleIdentifier(), out);
140             writeStringOrNull(object.getName(), out);
141             writeStringOrNull(object.getSchema(), out);
142
143             // Now do the extensions.
144
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)); //$NON-NLS-1$ //$NON-NLS-2$
151
}
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)); //$NON-NLS-1$ //$NON-NLS-2$
165
}
166     }
167
168     public void writeBundleModel(Namespace object, DataOutputStream out) {
169         try {
170             if (writeIndex(object, out))
171                 return;
172
173             // add this object to the object table first
174
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             // need to worry about cross links here
182
// now do extension points
183
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             // and then extensions
190
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)); //$NON-NLS-1$ //$NON-NLS-2$
197
}
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); //$NON-NLS-1$
203
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             // add this object to the object table first
212
addToObjectTable(object);
213             out.writeByte(RegistryCacheReader.OBJECT);
214             String JavaDoc[] 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)); //$NON-NLS-1$ //$NON-NLS-2$
220
}
221     }
222
223     private void writeStringOrNull(String JavaDoc 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 JavaDoc 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                 // at this point we have traversed the whole registry.
255
// if we failed lazily loading configuration elements,
256
// just discard the temporary file we have created
257
// to avoid overwriting the existing one with bogus data
258
RegistryCacheReader reader = registry.getCacheReader();
259                 if (reader != null && reader.hasFailed())
260                     safeOut.close(true);
261                 else
262                     // close the DataOutputStream normally
263
out.close();
264             }
265         } catch (IOException e) {
266             // an I/O failure would keep the extension elements unloaded
267
//TODO: log this exception?
268
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