[Registry-dev] svn commit r16796 - in trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc: dao utils

svn at wso2.org svn at wso2.org
Sun May 11 02:20:59 PDT 2008


Author: chathura
Date: Sun May 11 02:20:59 2008
New Revision: 16796

Log:

Implemented the content caching in the file system.
Now resource contents are cached in the file system (as temporary files) on first access.
Subsequent requests are served from the cache (without referring to the db) if it is available in the cache.

This improves the performance, as it is always necessary to write content to a file before serving a request (whether caching is implemented or not).
So the introducing the caching will minimize the number of file I/O and space used in the file system.



Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileData.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileManager.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/RegistryFileInputStream.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceVersionDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RestoreUtilDAO.java

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	Sun May 11 02:20:59 2008
@@ -21,11 +21,10 @@
 import org.wso2.registry.*;
 import org.wso2.registry.Collection;
 import org.wso2.registry.jdbc.utils.Transaction;
+import org.wso2.registry.jdbc.utils.FileManager;
 import org.wso2.registry.session.CurrentSession;
 import org.wso2.registry.utils.AuthorizationUtils;
 import org.wso2.registry.jdbc.dataobjects.ResourceDO;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 import java.io.*;
 import java.sql.*;
@@ -521,6 +520,10 @@
             String sql = "SELECT R.RID, R.PATH FROM RESOURCE R, DEPENDENCY D WHERE D.PARENT_RID=?" +
                     " AND D.CHILD_RID=R.RID ORDER BY R.PATH";
 
+            //String sql = "SELECT R.RID, R.PATH FROM RESOURCE R " +
+            //        "WHERE R.RID=(SELECT D.CHILD_RID FROM DEPENDENCY D WHERE D.PARENT_RID=?) " +
+            //        "ORDER BY R.PATH";
+
             PreparedStatement ps = conn.prepareStatement(sql);
             ps.setString(1, collection.getId());
 
@@ -675,7 +678,10 @@
     public InputStream getResourceContentStream(String contentID, Connection conn)
             throws RegistryException {
 
-        InputStream contentStream = null;
+        if (FileManager.hasFileBasedContent(contentID)) {
+            return FileManager.getFileBasedInputStream(contentID);
+
+        }
 
         try {
             String sql = "SELECT CONTENT_DATA FROM CONTENT WHERE CONTENT_ID=?";
@@ -685,26 +691,31 @@
 
             ResultSet result = ps.executeQuery();
 
+            InputStream contentStream = null;
             if (result.next()) {
-                contentStream = getDisconnectedStream(result.getBinaryStream("CONTENT_DATA"));
+                //contentStream = getDisconnectedStream(result.getBinaryStream("CONTENT_DATA"));
+                contentStream = FileManager.
+                        createFileBasedInputStream(contentID, result.getBinaryStream("CONTENT_DATA"));
             }
             ps.close();
 
+            return contentStream;
+
         } catch (SQLException e) {
             String msg = "Faild to get resource content from the database. " + e.getMessage();
             log.error(msg, e);
             throw new RegistryException(msg, e);
 
-        } catch (IOException e) {
-            String msg = "Failed to write resource content to a temporary file. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
         }
-
-        return contentStream;
+        
+        //} catch (IOException e) {
+        //    String msg = "Failed to write resource content to a temporary file. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
+        //}
     }
 
-    private InputStream getDisconnectedStream(InputStream connectedStream) throws IOException {
+    private InputStream getDisconnectedStreamD(InputStream connectedStream) throws IOException {
 
         if (connectedStream == null) {
             return null;
@@ -971,39 +982,40 @@
             // we should first write the content stream to a temporary file because JDBC
             // needs the length of the binary stream.
 
-            File tempFile;
-            try {
-                tempFile = File.createTempFile("reg", "tmp");
-                OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
-                InputStream bufferedIn = new BufferedInputStream(contentStream);
-
-                byte[] dataChunk = new byte[1024];
-                int byteCount;
-                while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
-                    fileStream.write(dataChunk, 0, byteCount);
-                }
-                fileStream.flush();
-                bufferedIn.close();
-                fileStream.close();
-
-                // there is no use of content stream after it is closed. so we can set it to null to
-                // avoid using it again.
-                resourceImpl.setContentStream(null);
-
-            } catch (IOException e) {
-
-                String msg = "Failed to write the resource content to a temporary file, " +
-                        "before writing to the database. " + e.getMessage();
-                log.error(msg, e);
-                throw new RegistryException(msg, e);
-            }
+            //File tempFile;
+            //try {
+            //    tempFile = File.createTempFile("reg", "tmp");
+            //    OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
+            //    InputStream bufferedIn = new BufferedInputStream(contentStream);
+            //
+            //    byte[] dataChunk = new byte[1024];
+            //    int byteCount;
+            //    while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
+            //        fileStream.write(dataChunk, 0, byteCount);
+            //    }
+            //    fileStream.flush();
+            //    bufferedIn.close();
+            //    fileStream.close();
+            //
+            //    // there is no use of content stream after it is closed. so we can set it to null to
+            //    // avoid using it again.
+            //    resourceImpl.setContentStream(null);
+            //
+            //} catch (IOException e) {
+            //
+            //    String msg = "Failed to write the resource content to a temporary file, " +
+            //            "before writing to the database. " + e.getMessage();
+            //    log.error(msg, e);
+            //    throw new RegistryException(msg, e);
+            //}
 
             try {
 
                 contentID = UUID.randomUUID().toString();
 
-                InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
-                long contentLength = tempFile.length();
+                InputStream tempFileStream =
+                        FileManager.createFileBasedInputStream(contentID, contentStream);
+                long contentLength = FileManager.getFile(contentID).length();
 
                 String sql = "INSERT INTO CONTENT (CONTENT_ID, CONTENT_DATA) VALUES (?, ?)";
 
@@ -1013,10 +1025,11 @@
                 ps.executeUpdate();
                 ps.close();
 
-            } catch (FileNotFoundException e) {
-                String msg = "Failed to read resource content from the temporary file. " + e.getMessage();
-                log.error(msg, e);
-                throw new RegistryException(msg, e);
+            //} catch (FileNotFoundException e) {
+            //    String msg = "Failed to read resource content from the temporary file. " +
+            //            e.getMessage();
+            //    log.error(msg, e);
+            //    throw new RegistryException(msg, e);
 
             } catch (SQLException e) {
 
@@ -1032,38 +1045,41 @@
     public void updateContent(String contentID, InputStream contentStream)
             throws RegistryException {
 
+        FileManager.deleteFileBasedContent(contentID);
+
         Connection conn = Transaction.getConnection();
 
         // we should first write the content stream to a temporary file because JDBC
         // needs the length of the binary stream.
 
-        File tempFile = null;
-        try {
-            tempFile = File.createTempFile("reg", "tmp");
-            OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
-            InputStream bufferedIn = new BufferedInputStream(contentStream);
-
-            byte[] dataChunk = new byte[1024];
-            int byteCount;
-            while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
-                fileStream.write(dataChunk, 0, byteCount);
-            }
-            fileStream.flush();
-            bufferedIn.close();
-            fileStream.close();
-
-        } catch (IOException e) {
-
-            String msg = "Failed to write the resource content to a temporary file, " +
-                    "before writing to the database. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
-        }
+        //File tempFile = null;
+        //try {
+        //    tempFile = File.createTempFile("reg", "tmp");
+        //    OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
+        //    InputStream bufferedIn = new BufferedInputStream(contentStream);
+        //
+        //    byte[] dataChunk = new byte[1024];
+        //    int byteCount;
+        //    while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
+        //        fileStream.write(dataChunk, 0, byteCount);
+        //    }
+        //    fileStream.flush();
+        //    bufferedIn.close();
+        //    fileStream.close();
+        //
+        //} catch (IOException e) {
+        //
+        //    String msg = "Failed to write the resource content to a temporary file, " +
+        //            "before writing to the database. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
+        //}
 
         try {
 
-            InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
-            long contentLength = tempFile.length();
+            InputStream tempFileStream =
+                    FileManager.createFileBasedInputStream(contentID, contentStream);
+            long contentLength = FileManager.getFile(contentID).length();
 
             String sql = "UPDATE CONTENT SET CONTENT_DATA=? WHERE CONTENT_ID=?";
 
@@ -1073,10 +1089,10 @@
             ps.executeUpdate();
             ps.close();
 
-        } catch (FileNotFoundException e) {
-            String msg = "Failed to read resource content from the temporary file. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
+        //} catch (FileNotFoundException e) {
+        //    String msg = "Failed to read resource content from the temporary file. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
 
         } catch (SQLException e) {
 
@@ -1196,40 +1212,42 @@
 
     public String addContent(InputStream contentStream) throws RegistryException {
 
+        String contentID = UUID.randomUUID().toString();
+
         Connection conn = Transaction.getConnection();
 
         // we should first write the content stream to a temporary file because JDBC
         // needs the length of the binary stream.
 
-        File tempFile;
-        try {
-            tempFile = File.createTempFile("reg", "tmp");
-            OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
-            InputStream bufferedIn = new BufferedInputStream(contentStream);
-
-            byte[] dataChunk = new byte[1024];
-            int byteCount;
-            while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
-                fileStream.write(dataChunk, 0, byteCount);
-            }
-            fileStream.flush();
-            bufferedIn.close();
-            fileStream.close();
-
-        } catch (IOException e) {
-
-            String msg = "Failed to write the resource content to a temporary file, " +
-                    "before writing to the database. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
-        }
+        //File tempFile;
+        //try {
+        //    tempFile = File.createTempFile("reg", "tmp");
+        //    OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
+        //    InputStream bufferedIn = new BufferedInputStream(contentStream);
+        //
+        //    byte[] dataChunk = new byte[1024];
+        //    int byteCount;
+        //    while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
+        //        fileStream.write(dataChunk, 0, byteCount);
+        //    }
+        //    fileStream.flush();
+        //    bufferedIn.close();
+        //    fileStream.close();
+        //
+        //} catch (IOException e) {
+        //
+        //    String msg = "Failed to write the resource content to a temporary file, " +
+        //            "before writing to the database. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
+        //}
 
         try {
 
-            String contentID = UUID.randomUUID().toString();
-
-            InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
-            long contentLength = tempFile.length();
+            //InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
+            InputStream tempFileStream =
+                    FileManager.createFileBasedInputStream(contentID, contentStream);
+            long contentLength = FileManager.getFile(contentID).length();
 
             String sql = "INSERT INTO CONTENT (CONTENT_ID, CONTENT_DATA) VALUES (?, ?)";
 
@@ -1241,10 +1259,10 @@
 
             return contentID;
 
-        } catch (FileNotFoundException e) {
-            String msg = "Failed to read resource content from the temporary file. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
+        //} catch (FileNotFoundException e) {
+        //    String msg = "Failed to read resource content from the temporary file. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
 
         } catch (SQLException e) {
 

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceVersionDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceVersionDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceVersionDAO.java	Sun May 11 02:20:59 2008
@@ -17,6 +17,7 @@
 package org.wso2.registry.jdbc.dao;
 
 import org.wso2.registry.jdbc.utils.Transaction;
+import org.wso2.registry.jdbc.utils.FileManager;
 import org.wso2.registry.jdbc.dataobjects.ResourceDO;
 import org.wso2.registry.*;
 import org.wso2.registry.Collection;
@@ -497,38 +498,40 @@
 
         if (contentStream != null) {
 
+            contentID = UUID.randomUUID().toString();
+
             // we should first write the content stream to a temporary file because JDBC
             // needs the length of the binary stream.
 
-            File tempFile;
-            try {
-                tempFile = File.createTempFile("reg", "tmp");
-                OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
-                InputStream bufferedIn = new BufferedInputStream(contentStream);
-
-                byte[] dataChunk = new byte[1024];
-                int byteCount;
-                while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
-                    fileStream.write(dataChunk, 0, byteCount);
-                }
-                fileStream.flush();
-                bufferedIn.close();
-                fileStream.close();
-
-            } catch (IOException e) {
-
-                String msg = "Failed to write the resource content to a temporary file, " +
-                        "before writing to the database. " + e.getMessage();
-                log.error(msg, e);
-                throw new RegistryException(msg, e);
-            }
+            //File tempFile;
+            //try {
+            //    tempFile = File.createTempFile("reg", "tmp");
+            //    OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(tempFile));
+            //    InputStream bufferedIn = new BufferedInputStream(contentStream);
+            //
+            //    byte[] dataChunk = new byte[1024];
+            //    int byteCount;
+            //    while ((byteCount = bufferedIn.read(dataChunk)) != -1) {
+            //        fileStream.write(dataChunk, 0, byteCount);
+            //    }
+            //    fileStream.flush();
+            //    bufferedIn.close();
+            //    fileStream.close();
+            //
+            //} catch (IOException e) {
+            //
+            //    String msg = "Failed to write the resource content to a temporary file, " +
+            //            "before writing to the database. " + e.getMessage();
+            //    log.error(msg, e);
+            //    throw new RegistryException(msg, e);
+            //}
 
             try {
 
-                contentID = UUID.randomUUID().toString();
-
-                InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
-                long contentLength = tempFile.length();
+                //InputStream tempFileStream = new BufferedInputStream(new FileInputStream(tempFile));
+                InputStream tempFileStream =
+                        FileManager.createFileBasedInputStream(contentID, contentStream);
+                long contentLength = FileManager.getFile(contentID).length();
 
                 String sql = "INSERT INTO CONTENT_VERSION " +
                         "(CONTENT_VERSION_ID, CONTENT_DATA) VALUES (?, ?)";
@@ -539,11 +542,11 @@
                 ps.executeUpdate();
                 ps.close();
 
-            } catch (FileNotFoundException e) {
-                String msg = "Failed to read resource content from the temporary file. " +
-                        e.getMessage();
-                log.error(msg, e);
-                throw new RegistryException(msg, e);
+            //} catch (FileNotFoundException e) {
+            //    String msg = "Failed to read resource content from the temporary file. " +
+            //            e.getMessage();
+            //    log.error(msg, e);
+            //    throw new RegistryException(msg, e);
 
             } catch (SQLException e) {
 
@@ -559,6 +562,10 @@
     public InputStream getResourceContentStream(String contentID, Connection conn)
             throws RegistryException {
 
+        if (FileManager.hasFileBasedContent(contentID)) {
+            return FileManager.getFileBasedInputStream(contentID);
+        }
+
         InputStream contentStream = null;
 
         try {
@@ -569,7 +576,9 @@
 
             ResultSet result = ps.executeQuery();
             if (result.next()) {
-                contentStream = getDisconnectedStream(result.getBinaryStream("CONTENT_DATA"));
+                //contentStream = getDisconnectedStream(result.getBinaryStream("CONTENT_DATA"));
+                contentStream = FileManager.
+                        createFileBasedInputStream(contentID, result.getBinaryStream("CONTENT_DATA"));
             }
             ps.close();
 
@@ -578,16 +587,17 @@
             log.error(msg, e);
             throw new RegistryException(msg, e);
 
-        } catch (IOException e) {
-            String msg = "Failed to write resource content to a temporary file. " + e.getMessage();
-            log.error(msg, e);
-            throw new RegistryException(msg, e);
         }
+        //} catch (IOException e) {
+        //    String msg = "Failed to write resource content to a temporary file. " + e.getMessage();
+        //    log.error(msg, e);
+        //    throw new RegistryException(msg, e);
+        //}
 
         return contentStream;
     }
 
-    private InputStream getDisconnectedStream(InputStream connectedStream) throws IOException {
+    private InputStream getDisconnectedStreamD(InputStream connectedStream) throws IOException {
 
         if (connectedStream == null) {
             return null;

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RestoreUtilDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RestoreUtilDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RestoreUtilDAO.java	Sun May 11 02:20:59 2008
@@ -188,7 +188,6 @@
         String contentID = resourceDAO.addContent(contentStream);
 
         return contentID;
-
     }
 
     public void copyResourceVersion(

Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileData.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileData.java	Sun May 11 02:20:59 2008
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.jdbc.utils;
+
+public class FileData {
+
+    /**
+     * Number of active input streams using the file.
+     */
+    private long connections;
+
+    /**
+     * Time at which the connections become 0.
+     */
+    private long idleFrom;
+
+    public FileData() {
+        connections = 0;
+        idleFrom = -1; // -1 indicates that the file is not idle
+    }
+
+    public long getConnections() {
+        return connections;
+    }
+
+    public void setConnections(long connections) {
+        this.connections = connections;
+    }
+
+    public void incrementConnection() {
+        connections++;
+        idleFrom = -1;
+    }
+
+    public void decrementConnection() {
+        connections--;
+
+        if (connections <= 0) {
+            idleFrom = System.currentTimeMillis();
+        }
+    }
+
+    public long getIdleFrom() {
+        return idleFrom;
+    }
+
+    public void setIdleFrom(long idleFrom) {
+        this.idleFrom = idleFrom;
+    }
+}

Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileManager.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/FileManager.java	Sun May 11 02:20:59 2008
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.jdbc.utils;
+
+import org.wso2.registry.RegistryException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.*;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+
+public class FileManager {
+
+    private static Log log = LogFactory.getLog(FileManager.class);
+
+    private static final long MAXIMUM_IDLE_TIME_FOR_FILE = 60 * 60 * 1000; // 1 hour
+
+    private static final long PREFERRED_FILE_COUNT = 100;
+
+    private static Map <String, FileData> fileDataMap = new HashMap <String, FileData> ();
+
+    private static String tempDir;
+
+    public static boolean hasFileBasedContent(String contentID) {
+
+        if (fileDataMap.containsKey(contentID)) {
+            return true;
+        }
+
+        return false;
+    }
+
+    public static InputStream getFileBasedInputStream(String contentID) throws RegistryException {
+
+        String tempDir = getTempDirectory();
+        String tempFilePath = tempDir + "reg" + contentID;
+        File tempFile = new File(tempFilePath);
+        if (!tempFile.exists()) {
+            String msg = "Temporary content file is not created for content " + contentID;
+            log.error(msg);
+            throw new RegistryException(msg);
+        }
+
+        try {
+            return new RegistryFileInputStream(
+                    new BufferedInputStream(new FileInputStream(tempFile)), contentID);
+
+        } catch (FileNotFoundException e) {
+
+            String msg = "Failed to create an input stream from the temp file " +
+                    tempFilePath + ". " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
+    public static InputStream createFileBasedInputStream(String contentID, InputStream inputStream)
+            throws RegistryException {
+
+        if (inputStream == null) {
+            return null;
+        }
+
+        String tempDir = getTempDirectory();
+        String tempFilePath = tempDir + "reg" + contentID;
+        File tempFile = new File(tempFilePath);
+        if (tempFile.exists()) {
+            FileData fileData = fileDataMap.get(contentID);
+            fileData.incrementConnection();
+
+        } else {
+
+            BufferedOutputStream out = null;
+
+            try {
+                tempFile.createNewFile();
+                tempFile.deleteOnExit();
+
+                FileData fileData = new FileData();
+                fileData.incrementConnection();
+                fileDataMap.put(contentID, fileData);
+
+                out = new BufferedOutputStream(new FileOutputStream(tempFile));
+                byte[] contentChunk = new byte[1024];
+                int byteCount;
+                while ((byteCount = inputStream.read(contentChunk)) != -1) {
+                    out.write(contentChunk, 0, byteCount);
+                }
+                out.flush();
+
+            } catch (IOException e) {
+
+                String msg = "Failed to write data to the temporary file " +
+                        tempFilePath + ". " + e.getMessage();
+                log.error(msg, e);
+                throw new RegistryException(msg, e);
+
+            } finally {
+
+                try {
+                    inputStream.close();
+
+                    if (out != null) {
+                        out.close();
+                    }
+
+                } catch (IOException e) {
+
+                    String msg = "Failed to close streams used for temporary file. "
+                            + e.getMessage();
+                    log.error(msg, e);
+                }
+
+            }
+        }
+
+
+        try {
+            return new RegistryFileInputStream(
+                    new BufferedInputStream(new FileInputStream(tempFile)), contentID);
+
+        } catch (FileNotFoundException e) {
+
+            String msg = "Failed to create an input stream from the temp file " +
+                    tempFilePath + ". " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
+    public static File getFile(String contentID) {
+
+        String tempDir = getTempDirectory();
+        String tempFilePath = tempDir + "reg" + contentID;
+        File tempFile = new File(tempFilePath);
+        if (tempFile.exists()) {
+            return tempFile;
+        }
+
+        return null;
+    }
+
+    public static synchronized void onStreamClosed(String contentID) {
+
+        FileData fileData = fileDataMap.get(contentID);
+        if (fileData != null) {
+            fileData.decrementConnection();
+            if (fileData.getConnections() <= 0 && fileDataMap.size() > PREFERRED_FILE_COUNT) {
+                deleteFileBasedContent(contentID);
+            }
+        }
+    }
+
+    public static void cleanupFiles() {
+
+        long currentTime = System.currentTimeMillis();
+
+        Iterator <String> contentIDs = fileDataMap.keySet().iterator();
+
+        while (contentIDs.hasNext()) {
+
+            String contentID = contentIDs.next();
+
+            FileData fileData = fileDataMap.get(contentID);
+            if (fileData.getConnections() <= 0 &&
+                    (currentTime - fileData.getIdleFrom()) >= MAXIMUM_IDLE_TIME_FOR_FILE) {
+
+                deleteFileBasedContent(contentID);
+                fileDataMap.remove(contentID);
+            }
+        }
+    }
+
+    public static void deleteFileBasedContent(String contentID) {
+
+        fileDataMap.remove(contentID);
+
+        File file = getFile(contentID);
+
+        if (file != null) {
+            file.delete();
+        }
+    }
+
+    private static String getTempDirectory() {
+
+        if (tempDir == null) {
+            tempDir = System.getProperty("java.io.tmpdir");
+            if (!tempDir.endsWith(File.separator)) {
+                tempDir = tempDir + File.separator;
+            }
+        }
+
+        return tempDir;
+    }
+}

Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/RegistryFileInputStream.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/utils/RegistryFileInputStream.java	Sun May 11 02:20:59 2008
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.jdbc.utils;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+public class RegistryFileInputStream extends InputStream {
+
+    private InputStream inputStream;
+    private String contentID;
+
+    public RegistryFileInputStream(InputStream inputStream, String contentID) {
+        this.inputStream = inputStream;
+        this.contentID = contentID;
+    }
+
+    public int read() throws IOException {
+        return inputStream.read();
+    }
+
+    public int read(byte[] b) throws IOException {
+        return inputStream.read(b);
+    }
+
+    public int read(byte[] b, int off, int len) throws IOException {
+        return inputStream.read(b, off, len);
+    }
+
+    public long skip(long n) throws IOException {
+        return inputStream.skip(n);
+    }
+
+    public int available() throws IOException {
+        return inputStream.available();
+    }
+
+    public void close() throws IOException {
+        FileManager.onStreamClosed(contentID);
+        inputStream.close();
+    }
+
+    public void mark(int readlimit) {
+        inputStream.mark(readlimit);
+    }
+
+    public void reset() throws IOException {
+        inputStream.reset();
+    }
+
+    public boolean markSupported() {
+        return inputStream.markSupported();
+    }
+}



More information about the Registry-dev mailing list