Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f10f2aa732d4e776082fd430d2edb17ea00679a (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
/*******************************************************************************
 * Copyright (c) 2012 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.commons.core.storage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.commons.core.CommonsCorePlugin;
import org.eclipse.osgi.util.NLS;

/**
 * @author Steffen Pingel
 */
public class CommonStore {

	/**
	 * Delays writing of mementos to avoid blocking UI thread.
	 */
	private class FlushJob extends Job {

		public FlushJob() {
			super("Flush context mementos"); //$NON-NLS-1$
			setSystem(true);
			setPriority(Job.SHORT);
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			flushPending();
			return Status.OK_STATUS;
		}

	}

	private static final long FLUSH_DELAY = 500;

	private boolean scheduled;

	private FlushJob flushJob;

	private final Map<File, CommonStorable> storableByLocation;

	private File location;

	public CommonStore(File location) {
		Assert.isNotNull(location);
		this.storableByLocation = new HashMap<File, CommonStorable>();
		this.location = location;
	}

	public synchronized ICommonStorable get(IPath path) {
		File file = getFile(path);
		CommonStorable storable = storableByLocation.get(file);
		if (storable == null) {
			storable = new CommonStorable(this, file);
			storableByLocation.put(file, storable);
		}
		return storable;
	}

	public File getLocation() {
		return location;
	}

	public void setLocation(File location) {
		Assert.isNotNull(location);
		this.location = location;
	}

	public void stop() {
		synchronized (this) {
			if (flushJob != null) {
				flushJob.cancel();
				flushJob = null;
			}
		}
		flushPending();
	}

	private File getFile(IPath path) {
		return getFile(path, true);
	}

	private File getFile(IPath path, boolean create) {
		File file = new File(location, path.toOSString());
		if (create && !file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		return file;
	}

	synchronized void schedule() {
		if (!scheduled) {
			if (flushJob == null) {
				flushJob = new FlushJob();
			}
			flushJob.schedule(FLUSH_DELAY);
		}
	}

	synchronized void flushPending() {
		MultiStatus status = new MultiStatus(CommonsCorePlugin.ID_PLUGIN, 0, "Failed to save storable", null); //$NON-NLS-1$
		for (CommonStorable memento : storableByLocation.values()) {
			if (memento.isDirty()) {
				IStatus result = memento.flush();
				status.add(result);
			}
		}
		if (!status.isOK()) {
			StatusHandler.log(status);
		}
	}

	synchronized void release(CommonStorable storable) {
		storableByLocation.remove(storable.getPath());
	}

	public void move(IPath oldPath, IPath newPath) throws CoreException {
		File oldFile = getFile(oldPath, false);
		// TODO lock hierarchy and throw an exception if oldFile is in use 
		if (oldFile.exists()) {
			File newFile = getFile(newPath, false);
			newFile.getParentFile().mkdirs();
			if (!oldFile.renameTo(newFile)) {
				throw new CoreException(new Status(IStatus.ERROR, CommonsCorePlugin.ID_PLUGIN, NLS.bind(
						"The target path ''{0}'' already exists", newPath)));
			}
		}
	}

}

Back to the top