KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > FileBase


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools;
6
7 import java.sql.SQLException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 import org.h2.engine.Constants;
11 import org.h2.util.FileUtils;
12
13 /**
14  * @author Thomas
15  */

16
17 public abstract class FileBase {
18     
19     protected boolean allFiles() {
20         return false;
21     }
22     
23     /**
24      * Get the list of database files.
25      *
26      * @param dir the directory (null for the current directory)
27      * @param db the database name (null for all databases)
28      * @param all - if true, files such as the lock, trace, hash index, and lob files are included. If false, only data, index and log files are returned
29      * @return the list of files
30      * @throws SQLException
31      */

32     public static ArrayList JavaDoc getDatabaseFiles(String JavaDoc dir, String JavaDoc db, boolean all) throws SQLException JavaDoc {
33         ArrayList JavaDoc files = new ArrayList JavaDoc();
34         if(dir == null || dir.equals("")) {
35             dir = ".";
36         }
37         String JavaDoc start = db == null ? null : FileUtils.normalize(dir + "/" + db);
38         String JavaDoc[] list = FileUtils.listFiles(dir);
39         for(int i=0; list!=null && i<list.length; i++) {
40             String JavaDoc 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 JavaDoc fileName = f;
69                     files.add(fileName);
70                 }
71             }
72         }
73         return files;
74     }
75
76     protected void processFiles(String JavaDoc dir, String JavaDoc db, boolean log) throws SQLException JavaDoc {
77         ArrayList JavaDoc files = getDatabaseFiles(dir, db, allFiles());
78         for(int i=0; i<files.size(); i++) {
79             String JavaDoc fileName = (String JavaDoc) 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 JavaDoc fileName) throws SQLException JavaDoc;
91
92 }
93
Popular Tags