[Mulgara-svn] r2084 - in trunk/src/jar: server/java/org/mulgara/server util/java/org/mulgara/util/io

pag at mulgara.org pag at mulgara.org
Thu Jan 5 19:08:24 UTC 2012


Author: pag
Date: 2012-01-05 19:08:24 +0000 (Thu, 05 Jan 2012)
New Revision: 2084

Modified:
   trunk/src/jar/server/java/org/mulgara/server/EmbeddedMulgaraServer.java
   trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java
Log:
Better resource cleanup

Modified: trunk/src/jar/server/java/org/mulgara/server/EmbeddedMulgaraServer.java
===================================================================
--- trunk/src/jar/server/java/org/mulgara/server/EmbeddedMulgaraServer.java	2012-01-05 19:07:32 UTC (rev 2083)
+++ trunk/src/jar/server/java/org/mulgara/server/EmbeddedMulgaraServer.java	2012-01-05 19:08:24 UTC (rev 2084)
@@ -944,16 +944,18 @@
       int port = EmbeddedMulgaraServer.getShutdownHookPort();
 
       // bind to the specified socket on the local host
+      Socket socket = null;
+      BufferedReader input = null;
       try {
         shutdownSocket = new ServerSocket(port, 0, InetAddress.getByName("localhost"));
 
         // wait until a request to stop the server
         while (!stop) {
           // wait for a shutdown request
-          Socket socket = shutdownSocket.accept();
+          socket = shutdownSocket.accept();
 
           // read the response from the client
-          BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+          input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 
           // check if the require request is correct
           String message = null;
@@ -976,6 +978,20 @@
       } catch (Exception ex) {
         log.error("Unable to establish shutdown socket on port " + port, ex);
       } finally {
+        if (input != null) {
+          try {
+            input.close();
+          } catch (IOException e) {
+            log.error("Unexpected problem closing input from a socket");
+          }
+        }
+        if (socket != null) {
+          try {
+            socket.close();
+          } catch (IOException e) {
+            log.error("Unexpected problem closing socket");
+          }
+        }
         // attempt to close the socket
         try {
           shutdownSocket.close();

Modified: trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java	2012-01-05 19:07:32 UTC (rev 2083)
+++ trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java	2012-01-05 19:08:24 UTC (rev 2084)
@@ -992,6 +992,9 @@
     /** The path of this metadata file. */
     private final File path;
 
+    /** The file to access */
+    private final RandomAccessFile raFile;
+    
     /** File for metadata about the hash table */
     private final FileChannel mdFile;
 
@@ -1013,15 +1016,15 @@
     public MetaData(File f) throws IOException {
       path = new File(f.getAbsolutePath() + MD_EXT);
       created = !path.exists();
-      RandomAccessFile file = new RandomAccessFile(path, "rw");
+      raFile = new RandomAccessFile(path, "rw");
       if (created) {
-        file.setLength(TOTAL_SIZE);
+        raFile.setLength(TOTAL_SIZE);
       } else {
         long length = path.length();
         if (length < TOTAL_SIZE) throw new IOException("HashMap Metadata file too short (" + length + ")");
         if (length > TOTAL_SIZE) throw new IOException("Corrupt Metadata file: too long (" + length + ")");
       }
-      mdFile = file.getChannel();
+      mdFile = raFile.getChannel();
       md = mdFile.map(FileChannel.MapMode.READ_WRITE, 0, TOTAL_SIZE);
       mdLong = md.asLongBuffer();
       mdInt = md.asIntBuffer();
@@ -1072,11 +1075,11 @@
     }
 
     public void close() throws IOException {
-      mdFile.close();
       md = null;
       mdLong = null;
       mdInt = null;
       mdLoadFactor = null;
+      raFile.close(); // This calls mdFile.close() internally
     }
     
     public void closeAndDelete() throws IOException {



More information about the Mulgara-svn mailing list