1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.lang.*; 10 import java.io.*; 11 import java.util.*; 12 import SOFA.SOFAnet.Core.Reporter; 13 14 19 abstract public class StorageBase 20 { 21 public Map map; 22 protected File baseDir; 23 protected String extension; 24 protected boolean loadOnInit; 25 26 27 34 public StorageBase(File baseDir, String extension, boolean loadOnInit) 35 { 36 this.baseDir = baseDir; 37 this.extension = extension; 38 this.loadOnInit = loadOnInit; 39 map = Collections.synchronizedMap(new HashMap()); 40 } 41 42 abstract protected StorageItem newItem(String name, File file); 43 44 47 public void init() 48 { 49 File [] files = baseDir.listFiles(); 50 if (files == null) 51 { 52 Reporter.error("Cannot open directory '" + baseDir + "'"); 53 return; 54 } 55 56 for (int i = 0; i < files.length; i++) 57 { 58 if (!files[i].isDirectory()) 59 { 60 String name = files[i].getName(); 61 if (name.endsWith("." + extension)) 62 { 63 name = name.substring(0, name.length() - extension.length() - 1); 64 StorageItem item = newItem(name, files[i].getAbsoluteFile()); 65 if (item != null) 66 { 67 boolean toBeDeleted = false; 68 if (loadOnInit) 69 { 70 item.loadFromStorage(); 71 toBeDeleted = item.toBeDeleted(); 72 } 73 if (toBeDeleted) item.deleteFromStorage(); 74 else map.put(item.getName(), item); 75 } 76 } 77 } 78 } 79 } 80 81 } 82 | Popular Tags |