FELIX-4930 : Support PersistenceManagers that provide their own caching heuristics. Apply patch from Ray Auge

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1686362 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java b/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java
new file mode 100644
index 0000000..bea1286
--- /dev/null
+++ b/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.felix.cm;
+
+
+/**
+ * <code>NotCachablePersistenceManager</code> is a marker interface which
+ * extends {@link PersistenceManager} to inform that no cache should be applied
+ * around this persistence manager. This gives the opportunity for the
+ * persistence manager to implement it's own caching heuristics.
+ * <p>
+ * To make implementations of this interface available to the Configuration
+ * Admin Service they must be registered as service for interface
+ * {@link PersistenceManager}.
+ * <p>
+ * See also {@link PersistenceManager}
+ *
+ * @since 1.1
+ */
+public interface NotCachablePersistenceManager extends PersistenceManager
+{
+}
diff --git a/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java b/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java
index 9a09ffe..fac686f 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java
@@ -28,6 +28,7 @@
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import org.apache.felix.cm.NotCachablePersistenceManager;
 import org.apache.felix.cm.PersistenceManager;
 import org.osgi.framework.Constants;
 
@@ -130,6 +131,11 @@
         try
         {
             lock.lock();
+            boolean fullyLoaded = this.fullyLoaded;
+            if ( pm instanceof NotCachablePersistenceManager )
+            {
+                fullyLoaded = false;
+            }
             // if not fully loaded, call back to the underlying persistence
             // manager and cach all dictionaries whose service.pid is set
             if ( !fullyLoaded )
@@ -158,7 +164,7 @@
                             }
                         }
                     }
-                    fullyLoaded = true;
+                    this.fullyLoaded = true;
                 }
             }
 
diff --git a/configadmin/src/main/java/org/apache/felix/cm/package-info.java b/configadmin/src/main/java/org/apache/felix/cm/package-info.java
index 32888b3..386f544 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/package-info.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("1.0")
+@Version("1.1")
 @Export(optional = "provide:=true")
 package org.apache.felix.cm;
 
diff --git a/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java b/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java
new file mode 100644
index 0000000..2268368
--- /dev/null
+++ b/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.felix.cm;
+
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+
+public class MockNotCachablePersistenceManager implements NotCachablePersistenceManager
+{
+
+    private final Map configs = new HashMap();
+
+
+    public void delete( String pid )
+    {
+        configs.remove( pid );
+    }
+
+
+    public boolean exists( String pid )
+    {
+        return configs.containsKey( pid );
+    }
+
+
+    public Enumeration getDictionaries()
+    {
+        return Collections.enumeration( configs.values() );
+    }
+
+
+    public Dictionary load( String pid ) throws IOException
+    {
+        Dictionary config = ( Dictionary ) configs.get( pid );
+        if ( config != null )
+        {
+            return config;
+        }
+
+        throw new IOException( "No such configuration: " + pid );
+    }
+
+
+    public void store( String pid, Dictionary properties )
+    {
+        configs.put( pid, properties );
+    }
+
+}
diff --git a/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java b/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java
new file mode 100644
index 0000000..a09925e
--- /dev/null
+++ b/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.felix.cm.impl;
+
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.felix.cm.MockNotCachablePersistenceManager;
+import org.apache.felix.cm.MockPersistenceManager;
+import org.apache.felix.cm.PersistenceManager;
+
+import org.osgi.framework.Constants;
+
+import junit.framework.TestCase;
+
+
+/**
+ * The <code>CachingPersistenceManagerProxyTest</code> class tests the issues
+ * related to caching of configurations.
+ * <p>
+ * @see <a href="https://issues.apache.org/jira/browse/FELIX-4930">FELIX-4930</a>
+ */
+public class CachingPersistenceManagerProxyTest extends TestCase
+{
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void test_caching_is_applied() throws Exception {
+        String pid = "testDefaultPersistenceManager";
+        SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
+
+        PersistenceManager pm = new MockPersistenceManager();
+        CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy( pm );
+
+        Dictionary dictionary = new Hashtable();
+        dictionary.put( "property1", "value1" );
+        dictionary.put( Constants.SERVICE_PID, pid );
+        pm.store( pid, dictionary );
+
+        Enumeration dictionaries = cpm.getDictionaries( filter );
+        List list = Collections.list( dictionaries );
+        assertEquals(1, list.size());
+
+        dictionary = new Hashtable();
+        dictionary.put( "property1", "value2" );
+        pid = "testDefaultPersistenceManager";
+        dictionary.put( Constants.SERVICE_PID, pid );
+        pm.store( pid, dictionary );
+
+        dictionaries = cpm.getDictionaries( filter );
+        list = Collections.list( dictionaries );
+        assertEquals(1, list.size());
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public void test_caching_is_avoided() throws Exception {
+        String pid = "testDefaultPersistenceManager";
+        SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
+
+        PersistenceManager pm = new MockNotCachablePersistenceManager();
+        CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy( pm );
+
+        Dictionary dictionary = new Hashtable();
+        dictionary.put( "property1", "value1" );
+        dictionary.put( Constants.SERVICE_PID, pid );
+        pm.store( pid, dictionary );
+
+        Enumeration dictionaries = cpm.getDictionaries( filter );
+        List list = Collections.list( dictionaries );
+        assertEquals(1, list.size());
+
+        dictionary = new Hashtable();
+        dictionary.put( "property1", "value2" );
+        pid = "testDefaultPersistenceManager";
+        dictionary.put( Constants.SERVICE_PID, pid );
+        pm.store( pid, dictionary );
+
+        dictionaries = cpm.getDictionaries( filter );
+        list = Collections.list( dictionaries );
+        assertEquals(0, list.size());
+    }
+
+}
\ No newline at end of file