felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r1030463 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: Felix.java cache/BundleCache.java util/SecureAction.java
Date Wed, 03 Nov 2010 13:44:39 GMT
Author: rickhall
Date: Wed Nov  3 13:44:39 2010
New Revision: 1030463

URL: http://svn.apache.org/viewvc?rev=1030463&view=rev
Log:
Add locking for the bundle cache. (FELIX-2646)

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=1030463&r1=1030462&r2=1030463&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Wed Nov  3 13:44:39
2010
@@ -579,8 +579,8 @@ public class Felix extends BundleImpl im
 
                 // Create the bundle cache, if necessary, so that we can reload any
                 // installed bundles.
-                m_cache = (BundleCache) m_configMutableMap.get(
-                    FelixConstants.FRAMEWORK_BUNDLECACHE_IMPL);
+                m_cache = (BundleCache)
+                    m_configMutableMap.get(FelixConstants.FRAMEWORK_BUNDLECACHE_IMPL);
                 if (m_cache == null)
                 {
                        try
@@ -4387,6 +4387,10 @@ public class Felix extends BundleImpl im
                 m_extensionManager.removeExtensions(Felix.this);
             }
 
+            // Dispose of the bundle cache.
+            m_cache.release();
+            m_cache = null;
+
             // Set the framework state to resolved.
             acquireBundleLock(Felix.this, Bundle.STOPPING);
             try

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java?rev=1030463&r1=1030462&r2=1030463&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
Wed Nov  3 13:44:39 2010
@@ -19,6 +19,8 @@
 package org.apache.felix.framework.cache;
 
 import java.io.*;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
 import java.util.*;
 
 import org.apache.felix.framework.Logger;
@@ -72,17 +74,93 @@ public class BundleCache
     protected static transient int BUFSIZE = 4096;
     protected static transient final String CACHE_DIR_NAME = "felix-cache";
     protected static transient final String CACHE_ROOTDIR_DEFAULT = ".";
+    protected static transient final String CACHE_LOCK_NAME = "cache.lock";
     protected static transient final String BUNDLE_DIR_PREFIX = "bundle";
 
     private static final SecureAction m_secureAction = new SecureAction();
 
     private final Logger m_logger;
     private final Map m_configMap;
+    private final FileLock m_lock;
 
     public BundleCache(Logger logger, Map configMap)
+        throws Exception
     {
         m_logger = logger;
         m_configMap = configMap;
+
+        // Create the cache directory, if it does not exist.
+        File cacheDir = determineCacheDir(m_configMap);
+        if (!getSecureAction().fileExists(cacheDir))
+        {
+            if (!getSecureAction().mkdirs(cacheDir))
+            {
+                m_logger.log(
+                    Logger.LOG_ERROR,
+                    "Unable to create cache directory: " + cacheDir);
+                throw new RuntimeException("Unable to create cache directory.");
+            }
+        }
+
+        File lockFile = new File(cacheDir, CACHE_LOCK_NAME);
+        FileChannel fc = null;
+        FileOutputStream fos = null;
+        FileInputStream fis = null;
+        try
+        {
+            if (!getSecureAction().fileExists(lockFile))
+            {
+                fos = getSecureAction().getFileOutputStream(lockFile);
+                fc = fos.getChannel();
+            }
+            else
+            {
+                fis = getSecureAction().getFileInputStream(lockFile);
+                fc = fis.getChannel();
+            }
+        }
+        catch (Exception ex)
+        {
+            try
+            {
+                if (fos != null) fos.close();
+                if (fis != null) fis.close();
+                if (fc != null) fc.close();
+            }
+            catch (Exception ex2)
+            {
+                // Ignore.
+            }
+            throw new Exception("Unable to create bundle cache lock file: " + ex);
+        }
+        try
+        {
+            m_lock = fc.tryLock();
+        }
+        catch (Exception ex)
+        {
+            throw new Exception("Unable to lock bundle cache: " + ex);
+        }
+
+    }
+
+    public synchronized void release()
+    {
+        if (m_lock != null)
+        {
+            try
+            {
+                m_lock.release();
+                m_lock.channel().close();
+            }
+            catch (Exception ex)
+            {
+                // Not much we can do here, just log it.
+                m_logger.log(
+                    Logger.LOG_WARNING,
+                    "Exception releasing bundle cache.", ex);
+            }
+        }
     }
 
     /* package */ static SecureAction getSecureAction()
@@ -114,20 +192,8 @@ public class BundleCache
             // Use the default value.
         }
 
-        // Create the cache directory, if it does not exist.
-        File cacheDir = determineCacheDir(m_configMap);
-        if (!getSecureAction().fileExists(cacheDir))
-        {
-            if (!getSecureAction().mkdirs(cacheDir))
-            {
-                m_logger.log(
-                    Logger.LOG_ERROR,
-                    "Unable to create cache directory: " + cacheDir);
-                throw new RuntimeException("Unable to create cache directory.");
-            }
-        }
-
         // Create the existing bundle archives in the directory, if any exist.
+        File cacheDir = determineCacheDir(m_configMap);
         List archiveList = new ArrayList();
         File[] children = getSecureAction().listDirectory(cacheDir);
         for (int i = 0; (children != null) && (i < children.length); i++)

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java?rev=1030463&r1=1030462&r2=1030463&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
Wed Nov  3 13:44:39 2010
@@ -412,7 +412,7 @@ public class SecureAction
         }
     }
 
-    public InputStream getFileInputStream(File file) throws IOException
+    public FileInputStream getFileInputStream(File file) throws IOException
     {
         if (System.getSecurityManager() != null)
         {
@@ -420,7 +420,7 @@ public class SecureAction
             {
                 Actions actions = (Actions) m_actions.get();
                 actions.set(Actions.GET_FILE_INPUT_ACTION, file);
-                return (InputStream) AccessController.doPrivileged(actions, m_acc);
+                return (FileInputStream) AccessController.doPrivileged(actions, m_acc);
             }
             catch (PrivilegedActionException ex)
             {
@@ -437,7 +437,7 @@ public class SecureAction
         }
     }
 
-    public OutputStream getFileOutputStream(File file) throws IOException
+    public FileOutputStream getFileOutputStream(File file) throws IOException
     {
         if (System.getSecurityManager() != null)
         {
@@ -445,7 +445,7 @@ public class SecureAction
             {
                 Actions actions = (Actions) m_actions.get();
                 actions.set(Actions.GET_FILE_OUTPUT_ACTION, file);
-                return (OutputStream) AccessController.doPrivileged(actions, m_acc);
+                return (FileOutputStream) AccessController.doPrivileged(actions, m_acc);
             }
             catch (PrivilegedActionException ex)
             {



Mime
View raw message