Make bundle cache instance-based, rather than static to facilitate
replacement. (FELIX-1625)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@820301 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index 38af166..04fe2d9 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -94,6 +94,9 @@
     // Framework's active start level.
     private volatile int m_activeStartLevel = FelixConstants.FRAMEWORK_INACTIVE_STARTLEVEL;
 
+    // Local bundle cache.
+    private BundleCache m_cache = null;
+
     // System bundle activator list.
     List m_activatorList = null;
 
@@ -487,7 +490,6 @@
 
                 // Create the bundle cache, if necessary, so that we can reload any
                 // installed bundles.
-/* TODO: CACHE - FIX
                 m_cache = (BundleCache) m_configMutableMap.get(
                     FelixConstants.FRAMEWORK_BUNDLECACHE_IMPL);
                 if (m_cache == null)
@@ -502,7 +504,7 @@
                            throw new BundleException("Error creating bundle cache.", ex);
                        }
                 }
-*/
+
                 // If this is the first time init is called, check to see if
                 // we need to flush the bundle cache.
                 if (getState() == Bundle.INSTALLED)
@@ -513,7 +515,7 @@
                     {
                         try
                         {
-                            BundleCache.delete(m_logger, m_configMap);
+                            m_cache.delete();
                         }
                         catch (Exception ex)
                         {
@@ -553,7 +555,7 @@
                 // First get cached bundle identifiers.
                 try
                 {
-                    archives = BundleCache.getArchives(m_logger, m_configMap);
+                    archives = m_cache.getArchives();
                 }
                 catch (Exception ex)
                 {
@@ -2312,7 +2314,7 @@
                 try
                 {
                     // Add the bundle to the cache.
-                    ba = BundleCache.create(m_logger, m_configMap, id, location, is);
+                    ba = m_cache.create(id, location, is);
                 }
                 catch (Exception ex)
                 {
@@ -2892,7 +2894,7 @@
         {
             if (bundle == this)
             {
-                return BundleCache.getSystemBundleDataFile(m_logger, m_configMap, s);
+                return m_cache.getSystemBundleDataFile(s);
             }
 
             return bundle.getArchive().getDataFile(s);
@@ -3676,8 +3678,7 @@
             BufferedReader br = null;
             try
             {
-                File file = BundleCache.getSystemBundleDataFile(
-                    m_logger, m_configMap, "bundle.id");
+                File file = m_cache.getSystemBundleDataFile("bundle.id");
                 is = m_secureAction.getFileInputStream(file);
                 br = new BufferedReader(new InputStreamReader(is));
                 return Long.parseLong(br.readLine());
@@ -3730,8 +3731,7 @@
             BufferedWriter bw = null;
             try
             {
-                File file = BundleCache.getSystemBundleDataFile(
-                    m_logger, m_configMap, "bundle.id");
+                File file = m_cache.getSystemBundleDataFile("bundle.id");
                 os = m_secureAction.getFileOutputStream(file);
                 bw = new BufferedWriter(new OutputStreamWriter(os));
                 String s = Long.toString(m_nextId);
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java b/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
index fdfa742..dd3cbe8 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
@@ -82,25 +82,34 @@
 
     private static final SecureAction m_secureAction = new SecureAction();
 
+    private final Logger m_logger;
+    private final Map m_configMap;
+
+    public BundleCache(Logger logger, Map configMap)
+    {
+        m_logger = logger;
+        m_configMap = configMap;
+    }
+
     /* package */ static SecureAction getSecureAction()
     {
         return m_secureAction;
     }
 
-    public static void delete(Logger logger, Map configMap) throws Exception
+    public synchronized void delete() throws Exception
     {
         // Delete the cache directory.
-        File cacheDir = determineCacheDir(configMap);
+        File cacheDir = determineCacheDir(m_configMap);
         deleteDirectoryTree(cacheDir);
     }
 
-    public static BundleArchive[] getArchives(Logger logger, Map configMap)
+    public BundleArchive[] getArchives()
         throws Exception
     {
         // Get buffer size value.
         try
         {
-            String sBufSize = (String) configMap.get(CACHE_BUFSIZE_PROP);
+            String sBufSize = (String) m_configMap.get(CACHE_BUFSIZE_PROP);
             if (sBufSize != null)
             {
                 BUFSIZE = Integer.parseInt(sBufSize);
@@ -112,12 +121,12 @@
         }
 
         // Create the cache directory, if it does not exist.
-        File cacheDir = determineCacheDir(configMap);
+        File cacheDir = determineCacheDir(m_configMap);
         if (!getSecureAction().fileExists(cacheDir))
         {
             if (!getSecureAction().mkdirs(cacheDir))
             {
-                logger.log(
+                m_logger.log(
                     Logger.LOG_ERROR,
                     "Unable to create cache directory: " + cacheDir);
                 throw new RuntimeException("Unable to create cache directory.");
@@ -137,12 +146,12 @@
                 // Recreate the bundle archive.
                 try
                 {
-                    archiveList.add(new BundleArchive(logger, configMap, children[i]));
+                    archiveList.add(new BundleArchive(m_logger, m_configMap, children[i]));
                 }
                 catch (Exception ex)
                 {
                     // Log and ignore.
-                    logger.log(Logger.LOG_ERROR,
+                    m_logger.log(Logger.LOG_ERROR,
                         "Error creating archive.", ex);
                 }
             }
@@ -152,11 +161,10 @@
             archiveList.toArray(new BundleArchive[archiveList.size()]);
     }
 
-    public static BundleArchive create(Logger logger, Map configMap,
-        long id, String location, InputStream is)
+    public BundleArchive create(long id, String location, InputStream is)
         throws Exception
     {
-        File cacheDir = determineCacheDir(configMap);
+        File cacheDir = determineCacheDir(m_configMap);
 
         // Construct archive root directory.
         File archiveRootDir =
@@ -166,7 +174,7 @@
         {
             // Create the archive and add it to the list of archives.
             BundleArchive ba =
-                new BundleArchive(logger, configMap, archiveRootDir, id, location, is);
+                new BundleArchive(m_logger, m_configMap, archiveRootDir, id, location, is);
             return ba;
         }
         catch (Exception ex)
@@ -175,7 +183,7 @@
             {
                 if (!BundleCache.deleteDirectoryTree(archiveRootDir))
                 {
-                    logger.log(
+                    m_logger.log(
                         Logger.LOG_ERROR,
                         "Unable to delete the archive directory: "
                             + archiveRootDir);
@@ -193,11 +201,11 @@
      * @return a <tt>File</tt> object corresponding to the specified file name.
      * @throws Exception if any error occurs.
     **/
-    public static File getSystemBundleDataFile(Logger logger, Map configMap, String fileName)
+    public File getSystemBundleDataFile(String fileName)
         throws Exception
     {
         // Make sure system bundle directory exists.
-        File sbDir = new File(determineCacheDir(configMap), BUNDLE_DIR_PREFIX + Long.toString(0));
+        File sbDir = new File(determineCacheDir(m_configMap), BUNDLE_DIR_PREFIX + Long.toString(0));
 
         // If the system bundle directory exists, then we don't
         // need to initialize since it has already been done.
@@ -206,7 +214,7 @@
             // Create system bundle directory, if it does not exist.
             if (!getSecureAction().mkdirs(sbDir))
             {
-                logger.log(
+                m_logger.log(
                     Logger.LOG_ERROR,
                     "Unable to create system bundle directory.");
                 throw new IOException("Unable to create system bundle directory.");