Changeset 889


Ignore:
Timestamp:
2008-12-01 17:24:52 (4 years ago)
Author:
bruno
Message:

dbmock: in case no temp dir is specified, use random one, to avoid conflicts when having multiple dbmock instances. Delete it on exit.

Location:
trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock/DbMockFinder.java

    r887 r889  
    3636import org.restlet.data.Form; 
    3737 
     38import javax.annotation.PreDestroy; 
     39 
    3840/** 
    3941 * Finder class that creates a MockResource class depending on an URI 
    40  *  
    41  *  
    4242 */ 
    4343public class DbMockFinder extends Finder { 
     
    4545    private String entitiesLocation; 
    4646    private String tempStore; 
     47    private boolean deleteTempStore; 
    4748 
    4849    private enum InitState { WAITING, SUCCESS, FAILED } 
     
    5657 
    5758    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); 
    5960    } 
    6061 
     
    6364        this.module = module; 
    6465        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        } 
    6694    } 
    6795 
     
    195223        this.tempStore = tempStore; 
    196224    } 
     225 
     226    @PreDestroy 
     227    public void destroy() { 
     228        DbMockUtils.deleteDirectory(new File(this.tempStore)); 
     229    } 
    197230} 
  • trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/main/java/org/kauriproject/dbmock/DbMockUtils.java

    r880 r889  
    367367        } 
    368368    } 
     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    } 
    369386} 
    370387 
Note: See TracChangeset for help on using the changeset viewer.