Changeset 889
- Timestamp:
- 2008-12-01 17:24:52 (4 years ago)
- Location:
- trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock
- Files:
-
- 2 edited
-
DbMockFinder.java (modified) (5 diffs)
-
DbMockUtils.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock/DbMockFinder.java
r887 r889 36 36 import org.restlet.data.Form; 37 37 38 import javax.annotation.PreDestroy; 39 38 40 /** 39 41 * Finder class that creates a MockResource class depending on an URI 40 *41 *42 42 */ 43 43 public class DbMockFinder extends Finder { … … 45 45 private String entitiesLocation; 46 46 private String tempStore; 47 private boolean deleteTempStore; 47 48 48 49 private enum InitState { WAITING, SUCCESS, FAILED } … … 56 57 57 58 public DbMockFinder(Context context, KauriModule module, String entitiesLocation) { 58 this(context, module, entitiesLocation, System.getProperty("java.io.tmpdir") + File.separator + ("dbmock"));59 this(context, module, entitiesLocation, null); 59 60 } 60 61 … … 63 64 this.module = module; 64 65 this.entitiesLocation = entitiesLocation; 65 this.tempStore = tempStore; 66 67 if (tempStore == null) { 68 this.tempStore = getTempStoreDir(); 69 this.deleteTempStore = true; 70 } else { 71 // if the user specified the temp store location himselve, then we assume the user 72 // wants to keep data between restarts, so don't delete it 73 this.tempStore = tempStore; 74 this.deleteTempStore = false; 75 } 76 } 77 78 private String getTempStoreDir() { 79 String suffix = (System.currentTimeMillis() % 100000) + "" + (int)(Math.random() * 100000); 80 while (true) { 81 String dirName = System.getProperty("java.io.tmpdir") + File.separator + ("dbmock_") + suffix; 82 File dir = new File(dirName); 83 if (dir.exists()) { 84 log.debug("Temporary storage directory already exists, trying another location. Currenty tried: " + dirName); 85 continue; 86 } 87 88 boolean dirCreated = dir.mkdirs(); 89 if (!dirCreated) { 90 throw new RuntimeException("Failed to created temporary storage directory at " + dirName); 91 } 92 return dir.getAbsolutePath(); 93 } 66 94 } 67 95 … … 195 223 this.tempStore = tempStore; 196 224 } 225 226 @PreDestroy 227 public void destroy() { 228 DbMockUtils.deleteDirectory(new File(this.tempStore)); 229 } 197 230 } -
trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock/DbMockUtils.java
r880 r889 367 367 } 368 368 } 369 370 public static void deleteDirectory(File dir) { 371 File[] files = dir.listFiles(); 372 if (files != null) { 373 for (File file : files) { 374 if (file.isDirectory()) { 375 deleteDirectory(file); 376 } else { 377 boolean deleted = file.delete(); 378 if (!deleted) 379 throw new RuntimeException("Could not delete file " + file.getAbsolutePath()); 380 } 381 } 382 } 383 if (!dir.delete()) 384 throw new RuntimeException("Could not delete file or directory " + dir.getAbsolutePath()); 385 } 369 386 } 370 387
Note: See TracChangeset
for help on using the changeset viewer.