xref: /trunk/main/hsqldb/patches/aoo_integration.patch (revision 7bb36cacf8246652ddb726ae355ac9c22563401f)
1--- misc/hsqldb/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/Logger.java
2+++ misc/build/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/Logger.java
3@@ -34,6 +34,7 @@
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7+import java.lang.reflect.Constructor;
8 import java.text.SimpleDateFormat;
9 import java.util.Locale;
10 import java.util.concurrent.atomic.AtomicInteger;
11@@ -137,6 +138,7 @@
12     private Crypto    crypto;
13     boolean           cryptLobs;
14     public FileAccess fileAccess;
15+    private boolean   isStoredFileAccess;
16     String            tempDirectoryPath;
17
18     //
19@@ -198,7 +200,12 @@
20         boolean hasFileProps = false;
21         boolean hasScript    = false;
22
23-        fileAccess = FileUtil.getFileAccess(database.isFilesInJar());
24+        fileAccess = newStoredFileAccess();
25+
26+        if (fileAccess == null) {
27+            fileAccess = FileUtil.getFileAccess(database.isFilesInJar());
28+        }
29+
30         propIsFileDatabase          = database.getType().isFileBased();
31         database.databaseProperties = new HsqlDatabaseProperties(database);
32         propTextAllowFullPath = database.databaseProperties.isPropertyTrue(
33@@ -283,6 +290,12 @@
34                 database.databaseProperties.setProperty(
35                     HsqlDatabaseProperties.hsqldb_lock_file, false);
36             }
37+        }
38+
39+        // the shadow file used for incremental backup is not reachable through FileAccess
40+        if (isStoredFileAccess) {
41+            database.databaseProperties.setProperty(
42+                HsqlDatabaseProperties.hsqldb_inc_backup, false);
43         }
44
45         setVariables();
46@@ -391,6 +404,10 @@
47             if (database.getType() == DatabaseType.DB_MEM) {
48                 tempDirectoryPath = database.getProperties().getStringProperty(
49                     HsqlDatabaseProperties.hsqldb_temp_directory);
50+            } else if (isStoredFileAccess) {
51+
52+                // no temporary directory next to the document
53+                tempDirectoryPath = null;
54             } else {
55                 tempDirectoryPath = database.getPath() + ".tmp";
56             }
57@@ -1102,7 +1119,7 @@
58
59     public synchronized void setIncrementBackup(boolean val) {
60
61-        if (val == propIncrementBackup) {
62+        if (val == propIncrementBackup || isStoredFileAccess) {
63             return;
64         }
65
66@@ -1313,8 +1330,67 @@
67
68     public FileAccess getFileAccess() {
69         return fileAccess;
70+    }
71+
72+    /**
73+     * True if the database files are kept in a storage supplied by the host
74+     * application instead of the file system.
75+     */
76+    public boolean isStoredFileAccess() {
77+        return isStoredFileAccess;
78+    }
79+
80+    // oj@openoffice.org - the database files are accessed through a class
81+    // named in the connection properties, with a key identifying the storage
82+    static final String url_fileaccess_class_name = "fileaccess_class_name";
83+    static final String url_storage_class_name    = "storage_class_name";
84+    static final String url_storage_key           = "storage_key";
85+
86+    static Class<?> loadStorageClass(String name) throws ClassNotFoundException {
87+
88+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
89+
90+        if (loader != null) {
91+            try {
92+                return loader.loadClass(name);
93+            } catch (ClassNotFoundException e) {}
94+        }
95+
96+        return Class.forName(name);
97     }
98
99+    private FileAccess newStoredFileAccess() {
100+
101+        String className =
102+            database.urlProperties.getProperty(url_fileaccess_class_name);
103+
104+        if (className == null) {
105+            return null;
106+        }
107+
108+        try {
109+            Class<?> fileAccessClass = loadStorageClass(className);
110+
111+            if (!FileAccess.class.isAssignableFrom(fileAccessClass)) {
112+                throw new ClassCastException(className);
113+            }
114+
115+            Constructor<?> constructor =
116+                fileAccessClass.getConstructor(Object.class);
117+            FileAccess access = (FileAccess) constructor.newInstance(
118+                database.urlProperties.getProperty(url_storage_key));
119+
120+            isStoredFileAccess = true;
121+
122+            return access;
123+        } catch (Throwable t) {
124+            throw Error.error(t, ErrorCode.FILE_IO_ERROR,
125+                              ErrorCode.M_Message_Pair, new Object[] {
126+                t.toString(), className
127+            });
128+        }
129+    }
130+
131     public boolean isFileDatabase() {
132         return propIsFileDatabase;
133     }
134--- misc/hsqldb/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/RAStorageWrapper.java
135+++ misc/build/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/RAStorageWrapper.java
136@@ -32,7 +32,9 @@
137 package org.hsqldb.persist;
138
139 import java.io.IOException;
140+import java.lang.reflect.Constructor;
141
142+import org.hsqldb.Database;
143 import org.hsqldb.lib.Storage;
144
145 /**
146@@ -52,6 +54,36 @@
147         this.file = virtualFile;
148     }
149
150+    /**
151+     * Opens a file in the storage supplied by the host application, using the
152+     * class and key given in the connection properties.
153+     */
154+    static RAStorageWrapper newInstance(Database database, String name,
155+                                        boolean readonly) throws IOException {
156+
157+        String className =
158+            database.urlProperties.getProperty(Logger.url_storage_class_name);
159+
160+        try {
161+            Class<?> storageClass = Logger.loadStorageClass(className);
162+
163+            if (!Storage.class.isAssignableFrom(storageClass)) {
164+                throw new ClassCastException(className);
165+            }
166+
167+            Constructor<?> constructor =
168+                storageClass.getConstructor(String.class, Boolean.class,
169+                                            Object.class);
170+            Storage storage = (Storage) constructor.newInstance(name,
171+                Boolean.valueOf(readonly),
172+                database.urlProperties.getProperty(Logger.url_storage_key));
173+
174+            return new RAStorageWrapper(storage);
175+        } catch (Exception e) {
176+            throw new IOException("invalid storage class: " + e, e);
177+        }
178+    }
179+
180     public long length() throws IOException {
181         return file.length();
182     }
183--- misc/hsqldb/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/RAFile.java
184+++ misc/build/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/RAFile.java
185@@ -92,6 +92,10 @@
186     static RandomAccessInterface newScaledRAFile(Database database,
187             String name, boolean readonly,
188             int type) throws FileNotFoundException, IOException {
189+
190+        if (type == DATA_FILE_STORED) {
191+            return RAStorageWrapper.newInstance(database, name, readonly);
192+        }
193
194         if (type == DATA_FILE_JAR) {
195             return new RAFileInJar(name);
196--- misc/hsqldb/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/DataFileCache.java
197+++ misc/build/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/DataFileCache.java
198@@ -156,7 +156,7 @@
199         cache = new Cache(this);
200
201         try {
202-            dataFile = new RAFileSimple(database.logger, dataFileName, "rw");
203+            dataFile = newSimpleFile(false);
204         } catch (Throwable t) {
205             throw Error.error(ErrorCode.FILE_IO_ERROR, t);
206         }
207@@ -224,6 +224,8 @@
208
209             if (database.isFilesInJar()) {
210                 fileType = RAFile.DATA_FILE_JAR;
211+            } else if (database.logger.isStoredFileAccess()) {
212+                fileType = RAFile.DATA_FILE_STORED;
213             } else if (isNio) {
214                 fileType = RAFile.DATA_FILE_NIO;
215             } else {
216@@ -263,8 +265,7 @@
217             boolean isSaved       = false;
218
219             if (preexists) {
220-                dataFile = new RAFileSimple(database.logger, dataFileName,
221-                                            "r");
222+                dataFile = newSimpleFile(true);
223
224                 long    length       = dataFile.length();
225                 boolean wrongVersion = false;
226@@ -389,7 +390,20 @@
227                               ErrorCode.M_DataFileCache_open, new Object[] {
228                 t.toString(), dataFileName
229             });
230+        }
231+    }
232+
233+    private RandomAccessInterface newSimpleFile(boolean readonly)
234+    throws IOException {
235+
236+        if (database.logger.isStoredFileAccess()) {
237+            return RAStorageWrapper.newInstance(database, dataFileName,
238+                                                readonly);
239         }
240+
241+        return new RAFileSimple(database.logger, dataFileName,
242+                                readonly ? "r"
243+                                         : "rw");
244     }
245
246     boolean setDataSpaceManager() {
247@@ -1368,7 +1382,29 @@
248      * @throws  HsqlException
249      */
250     void backupDataFile(boolean newFile) {
251-        backupFile(database, dataFileName, backupFileName, newFile);
252+
253+        if (!database.logger.isStoredFileAccess() || dataFile == null) {
254+            backupFile(database, dataFileName, backupFileName, newFile);
255+
256+            return;
257+        }
258+
259+        // the storage keeps one handle per file: reading the data file for
260+        // the archive would close the handle held by this cache
261+        try {
262+            dataFile.close();
263+            backupFile(database, dataFileName, backupFileName, newFile);
264+        } catch (IOException e) {
265+            throw Error.error(ErrorCode.DATA_FILE_ERROR, e);
266+        } finally {
267+            try {
268+                dataFile = RAFile.newScaledRAFile(database, dataFileName,
269+                                                  false,
270+                                                  RAFile.DATA_FILE_STORED);
271+            } catch (IOException e) {
272+                throw Error.error(ErrorCode.DATA_FILE_ERROR, e);
273+            }
274+        }
275     }
276
277     void backupNewDataFile(boolean newFile) {
278--- misc/hsqldb/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/LobStoreRAFile.java
279+++ misc/build/hsqldb-2.5.0/hsqldb/src/org/hsqldb/persist/LobStoreRAFile.java
280@@ -70,8 +70,14 @@
281         try {
282             boolean readonly = database.isFilesReadOnly();
283
284-            file = new RAFileSimple(database.logger, fileName, readonly ? "r"
285-                                                                        : "rws");
286+            if (database.logger.isStoredFileAccess()) {
287+                file = RAStorageWrapper.newInstance(database, fileName,
288+                                                    readonly);
289+            } else {
290+                file = new RAFileSimple(database.logger, fileName,
291+                                        readonly ? "r"
292+                                                 : "rws");
293+            }
294         } catch (Throwable t) {
295             throw Error.error(ErrorCode.DATA_FILE_ERROR, t);
296         }
297