Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / io / JarStorageFactory.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/io/JarStorageFactory.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/io/JarStorageFactory.java
new file mode 100644 (file)
index 0000000..bc27177
--- /dev/null
@@ -0,0 +1,138 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.io.JarStorageFactory\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.io;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.zip.ZipFile;\r
+\r
+import org.apache.derby.io.StorageFile;\r
+\r
+/**\r
+ * This class provides a Jar file based implementation of the StorageFactory interface. It is used by the\r
+ * database engine to access persistent data and transaction logs under the jar subsubprotocol.\r
+ */\r
+\r
+public class JarStorageFactory extends BaseStorageFactory\r
+{\r
+    ZipFile zipData;\r
+    \r
+    /**\r
+     * Construct a persistent StorageFile from a path name.\r
+     *\r
+     * @param path The path name of the file\r
+     *\r
+     * @return A corresponding StorageFile object\r
+     */\r
+    StorageFile newPersistentFile( String path)\r
+    {\r
+        return new JarDBFile( this, path);\r
+    }\r
+\r
+    /**\r
+     * Construct a StorageFile from a directory and file name.\r
+     *\r
+     * @param directoryName The directory part of the path name. Must not be null, nor may it be in the temp dir.\r
+     * @param fileName The name of the file within the directory.\r
+     *\r
+     * @return A corresponding StorageFile object\r
+     */\r
+    StorageFile newPersistentFile( String directoryName, String fileName)\r
+    {\r
+        if( directoryName == null || directoryName.length() == 0)\r
+            return newPersistentFile( fileName);\r
+        return new JarDBFile( this, directoryName, fileName);\r
+    }\r
+\r
+    /**\r
+     * Construct a StorageFile from a directory and file name.\r
+     *\r
+     * @param directoryName The directory part of the path name.\r
+     * @param fileName The name of the file within the directory.\r
+     *\r
+     * @return A corresponding StorageFile object\r
+     */\r
+    StorageFile newPersistentFile( StorageFile directoryName, String fileName)\r
+    {\r
+        if( directoryName == null)\r
+            return newPersistentFile( fileName);\r
+        return new JarDBFile( (JarDBFile) directoryName, fileName);\r
+    }\r
+\r
+    void doInit() throws IOException\r
+    {\r
+        if( dataDirectory == null)\r
+            return;\r
+        // Parse the dataDirectory name. It should be of the form "(jar-file)directory" or "jar-file"\r
+        int offset = 0;\r
+        while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset)))\r
+            offset ++;\r
+        int leftParen = -1;\r
+        int rightParen = -1;\r
+        if( offset < dataDirectory.length())\r
+        {\r
+            leftParen = dataDirectory.indexOf( '(', offset);\r
+            if( leftParen >= 0)\r
+                rightParen = dataDirectory.indexOf( ')', leftParen + 1);\r
+        }\r
+        File jarFile = null;\r
+        if( rightParen > 0)\r
+        {\r
+            jarFile = getJarFile( dataDirectory.substring( leftParen + 1, rightParen));\r
+            offset = rightParen + 1;\r
+            while( offset < dataDirectory.length() & Character.isSpaceChar( dataDirectory.charAt( offset)))\r
+                offset ++;\r
+            dataDirectory = dataDirectory.substring( offset, dataDirectory.length());\r
+        }\r
+        else\r
+        {\r
+            jarFile = getJarFile( dataDirectory);\r
+            dataDirectory = "";\r
+        }\r
+        zipData = new ZipFile( jarFile);\r
+        canonicalName = "(" + jarFile.getCanonicalPath() + ")" + dataDirectory;\r
+        separatedDataDirectory = dataDirectory + '/'; // Zip files use '/' as a separator\r
+        createTempDir();\r
+    } // end of doInit\r
+    \r
+    /**\r
+     * Close the opened jar/zip file on shutdown.\r
+     * (Fix for DERBY-2083).\r
+     */\r
+    public void shutdown() {\r
+        if (zipData != null) {\r
+            try {\r
+                zipData.close();\r
+            } catch (IOException e) {\r
+            }\r
+            zipData = null;\r
+        }\r
+    }\r
+\r
+    private File getJarFile( String name)\r
+    {\r
+        File jarFile = new File( name);\r
+        if( home != null && !jarFile.isAbsolute())\r
+            jarFile = new File( home, name);\r
+        return jarFile;\r
+    } // end of getJarFile\r
+}\r