Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2332963d1895463f0fd42a3cd7494c6edbff56a8 (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
/*******************************************************************************
 * 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:
 * Wind River Systems - initial API and implementation
 * William Chen (Wind River)- [345387]Open the remote files with a proper editor
 *******************************************************************************/
package org.eclipse.tm.te.tcf.filesystem.internal.handlers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.text.DecimalFormat;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tm.te.tcf.filesystem.activator.UIPlugin;
import org.eclipse.tm.te.tcf.filesystem.internal.nls.Messages;
import org.eclipse.tm.te.tcf.filesystem.model.FSTreeNode;

/**
 * The local file system cache used to manage the temporary files downloaded
 * from a remote file system.
 */
public class CacheManager {
	private static final String WS_AGENT_DIR_PREFIX = "agent_"; //$NON-NLS-1$

	// The singleton instance.
	private static CacheManager instance;
	// The formatter used to format the size displayed while downloading.
	private static DecimalFormat SIZE_FORMAT = new DecimalFormat("#,##0.##"); //$NON-NLS-1$
	// The default chunk size of the buffer used during downloading files.
	private static final int DEFAULT_CHUNK_SIZE = 5 * 1024;

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

	/**
	 * Get the local path of a node's cached file.
	 * <p>
	 * The preferred location is within the plugin's state location, in
	 * example <code>&lt;state location&gt;agent_<hashcode_of_peerId>/remote/path/to/the/file...</code>.
	 * <p>
	 * If the plugin is loaded in a RCP workspace-less environment, the
	 * fallback strategy is to use the users home directory.
	 *
	 * @param node
	 *            The file/folder node.
	 * @return The local path of the node's cached file.
	 */
	public IPath getCachePath(FSTreeNode node) {
        File location;
        try {
        	location = UIPlugin.getDefault().getStateLocation().toFile();
        }
        catch (IllegalStateException e) {
            // An RCP workspace-less environment (-data @none)
        	location = new File(System.getProperty("user.home"), ".tcf"); //$NON-NLS-1$ //$NON-NLS-2$
        	location = new File(location, "fs"); //$NON-NLS-1$
        }

        // Create the location if it not exist
		if (!location.exists()) location.mkdir();

		String agentId = node.peerNode.getPeer().getID();
		// Use Math.abs to avoid negative hash value.
		String agent = WS_AGENT_DIR_PREFIX + Math.abs(agentId.hashCode());
		IPath agentDir = new Path(location.getAbsolutePath()).append(agent);
		File agentDirFile = agentDir.toFile();
		if (!agentDirFile.exists()) {
			agentDirFile.mkdir();
		}

		return appendNodePath(agentDir, node);
	}

	/**
	 * Append the path with the specified node's context path.
	 *
	 * @param path
	 *            The path to be appended.
	 * @param node
	 *            The file/folder node.
	 * @return The path to the node.
	 */
	private IPath appendNodePath(IPath path, FSTreeNode node) {
		if (!node.isRoot()) {
			path = appendNodePath(path, node.parent);
			return appendPathSegment(node, path, node.name);
		}
		if (node.isWindowsNode()) {
			String name = node.name.substring(0, 1);
			return appendPathSegment(node, path, name);
		}
		return path;
	}

	/**
	 * Append the path with the segment "name". Create a directory
	 * if the node is a directory which does not yet exist.
	 *
	 * @param node The file/folder node.
	 * @param path The path to appended.
	 * @param name The segment's name.
	 * @return
	 */
	private IPath appendPathSegment(FSTreeNode node, IPath path, String name) {
		IPath newPath = path.append(name);
		File newFile = newPath.toFile();
		if (node.isDirectory() && !newFile.exists()) {
			newFile.mkdir();
		}
		return newPath;
	}

	/**
	 * Get the cache file's staleness. If the cache file does not exist or the
	 * file is already out-dated, then return true.
	 * <p>
	 * When a local cache file is created, the modified time is set to that of
	 * its remote corresponding file. So if its remote file is changed, the
	 * modified time stamp should be different from the local cache file's
	 * modified time stamp.
	 * <p>
	 * See the method "download" for more details.
	 * <p>
	 *
	 * @param node
	 *            The file/folder node.
	 * @return true if the file doesn't exist or it is stale.
	 */
	public boolean isCacheStale(FSTreeNode node) {
		IPath path = getCachePath(node);
		File file = path.toFile();
		return !file.exists() || file.lastModified() != node.attr.mtime;
	}

	/**
	 * Download the data of the file from the remote file system.
	 *
	 * @param node
	 *            The file node.
	 * @param parent
	 *            The shell parent used to display messages.
	 *
	 * @return true if it is successful, false there're errors or it is
	 *         canceled.
	 */
	public boolean download(final FSTreeNode node, Shell parent) {
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			@Override
			public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
				monitor.beginTask(NLS.bind(Messages.CacheManager_DowloadingFile, node.name), 100);
				try {
					boolean canceled = downloadFile(node, monitor);
					if (canceled)
						throw new InterruptedException();
				} catch (IOException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		TimeTriggeredProgressMonitorDialog dialog = new TimeTriggeredProgressMonitorDialog(
				parent, 250);
		dialog.setCancelable(true);
		File file = getCachePath(node).toFile();
		try {
			dialog.run(true, true, runnable);
			// If downloading is successful, set the last modified time to
			// that of its corresponding file.
			file.setLastModified(node.attr.mtime);
			// If the node is read-only, make the cache file read-only.
			if(!node.isWritable())
				file.setReadOnly();
			return true;
		} catch (InvocationTargetException e) {
			// Something's gone wrong. Roll back the downloading and display the
			// error.
			file.delete();
			displayError(node, parent, e);
		} catch (InterruptedException e) {
			// It is canceled. Just roll back the downloading result.
			file.delete();
		}
		return false;
	}

	/**
	 * Display the error in an error dialog.
	 *
	 * @param node
	 *            the file node.
	 * @param parent
	 *            the parent shell.
	 * @param e
	 *            The error exception.
	 */
	private void displayError(FSTreeNode node, Shell parent, InvocationTargetException e) {
		Throwable target = e.getTargetException();
		Throwable cause = target.getCause() != null ? target.getCause() : target;
		MessageDialog.openError(parent, Messages.CacheManager_DownloadingError, cause.getLocalizedMessage());
	}

	/**
	 * Download the specified file using the monitor to report the progress.
	 *
	 * @param node
	 *            The file to be downloaded.
	 * @param monitor
	 *            The monitor used to report the progress.
	 * @return true if it is canceled or else false.
	 * @throws IOException
	 *             an IOException thrown during downloading and storing data.
	 */
	protected boolean downloadFile(FSTreeNode node, IProgressMonitor monitor)
			throws IOException {
		BufferedInputStream input = null;
		BufferedOutputStream output = null;
		try {
			// Open the input stream of the node using the tcf stream protocol.
			URL url = node.getLocationURL();
			InputStream in = url.openStream();
			input = new BufferedInputStream(in);
			// Write the data to its local cache file.
			File file = getCachePath(node).toFile();
			if(file.exists() && !file.canWrite()){
				// If the file exists and is read-only, delete it.
				file.delete();
			}
			output = new BufferedOutputStream(new FileOutputStream(file));

			// The buffer used to download the file.
			byte[] data = new byte[DEFAULT_CHUNK_SIZE];
			// Calculate the chunk size of one percent.
			int chunk_size = (int) node.attr.size / 100;
			// Total size displayed on the progress dialog.
			String total_size = formatSize(node.attr.size);

			int percentRead = 0;
			long bytesRead = 0;
			int length;
			while ((length = input.read(data)) >= 0 && !monitor.isCanceled()) {
				output.write(data, 0, length);
				output.flush();
				bytesRead += length;
				if (chunk_size != 0) {
					int percent = (int) bytesRead / chunk_size;
					if (percent != percentRead) { // Update the progress.
						monitor.worked(percent - percentRead);
						percentRead = percent; // Remember the percentage.
						// Report the progress.
						monitor.subTask(NLS.bind(Messages.CacheManager_DownloadingProgress, formatSize(bytesRead), total_size));
					}
				}
			}
			return monitor.isCanceled();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (Exception e) {
				}
			}
			if (input != null) {
				try {
					input.close();
				} catch (Exception e) {
				}
			}
		}
	}

	/**
	 * Use the SIZE_FORMAT to format the file's size. The rule is: 1. If the
	 * size is less than 1024 bytes, then show it as "####" bytes. 2. If the
	 * size is less than 1024 KBs, while more than 1 KB, then show it as
	 * "####.##" KBs. 3. If the size is more than 1 MB, then show it as
	 * "####.##" MBs.
	 *
	 * @param size
	 *            The file size to be displayed.
	 * @return The string representation of the size.
	 */
	private String formatSize(long size) {
		double kbSize = size / 1024.0;
		if (kbSize < 1.0) {
			return SIZE_FORMAT.format(size) + Messages.CacheManager_Bytes;
		}
		double mbSize = kbSize / 1024.0;
		if (mbSize < 1.0)
			return SIZE_FORMAT.format(kbSize) + Messages.CacheManager_KBs;
		return SIZE_FORMAT.format(mbSize) + Messages.CacheManager_MBs;
	}
}

Back to the top