camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject [3/8] camel git commit: CAMEL-11190: Service Call - Allow to configure static server list from a property placholder
Date Mon, 24 Apr 2017 16:59:57 GMT
CAMEL-11190: Service Call - Allow to configure static server list from a property placholder


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/76481d98
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/76481d98
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/76481d98

Branch: refs/heads/master
Commit: 76481d98774959fae7db6741a67cc9e0fecd8eb9
Parents: bb563ea
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Mon Apr 24 16:39:57 2017 +0200
Committer: lburgazzoli <lburgazzoli@gmail.com>
Committed: Mon Apr 24 16:49:47 2017 +0200

----------------------------------------------------------------------
 .../impl/cloud/BlacklistServiceFilter.java      |  5 +++
 ...stServiceCallServiceFilterConfiguration.java | 40 ++++++++++++++++++++
 .../ServiceCallConfigurationDefinition.java     |  7 ++++
 .../ServiceCallExpressionConfiguration.java     | 17 +++++++++
 .../ServiceCallServiceChooserConfiguration.java | 17 +++++++++
 ...erviceCallServiceDiscoveryConfiguration.java | 17 +++++++++
 .../ServiceCallServiceFilterConfiguration.java  | 17 +++++++++
 ...iceCallServiceLoadBalancerConfiguration.java | 17 +++++++++
 ...erviceCallServiceDiscoveryConfiguration.java | 40 ++++++++++++++++++++
 .../cloud/AggregatingServiceDiscoveryTest.java  | 15 ++++++++
 .../cloud/ServiceCallConfigurationTest.java     | 34 ++++++++++++++++-
 11 files changed, 224 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/impl/cloud/BlacklistServiceFilter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/cloud/BlacklistServiceFilter.java
b/camel-core/src/main/java/org/apache/camel/impl/cloud/BlacklistServiceFilter.java
index 5a9a295..1df142c 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/cloud/BlacklistServiceFilter.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/cloud/BlacklistServiceFilter.java
@@ -18,6 +18,7 @@ package org.apache.camel.impl.cloud;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -108,6 +109,10 @@ public class BlacklistServiceFilter implements ServiceFilter {
         return services.stream().filter(s -> !this.services.contains(s)).collect(Collectors.toList());
     }
 
+    List<ServiceDefinition> getBlacklistedServices() {
+        return Collections.unmodifiableList(this.services);
+    }
+
     // *************************************************************************
     // Helpers
     // *************************************************************************

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/BlacklistServiceCallServiceFilterConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/BlacklistServiceCallServiceFilterConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/BlacklistServiceCallServiceFilterConfiguration.java
index 1d1a101..385b80b 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/BlacklistServiceCallServiceFilterConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/BlacklistServiceCallServiceFilterConfiguration.java
@@ -16,13 +16,19 @@
  */
 package org.apache.camel.model.cloud;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.ObjectHelper;
 
 @Metadata(label = "routing,cloud,service-filter")
 @XmlRootElement(name = "blacklistServiceFilter")
@@ -65,4 +71,38 @@ public class BlacklistServiceCallServiceFilterConfiguration extends ServiceCallS
         setServers(servers);
         return this;
     }
+
+    /**
+     * Sets the server list;
+     */
+    public BlacklistServiceCallServiceFilterConfiguration servers(String servers) {
+        if (ObjectHelper.isNotEmpty(servers)) {
+            String[] parts = servers.split(",");
+
+            if (this.servers == null) {
+                this.servers = new ArrayList<>();
+            }
+
+            this.servers.addAll(Arrays.asList(parts));
+        }
+
+        return this;
+    }
+
+    // *************************************************************************
+    // Utilities
+    // *************************************************************************
+
+    protected void postProcessFactoryParameters(CamelContext camelContext, Map<String,
Object> parameters) throws Exception  {
+        List<String> servers = List.class.cast(parameters.get("servers"));
+
+        if (ObjectHelper.isNotEmpty(servers)) {
+            final ListIterator<String> it = servers.listIterator();
+            while (it.hasNext()) {
+                it.set(camelContext.resolvePropertyPlaceholders(it.next()));
+            }
+
+            parameters.put("servers", servers);
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java
index 2d8fc96..984bdd1 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java
@@ -587,6 +587,13 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType
{
         return conf;
     }
 
+    public BlacklistServiceCallServiceFilterConfiguration blacklistFilet() {
+        BlacklistServiceCallServiceFilterConfiguration conf = new BlacklistServiceCallServiceFilterConfiguration();
+        setServiceFilterConfiguration(conf);
+
+        return conf;
+    }
+
     public ServiceCallConfigurationDefinition customFilter(String serviceFilter) {
         CustomServiceCallServiceFilterConfiguration conf = new CustomServiceCallServiceFilterConfiguration();
         conf.setServiceFilterRef(serviceFilter);

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java
index 1565b1b..8118233 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java
@@ -246,6 +246,23 @@ public class ServiceCallExpressionConfiguration extends IdentifiedType
implement
                     Map<String, Object> parameters = new HashMap<>();
                     IntrospectionSupport.getProperties(this, parameters, null, false);
 
+                    parameters.replaceAll(
+                        (k, v) -> {
+                            if (v != null && v instanceof String) {
+                                try {
+                                    v = camelContext.resolvePropertyPlaceholders((String)
v);
+                                } catch (Exception e) {
+                                    throw new IllegalArgumentException(
+                                        String.format("Exception while resolving %s (%s)",
k, v.toString()),
+                                        e
+                                    );
+                                }
+                            }
+
+                            return v;
+                        }
+                    );
+
                     // Convert properties to Map<String, String>
                     parameters.put("properties", getPropertiesAsMap(camelContext));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceChooserConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceChooserConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceChooserConfiguration.java
index 0228f72..e349409 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceChooserConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceChooserConfiguration.java
@@ -162,6 +162,23 @@ public class ServiceCallServiceChooserConfiguration extends IdentifiedType
imple
                 Map<String, Object> parameters = new HashMap<>();
                 IntrospectionSupport.getProperties(this, parameters, null, false);
 
+                parameters.replaceAll(
+                    (k, v) -> {
+                        if (v != null && v instanceof String) {
+                            try {
+                                v = camelContext.resolvePropertyPlaceholders((String) v);
+                            } catch (Exception e) {
+                                throw new IllegalArgumentException(
+                                    String.format("Exception while resolving %s (%s)", k,
v.toString()),
+                                    e
+                                );
+                            }
+                        }
+
+                        return v;
+                    }
+                );
+
                 // Convert properties to Map<String, String>
                 parameters.put("properties", getPropertiesAsMap(camelContext));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java
index 49e6c84..6e3a685 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java
@@ -169,6 +169,23 @@ public class ServiceCallServiceDiscoveryConfiguration extends IdentifiedType
imp
                 Map<String, Object> parameters = new HashMap<>();
                 IntrospectionSupport.getProperties(this, parameters, null, false);
 
+                parameters.replaceAll(
+                    (k, v) -> {
+                        if (v != null && v instanceof String) {
+                            try {
+                                v = camelContext.resolvePropertyPlaceholders((String) v);
+                            } catch (Exception e) {
+                                throw new IllegalArgumentException(
+                                    String.format("Exception while resolving %s (%s)", k,
v.toString()),
+                                    e
+                                );
+                            }
+                        }
+
+                        return v;
+                    }
+                );
+
                 // Convert properties to Map<String, String>
                 parameters.put("properties", getPropertiesAsMap(camelContext));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceFilterConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceFilterConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceFilterConfiguration.java
index afb2e94..60b9938 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceFilterConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceFilterConfiguration.java
@@ -162,6 +162,23 @@ public class ServiceCallServiceFilterConfiguration extends IdentifiedType
implem
                 Map<String, Object> parameters = new HashMap<>();
                 IntrospectionSupport.getProperties(this, parameters, null, false);
 
+                parameters.replaceAll(
+                    (k, v) -> {
+                        if (v != null && v instanceof String) {
+                            try {
+                                v = camelContext.resolvePropertyPlaceholders((String) v);
+                            } catch (Exception e) {
+                                throw new IllegalArgumentException(
+                                    String.format("Exception while resolving %s (%s)", k,
v.toString()),
+                                    e
+                                );
+                            }
+                        }
+
+                        return v;
+                    }
+                );
+
                 // Convert properties to Map<String, String>
                 parameters.put("properties", getPropertiesAsMap(camelContext));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java
index a1057a4..094e2f5 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceLoadBalancerConfiguration.java
@@ -162,6 +162,23 @@ public class ServiceCallServiceLoadBalancerConfiguration extends IdentifiedType
                 Map<String, Object> parameters = new HashMap<>();
                 IntrospectionSupport.getProperties(this, parameters, null, false);
 
+                parameters.replaceAll(
+                    (k, v) -> {
+                        if (v != null && v instanceof String) {
+                            try {
+                                v = camelContext.resolvePropertyPlaceholders((String) v);
+                            } catch (Exception e) {
+                                throw new IllegalArgumentException(
+                                    String.format("Exception while resolving %s (%s)", k,
v.toString()),
+                                    e
+                                );
+                            }
+                        }
+
+                        return v;
+                    }
+                );
+
                 // Convert properties to Map<String, String>
                 parameters.put("properties", getPropertiesAsMap(camelContext));
 

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/main/java/org/apache/camel/model/cloud/StaticServiceCallServiceDiscoveryConfiguration.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/StaticServiceCallServiceDiscoveryConfiguration.java
b/camel-core/src/main/java/org/apache/camel/model/cloud/StaticServiceCallServiceDiscoveryConfiguration.java
index 0c5dae1..d35a00c 100644
--- a/camel-core/src/main/java/org/apache/camel/model/cloud/StaticServiceCallServiceDiscoveryConfiguration.java
+++ b/camel-core/src/main/java/org/apache/camel/model/cloud/StaticServiceCallServiceDiscoveryConfiguration.java
@@ -17,13 +17,19 @@
 
 package org.apache.camel.model.cloud;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.ObjectHelper;
 
 @Metadata(label = "routing,cloud,service-discovery")
 @XmlRootElement(name = "staticServiceDiscovery")
@@ -66,4 +72,38 @@ public class StaticServiceCallServiceDiscoveryConfiguration extends ServiceCallS
         setServers(servers);
         return this;
     }
+
+    /**
+     * Sets the server list;
+     */
+    public StaticServiceCallServiceDiscoveryConfiguration servers(String servers) {
+        if (ObjectHelper.isNotEmpty(servers)) {
+            String[] parts = servers.split(",");
+
+            if (this.servers == null) {
+                this.servers = new ArrayList<>();
+            }
+
+            this.servers.addAll(Arrays.asList(parts));
+        }
+
+        return this;
+    }
+
+    // *************************************************************************
+    // Utilities
+    // *************************************************************************
+
+    protected void postProcessFactoryParameters(CamelContext camelContext, Map<String,
Object> parameters) throws Exception  {
+        List<String> servers = List.class.cast(parameters.get("servers"));
+
+        if (ObjectHelper.isNotEmpty(servers)) {
+            final ListIterator<String> it = servers.listIterator();
+            while (it.hasNext()) {
+                it.set(camelContext.resolvePropertyPlaceholders(it.next()));
+            }
+
+            parameters.put("servers", servers);
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/test/java/org/apache/camel/impl/cloud/AggregatingServiceDiscoveryTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/AggregatingServiceDiscoveryTest.java
b/camel-core/src/test/java/org/apache/camel/impl/cloud/AggregatingServiceDiscoveryTest.java
index 76a5a59..8fe4249 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/cloud/AggregatingServiceDiscoveryTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/AggregatingServiceDiscoveryTest.java
@@ -68,4 +68,19 @@ public class AggregatingServiceDiscoveryTest extends ContextTestSupport
{
         Assert.assertEquals(3, discovery.getServices("discovery1").size());
         Assert.assertEquals(1, discovery.getServices("discovery2").size());
     }
+
+    @Test
+    public void testMultiServiceDiscoveryConfigurationWithPlaceholders() throws Exception
{
+        System.setProperty("svc-list-1", "discovery1@localhost:1111,discovery1@localhost:1112");
+        System.setProperty("svc-list-2", "discovery1@localhost:1113,discovery2@localhost:1114");
+
+        AggregatingServiceCallServiceDiscoveryConfiguration multiConf = new AggregatingServiceCallServiceDiscoveryConfiguration();
+        multiConf.staticServiceDiscovery().servers("{{svc-list-1}}");
+        multiConf.staticServiceDiscovery().servers("{{svc-list-2}}");
+
+        AggregatingServiceDiscovery discovery = (AggregatingServiceDiscovery)multiConf.newInstance(context);
+        Assert.assertEquals(2, discovery.getDelegates().size());
+        Assert.assertEquals(3, discovery.getServices("discovery1").size());
+        Assert.assertEquals(1, discovery.getServices("discovery2").size());
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/76481d98/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java
b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java
index 67c0105..dc60788 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java
@@ -16,12 +16,14 @@
  */
 package org.apache.camel.impl.cloud;
 
+import java.util.List;
 import java.util.UUID;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.cloud.ServiceDefinition;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.SimpleRegistry;
 import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
@@ -282,8 +284,15 @@ public class ServiceCallConfigurationTest {
         try {
             System.setProperty("scall.name", "service-name");
             System.setProperty("scall.scheme", "file");
+            System.setProperty("scall.servers1", "hello-service@localhost:8081,hello-service@localhost:8082");
+            System.setProperty("scall.servers2", "hello-svc@localhost:8083,hello-svc@localhost:8084");
+            System.setProperty("scall.filter", "hello-svc@localhost:8083");
+
+            ServiceCallConfigurationDefinition global = new ServiceCallConfigurationDefinition();
+            global.blacklistFilet().servers("{{scall.filter}}");
 
             context = new DefaultCamelContext();
+            context.setServiceCallConfiguration(global);
             context.addRoutes(new RouteBuilder() {
                 @Override
                 public void configure() throws Exception {
@@ -293,7 +302,10 @@ public class ServiceCallConfigurationTest {
                             .name("{{scall.name}}")
                             .component("{{scall.scheme}}")
                             .uri("direct:{{scall.name}}")
-                            .serviceDiscovery(new StaticServiceDiscovery())
+                            .staticServiceDiscovery()
+                                .servers("{{scall.servers1}}")
+                                .servers("{{scall.servers2}}")
+                                .end()
                         .end();
                 }
             });
@@ -308,6 +320,21 @@ public class ServiceCallConfigurationTest {
             Assert.assertEquals("file", proc.getScheme());
             Assert.assertEquals("direct:service-name", proc.getUri());
 
+            DefaultServiceLoadBalancer lb = (DefaultServiceLoadBalancer)proc.getLoadBalancer();
+
+            Assert.assertTrue(lb.getServiceFilter() instanceof BlacklistServiceFilter);
+            BlacklistServiceFilter filter = (BlacklistServiceFilter)lb.getServiceFilter();
+            List<ServiceDefinition> blacklist = filter.getBlacklistedServices();
+            Assert.assertEquals(1, blacklist.size());
+
+            Assert.assertTrue(lb.getServiceDiscovery() instanceof StaticServiceDiscovery);
+
+            List<ServiceDefinition> services1 = lb.getServiceDiscovery().getServices("hello-service");
+            Assert.assertEquals(2,  filter.apply(services1).size());
+
+            List<ServiceDefinition> services2 = lb.getServiceDiscovery().getServices("hello-svc");
+            Assert.assertEquals(1, filter.apply(services2).size());
+
         } finally {
             if (context != null) {
                 context.stop();
@@ -315,7 +342,10 @@ public class ServiceCallConfigurationTest {
 
             // Cleanup system properties
             System.clearProperty("scall.name");
-            System.clearProperty("scall.component");
+            System.clearProperty("scall.scheme");
+            System.clearProperty("scall.servers1");
+            System.clearProperty("scall.servers2");
+            System.clearProperty("scall.filter");
         }
 
         context.stop();


Mime
View raw message