Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3b7f09f22382abcc0d1867f33121ed8a3ce1b241 (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
/*******************************************************************************
 * 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.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;

/**
 * @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 synchronized void copy(IPath source, IPath target, boolean overwrite) {
		// FIXME
	}

	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) {
		File file = new File(location, path.toOSString());
		if (!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());
	}

}

Back to the top