polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [4/4] polygene-java git commit: Configuration default back to System Properties and Environment variables.
Date Sun, 04 Jun 2017 10:20:31 GMT
Configuration default back to System Properties and Environment variables.

Signed-off-by: niclas <niclas@hedhman.org>


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/05ddab16
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/05ddab16
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/05ddab16

Branch: refs/heads/develop
Commit: 05ddab16a0108b46ac71dd0f05f327f50dd81c1b
Parents: c782f07
Author: niclas <niclas@hedhman.org>
Authored: Sun Jun 4 18:14:54 2017 +0800
Committer: niclas <niclas@hedhman.org>
Committed: Sun Jun 4 18:14:54 2017 +0800

----------------------------------------------------------------------
 .../polygene/api/composite/PropertyMapper.java  |  2 +-
 .../api/configuration/Configuration.java        | 68 ++++++++++++++++----
 .../ConfigurationInstantiationTest.java         | 31 ++++++++-
 3 files changed, 85 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/05ddab16/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
b/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
index acff127..59e1f67 100644
--- a/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
+++ b/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
@@ -114,7 +114,7 @@ public final class PropertyMapper
             }
             catch( NoSuchMethodException e )
             {
-                throw new IllegalArgumentException( "Could not find any property named "
+ objectObjectEntry.getKey() );
+//                throw new IllegalArgumentException( "Could not find any property named
" + objectObjectEntry.getKey() );
             }
             catch( IllegalAccessException e )
             {

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/05ddab16/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java
b/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java
index a47719f..bca886f 100644
--- a/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java
+++ b/core/api/src/main/java/org/apache/polygene/api/configuration/Configuration.java
@@ -23,7 +23,10 @@ package org.apache.polygene.api.configuration;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Map;
 import java.util.Objects;
+import java.util.Properties;
+import java.util.stream.Stream;
 import org.apache.polygene.api.PolygeneAPI;
 import org.apache.polygene.api.composite.Composite;
 import org.apache.polygene.api.composite.PropertyMapper;
@@ -285,15 +288,19 @@ public interface Configuration<T>
                         config = tryLoadXmlFile( buildUow, entityDescriptor, identity );
                         if( config == null )
                         {
-                            try
+                            config = tryLoadSystemProperties( buildUow, entityDescriptor,
identity );
+                            if( config == null )
                             {
-                                EntityBuilder<V> configBuilder = buildUow.newEntityBuilder(
-                                    serviceModel.<V>configurationType(), identity );
-                                configBuilder.newInstance();
-                            }
-                            catch( ConstraintViolationException e )
-                            {
-                                throw new NoSuchConfigurationException( configType, identity,
e );
+                                try
+                                {
+                                    EntityBuilder<V> configBuilder =
+                                        buildUow.newEntityBuilder( serviceModel.<V>configurationType(),
identity );
+                                    configBuilder.newInstance();
+                                }
+                                catch( ConstraintViolationException e )
+                                {
+                                    throw new NoSuchConfigurationException( configType, identity,
e );
+                                }
                             }
                         }
                     }
@@ -317,10 +324,47 @@ public interface Configuration<T>
             }
         }
 
-        private <V> V tryLoadPropertiesFile( UnitOfWork buildUow,
-                                             EntityDescriptor configType,
-                                             Identity identity
-                                           )
+        private <V> V tryLoadSystemProperties( UnitOfWork buildUow, EntityDescriptor
configType, Identity identity )
+            throws InstantiationException
+        {
+            @SuppressWarnings( "unchecked" )
+            EntityBuilder<V> configBuilder = buildUow.newEntityBuilder( (Class<V>)
configType.primaryType(), identity );
+            PropertyMapper.map( systemProperties(), (Composite) configBuilder.instance()
);
+            return configBuilder.newInstance();
+        }
+
+        private Properties systemProperties()
+        {
+            Stream<Map.Entry<?, ?>> allProps =
+                Stream.concat( System.getenv().entrySet().stream(), System.getProperties().entrySet().stream()
);
+            Properties props = new Properties();
+            allProps.forEach( entry -> props.put( transform( (String) entry.getKey() ),
entry.getValue() ) );
+            return props;
+        }
+
+        private String transform( String text )
+        {
+            boolean upper = false;
+            StringBuilder builder = new StringBuilder();
+            for( int i = 0; i < text.length(); i++ )
+            {
+                char ch = Character.toLowerCase( text.charAt( i ) );
+                if( ch == '.' )
+                {
+                    upper = true;
+                    continue;
+                }
+                if( upper )
+                {
+                    ch = Character.toUpperCase( ch );
+                    upper = false;
+                }
+                builder.append( ch );
+            }
+            return builder.toString();
+        }
+
+        private <V> V tryLoadPropertiesFile( UnitOfWork buildUow, EntityDescriptor
configType, Identity identity )
             throws InstantiationException
         {
             @SuppressWarnings( "unchecked" )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/05ddab16/core/runtime/src/test/java/org/apache/polygene/runtime/instantiation/ConfigurationInstantiationTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/instantiation/ConfigurationInstantiationTest.java
b/core/runtime/src/test/java/org/apache/polygene/runtime/instantiation/ConfigurationInstantiationTest.java
index 404f793..59b6880 100644
--- a/core/runtime/src/test/java/org/apache/polygene/runtime/instantiation/ConfigurationInstantiationTest.java
+++ b/core/runtime/src/test/java/org/apache/polygene/runtime/instantiation/ConfigurationInstantiationTest.java
@@ -44,20 +44,29 @@ public class ConfigurationInstantiationTest extends AbstractPolygeneTest
         module.services( MemoryEntityStoreService.class );
         module.services( MyService.class ).instantiateOnStartup();
         module.configurations( MyConfig.class );
+        System.setProperty( "path", "fakepath" );
     }
 
     @Test
     public void givenSpecialInitializableWhenStartingExpectOsNameToBeSet()
     {
         MyService myService = serviceFinder.findService( MyService.class ).get();
-        assertThat( myService.osName(), equalTo(System.getProperty( "os.name" )));
+        assertThat( myService.osName(), equalTo( System.getProperty( "os.name" ) ) );
+        if( myService.osName().equalsIgnoreCase( "Linux" ) )
+        {
+            assertThat( myService.home(), equalTo( System.getProperty( "user.home" ) ) );
+        }
+        assertThat( myService.path(), equalTo( System.getProperty( "path" ) ) );
     }
 
-
-    @Mixins( MyMixin.class)
+    @Mixins( MyMixin.class )
     public interface MyService
     {
         String osName();
+
+        String home();
+
+        String path();
     }
 
     public class MyMixin
@@ -73,6 +82,18 @@ public class ConfigurationInstantiationTest extends AbstractPolygeneTest
         }
 
         @Override
+        public String home()
+        {
+            return config.get().home().get();
+        }
+
+        @Override
+        public String path()
+        {
+            return config.get().path().get();
+        }
+
+        @Override
         public void create()
             throws Exception
         {
@@ -90,5 +111,9 @@ public class ConfigurationInstantiationTest extends AbstractPolygeneTest
     public interface MyConfig
     {
         Property<String> osName();
+
+        Property<String> home();
+
+        Property<String> path();
     }
 }


Mime
View raw message