Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b0d8405f4becf5b0981c829a14497620bd79f65 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * William Chen (Wind River)	[360494]Provide an "Open With" action in the pop 
 * 								up menu of file system nodes of Target Explorer.
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.internal.handlers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.tcf.te.tcf.filesystem.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.internal.preferences.TargetExplorerPreferencePage;
import org.eclipse.tcf.te.tcf.filesystem.model.FSTreeNode;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;

/**
 * A facility class to load and save persistent data such including resolved content types, file's
 * properties, and time stamps etc.
 */
public class PersistenceManager {
	// The XML element of unresolvable.
	private static final String ELEMENT_UNRESOLVABLE = "unresolvable"; //$NON-NLS-1$

	// The root element of "unresolvables"
	private static final String ELEMENT_UNRESOLVED = "unresolved"; //$NON-NLS-1$

	// The attribute "contentType" to specify the content type id of the file.
	private static final String ATTR_CONTENT_TYPE = "contentType"; //$NON-NLS-1$

	// The XML element of resolvable.
	private static final String ELEMENT_RESOLVABLE = "resolvable"; //$NON-NLS-1$

	// The root element of "resolvables"
	private static final String ELEMENT_RESOLVED = "resolved"; //$NON-NLS-1$

	// The root element of the memento for content type resolving.
	private static final String CONTENT_TYPE_ROOT = "contentTypes"; //$NON-NLS-1$

	// The XML file name used to store the resolved content types.
	private static final String CONTENT_TYPE_FILE = "contentTypes.xml"; //$NON-NLS-1$

	// The attribute "value"
	private static final String ATTR_VALUE = "value"; //$NON-NLS-1$

	// The attribute "local name" of a qualified name.
	private static final String ATTR_LOCAL_NAME = "localName"; //$NON-NLS-1$

	// The attribute "qualifier" of a qualified name.
	private static final String ATTR_QUALIFIER = "qualifier"; //$NON-NLS-1$

	// The attribute of a node's URL
	private static final String ATTR_URL = "URL"; //$NON-NLS-1$

	// The element "property" to record a file's property
	private static final String ELEMENT_PROPERTY = "property"; //$NON-NLS-1$

	// The element "file" to specify a file's entry.
	private static final String ELEMENT_FILE = "file"; //$NON-NLS-1$

	// The root element of properties.
	private static final String PERSISTENT_ROOT = "properties"; //$NON-NLS-1$

	// Time stamp file used to persist the time stamps of each file.
	private static final String TIMESTAMP_FILE = "timestamps.xml"; //$NON-NLS-1$

	// The file used to store persistent properties of each file.
	private static final String PERSISTENT_FILE = "persistent.xml"; //$NON-NLS-1$

	// The singleton instance.
	private static PersistenceManager instance;

	// The time stamp for each file.
	private Map<URL, Long> timestamps;

	// The persistent properties of the files.
	private Map<URL, Map<QualifiedName, String>> properties;

	// Already known resolved content type of file nodes specified by their URLs.
	private Map<URL, IContentType> resolved;

	// Already known unresolvable file nodes specified by their URLs.
	private Map<URL, URL> unresolved;

	/**
	 * Get the singleton cache manager.
	 * 
	 * @return The singleton cache manager.
	 */
	public static PersistenceManager getInstance() {
		if (instance == null) {
			instance = new PersistenceManager();
		}
		return instance;
	}

	/**
	 * Create a Persistent Manager instance.
	 */
	private PersistenceManager() {
		loadTimestamps();
		loadPersistentProperties();
		loadContentTypes();
	}

	/**
	 * If the node is already considered unresolvable.
	 * 
	 * @param node The file node.
	 * @return true if it is not resolvable or else false.
	 */
	public boolean isUnresovled(FSTreeNode node) {
		return unresolved.get(node.getLocationURL()) != null;
	}

	/**
	 * Get the resolved content type of the node.
	 * 
	 * @param node The file node.
	 * @return the content type of the node if it is resolvable or null.
	 */
	public IContentType getResolved(FSTreeNode node) {
		return resolved.get(node.getLocationURL());
	}

	/**
	 * Add the node and its content type to the resolved list.
	 * 
	 * @param node The file node.
	 * @param contentType Its content type.
	 */
	public void addResovled(FSTreeNode node, IContentType contentType) {
		resolved.put(node.getLocationURL(), contentType);
	}

	/**
	 * Add the node as an unresolvable node.
	 * 
	 * @param node The file node.
	 */
	public void addUnresolved(FSTreeNode node) {
		unresolved.put(node.getLocationURL(), node.getLocationURL());
	}

	/**
	 * If the option of "autosaving" is set to on.
	 * 
	 * @return true if it is auto saving or else false.
	 */
	public boolean isAutoSaving() {
		IPreferenceStore preferenceStore = UIPlugin.getDefault().getPreferenceStore();
		boolean autoSaving = preferenceStore
		                .getBoolean(TargetExplorerPreferencePage.PREF_AUTOSAVING);
		return autoSaving;
	}

	/**
	 * Load the persistent properties from the persistent file in the cache's root directory.
	 */
	private void loadPersistentProperties() {
		IMemento memento = readMemento(PERSISTENT_FILE, PERSISTENT_ROOT);
		properties = Collections.synchronizedMap(new HashMap<URL, Map<QualifiedName, String>>());
		IMemento[] children = memento.getChildren(ELEMENT_FILE);
		if (children != null && children.length > 0) {
			for (IMemento child : children) {
				try {
					String str = child.getString(ATTR_URL);
					URL url = new URL(str);
					Map<QualifiedName, String> nodeProperties = loadFileProperties(child);
					properties.put(url, nodeProperties);
				}
				catch (MalformedURLException e) {
				}
			}
		}
	}

	/**
	 * Load the content type information from the content type file.
	 */
	private void loadContentTypes() {
		IMemento memento = readMemento(CONTENT_TYPE_FILE, CONTENT_TYPE_ROOT);
		resolved = Collections.synchronizedMap(new HashMap<URL, IContentType>());
		unresolved = Collections.synchronizedMap(new HashMap<URL, URL>());
		IMemento mResolved = memento.getChild(ELEMENT_RESOLVED);
		if (mResolved != null) {
			IMemento[] children = mResolved.getChildren(ELEMENT_RESOLVABLE);
			if (children != null && children.length > 0) {
				for (IMemento child : children) {
					try {
						String str = child.getString(ATTR_URL);
						URL url = new URL(str);
						String id = child.getString(ATTR_CONTENT_TYPE);
						IContentType contentType = Platform.getContentTypeManager()
						                .getContentType(id);
						if (contentType != null) {
							resolved.put(url, contentType);
						}
					}
					catch (MalformedURLException e) {
					}
				}
			}
		}
		IMemento mUnresolved = memento.getChild(ELEMENT_UNRESOLVED);
		if (mUnresolved != null) {
			IMemento[] children = mUnresolved.getChildren(ELEMENT_UNRESOLVABLE);
			if (children != null && children.length > 0) {
				for (IMemento child : children) {
					try {
						String str = child.getString(ATTR_URL);
						URL url = new URL(str);
						unresolved.put(url, url);
					}
					catch (MalformedURLException e) {
					}
				}
			}
		}
	}

	/**
	 * Save the content type information to the content type file.
	 */
	private void saveContentTypes() {
		XMLMemento memento = XMLMemento.createWriteRoot(CONTENT_TYPE_ROOT);
		IMemento mResolved = memento.createChild(ELEMENT_RESOLVED);
		for (URL key : resolved.keySet()) {
			IContentType iContentType = resolved.get(key);
			IMemento mResolvable = mResolved.createChild(ELEMENT_RESOLVABLE);
			mResolvable.putString(ATTR_URL, key.toString());
			mResolvable.putString(ATTR_CONTENT_TYPE, iContentType.getId());
		}
		IMemento mUnresolved = memento.createChild(ELEMENT_UNRESOLVED);
		for (URL key : unresolved.keySet()) {
			IMemento mUnresolvable = mUnresolved.createChild(ELEMENT_UNRESOLVABLE);
			mUnresolvable.putString(ATTR_URL, key.toString());
		}
		writeMemento(memento, CONTENT_TYPE_FILE);
	}

	/**
	 * Load a file's properties from the memento node.
	 * 
	 * @param memento The memento node.
	 * @return The properties as a map.
	 */
	private Map<QualifiedName, String> loadFileProperties(IMemento memento) {
		Map<QualifiedName, String> properties = Collections
		                .synchronizedMap(new HashMap<QualifiedName, String>());
		IMemento[] children = memento.getChildren(ELEMENT_PROPERTY);
		if (children != null && children.length > 0) {
			for (IMemento child : children) {
				String qualifier = child.getString(ATTR_QUALIFIER);
				String localName = child.getString(ATTR_LOCAL_NAME);
				QualifiedName name = new QualifiedName(qualifier, localName);
				String value = child.getString(ATTR_VALUE);
				properties.put(name, value);
			}
		}
		return properties;
	}

	/**
	 * Read the memento from a memento file using the specified root element name.
	 * 
	 * @param mementoFile The memento file.
	 * @param mementoRoot The memento's root element name.
	 * @return A memento of this file or an empty memento if the file does not exist.
	 */
	private IMemento readMemento(String mementoFile, String mementoRoot) {
		File location = CacheManager.getInstance().getCacheRoot();
		File stateFile = new File(location, mementoFile);
		if (stateFile.exists()) {
			BufferedReader reader = null;
			try {
				FileInputStream input = new FileInputStream(stateFile);
				reader = new BufferedReader(new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
				IMemento memento = XMLMemento.createReadRoot(reader);
				return memento;
			}
			catch (IOException e) {
			}
			catch (WorkbenchException e) {
			}
			finally {
				if (reader != null) {
					try {
						reader.close();
					}
					catch (Exception e) {
					}
				}
			}
		}
		return XMLMemento.createWriteRoot(mementoRoot);
	}

	/**
	 * Save the time stamps to the persistent file.
	 */
	private void savePersistentProperties() {
		XMLMemento memento = XMLMemento.createWriteRoot(PERSISTENT_ROOT);
		for (URL key : properties.keySet()) {
			Map<QualifiedName, String> nodeProperties = properties.get(key);
			if (!nodeProperties.keySet().isEmpty()) {
				IMemento mFile = memento.createChild(ELEMENT_FILE);
				mFile.putString(ATTR_URL, key.toString());
				saveFileProperties(mFile, nodeProperties);
			}
		}
		writeMemento(memento, PERSISTENT_FILE);
	}

	/**
	 * Save the file's properties to a memento.
	 * 
	 * @param memento The memento object.
	 * @param properties The file properties.
	 */
	private void saveFileProperties(IMemento memento, Map<QualifiedName, String> properties) {
		for (QualifiedName name : properties.keySet()) {
			IMemento mProperty = memento.createChild(ELEMENT_PROPERTY);
			mProperty.putString(ATTR_QUALIFIER, name.getQualifier());
			mProperty.putString(ATTR_LOCAL_NAME, name.getLocalName());
			mProperty.putString(ATTR_VALUE, properties.get(name));
		}
	}

	/**
	 * Write the memento to a memento file.
	 * 
	 * @param memento The memento object.
	 * @param mementoFile The file to write to.
	 */
	private void writeMemento(XMLMemento memento, String mementoFile) {
		OutputStreamWriter writer = null;
		try {
			File location = CacheManager.getInstance().getCacheRoot();
			File stateFile = new File(location, mementoFile);
			FileOutputStream stream = new FileOutputStream(stateFile);
			writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
			memento.save(writer);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (writer != null) {
				try {
					writer.close();
				}
				catch (IOException e) {
				}
			}
		}
	}

	/**
	 * Get the file properties of the specified node from the properties map.
	 * 
	 * @param node The file node.
	 * @return The file properties object or empty properties object if it does not exist.
	 */
	public Map<QualifiedName, String> getPersistentProperties(FSTreeNode node) {
		Map<QualifiedName, String> nodeProperties = properties.get(node.getLocationURL());
		if (nodeProperties == null) {
			nodeProperties = Collections.synchronizedMap(new HashMap<QualifiedName, String>());
			properties.put(node.getLocationURL(), nodeProperties);
		}
		return nodeProperties;
	}

	/**
	 * Load the time stamps from the time stamps file in the cache's root directory.
	 */
	private void loadTimestamps() {
		timestamps = Collections.synchronizedMap(new HashMap<URL, Long>());
		File location = CacheManager.getInstance().getCacheRoot();
		File tsFile = new File(location, TIMESTAMP_FILE);
		if (tsFile.exists()) {
			Properties properties = new Properties();
			InputStream input = null;
			try {
				input = new BufferedInputStream(new FileInputStream(tsFile));
				properties.loadFromXML(input);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				if (input != null) {
					try {
						input.close();
					}
					catch (IOException e) {
					}
				}
			}
			Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
			while (keys.hasMoreElements()) {
				String key = keys.nextElement();
				String value = properties.getProperty(key);
				long timestamp = 0L;
				try {
					timestamp = Long.parseLong(value);
					timestamps.put(new URL(key), Long.valueOf(timestamp));
				}
				catch (Exception nfe) {
				}
			}
		}
	}

	/**
	 * Save the time stamps to the time stamps file.
	 */
	private void saveTimestamps() {
		Properties properties = new Properties();
		for (URL key : timestamps.keySet()) {
			Long timestamp = timestamps.get(key);
			properties.setProperty(key.toString(), timestamp.toString());
		}
		File location = CacheManager.getInstance().getCacheRoot();
		File fTimestamp = new File(location, TIMESTAMP_FILE);
		OutputStream output = null;
		try {
			output = new BufferedOutputStream(new FileOutputStream(fTimestamp));
			properties.storeToXML(output, null);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			if (output != null) {
				try {
					output.close();
				}
				catch (Exception e) {
				}
			}
		}
	}

	/**
	 * Set the time stamp of the FSTreeNode with the specified location.
	 * 
	 * @param url The FSTreeNode's location URL.
	 * @param timestamp The new base time stamp to be set.
	 */
	public void setBaseTimestamp(URL url, long timestamp) {
		timestamps.put(url, Long.valueOf(timestamp));
	}

	/**
	 * Remove the time stamp entry with the specified URL.
	 * 
	 * @param url The URL key.
	 */
	public void removeBaseTimestamp(URL url) {
		timestamps.remove(url);
	}

	/**
	 * Get the time stamp of the FSTreeNode with the specified location.
	 * 
	 * @param url The FSTreeNode's location URL.
	 * @return The FSTreeNode's base time stamp.
	 */
	public long getBaseTimestamp(URL url) {
		Long timestamp = timestamps.get(url);
		return timestamp == null ? 0L : timestamp.longValue();
	}

	/**
	 * Dispose the cache manager so that it has a chance to save the timestamps and the persistent
	 * properties.
	 */
	public void dispose() {
		saveTimestamps();
		savePersistentProperties();
		saveContentTypes();
	}
}

Back to the top