Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 259981 | Differences between
and this patch

Collapse All | Expand All

(-)supplement/src/org/eclipse/osgi/storagemanager/StorageManager.java (-10 / +22 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 87-100 Link Here
87
	private static final int FILETYPE_STANDARD = 0;
87
	private static final int FILETYPE_STANDARD = 0;
88
	private static final int FILETYPE_RELIABLEFILE = 1;
88
	private static final int FILETYPE_RELIABLEFILE = 1;
89
	private static final SecureAction secure = AccessController.doPrivileged(SecureAction.createSecureAction());
89
	private static final SecureAction secure = AccessController.doPrivileged(SecureAction.createSecureAction());
90
	private static final boolean tempCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanTempFiles")).booleanValue(); //$NON-NLS-1$
91
	private static final boolean openCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanupOnOpen")).booleanValue(); //$NON-NLS-1$
92
	private static final String MANAGER_FOLDER = ".manager"; //$NON-NLS-1$
90
	private static final String MANAGER_FOLDER = ".manager"; //$NON-NLS-1$
93
	private static final String TABLE_FILE = ".fileTable"; //$NON-NLS-1$
91
	private static final String TABLE_FILE = ".fileTable"; //$NON-NLS-1$
94
	private static final String LOCK_FILE = ".fileTableLock"; //$NON-NLS-1$
92
	private static final String LOCK_FILE = ".fileTableLock"; //$NON-NLS-1$
95
	private static final int MAX_LOCK_WAIT = 5000; // 5 seconds 
93
	private static final int MAX_LOCK_WAIT = 5000; // 5 seconds 
96
	// this should be static but the tests expect to be able to create new managers after changing this setting dynamically
94
	// these should be static but the tests expect to be able to create new managers after changing this setting dynamically
97
	private final boolean useReliableFiles = Boolean.valueOf(secure.getProperty("osgi.useReliableFiles")).booleanValue(); //$NON-NLS-1$
95
	private final boolean useReliableFiles = Boolean.valueOf(secure.getProperty("osgi.useReliableFiles")).booleanValue(); //$NON-NLS-1$
96
	private final boolean tempCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanTempFiles")).booleanValue(); //$NON-NLS-1$
97
	private final boolean openCleanup = Boolean.valueOf(secure.getProperty("osgi.embedded.cleanupOnOpen")).booleanValue(); //$NON-NLS-1$
98
98
99
	private class Entry {
99
	private class Entry {
100
		int readId;
100
		int readId;
Lines 554-559 Link Here
554
			if (error)
554
			if (error)
555
				fileStream.abort();
555
				fileStream.abort();
556
		}
556
		}
557
		// bug 259981 we should clean up
558
		if (openCleanup) {
559
			try {
560
				cleanup(false);
561
			} catch (IOException ex) {
562
				// If IOException is thrown from our custom method.
563
				// log and swallow for now.
564
				System.out.println("Unexpected IOException is thrown inside cleanupWithLock. Please look below for stacktrace");
565
				ex.printStackTrace(System.out);
566
			}
567
		}
557
		tableStamp = ReliableFile.lastModifiedVersion(tableFile);
568
		tableStamp = ReliableFile.lastModifiedVersion(tableFile);
558
	}
569
	}
559
570
Lines 584-594 Link Here
584
	 * This removal is only done if the instance of eclipse calling this method is the last instance using this storage manager.
595
	 * This removal is only done if the instance of eclipse calling this method is the last instance using this storage manager.
585
	 * @throws IOException
596
	 * @throws IOException
586
	 */
597
	 */
587
	private void cleanup() throws IOException {
598
	private void cleanup(boolean doLock) throws IOException {
588
		if (readOnly)
599
		if (readOnly)
589
			return;
600
			return;
590
		//Lock first, so someone else can not start while we're in the middle of cleanup
601
		//Lock first, so someone else can not start while we're in the middle of cleanup
591
		if (!lock(true))
602
		if (doLock && !lock(true))
592
			throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
603
			throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
593
		try {
604
		try {
594
			//Iterate through the temp files and delete them all, except the one representing this storage manager.
605
			//Iterate through the temp files and delete them all, except the one representing this storage manager.
Lines 636-642 Link Here
636
				}
647
				}
637
			}
648
			}
638
		} finally {
649
		} finally {
639
			release();
650
			if (doLock)
651
				release();
640
		}
652
		}
641
	}
653
	}
642
654
Lines 662-668 Link Here
662
		if (readOnly)
674
		if (readOnly)
663
			return;
675
			return;
664
		try {
676
		try {
665
			cleanup();
677
			cleanup(true);
666
		} catch (IOException e) {
678
		} catch (IOException e) {
667
			//Ignore and close.
679
			//Ignore and close.
668
		}
680
		}
Lines 680-691 Link Here
680
	 * @throws IOException if an error occurred opening the storage manager
692
	 * @throws IOException if an error occurred opening the storage manager
681
	 */
693
	 */
682
	public void open(boolean wait) throws IOException {
694
	public void open(boolean wait) throws IOException {
683
		if (openCleanup)
684
			cleanup();
685
		if (!readOnly) {
695
		if (!readOnly) {
686
			managerRoot.mkdirs();
696
			managerRoot.mkdirs();
687
			if (!managerRoot.exists())
697
			if (!managerRoot.exists())
688
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
698
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
699
			if (openCleanup)
700
				cleanup(true);
689
			boolean locked = lock(wait);
701
			boolean locked = lock(wait);
690
			if (!locked && wait)
702
			if (!locked && wait)
691
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
703
				throw new IOException(EclipseAdaptorMsg.fileManager_cannotLock);
(-)src/org/eclipse/osgi/tests/services/datalocation/FileManagerTests.java (-68 / +113 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2006 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-17 Link Here
11
package org.eclipse.osgi.tests.services.datalocation;
11
package org.eclipse.osgi.tests.services.datalocation;
12
12
13
import java.io.*;
13
import java.io.*;
14
15
import junit.framework.Test;
14
import junit.framework.Test;
16
import junit.framework.TestSuite;
15
import junit.framework.TestSuite;
17
import org.eclipse.core.runtime.Platform;
16
import org.eclipse.core.runtime.Platform;
Lines 54-68 Link Here
54
		if (file.isDirectory()) {
53
		if (file.isDirectory()) {
55
			File[] list = file.listFiles();
54
			File[] list = file.listFiles();
56
			if (list != null) {
55
			if (list != null) {
57
				for (int idx=0; idx<list.length; idx++) {
56
				for (int idx = 0; idx < list.length; idx++) {
58
					rm(list[idx]);
57
					rm(list[idx]);
59
				}
58
				}
60
			}
59
			}
61
		}
60
		}
62
		file.delete();
61
		file.delete();
63
	}
62
	}
64
	
63
65
	
66
	/**
64
	/**
67
	 * Open a filemanager in readonly and ensure files are
65
	 * Open a filemanager in readonly and ensure files are
68
	 * not created.
66
	 * not created.
Lines 76-82 Link Here
76
		manager1 = new StorageManager(testDir, null, true);
74
		manager1 = new StorageManager(testDir, null, true);
77
		try {
75
		try {
78
			manager1.open(true);
76
			manager1.open(true);
79
		} catch(IOException e) {
77
		} catch (IOException e) {
80
			fail("unexpected exception", e);
78
			fail("unexpected exception", e);
81
		}
79
		}
82
		files = testDir.list();
80
		files = testDir.list();
Lines 84-104 Link Here
84
		try {
82
		try {
85
			manager1.add(fileName);
83
			manager1.add(fileName);
86
			fail("add succedded");
84
			fail("add succedded");
87
		} catch(IOException e) {
85
		} catch (IOException e) {
88
			//good
86
			//good
89
		}
87
		}
90
		
88
91
		try {
89
		try {
92
			manager1.lookup(fileName, true);
90
			manager1.lookup(fileName, true);
93
			fail("lookup succedded");
91
			fail("lookup succedded");
94
		} catch(IOException e) {
92
		} catch (IOException e) {
95
			//good
93
			//good
96
		}
94
		}
97
		
95
98
		try {
96
		try {
99
			manager1.createTempFile(fileName);
97
			manager1.createTempFile(fileName);
100
			fail("create temp file succedded");
98
			fail("create temp file succedded");
101
		} catch(IOException e) {
99
		} catch (IOException e) {
102
			//good
100
			//good
103
		}
101
		}
104
		files = testDir.list();
102
		files = testDir.list();
Lines 116-127 Link Here
116
			manager2.update(new String[] {fileName}, new String[] {tmpFile.getName()});
114
			manager2.update(new String[] {fileName}, new String[] {tmpFile.getName()});
117
			manager2.close();
115
			manager2.close();
118
			manager2 = null;
116
			manager2 = null;
119
		} catch(IOException e) {
117
		} catch (IOException e) {
120
			fail("unexpected exception", e);
118
			fail("unexpected exception", e);
121
		}		
119
		}
122
	}
120
	}
123
	
121
124
	
125
	void writeToFile(File file, String str) throws IOException {
122
	void writeToFile(File file, String str) throws IOException {
126
		FileOutputStream fos = new FileOutputStream(file);
123
		FileOutputStream fos = new FileOutputStream(file);
127
		try {
124
		try {
Lines 133-139 Link Here
133
		}
130
		}
134
	}
131
	}
135
132
136
	
137
	private String getInputStreamContents(InputStream is) throws IOException {
133
	private String getInputStreamContents(InputStream is) throws IOException {
138
		StringBuffer sb = new StringBuffer();
134
		StringBuffer sb = new StringBuffer();
139
		byte[] data = new byte[64];
135
		byte[] data = new byte[64];
Lines 141-154 Link Here
141
		try {
137
		try {
142
			while ((len = is.read(data)) != -1) {
138
			while ((len = is.read(data)) != -1) {
143
				sb.append(new String(data, 0, len));
139
				sb.append(new String(data, 0, len));
144
			} 
140
			}
145
		} finally {
141
		} finally {
146
			is.close();
142
			is.close();
147
		}
143
		}
148
		return sb.toString();
144
		return sb.toString();
149
	}
145
	}
150
	
146
151
	
152
	/**
147
	/**
153
	 * This tests a FM update where the a specific file is managed with a 
148
	 * This tests a FM update where the a specific file is managed with a 
154
	 * revision number of (n) and an update is requested while there already
149
	 * revision number of (n) and an update is requested while there already
Lines 161-170 Link Here
161
		manager1 = new StorageManager(base, null);
156
		manager1 = new StorageManager(base, null);
162
		try {
157
		try {
163
			manager1.open(false);
158
			manager1.open(false);
164
			File file1 = new File(base, testFile+".1");
159
			File file1 = new File(base, testFile + ".1");
165
			File file2 = new File(base, testFile+".2");
160
			File file2 = new File(base, testFile + ".2");
166
			File file3 = new File(base, testFile+".3");
161
			File file3 = new File(base, testFile + ".3");
167
			File file4 = new File(base, testFile+".4");
162
			File file4 = new File(base, testFile + ".4");
168
			if (file1.exists() || file2.exists() || file3.exists() || file4.exists()) {
163
			if (file1.exists() || file2.exists() || file3.exists() || file4.exists()) {
169
				fail("Test files already exists.");
164
				fail("Test files already exists.");
170
				return;
165
				return;
Lines 178-184 Link Here
178
				fail("Failed to create a single test file");
173
				fail("Failed to create a single test file");
179
				return;
174
				return;
180
			}
175
			}
181
			
176
182
			// create file version (2) outside filemanager
177
			// create file version (2) outside filemanager
183
			writeToFile(file2, "file2 exists");
178
			writeToFile(file2, "file2 exists");
184
179
Lines 190-196 Link Here
190
				fail("Failed to skip existing filemanager file.");
185
				fail("Failed to skip existing filemanager file.");
191
				return;
186
				return;
192
			}
187
			}
193
			
188
194
			// open a new manager, ensure a lookup results in file version (3)
189
			// open a new manager, ensure a lookup results in file version (3)
195
			manager2 = new StorageManager(base, null);
190
			manager2 = new StorageManager(base, null);
196
			manager2.open(true);
191
			manager2.open(true);
Lines 203-220 Link Here
203
			assertTrue(file.exists());
198
			assertTrue(file.exists());
204
			FileInputStream fis = new FileInputStream(file);
199
			FileInputStream fis = new FileInputStream(file);
205
			assertEquals(getInputStreamContents(fis), "file 3 contents");
200
			assertEquals(getInputStreamContents(fis), "file 3 contents");
206
			
201
207
			manager2.close();
202
			manager2.close();
208
			manager2=null;
203
			manager2 = null;
209
			
204
210
			manager1.close();
205
			manager1.close();
211
			manager1=null;
206
			manager1 = null;
212
		} catch(IOException e) {
207
		} catch (IOException e) {
213
			fail("unexpected exception", e);
208
			fail("unexpected exception", e);
214
		}
209
		}
215
	}
210
	}
216
211
217
	
218
	/**
212
	/**
219
	 * This tests that FM apis throw exceptions if FM has not yet been opened
213
	 * This tests that FM apis throw exceptions if FM has not yet been opened
220
	 * or if FM has been closed.  
214
	 * or if FM has been closed.  
Lines 231-247 Link Here
231
			this.writeToFile(tmpFile, "File exists");
225
			this.writeToFile(tmpFile, "File exists");
232
			manager1.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
226
			manager1.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
233
			manager1.add(scratchFile);
227
			manager1.add(scratchFile);
234
		} catch(IOException e) {
228
		} catch (IOException e) {
235
			fail("unexpected exception", e);
229
			fail("unexpected exception", e);
236
		}
230
		}
237
		
231
238
		// create a new manager, and try making calls
232
		// create a new manager, and try making calls
239
		manager2 = new StorageManager(base, null);
233
		manager2 = new StorageManager(base, null);
240
		checkOpen(false, permanentFile, scratchFile);
234
		checkOpen(false, permanentFile, scratchFile);
241
		// open the manager, try again
235
		// open the manager, try again
242
		try {
236
		try {
243
			manager2.open(true);
237
			manager2.open(true);
244
		} catch(IOException e) {
238
		} catch (IOException e) {
245
			fail("unexpected exception", e);
239
			fail("unexpected exception", e);
246
		}
240
		}
247
		checkOpen(true, permanentFile, scratchFile);
241
		checkOpen(true, permanentFile, scratchFile);
Lines 249-255 Link Here
249
		manager2.close();
243
		manager2.close();
250
		checkOpen(false, permanentFile, scratchFile);
244
		checkOpen(false, permanentFile, scratchFile);
251
		manager2 = null;
245
		manager2 = null;
252
		
246
253
		manager1.close();
247
		manager1.close();
254
		manager1 = null;
248
		manager1 = null;
255
	}
249
	}
Lines 261-267 Link Here
261
			if (!open)
255
			if (!open)
262
				fail("add did not fail.");
256
				fail("add did not fail.");
263
			manager2.remove("failFile");
257
			manager2.remove("failFile");
264
		} catch(IOException e) {
258
		} catch (IOException e) {
265
			if (open)
259
			if (open)
266
				fail("unexpected exception", e);
260
				fail("unexpected exception", e);
267
		}
261
		}
Lines 271-277 Link Here
271
			manager2.lookup(permanentFile, false);
265
			manager2.lookup(permanentFile, false);
272
			if (!open)
266
			if (!open)
273
				fail("lookup did not fail.");
267
				fail("lookup did not fail.");
274
		} catch(IOException e) {
268
		} catch (IOException e) {
275
			if (open)
269
			if (open)
276
				fail("unexpected exception", e);
270
				fail("unexpected exception", e);
277
		}
271
		}
Lines 283-295 Link Here
283
		} catch (IOException e) {
277
		} catch (IOException e) {
284
			fail("unexpected exception", e);
278
			fail("unexpected exception", e);
285
			tmpFile = null;
279
			tmpFile = null;
286
		}	
280
		}
287
		if (tmpFile != null) {
281
		if (tmpFile != null) {
288
			try {
282
			try {
289
				manager2.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
283
				manager2.update(new String[] {permanentFile}, new String[] {tmpFile.getName()});
290
				if (!open)
284
				if (!open)
291
					fail("update did not fail.");
285
					fail("update did not fail.");
292
			} catch(IOException e) {
286
			} catch (IOException e) {
293
				if (open)
287
				if (open)
294
					fail("unexpected exception", e);
288
					fail("unexpected exception", e);
295
			}
289
			}
Lines 299-313 Link Here
299
			manager2.remove(scratchFile);
293
			manager2.remove(scratchFile);
300
			if (!open)
294
			if (!open)
301
				fail("remove did not fail");
295
				fail("remove did not fail");
302
			else // add the file back if expected to complete
296
			else
297
				// add the file back if expected to complete
303
				manager2.add(scratchFile);
298
				manager2.add(scratchFile);
304
			} catch(IOException e) {
299
		} catch (IOException e) {
305
			if (open)
300
			if (open)
306
				fail("unexpected exception", e);
301
				fail("unexpected exception", e);
307
		}
302
		}
308
	}
303
	}
309
	
304
310
	
311
	/**
305
	/**
312
	 * This tests FM remove() then add().  On a remove(), the file can not be deleted as it may
306
	 * This tests FM remove() then add().  On a remove(), the file can not be deleted as it may
313
	 * be in use by another FM. Then add() the file back and see if the version number written to
307
	 * be in use by another FM. Then add() the file back and see if the version number written to
Lines 319-327 Link Here
319
	 */
313
	 */
320
	public void testRemoveThenAdd() {
314
	public void testRemoveThenAdd() {
321
		String fileName = "testRemoveThenAdd.txt";
315
		String fileName = "testRemoveThenAdd.txt";
322
		File file1 = new File(base, fileName+".1");
316
		File file1 = new File(base, fileName + ".1");
323
		File file2 = new File(base, fileName+".2");
317
		File file2 = new File(base, fileName + ".2");
324
		File file3 = new File(base, fileName+".3");
318
		File file3 = new File(base, fileName + ".3");
325
		manager1 = new StorageManager(base, null);
319
		manager1 = new StorageManager(base, null);
326
		// create a permanent file
320
		// create a permanent file
327
		try {
321
		try {
Lines 340-346 Link Here
340
			// sanity check
334
			// sanity check
341
			if (file1.exists() || !file2.exists() || file3.exists())
335
			if (file1.exists() || !file2.exists() || file3.exists())
342
				fail("Failed creating a file revision");
336
				fail("Failed creating a file revision");
343
			
337
344
			manager2 = new StorageManager(base, null);
338
			manager2 = new StorageManager(base, null);
345
			manager2.open(true);
339
			manager2.open(true);
346
			manager2.remove(fileName);
340
			manager2.remove(fileName);
Lines 359-365 Link Here
359
			assertNotNull(testFile);
353
			assertNotNull(testFile);
360
			assertTrue(testFile.getName().endsWith(".3"));
354
			assertTrue(testFile.getName().endsWith(".3"));
361
			assertTrue(file3.exists());
355
			assertTrue(file3.exists());
362
			
356
363
			// open a new manager, ensure that the database was updated
357
			// open a new manager, ensure that the database was updated
364
			// by checking version is also #3
358
			// by checking version is also #3
365
			manager1 = new StorageManager(base, null);
359
			manager1 = new StorageManager(base, null);
Lines 367-382 Link Here
367
			testFile = manager1.lookup(fileName, false);
361
			testFile = manager1.lookup(fileName, false);
368
			assertNotNull(testFile);
362
			assertNotNull(testFile);
369
			assertTrue(testFile.getName().endsWith(".3"));
363
			assertTrue(testFile.getName().endsWith(".3"));
370
			
364
371
			manager1.close();
365
			manager1.close();
372
			manager1 = null;
366
			manager1 = null;
373
			manager2.close();
367
			manager2.close();
374
			manager2 = null;
368
			manager2 = null;
375
		} catch(IOException e) {
369
		} catch (IOException e) {
376
			fail("unexpected exception", e);
370
			fail("unexpected exception", e);
377
		}
371
		}
378
	}
372
	}
379
	
373
380
	/**
374
	/**
381
	 * Test multiple FM do not cleanup any old files as they may be in use.
375
	 * Test multiple FM do not cleanup any old files as they may be in use.
382
	 */
376
	 */
Lines 388-395 Link Here
388
			// this is a Windows-only test
382
			// this is a Windows-only test
389
			return;
383
			return;
390
		String fileName = "testMultipleFileManagers.txt";
384
		String fileName = "testMultipleFileManagers.txt";
391
		File file1 = new File(base, fileName+".1");
385
		File file1 = new File(base, fileName + ".1");
392
		File file2 = new File(base, fileName+".2");
386
		File file2 = new File(base, fileName + ".2");
393
		manager1 = new StorageManager(base, null);
387
		manager1 = new StorageManager(base, null);
394
		try {
388
		try {
395
			manager1.open(true);
389
			manager1.open(true);
Lines 398-410 Link Here
398
			file = manager1.createTempFile(fileName);
392
			file = manager1.createTempFile(fileName);
399
			writeToFile(file, "test contents #1");
393
			writeToFile(file, "test contents #1");
400
			manager1.update(new String[] {fileName}, new String[] {file.getName()});
394
			manager1.update(new String[] {fileName}, new String[] {file.getName()});
401
			
395
402
			// ensure file is version #1
396
			// ensure file is version #1
403
			file = manager1.lookup(fileName, false);
397
			file = manager1.lookup(fileName, false);
404
			assertNotNull(file);
398
			assertNotNull(file);
405
			assertTrue(file.getName().endsWith(".1"));
399
			assertTrue(file.getName().endsWith(".1"));
406
			assertTrue(file1.exists());
400
			assertTrue(file1.exists());
407
			
401
408
			//new fileMangager using version #1
402
			//new fileMangager using version #1
409
			manager2 = new StorageManager(base, null);
403
			manager2 = new StorageManager(base, null);
410
			manager2.open(true);
404
			manager2.open(true);
Lines 413-419 Link Here
413
			assertNotNull(file);
407
			assertNotNull(file);
414
			assertTrue(file.getName().endsWith(".1"));
408
			assertTrue(file.getName().endsWith(".1"));
415
			assertTrue(file1.exists() && !file2.exists());
409
			assertTrue(file1.exists() && !file2.exists());
416
			
410
417
			// back to manager #1, update file again, close
411
			// back to manager #1, update file again, close
418
			file = manager1.createTempFile(fileName);
412
			file = manager1.createTempFile(fileName);
419
			writeToFile(file, "test contents #2");
413
			writeToFile(file, "test contents #2");
Lines 426-432 Link Here
426
			// both files better still exists
420
			// both files better still exists
427
			assertTrue(file1.exists());
421
			assertTrue(file1.exists());
428
			assertTrue(file2.exists());
422
			assertTrue(file2.exists());
429
			
423
430
			// manager #2
424
			// manager #2
431
			// sanity check
425
			// sanity check
432
			file = manager2.lookup(fileName, false);
426
			file = manager2.lookup(fileName, false);
Lines 437-456 Link Here
437
			manager2 = null;
431
			manager2 = null;
438
			assertTrue(!file1.exists());
432
			assertTrue(!file1.exists());
439
			assertTrue(file2.exists());
433
			assertTrue(file2.exists());
440
			
434
441
			// new manager1, does it get version 1?
435
			// new manager1, does it get version 1?
442
			manager1 = new StorageManager(base, null);
436
			manager1 = new StorageManager(base, null);
443
			manager1.open(true);
437
			manager1.open(true);
444
			file = manager1.lookup(fileName, false);
438
			file = manager1.lookup(fileName, false);
445
			assertNotNull(file);
439
			assertNotNull(file);
446
			assertTrue(file.getName().endsWith(".2"));
440
			assertTrue(file.getName().endsWith(".2"));
447
			manager1.close();		
441
			manager1.close();
448
			manager1 = null;
442
			manager1 = null;
449
		} catch(IOException e) {
443
		} catch (IOException e) {
450
			fail("unexpected exception", e);
444
			fail("unexpected exception", e);
451
		}
445
		}
452
	}
446
	}
453
	
447
454
	/**
448
	/**
455
	 * This test will verify that a FM will fail if a lock is held
449
	 * This test will verify that a FM will fail if a lock is held
456
	 */
450
	 */
Lines 460-470 Link Here
460
			// this is a Windows-only test
454
			// this is a Windows-only test
461
			return;
455
			return;
462
		String fileName = "testJavaIOLocking";
456
		String fileName = "testJavaIOLocking";
463
		File lockFile = new File(new File(base,".manager"),".fileTableLock");
457
		File lockFile = new File(new File(base, ".manager"), ".fileTableLock");
464
		lockFile.getParentFile().mkdirs();
458
		lockFile.getParentFile().mkdirs();
465
		try {
459
		try {
466
			new FileOutputStream(lockFile).close();
460
			new FileOutputStream(lockFile).close();
467
		} catch(IOException e) {
461
		} catch (IOException e) {
468
			fail("unexpected exception", e);
462
			fail("unexpected exception", e);
469
		}
463
		}
470
		assertTrue(lockFile.exists());
464
		assertTrue(lockFile.exists());
Lines 476-505 Link Here
476
			try {
470
			try {
477
				manager1.open(true); // wait for lock
471
				manager1.open(true); // wait for lock
478
				fail("open with lock succedded");
472
				fail("open with lock succedded");
479
			} catch(IOException e) {
473
			} catch (IOException e) {
480
				//good
474
				//good
481
			}
475
			}
482
			
476
483
			manager1.open(false); // don't wait, should work
477
			manager1.open(false); // don't wait, should work
484
			try {
478
			try {
485
				manager1.add(fileName);
479
				manager1.add(fileName);
486
				fail("add succedded");
480
				fail("add succedded");
487
			} catch(IOException e) {
481
			} catch (IOException e) {
488
				//good
482
				//good
489
			}
483
			}
490
			//sanity check, file should not be managed
484
			//sanity check, file should not be managed
491
			assertNull(manager1.lookup(fileName, false));
485
			assertNull(manager1.lookup(fileName, false));
492
			manager1.close();
486
			manager1.close();
493
			manager1 = null;
487
			manager1 = null;
494
		} catch(IOException e) {
488
		} catch (IOException e) {
495
			fail("unexpected exception", e);
489
			fail("unexpected exception", e);
496
		} finally {
490
		} finally {
497
			try {
491
			try {
498
				if (fos != null)
492
				if (fos != null)
499
					fos.close();
493
					fos.close();
500
			} catch(IOException e) {
494
			} catch (IOException e) {
501
				fail("unexpected exception", e);
495
				fail("unexpected exception", e);
502
			}
496
			}
503
		}
497
		}
504
	}
498
	}
499
500
	public void testCleanup() {
501
		String fileName = "testCleanup.txt";
502
		File file1 = new File(base, fileName + ".1");
503
		File file2 = new File(base, fileName + ".2");
504
		File file3 = new File(base, fileName + ".3");
505
		System.setProperty("osgi.embedded.cleanupOnOpen", "true");
506
		// create a permanent file
507
		try {
508
			manager1 = new StorageManager(base, null);
509
			manager1.open(true);
510
			manager1.add(fileName);
511
			// create version (1)
512
			File tmpFile = manager1.createTempFile(fileName);
513
			writeToFile(tmpFile, "File exists #1");
514
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
515
			// sanity check
516
			assertTrue(file1.toString(), file1.exists());
517
			assertFalse(file2.toString(), file2.exists());
518
			assertFalse(file3.toString(), file3.exists());
519
520
			// do it again, now version (2)
521
			tmpFile = manager1.createTempFile(fileName);
522
			writeToFile(tmpFile, "File exists #2");
523
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
524
			// sanity check
525
			assertFalse(file1.toString(), file1.exists());
526
			assertTrue(file2.toString(), file2.exists());
527
			assertFalse(file3.toString(), file3.exists());
528
529
			// do it again, now version (3)
530
			tmpFile = manager1.createTempFile(fileName);
531
			writeToFile(tmpFile, "File exists #3");
532
			manager1.update(new String[] {fileName}, new String[] {tmpFile.getName()});
533
			// sanity check
534
			assertFalse(file1.toString(), file1.exists());
535
			assertFalse(file2.toString(), file2.exists());
536
			assertTrue(file3.toString(), file3.exists());
537
538
			manager1.close(); // force a cleanup
539
			manager1 = null;
540
			// sanity check
541
			assertFalse(file1.toString(), file1.exists());
542
			assertFalse(file2.toString(), file2.exists());
543
			assertTrue(file3.toString(), file3.exists());
544
		} catch (IOException e) {
545
			fail("unexpected exception", e);
546
		} finally {
547
			System.setProperty("osgi.embedded.cleanupOnOpen", "false");
548
		}
549
	}
505
}
550
}
(-)src/org/eclipse/osgi/tests/services/datalocation/StreamManagerTests.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 IBM Corporation and others.
2
 * Copyright (c) 2004, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 184-189 Link Here
184
			manager1 = null;
184
			manager1 = null;
185
		} catch (IOException e) {
185
		} catch (IOException e) {
186
			fail("unexepected exception", e);
186
			fail("unexepected exception", e);
187
		} finally {
188
			System.setProperty("osgi.useReliableFiles", "false"); // force reliable files off
187
		}
189
		}
188
	}
190
	}
189
191

Return to bug 259981