/
PersistentMap.java
451 lines (389 loc) · 9.69 KB
/
PersistentMap.java
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright (c) 2014-2016 Eike Stepper (Loehne, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.oomph.p2.internal.core;
import org.eclipse.oomph.p2.core.AgentManagerElement;
import org.eclipse.oomph.util.IORuntimeException;
import org.eclipse.oomph.util.IOUtil;
import org.eclipse.oomph.util.StringUtil;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osgi.util.NLS;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Eike Stepper
*/
public abstract class PersistentMap<E>
{
private final Charset charset;
private final File file;
private final File tempFile;
private final File lockFile;
private final Map<String, E> elements = new LinkedHashMap<>();
public PersistentMap(File file, Charset charset)
{
this.charset = charset;
this.file = file;
if (file != null)
{
File folder = file.getParentFile();
folder.mkdirs();
String name = file.getName();
tempFile = new File(folder, name + ".temp"); //$NON-NLS-1$
lockFile = IOUtil.canWriteFolder(folder) ? new File(folder, name + ".lock") : null; //$NON-NLS-1$
}
else
{
tempFile = null;
lockFile = null;
}
}
public final void load()
{
final boolean[] needsSave = { false };
if (file != null && file.exists())
{
FileWriter lock = lock();
try
{
load(new KeyHandler()
{
@Override
public void handleKey(String key, String extraInfo) throws Exception
{
E element = loadElement(key, extraInfo);
if (element == null)
{
needsSave[0] = true;
return;
}
if (element instanceof AgentManagerElement)
{
AgentManagerElement agentManagerElement = (AgentManagerElement)element;
if (!agentManagerElement.isValid())
{
needsSave[0] = true;
return;
}
}
elements.put(key, element);
}
});
}
finally
{
unlock(lock);
}
}
else
{
initializeFirstTime();
needsSave[0] = true;
}
if (needsSave[0])
{
save(null, null);
}
}
public final File getFile()
{
return file;
}
public final Charset getCharset()
{
return charset;
}
public final synchronized Set<String> getElementKeys()
{
return new HashSet<>(elements.keySet());
}
public final synchronized Collection<E> getElements()
{
return new ArrayList<>(elements.values());
}
public final synchronized E getElement(String key)
{
return elements.get(key);
}
public final synchronized boolean containsElement(String key)
{
return elements.containsKey(key);
}
public final synchronized E addElement(String key, String extraInfo)
{
E element = elements.get(key);
if (element == null)
{
element = createElement(key, extraInfo);
if (element != null)
{
elements.put(key, element);
save(key, null);
}
}
return element;
}
public final synchronized E remap(String oldKey, String newKey)
{
E element = elements.remove(oldKey);
if (element != null)
{
elements.put(newKey, element);
save(newKey, oldKey);
}
return element;
}
public final synchronized void removeElement(String key)
{
E element = elements.remove(key);
if (element != null)
{
save(null, key);
}
}
public final synchronized boolean refresh()
{
return reconcile(null, null);
}
/**
* Restore an element from the persisted information. In contrast to {@link #createElement(String, String)},
* which is also used by {@link #addElement(String, String)} to explicitly add elements, loading might need
* additional checks to avoid restoring stale entries (which e.g. don't exist anymore).
* <p>
* The default implementation simply delegates to {@link #createElement(String, String)}.
*
* @return the new element or <code>null</code> if no element should be created.
* @see #createElement(String, String)
* @see #load()
* @see #refresh()
*/
protected E loadElement(String key, String extraInfo)
{
return createElement(key, extraInfo);
}
/**
* @return the new element or <code>null</code> if no element should be created.
*/
protected abstract E createElement(String key, String extraInfo);
protected void initializeFirstTime()
{
}
private void load(KeyHandler handler)
{
FileReader infoReader = null;
try
{
infoReader = new FileReader(file, charset);
BufferedReader bufferedReader = new BufferedReader(infoReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
try
{
String key;
String extraInfo;
int pos = line.indexOf('|');
if (pos == -1)
{
key = line;
extraInfo = null;
}
else
{
key = line.substring(0, pos);
extraInfo = line.substring(pos + 1);
}
handler.handleKey(key, extraInfo);
}
catch (Exception ex)
{
P2CorePlugin.INSTANCE.log(ex);
}
}
IOUtil.close(bufferedReader);
}
catch (IOException ex)
{
throw new IORuntimeException(ex);
}
finally
{
IOUtil.closeSilent(infoReader);
}
}
public void save(String addedKey, final String removedKey)
{
if (file != null)
{
FileWriter lock = lock();
FileWriter tempWriter = null;
try
{
if (addedKey != null || removedKey != null)
{
if (file.exists())
{
reconcile(addedKey, removedKey);
}
}
tempWriter = new FileWriter(tempFile, charset);
BufferedWriter bufferedWriter = new BufferedWriter(tempWriter);
List<String> sortedKeys = new ArrayList<>(elements.keySet());
Collections.sort(sortedKeys);
for (String key : sortedKeys)
{
bufferedWriter.write(key);
E element = elements.get(key);
if (element instanceof ExtraInfoProvider)
{
String extraInfo = ((ExtraInfoProvider)element).getExtraInfo();
bufferedWriter.write('|');
bufferedWriter.write(extraInfo);
}
bufferedWriter.write(StringUtil.NL);
}
bufferedWriter.flush();
IOUtil.closeSilent(tempWriter);
tempWriter = null;
file.delete();
tempFile.renameTo(file);
}
catch (IOException ex)
{
throw new IORuntimeException(ex);
}
finally
{
IOUtil.closeSilent(tempWriter);
unlock(lock);
}
}
}
private boolean reconcile(String addedKey, final String removedKey)
{
final boolean[] changed = { false };
final Set<String> fileKeys = new HashSet<>();
fileKeys.add(addedKey);
try
{
load(new KeyHandler()
{
@Override
public void handleKey(String key, String extraInfo) throws Exception
{
fileKeys.add(key);
if (!key.equals(removedKey) && !elements.containsKey(key))
{
E element = createElement(key, extraInfo);
if (element != null)
{
elements.put(key, element);
changed[0] = true;
}
}
}
});
}
catch (IORuntimeException ex)
{
P2CorePlugin.INSTANCE.log(ex, IStatus.WARNING);
}
for (Iterator<String> it = elements.keySet().iterator(); it.hasNext();)
{
String key = it.next();
if (!fileKeys.contains(key))
{
it.remove();
changed[0] = true;
}
}
return changed[0];
}
private FileWriter lock()
{
if (lockFile == null)
{
return null;
}
long start = System.currentTimeMillis();
for (;;)
{
try
{
return new FileWriter(lockFile, charset);
}
catch (IOException ex)
{
if (System.currentTimeMillis() - start >= 10000L)
{
throw new RuntimeException(NLS.bind(Messages.PersistentMap_LockTimeout_exception, lockFile), ex);
}
}
try
{
Thread.sleep(10L);
}
catch (InterruptedException ex)
{
throw new RuntimeException(NLS.bind(Messages.PersistentMap_LockInterrupted_exception, lockFile), ex);
}
}
}
private void unlock(FileWriter lock)
{
if (lock != null)
{
try
{
IOUtil.close(lock);
}
catch (Exception ex)
{
P2CorePlugin.INSTANCE.log(ex);
}
try
{
lockFile.delete();
}
catch (Exception ex)
{
// Ignore
}
}
}
/**
* @author Eike Stepper
*/
private interface KeyHandler
{
public void handleKey(String key, String extraInfo) throws Exception;
}
/**
* @author Eike Stepper
*/
public interface ExtraInfoProvider
{
public String getExtraInfo();
}
}