You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2016/07/05 11:33:18 UTC
svn commit: r1751450 - in /felix/sandbox/cziegeler/configadmin-r7/src:
main/java/org/apache/felix/cm/impl/ test/java/org/apache/felix/cm/impl/
Author: cziegeler
Date: Tue Jul 5 11:33:18 2016
New Revision: 1751450
URL: http://svn.apache.org/viewvc?rev=1751450&view=rev
Log:
Sync with trunk
Modified:
felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java
felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/RankingComparator.java
felix/sandbox/cziegeler/configadmin-r7/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
Modified: felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java?rev=1751450&r1=1751449&r2=1751450&view=diff
==============================================================================
--- felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java (original)
+++ felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/CaseInsensitiveDictionary.java Tue Jul 5 11:33:18 2016
@@ -134,6 +134,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#elements()
*/
+ @Override
public Enumeration<Object> elements()
{
return Collections.enumeration( internalMap.values() );
@@ -145,6 +146,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#get(java.lang.Object)
*/
+ @Override
public Object get( Object key )
{
if ( key == null )
@@ -161,6 +163,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#isEmpty()
*/
+ @Override
public boolean isEmpty()
{
return internalMap.isEmpty();
@@ -172,6 +175,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#keys()
*/
+ @Override
public Enumeration<String> keys()
{
return Collections.enumeration( internalMap.keySet() );
@@ -183,6 +187,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#put(java.lang.String, java.lang.Object)
*/
+ @Override
public Object put( String key, Object value )
{
if ( key == null || value == null )
@@ -202,6 +207,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#remove(java.lang.Object)
*/
+ @Override
public Object remove( Object key )
{
if ( key == null )
@@ -218,6 +224,7 @@ public class CaseInsensitiveDictionary e
*
* @see java.util.Dictionary#size()
*/
+ @Override
public int size()
{
return internalMap.size();
@@ -349,11 +356,140 @@ public class CaseInsensitiveDictionary e
// ---------- Object Overwrites --------------------------------------------
+ @Override
public String toString()
{
return internalMap.toString();
}
+ @Override
+ public int hashCode()
+ {
+ return internalMap.hashCode();
+ }
+
+ @Override
+ public synchronized boolean equals(final Object o)
+ {
+ if (o == this)
+ {
+ return true;
+ }
+
+ if (!(o instanceof Dictionary))
+ {
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ final Dictionary<String,Object> t = (Dictionary<String,Object>) o;
+ if (t.size() != size())
+ {
+ return false;
+ }
+
+ try
+ {
+ final Enumeration<String> keys = keys();
+ while ( keys.hasMoreElements() )
+ {
+ final String key = keys.nextElement();
+ final Object value = get(key);
+
+ if (!value.equals(t.get(key)))
+ {
+ return false;
+ }
+ }
+ }
+ catch (ClassCastException unused)
+ {
+ return false;
+ }
+ catch (NullPointerException unused)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ public static Dictionary<String, Object> unmodifiable(Dictionary<String, Object> dict) {
+ return new UnmodifiableDictionary(dict);
+ }
+
+ public static final class UnmodifiableDictionary extends Dictionary<String, Object>
+ {
+ private final Dictionary<String, Object> delegatee;
+
+ public UnmodifiableDictionary(final Dictionary<String, Object> delegatee)
+ {
+ this.delegatee = delegatee;
+ }
+
+ @Override
+ public Object put(String key, Object value)
+ {
+ // prevent put
+ return null;
+ }
+
+ @Override
+ public Object remove(Object key)
+ {
+ // prevent remove
+ return null;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return delegatee.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ return delegatee.equals(obj);
+ }
+
+ @Override
+ public String toString()
+ {
+ return delegatee.toString();
+ }
+
+ @Override
+ public int size()
+ {
+ return delegatee.size();
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return delegatee.isEmpty();
+ }
+
+ @Override
+ public Enumeration<String> keys()
+ {
+ return delegatee.keys();
+ }
+
+ @Override
+ public Enumeration<Object> elements()
+ {
+ return delegatee.elements();
+ }
+
+ @Override
+ public Object get(Object key)
+ {
+ return delegatee.get(key);
+ }
+ }
+
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator implements Comparator<String>
Modified: felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java?rev=1751450&r1=1751449&r2=1751450&view=diff
==============================================================================
--- felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java (original)
+++ felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java Tue Jul 5 11:33:18 2016
@@ -818,7 +818,7 @@ public class ConfigurationManager implem
List pmList = new ArrayList();
CachingPersistenceManagerProxy[] pm;
- ServiceReference[] refs = persistenceManagerTracker.getServiceReferences();
+ ServiceReference<?>[] refs = persistenceManagerTracker.getServiceReferences();
if ( refs == null || refs.length == 0 )
{
pm = new CachingPersistenceManagerProxy[0];
@@ -1110,7 +1110,7 @@ public class ConfigurationManager implem
public void callPlugins( final Dictionary props, final ServiceReference sr, final String configPid,
final String factoryPid )
{
- ServiceReference[] plugins = null;
+ ServiceReference<?>[] plugins = null;
try
{
final String targetPid = (factoryPid == null) ? configPid : factoryPid;
@@ -1137,13 +1137,22 @@ public class ConfigurationManager implem
// call the plugins in order
for ( int i = 0; i < plugins.length; i++ )
{
- ServiceReference pluginRef = plugins[i];
+ ServiceReference<?> pluginRef = plugins[i];
ConfigurationPlugin plugin = ( ConfigurationPlugin ) bundleContext.getService( pluginRef );
if ( plugin != null )
{
+ // if cmRanking is below 0 or above 1000, ignore modifications from the plugin
+ boolean ignore = false;
+ Object rankObj = pluginRef.getProperty( ConfigurationPlugin.CM_RANKING );
+ if ( rankObj instanceof Integer )
+ {
+ final int ranking = ( ( Integer ) rankObj ).intValue();
+ ignore = (ranking < 0 ) || (ranking > 1000);
+ }
+
try
{
- plugin.modifyConfiguration( sr, props );
+ plugin.modifyConfiguration( sr, ignore ? CaseInsensitiveDictionary.unmodifiable(props) : props );
}
catch ( Throwable t )
{
Modified: felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/RankingComparator.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/RankingComparator.java?rev=1751450&r1=1751449&r2=1751450&view=diff
==============================================================================
--- felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/RankingComparator.java (original)
+++ felix/sandbox/cziegeler/configadmin-r7/src/main/java/org/apache/felix/cm/impl/RankingComparator.java Tue Jul 5 11:33:18 2016
@@ -29,10 +29,10 @@ import org.osgi.service.cm.Configuration
/**
* The <code>RankingComparator</code> may be used to maintain sorted
* sets or to sort arrays such that the first element in the set or
- * array is the one to use first and the last elementis the one to
+ * array is the one to use first and the last elements the one to
* use last.
*/
-public abstract class RankingComparator implements Comparator
+public abstract class RankingComparator implements Comparator<ServiceReference<?>>
{
/**
@@ -47,20 +47,20 @@ public abstract class RankingComparator
* <li><code>> 0</code> if obj1 has lower ranking than obj2</li>
* </ul>
*/
- public static Comparator SRV_RANKING = new RankingComparator()
+ public static Comparator<ServiceReference<?>> SRV_RANKING = new RankingComparator()
{
- public int compare( Object obj1, Object obj2 )
+ public int compare( ServiceReference<?> obj1, ServiceReference<?> obj2 )
{
- final long id1 = this.getLong( ( ServiceReference ) obj1, Constants.SERVICE_ID );
- final long id2 = this.getLong( ( ServiceReference ) obj2, Constants.SERVICE_ID );
+ final long id1 = this.getLong( obj1, Constants.SERVICE_ID );
+ final long id2 = this.getLong( obj2, Constants.SERVICE_ID );
if ( id1 == id2 )
{
return 0;
}
- final long rank1 = this.getLong( ( ServiceReference ) obj1, Constants.SERVICE_RANKING );
- final long rank2 = this.getLong( ( ServiceReference ) obj2, Constants.SERVICE_RANKING );
+ final int rank1 = this.getInteger( obj1, Constants.SERVICE_RANKING );
+ final int rank2 = this.getInteger( obj2, Constants.SERVICE_RANKING );
if ( rank1 == rank2 )
{
@@ -79,7 +79,8 @@ public abstract class RankingComparator
* the Configuration Admin specification. This results in collections
* where the first element has the lowest ranking value and the last
* element has the highest ranking value. Order amongst elements with
- * the same ranking value is left undefined. Thus the results of this
+ * the same ranking value is left undefined, however we order it
+ * by service id, lowest last. Thus the results of this
* comparator are as follows:
* <ul>
* <li><code>< 0</code> if obj1 has lower ranking than obj2</li>
@@ -87,33 +88,53 @@ public abstract class RankingComparator
* <li><code>> 0</code> if obj1 has higher ranking than obj2</li>
* </ul>
*/
- public static Comparator CM_RANKING = new RankingComparator()
+ public static Comparator<ServiceReference<?>> CM_RANKING = new RankingComparator()
{
- public int compare( Object obj1, Object obj2 )
+ public int compare( ServiceReference<?> obj1, ServiceReference<?> obj2 )
{
- final long rank1 = this.getLong( ( ServiceReference ) obj1, ConfigurationPlugin.CM_RANKING );
- final long rank2 = this.getLong( ( ServiceReference ) obj2, ConfigurationPlugin.CM_RANKING );
+ final long id1 = this.getLong( obj1, Constants.SERVICE_ID );
+ final long id2 = this.getLong( obj2, Constants.SERVICE_ID );
- if ( rank1 == rank2 )
+ if ( id1 == id2 )
{
return 0;
}
+ final int rank1 = this.getInteger( obj1, ConfigurationPlugin.CM_RANKING );
+ final int rank2 = this.getInteger( obj2, ConfigurationPlugin.CM_RANKING );
+
+ if ( rank1 == rank2 )
+ {
+ return ( id1 > id2 ) ? -1 : 1;
+ }
+
return ( rank1 < rank2 ) ? -1 : 1;
}
};
- protected long getLong( ServiceReference sr, String property )
+
+ protected int getInteger( ServiceReference<?> sr, String property )
{
Object rankObj = sr.getProperty( property );
- if ( rankObj instanceof Number )
+ if ( rankObj instanceof Integer )
{
- return ( ( Number ) rankObj ).longValue();
+ return ( ( Integer ) rankObj ).intValue();
}
// null or not an integer
return 0;
}
+ protected long getLong( ServiceReference<?> sr, String property )
+ {
+ Object rankObj = sr.getProperty( property );
+ if ( rankObj instanceof Long )
+ {
+ return ( ( Long ) rankObj ).longValue();
+ }
+
+ // null or not a long
+ return 0;
+ }
}
Modified: felix/sandbox/cziegeler/configadmin-r7/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/configadmin-r7/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java?rev=1751450&r1=1751449&r2=1751450&view=diff
==============================================================================
--- felix/sandbox/cziegeler/configadmin-r7/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java (original)
+++ felix/sandbox/cziegeler/configadmin-r7/src/test/java/org/apache/felix/cm/impl/RankingComparatorTest.java Tue Jul 5 11:33:18 2016
@@ -27,26 +27,26 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
-import junit.framework.TestCase;
-
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationPlugin;
+import junit.framework.TestCase;
+
public class RankingComparatorTest extends TestCase
{
- private final Comparator srvRank = RankingComparator.SRV_RANKING;
- private final Comparator cmRank = RankingComparator.CM_RANKING;
+ private final Comparator<ServiceReference<?>> srvRank = RankingComparator.SRV_RANKING;
+ private final Comparator<ServiceReference<?>> cmRank = RankingComparator.CM_RANKING;
public void test_service_ranking_no_property()
{
- ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
- ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
- ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?> r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
assertTrue( srvRank.compare( r1, r1 ) == 0 );
assertTrue( srvRank.compare( r1, r2 ) < 0 );
@@ -61,24 +61,24 @@ public class RankingComparatorTest exten
assertTrue( srvRank.compare( r3, r3 ) == 0 );
assertTrue( cmRank.compare( r1, r1 ) == 0 );
- assertTrue( cmRank.compare( r1, r2 ) == 0 );
- assertTrue( cmRank.compare( r1, r3 ) == 0 );
+ assertTrue( cmRank.compare( r1, r2 ) > 0 );
+ assertTrue( cmRank.compare( r1, r3 ) > 0 );
- assertTrue( cmRank.compare( r2, r1 ) == 0 );
+ assertTrue( cmRank.compare( r2, r1 ) < 0 );
assertTrue( cmRank.compare( r2, r2 ) == 0 );
- assertTrue( cmRank.compare( r2, r3 ) == 0 );
+ assertTrue( cmRank.compare( r2, r3 ) > 0 );
- assertTrue( cmRank.compare( r3, r1 ) == 0 );
- assertTrue( cmRank.compare( r3, r2 ) == 0 );
+ assertTrue( cmRank.compare( r3, r1 ) < 0 );
+ assertTrue( cmRank.compare( r3, r2 ) < 0 );
assertTrue( cmRank.compare( r3, r3 ) == 0 );
}
public void test_service_ranking_property()
{
- ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?> r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
assertTrue( srvRank.compare( r1, r1 ) == 0 );
assertTrue( srvRank.compare( r1, r2 ) < 0 );
@@ -96,11 +96,11 @@ public class RankingComparatorTest exten
public void test_service_cm_ranking_property()
{
- ServiceReference r1 = new MockServiceReference()
+ ServiceReference<?> r1 = new MockServiceReference()
.setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
assertTrue( cmRank.compare( r1, r1 ) == 0 );
assertTrue( cmRank.compare( r1, r2 ) > 0 );
@@ -118,10 +118,10 @@ public class RankingComparatorTest exten
public void test_service_ranking_sort()
{
- ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
- ServiceReference[] refs =
+ ServiceReference<?> r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?>[] refs =
{ r1, r2, r3 };
assertSame( r1, refs[0] );
@@ -138,16 +138,16 @@ public class RankingComparatorTest exten
public void test_service_ranking_set()
{
- ServiceReference r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
+ ServiceReference<?> r1 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( 100 ) );
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, new Integer( -100 ) );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( Constants.SERVICE_RANKING, null );
- Set refSet = new TreeSet( srvRank );
+ Set<ServiceReference<?>> refSet = new TreeSet<ServiceReference<?>>( srvRank );
refSet.add( r1 );
refSet.add( r2 );
refSet.add( r3 );
- Iterator refIter = refSet.iterator();
+ Iterator<ServiceReference<?>> refIter = refSet.iterator();
assertSame( r1, refIter.next() );
assertSame( r3, refIter.next() );
assertSame( r2, refIter.next() );
@@ -156,12 +156,12 @@ public class RankingComparatorTest exten
public void test_service_cm_ranking_sort()
{
- ServiceReference r1 = new MockServiceReference()
+ ServiceReference<?> r1 = new MockServiceReference()
.setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
- ServiceReference[] refs =
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
+ ServiceReference<?>[] refs =
{ r1, r2, r3 };
assertSame( r1, refs[0] );
@@ -178,29 +178,29 @@ public class RankingComparatorTest exten
public void test_service_cm_ranking_set()
{
- ServiceReference r1 = new MockServiceReference()
+ ServiceReference<?> r1 = new MockServiceReference()
.setProperty( ConfigurationPlugin.CM_RANKING, new Integer( 100 ) );
- ServiceReference r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
+ ServiceReference<?> r2 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING,
new Integer( -100 ) );
- ServiceReference r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
+ ServiceReference<?> r3 = new MockServiceReference().setProperty( ConfigurationPlugin.CM_RANKING, null );
- Set refSet = new TreeSet( cmRank );
+ Set<ServiceReference<?>> refSet = new TreeSet<ServiceReference<?>>( cmRank );
refSet.add( r1 );
refSet.add( r2 );
refSet.add( r3 );
- Iterator refIter = refSet.iterator();
+ Iterator<ServiceReference<?>> refIter = refSet.iterator();
assertSame( r2, refIter.next() );
assertSame( r3, refIter.next() );
assertSame( r1, refIter.next() );
}
- private static class MockServiceReference implements ServiceReference
+ private static class MockServiceReference implements ServiceReference<Object>
{
static long id = 0;
- private final Map props = new HashMap();
+ private final Map<String, Object> props = new HashMap<String, Object>();
{
props.put( Constants.SERVICE_ID, new Long( id ) );
@@ -222,42 +222,49 @@ public class RankingComparatorTest exten
}
+ @Override
public Object getProperty( String key )
{
return props.get( key );
}
+ @Override
public String[] getPropertyKeys()
{
- return ( String[] ) props.keySet().toArray( new String[props.size()] );
+ return props.keySet().toArray( new String[props.size()] );
}
+ @Override
public Bundle getBundle()
{
return null;
}
+ @Override
public Bundle[] getUsingBundles()
{
return null;
}
+ @Override
public boolean isAssignableTo( Bundle bundle, String className )
{
return false;
}
+ @Override
public int compareTo( Object reference )
{
return 0;
}
+ @Override
public String toString()
{
return "ServiceReference " + getProperty( Constants.SERVICE_ID );