Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3cc5f255047872ea5429b6fce66d954a360a713f (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 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.internal.tasks.core.externalization;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.net.Policy;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.core.externalization.IExternalizationContext.Kind;

/**
 * @author Rob Elves
 * @since 3.0
 */
public class ExternalizationManager {

	private static final int SAVE_DELAY = 90 * 1000;

	private final ExternalizationJob saveJob;

	private IStatus loadStatus;

	private String rootFolderPath;

	private static volatile boolean saveDisabled = false;

	private final List<IExternalizationParticipant> externalizationParticipants;

	private boolean forceSave = false;

	public ExternalizationManager(String rootFolderPath) {
		Assert.isNotNull(rootFolderPath);
		this.externalizationParticipants = new CopyOnWriteArrayList<IExternalizationParticipant>();
		this.forceSave = false;
		this.saveJob = createJob();
		setRootFolderPath(rootFolderPath);
	}

	private ExternalizationJob createJob() {
		ExternalizationJob job = new ExternalizationJob(Messages.ExternalizationManager_Task_List_Save_Job);
		job.setUser(false);
		job.setSystem(true);
		return job;
	}

	public void addParticipant(IExternalizationParticipant participant) {
		Assert.isNotNull(participant);
		externalizationParticipants.add(participant);
	}

	public IStatus load() {
		try {
			saveDisabled = true;
			loadStatus = null;

			List<IStatus> statusList = new ArrayList<IStatus>();
			IProgressMonitor monitor = Policy.monitorFor(null);
			for (IExternalizationParticipant participant : externalizationParticipants) {
				IStatus status = load(participant, monitor);
				if (status != null) {
					statusList.add(status);
				}
			}

			if (statusList.size() > 0) {
				loadStatus = new MultiStatus(ITasksCoreConstants.ID_PLUGIN, IStatus.ERROR,
						statusList.toArray(new IStatus[0]), "Failed to load Task List", null); //$NON-NLS-1$
			}
			return loadStatus;
		} finally {
			saveDisabled = false;
		}
	}

	public IStatus load(final IExternalizationParticipant participant, final IProgressMonitor monitor) {
		final IStatus[] result = new IStatus[1];
		final ExternalizationContext context = new ExternalizationContext(Kind.LOAD, rootFolderPath);
		ISchedulingRule rule = participant.getSchedulingRule();
		try {
			Job.getJobManager().beginRule(rule, monitor);
			SafeRunner.run(new ISafeRunnable() {
				public void handleException(Throwable e) {
					if (e instanceof CoreException) {
						result[0] = ((CoreException) e).getStatus();
					} else {
						result[0] = new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, "Load participant failed", //$NON-NLS-1$
								e);
					}
				}

				public void run() throws Exception {
					participant.execute(context, monitor);
				}
			});
		} finally {
			Job.getJobManager().endRule(rule);
		}
		return result[0];
	}

	public void setRootFolderPath(String rootFolderPath) {
		Assert.isNotNull(rootFolderPath);
		this.rootFolderPath = rootFolderPath;
		saveJob.setContext(new ExternalizationContext(Kind.SAVE, rootFolderPath));
	}

	public void requestSave() {
		if (!saveDisabled) {
			saveJob.schedule(SAVE_DELAY);
		}
	}

	public void stop() {
		try {
			saveDisabled = true;

			// run save job as early as possible
			saveNow();
		} catch (InterruptedException e) {
			StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
					"Task List save on shutdown canceled.", e)); //$NON-NLS-1$
		}
	}

	/**
	 * Performs a full save, trying until one has succeeded
	 */
	public void saveNow() throws InterruptedException {
		saveJob.setFullSavePending();
		saveJob.wakeUp();
		saveJob.join();

		// make sure that we actually have done a full save before we exit this method
		while (saveJob.isFullSavePending()) {
			saveJob.schedule();
			saveJob.wakeUp();
			saveJob.join();
		}
	}

	/**
	 * Clients invoking this method must hold all necessary scheduling rules.
	 */
	public void save(boolean force) {
		try {
			forceSave = force;
			saveJob.run(new NullProgressMonitor());
		} finally {
			forceSave = false;
		}
	}

	public IStatus getLoadStatus() {
		return loadStatus;
	}

	private class ExternalizationJob extends Job {

		private volatile IExternalizationContext context;

		private volatile boolean isFullSavePending = false;

		public ExternalizationJob(String jobTitle) {
			super(jobTitle);
		}

		public boolean isFullSavePending() {
			return isFullSavePending;
		}

		public void setFullSavePending() {
			isFullSavePending = true;
		}

		public void setContext(IExternalizationContext saveContext) {
			this.context = saveContext;
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			IExternalizationContext context = this.context;
			switch (context.getKind()) {
			case SAVE:
				try {
					monitor.beginTask(Messages.ExternalizationManager_Saving_, externalizationParticipants.size());

					boolean fullSave = isFullSavePending;
					isFullSavePending = false;

					for (IExternalizationParticipant participant : externalizationParticipants) {
						ISchedulingRule rule = participant.getSchedulingRule();
						if (forceSave || participant.isDirty(fullSave)) {
							try {
								Job.getJobManager().beginRule(rule, monitor);
								monitor.setTaskName(MessageFormat.format(Messages.ExternalizationManager_Saving_X,
										participant.getDescription()));
								participant.execute(context, new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
							} catch (CoreException e) {
								StatusHandler.log(new Status(IStatus.WARNING, ITasksCoreConstants.ID_PLUGIN,
										"Save failed for " + participant.getDescription(), e)); //$NON-NLS-1$
							} finally {
								Job.getJobManager().endRule(rule);
							}
						}
						monitor.worked(1);
					}
				} finally {
					monitor.done();
				}
				break;
			default:
				StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN,
						"Unsupported externalization kind: " + context.getKind())); //$NON-NLS-1$
			}
			return Status.OK_STATUS;
		}
	}

	private class ExternalizationContext implements IExternalizationContext {

		private final Kind kind;

		private final String rootPath;

		public ExternalizationContext(IExternalizationContext.Kind kind, String rootPath) {
			this.kind = kind;
			this.rootPath = rootPath;
		}

		public Kind getKind() {
			return kind;
		}

		public String getRootPath() {
			return rootPath;
		}
	}

}

Back to the top