Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e28abd4a3fd8d7d3b9f4d16f11c18661a1ed4d3f (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.core.internal.utils;

import java.io.File;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;

/**
 * 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 singleton instance.
	private static volatile PersistenceManager instance;

	// The time stamp for each file.
	Map<URI, FileState> digests;

	// Already known resolved content type of file nodes specified by their URIs.
	Map<URI, IContentType> resolved;

	// Already known unresolvable file nodes specified by their URIs.
	Map<URI, URI> unresolved;

	// The persistent properties of the files.
	Map<URI, Map<QualifiedName, String>> properties;
	
	// The file used to store persistent properties of each file.
	private static final String PERSISTENT_FILE = "persistent.ini"; //$NON-NLS-1$

	/**
	 * 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() {
    	digests = new HashMap<URI, FileState>();
    	resolved = new HashMap<URI, IContentType>();
    	unresolved = new HashMap<URI, URI>();
    	properties = new HashMap<URI, Map<QualifiedName, String>>();
		SafeRunner.run(new ISafeRunnable(){
			@Override
            public void handleException(Throwable exception) {
				// Ignore on purpose
            }
			@Override
            public void run() throws Exception {
				IURIPersistenceService service = ServiceManager.getInstance().getService(IURIPersistenceService.class);
				File location = CacheManager.getCacheRoot();
				File persistentFile = new File(location, PERSISTENT_FILE);
				if (persistentFile.exists()) {
					service.read(PersistenceManager.this, persistentFile.getAbsoluteFile().toURI());
				}
            }});
	}

	/**
	 * 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.getLocationURI()) != 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.getLocationURI());
	}

	/**
	 * 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.getLocationURI(), contentType);
	}

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

	/**
	 * Set the time stamp of the FSTreeNode with the specified location.
	 *
	 * @param uri The FSTreeNode's location URI.
	 * @param digest The new base time stamp to be set.
	 */
	public void setFileDigest(URI uri, FileState digest) {
		digests.put(uri, digest);
	}

	/**
	 * Remove the time stamp entry with the specified URI.
	 *
	 * @param uri The URI key.
	 */
	public void removeFileDigest(URI uri) {
		digests.remove(uri);
	}

	/**
	 * Get the time stamp of the FSTreeNode with the specified location.
	 *
	 * @param uri The FSTreeNode's location URI.
	 * @return The FSTreeNode's base time stamp.
	 */
	public FileState getFileDigest(FSTreeNode node) {
		URI uri = node.getLocationURI();
		FileState digest = digests.get(uri);
		if(digest == null) {
			digest = new FileState(node);
			digests.put(uri, digest);
		}
		digest.setNode(node);
		return digest;
	}

	/**
	 * 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.getLocationURI());
		if (nodeProperties == null) {
			nodeProperties = Collections.synchronizedMap(new HashMap<QualifiedName, String>());
			properties.put(node.getLocationURI(), nodeProperties);
		}
		return nodeProperties;
	}

	/**
	 * Dispose the cache manager so that it has a chance to save the digests and the persistent
	 * properties.
	 */
	public void dispose() {
		SafeRunner.run(new ISafeRunnable(){
			@Override
            public void handleException(Throwable exception) {
				// Ignore on purpose.
            }
			@Override
            public void run() throws Exception {
				IURIPersistenceService service = ServiceManager.getInstance().getService(IURIPersistenceService.class);
				File location = CacheManager.getCacheRoot();
				File persistentFile = new File(location, PERSISTENT_FILE);
				service.write(PersistenceManager.this, persistentFile.getAbsoluteFile().toURI());
            }});
	}

	/**
	 * Returns if or if not the persistence manager needs to be disposed.
	 *
	 * @return <code>True</code> if the persistence manager needs disposal, <code>false</code> otherwise.
	 */
	public final static boolean needsDisposal() {
		return instance != null;
	}
}

Back to the top