tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [3/5] incubator-tamaya git commit: TAMAYA-236: improve ordinal handling.
Date Sun, 26 Feb 2017 23:05:07 GMT
TAMAYA-236: improve ordinal handling.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/3ecc1d59
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/3ecc1d59
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/3ecc1d59

Branch: refs/heads/master
Commit: 3ecc1d592b4a0f467c0de074cdf54368e6d0f2e2
Parents: f83f291
Author: anatole <anatole@apache.org>
Authored: Thu Feb 23 01:01:37 2017 +0100
Committer: anatole <anatole@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100

----------------------------------------------------------------------
 .../apache/tamaya/ConfigurationProvider.java    | 15 +++--
 .../internal/DefaultConfigurationContext.java   | 63 +-------------------
 .../core/internal/PropertySourceComparator.java | 41 ++++++++++++-
 .../core/internal/WrappedPropertySource.java    |  4 +-
 .../core/propertysource/BasePropertySource.java |  1 -
 .../apache/tamaya/core/TestPropertySource.java  |  1 -
 .../DefaultConfigurationContextBuilderTest.java |  7 +--
 .../ConverterTestsPropertySource.java           |  4 --
 .../propertysource/BasePropertySourceTest.java  |  3 +-
 9 files changed, 53 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
index 6927d94..a6ef03c 100644
--- a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
+++ b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
@@ -31,9 +31,8 @@ import java.util.logging.Logger;
 public final class ConfigurationProvider {
 
     private static final Logger LOG = Logger.getLogger(ConfigurationProvider.class.getName());
-    private static final ConfigurationProviderSpi PROVIDER_SPI = loadSpi();
 
-    private static ConfigurationProviderSpi loadSpi() {
+    private static ConfigurationProviderSpi spi() {
         ConfigurationProviderSpi spi = ServiceContextManager.getServiceContext()
                 .getService(ConfigurationProviderSpi.class);
         if(spi==null){
@@ -53,7 +52,7 @@ public final class ConfigurationProvider {
      * @return the corresponding Configuration instance, never null.
      */
     public static Configuration getConfiguration() {
-        return PROVIDER_SPI.getConfiguration();
+        return spi().getConfiguration();
     }
 
     /**
@@ -63,7 +62,7 @@ public final class ConfigurationProvider {
      * @return a new Configuration instance, never null.
      */
     public static Configuration createConfiguration(ConfigurationContext context) {
-        return PROVIDER_SPI.createConfiguration(context);
+        return spi().createConfiguration(context);
     }
 
     /**
@@ -74,7 +73,7 @@ public final class ConfigurationProvider {
      */
     @Deprecated
     public static ConfigurationContext getConfigurationContext() {
-        return PROVIDER_SPI.getConfigurationContext();
+        return spi().getConfigurationContext();
     }
 
     /**
@@ -91,7 +90,7 @@ public final class ConfigurationProvider {
      */
     @Deprecated
     public static void setConfigurationContext(ConfigurationContext context) {
-        PROVIDER_SPI.setConfigurationContext(context);
+        spi().setConfigurationContext(context);
     }
 
     /**
@@ -107,7 +106,7 @@ public final class ConfigurationProvider {
      */
     public static void setConfiguration(Configuration config) {
         LOG.info("TAMAYA Applying new Configuration: " + config);
-        PROVIDER_SPI.setConfiguration(config);
+        spi().setConfiguration(config);
     }
 
     /**
@@ -121,7 +120,7 @@ public final class ConfigurationProvider {
      * @see org.apache.tamaya.spi.ConfigurationContext
      */
     public static ConfigurationContextBuilder getConfigurationContextBuilder() {
-        return PROVIDER_SPI.getConfigurationContextBuilder();
+        return spi().getConfigurationContextBuilder();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java
b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java
index 4c6bc7b..a1b19d1 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java
@@ -111,7 +111,7 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
             writeLock.lock();
             List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
             newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
-            Collections.sort(newPropertySources, new PropertySourceComparator());
+            Collections.sort(newPropertySources, PropertySourceComparator.getInstance());
 
             this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
         } finally {
@@ -150,12 +150,11 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         if(immutablePropertySources.isEmpty()){
             b.append("  No property sources loaded.\n\n");
         }else {
-            b.append("  CLASS                         NAME                              
                                   ORDINAL SCANNABLE SIZE    STATE     ERROR\n\n");
+            b.append("  CLASS                         NAME                              
                               SCANNABLE SIZE    STATE     ERROR\n\n");
             for (PropertySource ps : immutablePropertySources) {
                 b.append("  ");
                 appendFormatted(b, ps.getClass().getSimpleName(), 30);
                 appendFormatted(b, ps.getName(), 70);
-                appendFormatted(b, String.valueOf(ps.getOrdinal()), 8);
                 appendFormatted(b, String.valueOf(ps.isScannable()), 10);
                 if (ps.isScannable()) {
                     appendFormatted(b, String.valueOf(ps.getProperties().size()), 8);
@@ -228,64 +227,6 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         return s.replace('\n', ' ').replace('\r', ' ');
     }
 
-    private static class PropertySourceComparator implements Comparator<PropertySource>,
Serializable {
-
-        private static final long serialVersionUID = 1L;
-
-        /**
-         * Order property source reversely, the most important come first.
-         *
-         * @param source1 the first PropertySource
-         * @param source2 the second PropertySource
-         * @return the comparison result.
-         */
-        private int comparePropertySources(PropertySource source1, PropertySource source2)
{
-            if (source1.getOrdinal() < source2.getOrdinal()) {
-                return -1;
-            } else if (source1.getOrdinal() > source2.getOrdinal()) {
-                return 1;
-            } else {
-                return source1.getClass().getName().compareTo(source2.getClass().getName());
-            }
-        }
-
-        @Override
-        public int compare(PropertySource source1, PropertySource source2) {
-            return comparePropertySources(source1, source2);
-        }
-    }
-
-    private static class PropertyFilterComparator implements Comparator<PropertyFilter>,
Serializable{
-
-        private static final long serialVersionUID = 1L;
-
-        /**
-         * Compare 2 filters for ordering the filter chain.
-         *
-         * @param filter1 the first filter
-         * @param filter2 the second filter
-         * @return the comparison result
-         */
-        private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2)
{
-            Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
-            Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
-            int ord1 = prio1 != null ? prio1.value() : 0;
-            int ord2 = prio2 != null ? prio2.value() : 0;
-
-            if (ord1 < ord2) {
-                return -1;
-            } else if (ord1 > ord2) {
-                return 1;
-            } else {
-                return filter1.getClass().getName().compareTo(filter2.getClass().getName());
-            }
-        }
-
-        @Override
-        public int compare(PropertyFilter filter1, PropertyFilter filter2) {
-            return comparePropertyFilters(filter1, filter2);
-        }
-    }
 
     @Override
     public List<PropertySource> getPropertySources() {

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
index 0b46f1c..a0006b4 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java
@@ -19,9 +19,15 @@
 package org.apache.tamaya.core.internal;
 
 import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValue;
 
+import javax.annotation.Priority;
 import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Comparator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Comparator for ordering of PropertySources based on their ordinal method and class name.
@@ -30,6 +36,8 @@ public class PropertySourceComparator implements Comparator<PropertySource>,
Ser
 
     private static final long serialVersionUID = 1L;
 
+    private static final Logger LOG = Logger.getLogger(PropertySourceComparator.class.getName());
+
     private static final PropertySourceComparator INSTANCE = new PropertySourceComparator();
 
     /** Singleton constructor. */
@@ -51,15 +59,44 @@ public class PropertySourceComparator implements Comparator<PropertySource>,
Ser
      * @return the comparison result.
      */
     private int comparePropertySources(PropertySource source1, PropertySource source2) {
-        if (source1.getOrdinal() < source2.getOrdinal()) {
+        if (getOrdinal(source1) < getOrdinal(source2)) {
             return -1;
-        } else if (source1.getOrdinal() > source2.getOrdinal()) {
+        } else if (getOrdinal(source1) > getOrdinal(source2)) {
             return 1;
         } else {
             return source1.getClass().getName().compareTo(source2.getClass().getName());
         }
     }
 
+    public static int getOrdinal(PropertySource propertySource) {
+        PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL);
+        if(ordinalValue!=null){
+            try{
+                return Integer.parseInt(ordinalValue.getValue().trim());
+            }catch(Exception e){
+                LOG.finest("Failed to parse ordinal from " + PropertySource.TAMAYA_ORDINAL
+
+                        " in " + propertySource.getName()+": "+ordinalValue.getValue());
+            }
+        }
+        try {
+            Method method = propertySource.getClass().getMethod("getOrdinal");
+            if(int.class.equals(method.getReturnType())){
+                try {
+                    return (int)method.invoke(propertySource);
+                } catch (Exception e) {
+                    LOG.log(Level.FINEST, "Error calling int getOrdinal() on " + propertySource.getName(),
e);
+                }
+            }
+        } catch (NoSuchMethodException e) {
+            LOG.finest("No int getOrdinal() method found in " + propertySource.getName());
+        }
+        Priority prio = propertySource.getClass().getAnnotation(Priority.class);
+        if(prio!=null){
+            return prio.value();
+        }
+        return 0;
+    }
+
     @Override
     public int compare(PropertySource source1, PropertySource source2) {
         return comparePropertySources(source1, source2);

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java
b/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java
index a6f4742..db1a911 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/internal/WrappedPropertySource.java
@@ -57,12 +57,11 @@ class WrappedPropertySource implements PropertySource{
         return new WrappedPropertySource(ps, ordinal);
     }
 
-    @Override
     public int getOrdinal() {
         if(this.ordinal!=null){
             return this.ordinal;
         }
-        return delegate.getOrdinal();
+        return PropertySourceComparator.getOrdinal(delegate);
     }
 
     public void setOrdinal(Integer ordinal) {
@@ -122,7 +121,6 @@ class WrappedPropertySource implements PropertySource{
                 ", scannable=" + isScannable() +
                 ", loadedAt=" + loaded +
                 ", delegate-class=" + delegate.getClass().getName() +
-                ", delegate-ordinal=" + delegate.getOrdinal() +
                 '}';
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
index c59f78f..87aaefc 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
@@ -106,7 +106,6 @@ public abstract class BasePropertySource implements PropertySource {
         this.defaultOrdinal = defaultOrdinal;
     }
 
-    @Override
     public int getOrdinal() {
         Integer ordinal = this.ordinal;
         if(ordinal!=null){

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java b/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java
index 1125f7a..50e26e4 100644
--- a/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java
+++ b/code/core/src/test/java/org/apache/tamaya/core/TestPropertySource.java
@@ -43,7 +43,6 @@ public class TestPropertySource implements PropertySource {
         this.ordinal = ordinal;
     }
 
-    @Override
     public int getOrdinal() {
         return ordinal;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java
b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java
index bbb9d14..9564bda 100644
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilderTest.java
@@ -23,6 +23,7 @@ import org.apache.tamaya.TypeLiteral;
 import org.apache.tamaya.spi.*;
 import org.junit.Test;
 
+import javax.annotation.Priority;
 import java.util.Collections;
 import java.util.Map;
 
@@ -191,6 +192,7 @@ public class DefaultConfigurationContextBuilderTest {
     }
 
 
+    @Priority(200)
     private static class TestPropertySource implements PropertySource{
 
         private String id;
@@ -204,11 +206,6 @@ public class DefaultConfigurationContextBuilderTest {
         }
 
         @Override
-        public int getOrdinal() {
-            return 200;
-        }
-
-        @Override
         public String getName() {
             return id!=null?id:"TestPropertySource";
         }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
index c2fe2d3..121e331 100644
--- a/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
+++ b/code/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java
@@ -28,10 +28,6 @@ import java.util.Map;
  * Test Property Source used by converter tests.
  */
 public class ConverterTestsPropertySource implements PropertySource{
-    @Override
-    public int getOrdinal() {
-        return 0;
-    }
 
     @Override
     public String getName(){

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3ecc1d59/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java
b/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java
index 8ab1baf..37cc86a 100644
--- a/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java
+++ b/code/core/src/test/java/org/apache/tamaya/core/propertysource/BasePropertySourceTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.tamaya.core.propertysource;
 
+import org.apache.tamaya.core.internal.PropertySourceComparator;
 import org.apache.tamaya.spi.PropertySource;
 import org.apache.tamaya.spi.PropertyValue;
 import org.apache.tamaya.spi.PropertyValueBuilder;
@@ -51,7 +52,7 @@ public class BasePropertySourceTest {
             }
         };
 
-        Assert.assertEquals(56, defaultPropertySource.getOrdinal());
+        Assert.assertEquals(56, PropertySourceComparator.getOrdinal(defaultPropertySource));
         Assert.assertEquals(1000, new OverriddenOrdinalPropertySource().getOrdinal());
 
         // propertySource with invalid ordinal


Mime
View raw message