1 5 package org.h2.tools; 6 7 import java.sql.SQLException ; 8 import java.util.ArrayList ; 9 10 import org.h2.engine.Constants; 11 import org.h2.util.FileUtils; 12 13 16 17 public abstract class FileBase { 18 19 protected boolean allFiles() { 20 return false; 21 } 22 23 32 public static ArrayList getDatabaseFiles(String dir, String db, boolean all) throws SQLException { 33 ArrayList files = new ArrayList (); 34 if(dir == null || dir.equals("")) { 35 dir = "."; 36 } 37 String start = db == null ? null : FileUtils.normalize(dir + "/" + db); 38 String [] list = FileUtils.listFiles(dir); 39 for(int i=0; list!=null && i<list.length; i++) { 40 String f = list[i]; 41 boolean ok = false; 42 if(f.endsWith(Constants.SUFFIX_DATA_FILE)) { 43 ok = true; 44 } else if(f.endsWith(Constants.SUFFIX_INDEX_FILE)) { 45 ok = true; 46 } else if(f.endsWith(Constants.SUFFIX_LOG_FILE)) { 47 ok = true; 48 } else if(f.endsWith(Constants.SUFFIX_HASH_FILE)) { 49 ok = true; 50 } else if(f.endsWith(Constants.SUFFIX_LOBS_DIRECTORY)) { 51 files.addAll(getDatabaseFiles(f, null, all)); 52 ok = true; 53 } else if(f.endsWith(Constants.SUFFIX_LOB_FILE)) { 54 ok = true; 55 } else if(f.endsWith(Constants.SUFFIX_SUMMARY_FILE)) { 56 ok = true; 57 } else if(all) { 58 if(f.endsWith(Constants.SUFFIX_LOCK_FILE)) { 59 ok = true; 60 } else if(f.endsWith(Constants.SUFFIX_TEMP_FILE)) { 61 ok = true; 62 } else if(f.endsWith(Constants.SUFFIX_TRACE_FILE)) { 63 ok = true; 64 } 65 } 66 if(ok) { 67 if(db == null || FileUtils.fileStartsWith(f, start+".") || FileUtils.isInMemory(dir)) { 68 String fileName = f; 69 files.add(fileName); 70 } 71 } 72 } 73 return files; 74 } 75 76 protected void processFiles(String dir, String db, boolean log) throws SQLException { 77 ArrayList files = getDatabaseFiles(dir, db, allFiles()); 78 for(int i=0; i<files.size(); i++) { 79 String fileName = (String ) files.get(i); 80 process(fileName); 81 if(log) { 82 System.out.println("processed: "+fileName); 83 } 84 } 85 if(files.size() == 0 && log) { 86 System.out.println("No database files found"); 87 } 88 } 89 90 protected abstract void process(String fileName) throws SQLException ; 91 92 } 93 | Popular Tags |