Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 829d2bccb3623f634eea08a4209890d16cc0ab0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.registry;

import java.io.*;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.spi.RegistryContributor;
import org.eclipse.osgi.util.NLS;

public class TableReader {
	//Markers in the cache 
	static final int NULL = 0;
	static final int OBJECT = 1;
	static final int LOBJECT = 2;

	//The version of the cache
	static final int CACHE_VERSION = 8;
	// Version 1 -> 2: the contributor Ids changed from "long" to "String"
	// Version 2 -> 3: added namespace index and the table of contributors
	// Version 3 -> 4: offset table saved in a binary form (performance)
	// Version 4 -> 5: remove support added in version 4 to save offset table in a binary form (performance)
	// Version 5 -> 6: replace HashtableOfInt with OffsetTable (memory usage optimization)
	// Version 6 -> 7: added option for multi-language support
	// Version 7 -> 8: added support for large UTF-8 strings

	//Informations representing the MAIN file
	static final String MAIN = ".mainData"; //$NON-NLS-1$
	BufferedRandomInputStream mainDataFile = null;
	DataInputStream mainInput = null;

	//Informations representing the EXTRA file
	static final String EXTRA = ".extraData"; //$NON-NLS-1$
	BufferedRandomInputStream extraDataFile = null;
	DataInputStream extraInput = null;

	//The table file
	static final String TABLE = ".table"; //$NON-NLS-1$
	File tableFile;

	//The contributions file
	static final String CONTRIBUTIONS = ".contributions"; //$NON-NLS-1$
	File contributionsFile;

	//The contributor file
	static final String CONTRIBUTORS = ".contributors"; //$NON-NLS-1$
	File contributorsFile;

	//The namespace file
	static final String NAMESPACES = ".namespaces"; //$NON-NLS-1$
	File namespacesFile;

	//The orphan file
	static final String ORPHANS = ".orphans"; //$NON-NLS-1$
	File orphansFile;

	static final String UTF_8 = "UTF-8"; //$NON-NLS-1$

	//Status code
	private static final byte fileError = 0;
	private static final boolean DEBUG = false; //TODO need to change

	private boolean holdObjects = false;

	private ExtensionRegistry registry;

	private SoftReference stringPool;

	void setMainDataFile(File main) throws IOException {
		mainDataFile = new BufferedRandomInputStream(main);
		mainInput = new DataInputStream(mainDataFile);
	}

	void setExtraDataFile(File extra) throws IOException {
		extraDataFile = new BufferedRandomInputStream(extra);
		extraInput = new DataInputStream(extraDataFile);
	}

	void setTableFile(File table) {
		tableFile = table;
	}

	void setContributionsFile(File namespace) {
		contributionsFile = namespace;
	}

	void setContributorsFile(File file) {
		contributorsFile = file;
	}

	void setNamespacesFile(File file) {
		namespacesFile = file;
	}

	void setOrphansFile(File orphan) {
		orphansFile = orphan;
	}

	public TableReader(ExtensionRegistry registry) {
		this.registry = registry;
	}

	// Don't need to synchronize - called only from a synchronized method
	public Object[] loadTables(long expectedTimestamp) {
		HashtableOfStringAndInt extensionPoints;

		DataInputStream tableInput = null;
		try {
			tableInput = new DataInputStream(new BufferedInputStream(new FileInputStream(tableFile)));
			if (!checkCacheValidity(tableInput, expectedTimestamp))
				return null;

			Integer nextId = new Integer(tableInput.readInt());
			OffsetTable offsets = OffsetTable.load(tableInput);
			extensionPoints = new HashtableOfStringAndInt();
			extensionPoints.load(tableInput);
			return new Object[] {offsets, extensionPoints, nextId};
		} catch (IOException e) {
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, RegistryMessages.meta_registryCacheReadProblems, e));
			return null;
		} finally {
			if (tableInput != null)
				try {
					tableInput.close();
				} catch (IOException e1) {
					//Ignore
				}
		}

	}

	//	Check various aspect of the cache to see if it's valid 
	private boolean checkCacheValidity(DataInputStream in, long expectedTimestamp) {
		int version;
		try {
			version = in.readInt();
			if (version != CACHE_VERSION)
				return false;

			long installStamp = in.readLong();
			long registryStamp = in.readLong();
			long mainDataFileSize = in.readLong();
			long extraDataFileSize = in.readLong();
			long contributionsFileSize = in.readLong();
			long contributorsFileSize = in.readLong();
			long namespacesFileSize = in.readLong();
			long orphansFileSize = in.readLong();
			String osStamp = readUTF(in, OBJECT);
			String windowsStamp = readUTF(in, OBJECT);
			String localeStamp = readUTF(in, OBJECT);
			boolean multiLanguage = in.readBoolean();

			boolean validTime = (expectedTimestamp == 0 || expectedTimestamp == registryStamp);
			boolean validInstall = (installStamp == registry.computeState());
			boolean validOS = (osStamp.equals(RegistryProperties.getProperty(IRegistryConstants.PROP_OS, RegistryProperties.empty)));
			boolean validWS = (windowsStamp.equals(RegistryProperties.getProperty(IRegistryConstants.PROP_WS, RegistryProperties.empty)));
			boolean validNL = (localeStamp.equals(RegistryProperties.getProperty(IRegistryConstants.PROP_NL, RegistryProperties.empty)));
			boolean validMultiLang = (registry.isMultiLanguage() == multiLanguage);

			if (!validTime || !validInstall || !validOS || !validWS || !validNL || !validMultiLang)
				return false;

			boolean validMain = (mainDataFileSize == mainDataFile.length());
			boolean validExtra = (extraDataFileSize == extraDataFile.length());
			boolean validContrib = (contributionsFileSize == contributionsFile.length());
			boolean validContributors = (contributorsFileSize == contributorsFile.length());
			boolean validNamespace = (namespacesFileSize == namespacesFile.length());
			boolean validOrphan = (orphansFileSize == orphansFile.length());

			return (validMain && validExtra && validContrib && validContributors && validNamespace && validOrphan);
		} catch (IOException e) {
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, RegistryMessages.meta_registryCacheInconsistent, e));
			return false;
		}
	}

	public Object loadConfigurationElement(int offset) {
		try {
			synchronized (mainDataFile) {
				goToInputFile(offset);
				return basicLoadConfigurationElement(mainInput, null);
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, mainDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading a configuration element (" + offset + ") from the registry cache", e)); //$NON-NLS-1$//$NON-NLS-2$
			return null;
		}
	}

	private ConfigurationElement basicLoadConfigurationElement(DataInputStream is, String actualContributorId) throws IOException {
		int self = is.readInt();
		String contributorId = readStringOrNull(is);
		String name = readStringOrNull(is);
		int parentId = is.readInt();
		byte parentType = is.readByte();
		int misc = is.readInt();//this is set in second level CEs, to indicate where in the extra data file the children CEs are
		String[] propertiesAndValue = readPropertiesAndValue(is);
		int[] children = readArray(is);
		if (actualContributorId == null)
			actualContributorId = contributorId;
		ConfigurationElement result = getObjectFactory().createConfigurationElement(self, actualContributorId, name, propertiesAndValue, children, misc, parentId, parentType, true);
		if (registry.isMultiLanguage()) { // cache is multi-language too or it would have failed validation
			int numberOfLocales = is.readInt();
			DirectMap translated = null;
			if (numberOfLocales != 0) {
				translated = new DirectMap(numberOfLocales, 0.5f);
				String[] NLs = readStringArray(is);
				for (int i = 0; i < numberOfLocales; i++) {
					String[] translatedProperties = readStringArray(is);
					translated.put(NLs[i], translatedProperties);
				}
			}
			ConfigurationElementMulti multiCE = (ConfigurationElementMulti) result;
			if (translated != null)
				multiCE.setTranslatedProperties(translated);
		}
		return result;
	}

	private String[] readStringArray(DataInputStream is) throws IOException {
		int size = is.readInt();
		if (size == 0)
			return null;
		String[] result = new String[size];
		for (int i = 0; i < size; i++) {
			result[i] = readStringOrNull(is);
		}
		return result;
	}

	public Object loadThirdLevelConfigurationElements(int offset, RegistryObjectManager objectManager) {
		try {
			synchronized (extraDataFile) {
				goToExtraFile(offset);
				return loadConfigurationElementAndChildren(null, extraInput, 3, Integer.MAX_VALUE, objectManager, null);
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, extraDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading a third level configuration element (" + offset + ") from the registry cache", e)); //$NON-NLS-1$//$NON-NLS-2$
			return null;
		}
	}

	//Read a whole configuration element subtree
	private ConfigurationElement loadConfigurationElementAndChildren(DataInputStream is, DataInputStream extraIs, int depth, int maxDepth, RegistryObjectManager objectManager, String namespaceOwnerId) throws IOException {
		DataInputStream currentStream = is;
		if (depth > 2)
			currentStream = extraIs;

		ConfigurationElement ce = basicLoadConfigurationElement(currentStream, namespaceOwnerId);
		if (namespaceOwnerId == null)
			namespaceOwnerId = ce.getContributorId();
		int[] children = ce.getRawChildren();
		if (depth + 1 > maxDepth)
			return ce;

		for (int i = 0; i < children.length; i++) {
			ConfigurationElement tmp = loadConfigurationElementAndChildren(currentStream, extraIs, depth + 1, maxDepth, objectManager, namespaceOwnerId);
			objectManager.add(tmp, holdObjects);
		}
		return ce;
	}

	private String[] readPropertiesAndValue(DataInputStream inputStream) throws IOException {
		int numberOfProperties = inputStream.readInt();
		if (numberOfProperties == 0)
			return RegistryObjectManager.EMPTY_STRING_ARRAY;
		String[] properties = new String[numberOfProperties];
		for (int i = 0; i < numberOfProperties; i++) {
			properties[i] = readStringOrNull(inputStream);
		}
		return properties;
	}

	public Object loadExtension(int offset) {
		try {
			synchronized (mainDataFile) {
				goToInputFile(offset);
				return basicLoadExtension(mainInput);
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, mainDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading an extension (" + offset + ") from the registry cache", e)); //$NON-NLS-1$//$NON-NLS-2$
		}
		return null;
	}

	private Extension basicLoadExtension(DataInputStream inputStream) throws IOException {
		int self = inputStream.readInt();
		String simpleId = readStringOrNull(mainInput);
		String namespace = readStringOrNull(mainInput);
		int[] children = readArray(mainInput);
		int extraData = mainInput.readInt();
		return getObjectFactory().createExtension(self, simpleId, namespace, children, extraData, true);
	}

	public ExtensionPoint loadExtensionPointTree(int offset, RegistryObjectManager objects) {
		try {
			synchronized (mainDataFile) {
				ExtensionPoint xpt = (ExtensionPoint) loadExtensionPoint(offset);
				int[] children = xpt.getRawChildren();
				int nbrOfExtension = children.length;
				for (int i = 0; i < nbrOfExtension; i++) {
					Extension loaded = basicLoadExtension(mainInput);
					objects.add(loaded, holdObjects);
				}

				for (int i = 0; i < nbrOfExtension; i++) {
					int nbrOfCe = mainInput.readInt();
					for (int j = 0; j < nbrOfCe; j++) {
						// note that max depth is set to 2 and extra input is never going to 
						// be used in this call to the loadConfigurationElementAndChildren().
						objects.add(loadConfigurationElementAndChildren(mainInput, null, 1, 2, objects, null), holdObjects);
					}
				}
				return xpt;
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, mainDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading an extension point tree (" + offset + ") from the registry cache", e)); //$NON-NLS-1$//$NON-NLS-2$
			return null;
		}
	}

	private Object loadExtensionPoint(int offset) {
		try {
			goToInputFile(offset);
			return basicLoadExtensionPoint();
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, mainDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading an extension point (" + offset + ") from the registry cache", e)); //$NON-NLS-1$ //$NON-NLS-2$
			return null;
		}
	}

	private ExtensionPoint basicLoadExtensionPoint() throws IOException {
		int self = mainInput.readInt();
		int[] children = readArray(mainInput);
		int extraData = mainInput.readInt();
		return getObjectFactory().createExtensionPoint(self, children, extraData, true);
	}

	private int[] readArray(DataInputStream in) throws IOException {
		int arraySize = in.readInt();
		if (arraySize == 0)
			return RegistryObjectManager.EMPTY_INT_ARRAY;
		int[] result = new int[arraySize];
		for (int i = 0; i < arraySize; i++) {
			result[i] = in.readInt();
		}
		return result;
	}

	private void goToInputFile(int offset) throws IOException {
		mainDataFile.seek(offset);
	}

	private void goToExtraFile(int offset) throws IOException {
		extraDataFile.seek(offset);
	}

	private String readStringOrNull(DataInputStream in) throws IOException {
		byte type = in.readByte();
		if (type == NULL)
			return null;
		return readUTF(in, type);
	}

	public String[] loadExtensionExtraData(int dataPosition) {
		try {
			synchronized (extraDataFile) {
				goToExtraFile(dataPosition);
				return basicLoadExtensionExtraData();
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, extraDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading extension label (" + dataPosition + ") from the registry cache", e)); //$NON-NLS-1$ //$NON-NLS-2$
			return null;
		}
	}

	private String[] basicLoadExtensionExtraData() throws IOException {
		return new String[] {readStringOrNull(extraInput), readStringOrNull(extraInput), readStringOrNull(extraInput)};
	}

	public String[] loadExtensionPointExtraData(int offset) {
		try {
			synchronized (extraDataFile) {
				goToExtraFile(offset);
				return basicLoadExtensionPointExtraData();
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, extraDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			if (DEBUG)
				log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, "Error reading extension point data (" + offset + ") from the registry cache", e)); //$NON-NLS-1$ //$NON-NLS-2$
			return null;
		}
	}

	private String[] basicLoadExtensionPointExtraData() throws IOException {
		String[] result = new String[5];
		result[0] = readStringOrNull(extraInput); //the label
		result[1] = readStringOrNull(extraInput); //the schema
		result[2] = readStringOrNull(extraInput); //the fully qualified name
		result[3] = readStringOrNull(extraInput); //the namespace
		result[4] = readStringOrNull(extraInput); //the contributor Id 
		return result;
	}

	public KeyedHashSet loadContributions() {
		DataInputStream namespaceInput = null;
		try {
			synchronized (contributionsFile) {
				namespaceInput = new DataInputStream(new BufferedInputStream(new FileInputStream(contributionsFile)));
				int size = namespaceInput.readInt();
				KeyedHashSet result = new KeyedHashSet(size);
				for (int i = 0; i < size; i++) {
					String contributorId = readStringOrNull(namespaceInput);
					Contribution n = getObjectFactory().createContribution(contributorId, true);
					n.setRawChildren(readArray(namespaceInput));
					result.add(n);
				}
				return result;
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, contributionsFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			return null;
		} finally {
			if (namespaceInput != null)
				try {
					namespaceInput.close();
				} catch (IOException e1) {
					//Ignore
				}
		}
	}

	final static float contributorsLoadFactor = 1.2f; // allocate more memory to avoid resizing

	public HashMap loadContributors() {
		HashMap result = null;
		DataInputStream contributorsInput = null;
		try {
			synchronized (contributorsFile) {
				contributorsInput = new DataInputStream(new BufferedInputStream(new FileInputStream(contributorsFile)));
				int size = contributorsInput.readInt();
				result = new HashMap((int) (size * contributorsLoadFactor));
				for (int i = 0; i < size; i++) {
					String id = readStringOrNull(contributorsInput);
					String name = readStringOrNull(contributorsInput);
					String hostId = readStringOrNull(contributorsInput);
					String hostName = readStringOrNull(contributorsInput);
					result.put(id, new RegistryContributor(id, name, hostId, hostName));
				}
			}
			return result;
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, contributorsFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			return null;
		} finally {
			if (contributorsInput != null)
				try {
					contributorsInput.close();
				} catch (IOException e1) {
					//Ignore
				}
		}
	}

	public KeyedHashSet loadNamespaces() {
		DataInputStream namespaceInput = null;
		try {
			synchronized (namespacesFile) {
				namespaceInput = new DataInputStream(new BufferedInputStream(new FileInputStream(namespacesFile)));
				int size = namespaceInput.readInt();
				KeyedHashSet result = new KeyedHashSet(size);
				for (int i = 0; i < size; i++) {
					String key = readStringOrNull(namespaceInput);
					RegistryIndexElement indexElement = new RegistryIndexElement(key);
					indexElement.updateExtensionPoints(readArray(namespaceInput), true);
					indexElement.updateExtensions(readArray(namespaceInput), true);
					result.add(indexElement);
				}
				return result;
			}
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, namespacesFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			return null;
		} finally {
			if (namespaceInput != null)
				try {
					namespaceInput.close();
				} catch (IOException e1) {
					//Ignore
				}
		}
	}

	private void loadAllOrphans(RegistryObjectManager objectManager) throws IOException {
		//Read the extensions and configuration elements of the orphans
		int orphans = objectManager.getOrphanExtensions().size();
		for (int k = 0; k < orphans; k++) {
			int numberOfOrphanExtensions = mainInput.readInt();
			for (int i = 0; i < numberOfOrphanExtensions; i++) {
				loadFullExtension(objectManager);
			}
			for (int i = 0; i < numberOfOrphanExtensions; i++) {
				int nbrOfCe = mainInput.readInt();
				for (int j = 0; j < nbrOfCe; j++) {
					objectManager.add(loadConfigurationElementAndChildren(mainInput, extraInput, 1, Integer.MAX_VALUE, objectManager, null), true);
				}
			}
		}
	}

	// Do not need to synchronize - called only from a synchronized method
	public boolean readAllCache(RegistryObjectManager objectManager) {
		try {
			int size = objectManager.getExtensionPoints().size();
			for (int i = 0; i < size; i++) {
				objectManager.add(readAllExtensionPointTree(objectManager), holdObjects);
			}
			loadAllOrphans(objectManager);
		} catch (IOException e) {
			String message = NLS.bind(RegistryMessages.meta_regCacheIOExceptionReading, mainDataFile);
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, message, e));
			return false;
		}
		return true;
	}

	private ExtensionPoint readAllExtensionPointTree(RegistryObjectManager objectManager) throws IOException {
		ExtensionPoint xpt = loadFullExtensionPoint();
		int[] children = xpt.getRawChildren();
		int nbrOfExtension = children.length;
		for (int i = 0; i < nbrOfExtension; i++) {
			loadFullExtension(objectManager);
		}

		for (int i = 0; i < nbrOfExtension; i++) {
			int nbrOfCe = mainInput.readInt();
			for (int j = 0; j < nbrOfCe; j++) {
				objectManager.add(loadConfigurationElementAndChildren(mainInput, extraInput, 1, Integer.MAX_VALUE, objectManager, null), true);
			}
		}
		return xpt;
	}

	private ExtensionPoint loadFullExtensionPoint() throws IOException { //TODO I don't like this. 
		ExtensionPoint xpt = basicLoadExtensionPoint();
		String[] tmp = basicLoadExtensionPointExtraData();
		xpt.setLabel(tmp[0]);
		xpt.setSchema(tmp[1]);
		xpt.setUniqueIdentifier(tmp[2]);
		xpt.setNamespace(tmp[3]);
		xpt.setContributorId(tmp[4]);
		return xpt;
	}

	private Extension loadFullExtension(RegistryObjectManager objectManager) throws IOException {
		String[] tmp;
		Extension loaded = basicLoadExtension(mainInput);
		tmp = basicLoadExtensionExtraData();
		loaded.setLabel(tmp[0]);
		loaded.setExtensionPointIdentifier(tmp[1]);
		loaded.setContributorId(tmp[2]);
		objectManager.add(loaded, holdObjects);
		return loaded;
	}

	public HashMap loadOrphans() {
		DataInputStream orphanInput = null;
		try {
			synchronized (orphansFile) {
				orphanInput = new DataInputStream(new BufferedInputStream(new FileInputStream(orphansFile)));
				int size = orphanInput.readInt();
				HashMap result = new HashMap(size);
				for (int i = 0; i < size; i++) {
					String key = readUTF(orphanInput, OBJECT);
					int[] value = readArray(orphanInput);
					result.put(key, value);
				}
				return result;
			}
		} catch (IOException e) {
			return null;
		} finally {
			if (orphanInput != null)
				try {
					orphanInput.close();
				} catch (IOException e1) {
					//ignore
				}
		}
	}

	// Don't need to synchronize - called only from a synchronized method
	public void setHoldObjects(boolean holdObjects) {
		this.holdObjects = holdObjects;
	}

	private void log(Status status) {
		registry.log(status);
	}

	private RegistryObjectFactory getObjectFactory() {
		return registry.getElementFactory();
	}

	// Returns a file name used to test if cache is actually present at a given location
	public static String getTestFileName() {
		return TABLE;
	}

	public void close() {
		try {
			if (mainInput != null)
				mainInput.close();
			if (extraInput != null)
				extraInput.close();
		} catch (IOException e) {
			log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, fileError, RegistryMessages.meta_registryCacheReadProblems, e));
		}
	}

	private String readUTF(DataInputStream in, int type) throws IOException {
		String value;
		if (type == LOBJECT) {
			int length = in.readInt();
			byte[] data = new byte[length];
			in.readFully(data);
			value = new String(data, UTF_8);
		} else {
			value = in.readUTF();
		}

		Map map = null;
		if (stringPool != null) {
			map = (Map) stringPool.get();
		}
		if (map == null) {
			map = new HashMap();
			stringPool = new SoftReference(map);
		}

		String pooledString = (String) map.get(value);
		if (pooledString == null) {
			map.put(value, value);
			return value;
		}

		return pooledString;
	}
}

Back to the top