Author: cziegeler Date: Fri Jun 19 06:29:00 2015 New Revision: 1686362 URL: http://svn.apache.org/r1686362 Log: FELIX-4930 : Support PersistenceManagers that provide their own caching heuristics. Apply patch from Ray Auge
Added: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java (with props) felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java (with props) felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java (with props) Modified: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java felix/trunk/configadmin/src/main/java/org/apache/felix/cm/package-info.java Added: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java?rev=1686362&view=auto ============================================================================== --- felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java (added) +++ felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java Fri Jun 19 06:29:00 2015 @@ -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 +{ +} Propchange: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/NotCachablePersistenceManager.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Modified: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java?rev=1686362&r1=1686361&r2=1686362&view=diff ============================================================================== --- felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java (original) +++ felix/trunk/configadmin/src/main/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxy.java Fri Jun 19 06:29:00 2015 @@ -28,6 +28,7 @@ import java.util.concurrent.locks.Lock; 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 @@ class CachingPersistenceManagerProxy imp 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 @@ class CachingPersistenceManagerProxy imp } } } - fullyLoaded = true; + this.fullyLoaded = true; } } Modified: felix/trunk/configadmin/src/main/java/org/apache/felix/cm/package-info.java URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/main/java/org/apache/felix/cm/package-info.java?rev=1686362&r1=1686361&r2=1686362&view=diff ============================================================================== --- felix/trunk/configadmin/src/main/java/org/apache/felix/cm/package-info.java (original) +++ felix/trunk/configadmin/src/main/java/org/apache/felix/cm/package-info.java Fri Jun 19 06:29:00 2015 @@ -17,7 +17,7 @@ * under the License. */ -@Version("1.0") +@Version("1.1") @Export(optional = "provide:=true") package org.apache.felix.cm; Added: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java?rev=1686362&view=auto ============================================================================== --- felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java (added) +++ felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java Fri Jun 19 06:29:00 2015 @@ -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 ); + } + +} Propchange: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/MockNotCachablePersistenceManager.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java URL: http://svn.apache.org/viewvc/felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java?rev=1686362&view=auto ============================================================================== --- felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java (added) +++ felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java Fri Jun 19 06:29:00 2015 @@ -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 Propchange: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/configadmin/src/test/java/org/apache/felix/cm/impl/CachingPersistenceManagerProxyTest.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url