openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1769542 - in /openwebbeans/meecrowave/trunk/meecrowave-core/src: main/java/org/apache/meecrowave/Meecrowave.java test/java/org/apache/meecrowave/MeecrowaveTest.java
Date Sun, 13 Nov 2016 19:45:06 GMT
Author: rmannibucau
Date: Sun Nov 13 19:45:06 2016
New Revision: 1769542

URL: http://svn.apache.org/viewvc?rev=1769542&view=rev
Log:
ability to bind config from the properties of the container, next step will likely be to have
dynamic @CliOption

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java?rev=1769542&r1=1769541&r2=1769542&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
Sun Nov 13 19:45:06 2016
@@ -86,6 +86,8 @@ import java.util.TreeMap;
 import java.util.function.Consumer;
 import java.util.stream.Stream;
 
+import static java.beans.Introspector.decapitalize;
+import static java.lang.Character.toUpperCase;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.emptySet;
 import static java.util.Optional.ofNullable;
@@ -1545,6 +1547,60 @@ public class Meecrowave implements AutoC
                 }
             }
         }
+
+        public <T> T bind(final T instance, final String prefix) {
+            ofNullable(properties.stringPropertyNames()).orElse(emptySet()).stream()
+                    .filter(p -> p.startsWith(prefix))
+                    .forEach(p -> {
+                        final String value = properties.getProperty(p);
+
+                        // convert iphen case to camel case (a-simple-sample becomes aSimpleSample)
+                        final int startIdx = prefix.length();
+                        final StringBuilder nameBuilder = new StringBuilder(p.length() /*ok
this is wrong but allocates something big enough*/);
+                        nameBuilder.append(decapitalize(p.substring(startIdx, startIdx +
1)));
+                        boolean uppercase = false;
+                        for (int i = 1; i < p.length() - prefix.length(); i++) {
+                            final char c = p.charAt(startIdx + i);
+                            if (c == '-') {
+                                uppercase = true;
+                            } else if (uppercase) {
+                                nameBuilder.append(toUpperCase(c));
+                                uppercase = false;
+                            } else {
+                                nameBuilder.append(c);
+                            }
+                        }
+
+                        final String name = nameBuilder.toString();
+                        Class<?> current = instance.getClass();
+                        do {
+                            try {
+                                final Field f = instance.getClass().getDeclaredField(name);
+                                if (!f.isAccessible()) {
+                                    f.setAccessible(true);
+                                }
+                                final Class<?> type = f.getType();
+                                if (type == String.class) {
+                                    f.set(instance, value);
+                                } else if (type == int.class) {
+                                    f.set(instance, Integer.parseInt(value));
+                                } else if (type == boolean.class) {
+                                    f.set(instance, Boolean.parseBoolean(value));
+                                } else {
+                                    throw new IllegalArgumentException("Unsupported type
" + type);
+                                }
+                                return;
+                            } catch (final NoSuchFieldException e) {
+                                // continue
+                            } catch (final IllegalAccessException e) {
+                                new LogFacade(Meecrowave.class.getName()).warn("Can't set
" + value + " to " + name + " on " + instance);
+                            }
+                            current = current.getSuperclass();
+                        } while (current != Object.class && current != null);
+                        throw new IllegalArgumentException("Didn't find " + name + " on "
+ instance);
+                    });
+            return instance;
+        }
     }
 
     public static class LoginConfigBuilder {

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java?rev=1769542&r1=1769541&r2=1769542&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/test/java/org/apache/meecrowave/MeecrowaveTest.java
Sun Nov 13 19:45:06 2016
@@ -35,10 +35,29 @@ import java.net.URL;
 import java.util.stream.Stream;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 public class MeecrowaveTest {
     @Test
+    public void configBinding() {
+        final MyConfig config = new Meecrowave.Builder()
+                .property("my-prefix-port", "1234")
+                .property("my-prefix-another-port", "5678")
+                .property("my-prefix-a-last-port-value", "9632")
+                .property("my-prefix-passthrough", "any value")
+                .property("my-prefix-bool", "true")
+                .bind(new MyConfig(), "my-prefix-");
+        assertNotNull(config);
+        assertEquals(1234, config.port);
+        assertEquals(5678, config.anotherPort);
+        assertEquals(9632, config.aLastPortValue);
+        assertEquals("any value", config.passthrough);
+        assertTrue(config.bool);
+    }
+
+    @Test
     public void simpleWebapp() {
         final File root = new File("target/MeecrowaveTest/simpleWebapp/app");
         FileUtils.mkDir(root);
@@ -113,4 +132,12 @@ public class MeecrowaveTest {
         }
         return null;
     }
+
+    public static class MyConfig {
+        private int port;
+        private int anotherPort;
+        private int aLastPortValue;
+        private String passthrough;
+        private boolean bool;
+    }
 }



Mime
View raw message