You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/01/23 09:41:15 UTC

[camel] branch master updated (a5f9e25 -> 3d8e612)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from a5f9e25  Upgrade Chronicle Wire to version 1.10.12
     new 239a862  Added dns activation policy
     new 3f9c1ef  Added A record and unit tests
     new cac59be  Added Dns Activation documentation
     new 3d8e612  Fixed formatting to comply with rules

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel-dns/src/main/docs/dns-component.adoc     |  21 ++
 .../camel/component/dns/policy/DnsActivation.java  | 142 ++++++++++++++
 .../component/dns/policy/DnsActivationPolicy.java  | 215 +++++++++++++++++++++
 .../component/dns/policy/DnsActivationTest.java}   |  22 ++-
 4 files changed, 391 insertions(+), 9 deletions(-)
 create mode 100644 components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
 create mode 100644 components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
 copy components/{camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/Defaults.java => camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java} (60%)

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 03/04: Added Dns Activation documentation

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cac59be3fe4f6e1e560643719ff12779479d323c
Author: Max Fortun <Ma...@wsj.com>
AuthorDate: Fri Jan 19 13:15:10 2018 -0500

    Added Dns Activation documentation
---
 .../camel-dns/src/main/docs/dns-component.adoc      | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/components/camel-dns/src/main/docs/dns-component.adoc b/components/camel-dns/src/main/docs/dns-component.adoc
index 9990371..57eaefa 100644
--- a/components/camel-dns/src/main/docs/dns-component.adoc
+++ b/components/camel-dns/src/main/docs/dns-component.adoc
@@ -145,6 +145,27 @@ Dig is a Unix command-line utility to run DNS queries.
 
 The query must be provided in the header with key `"dns.query"`.
 
+
+### Dns Activation Policy
+
+DnsActivationPolicy can be used to dynamically start and stop routes based on dns state.
+
+If you have instances of the same component running in different regions you can configure a route in each region to activate only if dns is pointing to its region.
+
+i.e. You may have an instance in NYC and an instance in SFO. You would configure a service CNAME service.example.com to point to nyc-service.example.com to bring NYC instance up and SFO instance down. When you change the CNAME service.example.com to point to sfo-service.example.com -- nyc instance would stop its routes and sfo will bring its routes up. This allows you to switch regions without restarting actual components.
+
+[source,xml]
+----------------------------------------------------------------------------------------------------------------
+	<bean id="dnsActivationPolicy" class="org.apache.camel.component.dns.policy.DnsActivationPolicy">
+		<property name="hostname" value="service.example.com" />
+		<property name="resolvesTo" value="nyc-service.example.com" />
+		<property name="ttl" value="60000" />
+	</bean>
+
+	<route id="routeId" autoStartup="false" routePolicyRef="dnsActivationPolicy">
+	</route>
+----------------------------------------------------------------------------------------------------------------
+
 ### See Also
 
 * link:configuring-camel.html[Configuring Camel]

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 02/04: Added A record and unit tests

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3f9c1ef64be44573478d2f052afee8fd3bdf4ada
Author: Max Fortun <Ma...@wsj.com>
AuthorDate: Fri Jan 19 12:37:41 2018 -0500

    Added A record and unit tests
---
 .../camel/component/dns/policy/DnsActivation.java  |  7 ++-----
 .../component/dns/policy/DnsActivationTest.java    | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
index ddf352e..4eb8ad5 100644
--- a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
+++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
@@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
 public class DnsActivation {
     private static final transient Logger logger = LoggerFactory.getLogger(DnsActivation.class);
 
-	private final static String[] DNS_TYPES_CNAME = {"CNAME"}; 
+	private final static String[] DNS_TYPES = {"CNAME", "A"}; 
 
 	private String hostname;
 	private List<String> resolvesTo = new ArrayList<String>();
@@ -70,7 +70,7 @@ public class DnsActivation {
 				String hostname = hostnames.remove(0);
 				InetAddress inetAddress = InetAddress.getByName(hostname);
 				InitialDirContext initialDirContext = new InitialDirContext();
-				Attributes attributes = initialDirContext.getAttributes("dns:/" + inetAddress.getHostName(), DNS_TYPES_CNAME);
+				Attributes attributes = initialDirContext.getAttributes("dns:/" + inetAddress.getHostName(), DNS_TYPES);
 				attributeEnumeration = attributes.getAll();
 				while(attributeEnumeration.hasMore()) {
 					Attribute attribute = (Attribute)attributeEnumeration.next();
@@ -113,9 +113,6 @@ public class DnsActivation {
 			while (inetAddressesEnumeration.hasMoreElements()) {
 				InetAddress inetAddress = inetAddressesEnumeration.nextElement();
 				String ip = inetAddress.getHostAddress();
-				if(ip.startsWith("127.")) {
-					continue;
-				}
 				logger.debug("Local ip: "+ip);
 				localIps.add(ip);
 			}
diff --git a/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java b/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java
new file mode 100644
index 0000000..ad74e09
--- /dev/null
+++ b/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java
@@ -0,0 +1,22 @@
+package org.apache.camel.component.dns.policy;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class DnsActivationTest {
+    @Test
+    public void testDnsActivation() throws Exception {
+
+        DnsActivation dnsActivationActive = new DnsActivation("localhost", Arrays.asList("127.0.0.1"));
+        assertTrue(dnsActivationActive.isActive());
+
+        DnsActivation dnsActivationInactive = new DnsActivation("localhost", Arrays.asList("127.0.0.2"));
+        assertFalse(dnsActivationInactive.isActive());
+
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 04/04: Fixed formatting to comply with rules

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3d8e61214a9902c5a888a7f875e02316f2a6cd27
Author: Max Fortun <Ma...@wsj.com>
AuthorDate: Mon Jan 22 12:12:25 2018 -0500

    Fixed formatting to comply with rules
---
 .../camel/component/dns/policy/DnsActivation.java  | 229 +++++++------
 .../component/dns/policy/DnsActivationPolicy.java  | 374 +++++++++++----------
 .../component/dns/policy/DnsActivationTest.java    |  19 +-
 3 files changed, 332 insertions(+), 290 deletions(-)

diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
index 4eb8ad5..b7fa1d5 100644
--- a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
+++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
@@ -1,18 +1,37 @@
-package org.apache.camel.component.dns.policy;
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
-import javax.naming.directory.Attributes;
-import javax.naming.directory.Attribute;
-import javax.naming.directory.InitialDirContext;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
+package org.apache.camel.component.dns.policy;
 
-import java.net.NetworkInterface;
 import java.net.InetAddress;
+import java.net.NetworkInterface;
 import java.net.UnknownHostException;
 
-import java.util.List;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.InitialDirContext;
+
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,102 +41,102 @@ import org.slf4j.LoggerFactory;
  */
 
 public class DnsActivation {
-    private static final transient Logger logger = LoggerFactory.getLogger(DnsActivation.class);
-
-	private final static String[] DNS_TYPES = {"CNAME", "A"}; 
-
-	private String hostname;
-	private List<String> resolvesTo = new ArrayList<String>();
-
-	public DnsActivation() throws Exception {
-	}
-
-	public DnsActivation(String hostname, List<String> resolvesTo) throws Exception {
-		this.hostname = hostname;
-		this.resolvesTo.addAll(resolvesTo);
-	}
-
-	public void setHostname(String hostname) {
-		this.hostname = hostname;
-	}
-
-	public void setResolvesTo(List<String> resolvesTo) {
-		this.resolvesTo.addAll(resolvesTo);
-	}
-
-	public void setResolvesTo(String resolvesTo) {
-		this.resolvesTo.add(resolvesTo);
-	}
-
-	public boolean isActive() {
-		if(resolvesTo.isEmpty()) {
-			try {
-				resolvesTo.addAll(getLocalIps());
-			} catch(Exception e) {
-				logger.warn("Failed to get local ips and resolvesTo not specified. Identifying as inactive.", e);
-				return false;
-			}
-		}
-
-		logger.debug("Resolving "+hostname);
-		ArrayList<String> hostnames = new ArrayList<String>();
-		hostnames.add(hostname);
-
-		ArrayList<String> resolved = new ArrayList<String>();
-		while(!hostnames.isEmpty()) {
-			NamingEnumeration attributeEnumeration = null;
-			try {
-				String hostname = hostnames.remove(0);
-				InetAddress inetAddress = InetAddress.getByName(hostname);
-				InitialDirContext initialDirContext = new InitialDirContext();
-				Attributes attributes = initialDirContext.getAttributes("dns:/" + inetAddress.getHostName(), DNS_TYPES);
-				attributeEnumeration = attributes.getAll();
-				while(attributeEnumeration.hasMore()) {
-					Attribute attribute = (Attribute)attributeEnumeration.next();
-					String id = attribute.getID();
-					String value = (String)attribute.get();
-					if(resolvesTo.contains(value)) {
-						logger.debug(id+" = " + value + " matched. Identifying as active.");
-						return true;
-					}
-					logger.debug(id+" = " + value);
-					if(id.equals("CNAME") && !resolved.contains(value)) {
-						hostnames.add(value);
-					}
-					resolved.add(value);
-				}
-			} catch(Exception e) {
-				logger.warn(hostname, e);
-			} finally {
-				if(attributeEnumeration != null) {
-					try {
-						attributeEnumeration.close();
-					} catch(Exception e) {
-						logger.warn("Failed to close attributeEnumeration. Memory leak possible.", e);
-					}
-					attributeEnumeration = null;
-				}
-			}
-		}
-		return false;
-	}
-
-	private List<String> getLocalIps() throws Exception {
-		List<String> localIps = new ArrayList<String>();
-
-		Enumeration<NetworkInterface> networkInterfacesEnumeration = NetworkInterface.getNetworkInterfaces();
-		while(networkInterfacesEnumeration.hasMoreElements()) {
-			NetworkInterface networkInterface = networkInterfacesEnumeration.nextElement();
-
-			Enumeration<InetAddress> inetAddressesEnumeration = networkInterface.getInetAddresses();
-			while (inetAddressesEnumeration.hasMoreElements()) {
-				InetAddress inetAddress = inetAddressesEnumeration.nextElement();
-				String ip = inetAddress.getHostAddress();
-				logger.debug("Local ip: "+ip);
-				localIps.add(ip);
-			}
-		}
-		return localIps;
-	}
+    private static final transient String[] DNS_TYPES = {"CNAME", "A"}; 
+    private static final transient Logger LOG = LoggerFactory.getLogger(DnsActivation.class);
+
+
+    private String hostname;
+    private List<String> resolvesTo = new ArrayList<String>();
+
+    public DnsActivation() throws Exception {
+    }
+
+    public DnsActivation(String hostname, List<String> resolvesTo) throws Exception {
+        this.hostname = hostname;
+        this.resolvesTo.addAll(resolvesTo);
+    }
+
+    public void setHostname(String hostname) {
+        this.hostname = hostname;
+    }
+
+    public void setResolvesTo(List<String> resolvesTo) {
+        this.resolvesTo.addAll(resolvesTo);
+    }
+
+    public void setResolvesTo(String resolvesTo) {
+        this.resolvesTo.add(resolvesTo);
+    }
+
+    public boolean isActive() {
+        if (resolvesTo.isEmpty()) {
+            try {
+                resolvesTo.addAll(getLocalIps());
+            } catch (Exception e) {
+                LOG.warn("Failed to get local ips and resolvesTo not specified. Identifying as inactive.", e);
+                return false;
+            }
+        }
+
+        LOG.debug("Resolving " + hostname);
+        ArrayList<String> hostnames = new ArrayList<String>();
+        hostnames.add(hostname);
+
+        ArrayList<String> resolved = new ArrayList<String>();
+        while (!hostnames.isEmpty()) {
+            NamingEnumeration attributeEnumeration = null;
+            try {
+                String hostname = hostnames.remove(0);
+                InetAddress inetAddress = InetAddress.getByName(hostname);
+                InitialDirContext initialDirContext = new InitialDirContext();
+                Attributes attributes = initialDirContext.getAttributes("dns:/" + inetAddress.getHostName(), DNS_TYPES);
+                attributeEnumeration = attributes.getAll();
+                while (attributeEnumeration.hasMore()) {
+                    Attribute attribute = (Attribute)attributeEnumeration.next();
+                    String id = attribute.getID();
+                    String value = (String)attribute.get();
+                    if (resolvesTo.contains(value)) {
+                        LOG.debug(id + " = " + value + " matched. Identifying as active.");
+                        return true;
+                    }
+                    LOG.debug(id + " = " + value);
+                    if (id.equals("CNAME") && !resolved.contains(value)) {
+                        hostnames.add(value);
+                    }
+                    resolved.add(value);
+                }
+            } catch (Exception e) {
+                LOG.warn(hostname, e);
+            } finally {
+                if (attributeEnumeration != null) {
+                    try {
+                        attributeEnumeration.close();
+                    } catch (Exception e) {
+                        LOG.warn("Failed to close attributeEnumeration. Memory leak possible.", e);
+                    }
+                    attributeEnumeration = null;
+                }
+            }
+        }
+        return false;
+    }
+
+    private List<String> getLocalIps() throws Exception {
+        List<String> localIps = new ArrayList<String>();
+
+        Enumeration<NetworkInterface> networkInterfacesEnumeration = NetworkInterface.getNetworkInterfaces();
+        while (networkInterfacesEnumeration.hasMoreElements()) {
+            NetworkInterface networkInterface = networkInterfacesEnumeration.nextElement();
+
+            Enumeration<InetAddress> inetAddressesEnumeration = networkInterface.getInetAddresses();
+            while (inetAddressesEnumeration.hasMoreElements()) {
+                InetAddress inetAddress = inetAddressesEnumeration.nextElement();
+                String ip = inetAddress.getHostAddress();
+                LOG.debug("Local ip: " + ip);
+                localIps.add(ip);
+            }
+        }
+        return localIps;
+    }
 }
 
diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
index c401a38..74e6825 100644
--- a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
+++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
@@ -1,11 +1,29 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.apache.camel.component.dns.policy;
 
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.HashMap;
 import java.util.Timer;
 import java.util.TimerTask;
-import java.util.Collections;
+
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.camel.Consumer;
@@ -13,197 +31,185 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Route;
 import org.apache.camel.ServiceStatus;
-import org.apache.camel.util.ServiceHelper;
 
-import org.apache.camel.spi.RoutePolicy;
+import org.apache.camel.impl.LoggingExceptionHandler;
+
 import org.apache.camel.spi.ExceptionHandler;
+import org.apache.camel.spi.RoutePolicy;
 
-import org.apache.camel.impl.LoggingExceptionHandler;
 import org.apache.camel.support.RoutePolicySupport;
 
+import org.apache.camel.util.ServiceHelper;
+
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class DnsActivationPolicy extends RoutePolicySupport {
-	private final static Logger logger = LoggerFactory.getLogger(DnsActivationPolicy.class);
-
-	private ExceptionHandler exceptionHandler;
-	
-	private DnsActivation dnsActivation;
-	private long ttl;
-
-	private Map<String,Route> routes = new ConcurrentHashMap<String, Route>();
-	private Timer timer;
-
-	public DnsActivationPolicy() throws Exception
-	{
-		dnsActivation = new DnsActivation();
-	}
-
-	public void onInit(Route route)
-	{
-		logger.debug("onInit "+route.getId());
-		routes.put(route.getId(), route);
-	}
-
-	public void onRemove(Route route)
-	{
-		logger.debug("onRemove "+route.getId());
-		// noop
-	}
-
-	@Override
-	public void onStart(Route route)
-	{
-		logger.debug("onStart "+route.getId());
-		// noop
-	}
-
-	@Override
-	public void onStop(Route route)
-	{
-		logger.debug("onStop "+route.getId());
-		// noop
-	}
-
-	@Override
-	public void onSuspend(Route route)
-	{
-		logger.debug("onSuspend "+route.getId());
-		// noop
-	}
-
-	@Override
-	public void onResume(Route route)
-	{
-		logger.debug("onResume "+route.getId());
-		// noop
-	}
-
-	public void onExchangeBegin(Route route, Exchange exchange)
-	{
-		logger.debug("onExchange start "+route.getId()+"/"+exchange.getExchangeId());
-		// noop
-	}
-
-	public void onExchangeDone(Route route, Exchange exchange)
-	{
-		logger.debug("onExchange end "+route.getId()+"/"+exchange.getExchangeId());
-		// noop
-	}
-
-	@Override
-	protected void doStart() throws Exception
-	{
-		logger.debug("doStart");
-		timer = new Timer();
-		timer.schedule(new DnsActivationTask(), 0, ttl);
-		// noop
-	}
-
-	@Override
-	protected void doStop() throws Exception
-	{
-		logger.debug("doStop");
-		if(timer != null)
-		{
-			timer.cancel();
-			timer = null;
-		}
-
-		// noop
-	}
-
-	public ExceptionHandler getExceptionHandler() {
-		if (exceptionHandler == null) {
-			exceptionHandler = new LoggingExceptionHandler(getClass());
-		}
-		return exceptionHandler;
-	}
-
-	public void setExceptionHandler(ExceptionHandler exceptionHandler) {
-		this.exceptionHandler = exceptionHandler;
-	}
-
-	public void setHostname(String hostname) {
-		dnsActivation.setHostname(hostname);
-	}
-
-	public void setResolvesTo(List<String> resolvesTo) {
-		dnsActivation.setResolvesTo(resolvesTo);
-	}
-
-	public void setResolvesTo(String resolvesTo) {
-		dnsActivation.setResolvesTo(resolvesTo);
-	}
-
-	public void setTtl(String ttl) {
-		this.ttl = Long.parseLong(ttl);
-	}
-
-	private void startRouteImpl(Route route) throws Exception {
-		ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
-
-		if(routeStatus == ServiceStatus.Stopped) {
-			logger.info("Starting "+route.getId());
-			startRoute(route);
-		} else if(routeStatus == ServiceStatus.Suspended) {
-			logger.info("Resuming "+route.getId());
-			startConsumer(route.getConsumer());
-		} else {
-			logger.debug("Nothing to do "+route.getId()+" is "+routeStatus);
-		}
-	}
-
-	private void startRoutes()
-	{
-			for(String routeId : routes.keySet()) {
-				try {
-					Route route = routes.get(routeId);
-					startRouteImpl(route);
-				} catch(Exception e) {
-					logger.warn(routeId, e);
-				}
-			}
-	}
-
-	private void stopRouteImpl(Route route) throws Exception {
-		ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
-
-		if(routeStatus == ServiceStatus.Started) {
-			logger.info("Stopping "+route.getId());
-			stopRoute(route);
-		} else {
-			logger.debug("Nothing to do "+route.getId()+" is "+routeStatus);
-		}
-	}
-
-	private void stopRoutes()
-	{
-			for(String routeId : routes.keySet()) {
-				try {
-					Route route = routes.get(routeId);
-					stopRouteImpl(route);
-				} catch(Exception e) {
-					logger.warn(routeId, e);
-				}
-			}
-	}
-
- 	class DnsActivationTask extends TimerTask {
-		public void run() {
-			try {
-				if(dnsActivation.isActive()) {
-					startRoutes();
-				} else {
-					stopRoutes();
-				}
-			}
-			catch(Exception e) {
-				logger.warn("DnsActivation TimerTask failed", e);
-			}
-		}
-	}
+    private static final transient Logger LOG = LoggerFactory.getLogger(DnsActivationPolicy.class);
+
+    private ExceptionHandler exceptionHandler;
+    
+    private DnsActivation dnsActivation;
+    private long ttl;
+
+    private Map<String, Route> routes = new ConcurrentHashMap<String, Route>();
+    private Timer timer;
+
+    public DnsActivationPolicy() throws Exception {
+        dnsActivation = new DnsActivation();
+    }
+
+    public void onInit(Route route) {
+        LOG.debug("onInit " + route.getId());
+        routes.put(route.getId(), route);
+    }
+
+    public void onRemove(Route route) {
+        LOG.debug("onRemove " + route.getId());
+        // noop
+    }
+
+    @Override
+    public void onStart(Route route) {
+        LOG.debug("onStart " + route.getId());
+        // noop
+    }
+
+    @Override
+    public void onStop(Route route) {
+        LOG.debug("onStop " + route.getId());
+        // noop
+    }
+
+    @Override
+    public void onSuspend(Route route) {
+        LOG.debug("onSuspend " + route.getId());
+        // noop
+    }
+
+    @Override
+    public void onResume(Route route) {
+        LOG.debug("onResume " + route.getId());
+        // noop
+    }
+
+    public void onExchangeBegin(Route route, Exchange exchange) {
+        LOG.debug("onExchange start " + route.getId() + "/" + exchange.getExchangeId());
+        // noop
+    }
+
+    public void onExchangeDone(Route route, Exchange exchange) {
+        LOG.debug("onExchange end " + route.getId() + "/" + exchange.getExchangeId());
+        // noop
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        LOG.debug("doStart");
+        timer = new Timer();
+        timer.schedule(new DnsActivationTask(), 0, ttl);
+        // noop
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        LOG.debug("doStop");
+        if (timer != null) {
+            timer.cancel();
+            timer = null;
+        }
+
+        // noop
+    }
+
+    public ExceptionHandler getExceptionHandler() {
+        if (exceptionHandler == null) {
+            exceptionHandler = new LoggingExceptionHandler(getClass());
+        }
+        return exceptionHandler;
+    }
+
+    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
+        this.exceptionHandler = exceptionHandler;
+    }
+
+    public void setHostname(String hostname) {
+        dnsActivation.setHostname(hostname);
+    }
+
+    public void setResolvesTo(List<String> resolvesTo) {
+        dnsActivation.setResolvesTo(resolvesTo);
+    }
+
+    public void setResolvesTo(String resolvesTo) {
+        dnsActivation.setResolvesTo(resolvesTo);
+    }
+
+    public void setTtl(String ttl) {
+        this.ttl = Long.parseLong(ttl);
+    }
+
+    private void startRouteImpl(Route route) throws Exception {
+        ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
+
+        if (routeStatus == ServiceStatus.Stopped) {
+            LOG.info("Starting " + route.getId());
+            startRoute(route);
+        } else if (routeStatus == ServiceStatus.Suspended) {
+            LOG.info("Resuming " + route.getId());
+            startConsumer(route.getConsumer());
+        } else {
+            LOG.debug("Nothing to do " + route.getId() + " is " + routeStatus);
+        }
+    }
+
+    private void startRoutes() {
+        for (String routeId : routes.keySet()) {
+            try {
+                Route route = routes.get(routeId);
+                startRouteImpl(route);
+            } catch (Exception e) {
+                LOG.warn(routeId, e);
+            }
+        }
+    }
+
+    private void stopRouteImpl(Route route) throws Exception {
+        ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
+
+        if (routeStatus == ServiceStatus.Started) {
+            LOG.info("Stopping " + route.getId());
+            stopRoute(route);
+        } else {
+            LOG.debug("Nothing to do " + route.getId() + " is " + routeStatus);
+        }
+    }
+
+    private void stopRoutes() {
+        for (String routeId : routes.keySet()) {
+            try {
+                Route route = routes.get(routeId);
+                stopRouteImpl(route);
+            } catch (Exception e) {
+                LOG.warn(routeId, e);
+            }
+        }
+    }
+
+    class DnsActivationTask extends TimerTask {
+        public void run() {
+            try {
+                if (dnsActivation.isActive()) {
+                    startRoutes();
+                } else {
+                    stopRoutes();
+                }
+            } catch (Exception e) {
+                LOG.warn("DnsActivation TimerTask failed", e);
+            }
+        }
+    }
 }
 
 
diff --git a/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java b/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java
index ad74e09..1071a31 100644
--- a/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java
+++ b/components/camel-dns/src/test/java/org/apache/camel/component/dns/policy/DnsActivationTest.java
@@ -1,3 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.apache.camel.component.dns.policy;
 
 import java.util.Arrays;
@@ -5,8 +22,8 @@ import java.util.List;
 
 import org.junit.Test;
 
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 public class DnsActivationTest {
     @Test

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 01/04: Added dns activation policy

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 239a862fc1240ffcd6dee76264447f36ac660bdd
Author: Max Fortun <Ma...@wsj.com>
AuthorDate: Fri Jan 19 12:18:34 2018 -0500

    Added dns activation policy
---
 .../camel/component/dns/policy/DnsActivation.java  | 126 +++++++++++++
 .../component/dns/policy/DnsActivationPolicy.java  | 209 +++++++++++++++++++++
 2 files changed, 335 insertions(+)

diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
new file mode 100644
index 0000000..ddf352e
--- /dev/null
+++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivation.java
@@ -0,0 +1,126 @@
+package org.apache.camel.component.dns.policy;
+
+import javax.naming.directory.Attributes;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+import java.net.NetworkInterface;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Enumeration;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/* 
+ * Check if a hostname resolves to a specified cname or an ip
+ */
+
+public class DnsActivation {
+    private static final transient Logger logger = LoggerFactory.getLogger(DnsActivation.class);
+
+	private final static String[] DNS_TYPES_CNAME = {"CNAME"}; 
+
+	private String hostname;
+	private List<String> resolvesTo = new ArrayList<String>();
+
+	public DnsActivation() throws Exception {
+	}
+
+	public DnsActivation(String hostname, List<String> resolvesTo) throws Exception {
+		this.hostname = hostname;
+		this.resolvesTo.addAll(resolvesTo);
+	}
+
+	public void setHostname(String hostname) {
+		this.hostname = hostname;
+	}
+
+	public void setResolvesTo(List<String> resolvesTo) {
+		this.resolvesTo.addAll(resolvesTo);
+	}
+
+	public void setResolvesTo(String resolvesTo) {
+		this.resolvesTo.add(resolvesTo);
+	}
+
+	public boolean isActive() {
+		if(resolvesTo.isEmpty()) {
+			try {
+				resolvesTo.addAll(getLocalIps());
+			} catch(Exception e) {
+				logger.warn("Failed to get local ips and resolvesTo not specified. Identifying as inactive.", e);
+				return false;
+			}
+		}
+
+		logger.debug("Resolving "+hostname);
+		ArrayList<String> hostnames = new ArrayList<String>();
+		hostnames.add(hostname);
+
+		ArrayList<String> resolved = new ArrayList<String>();
+		while(!hostnames.isEmpty()) {
+			NamingEnumeration attributeEnumeration = null;
+			try {
+				String hostname = hostnames.remove(0);
+				InetAddress inetAddress = InetAddress.getByName(hostname);
+				InitialDirContext initialDirContext = new InitialDirContext();
+				Attributes attributes = initialDirContext.getAttributes("dns:/" + inetAddress.getHostName(), DNS_TYPES_CNAME);
+				attributeEnumeration = attributes.getAll();
+				while(attributeEnumeration.hasMore()) {
+					Attribute attribute = (Attribute)attributeEnumeration.next();
+					String id = attribute.getID();
+					String value = (String)attribute.get();
+					if(resolvesTo.contains(value)) {
+						logger.debug(id+" = " + value + " matched. Identifying as active.");
+						return true;
+					}
+					logger.debug(id+" = " + value);
+					if(id.equals("CNAME") && !resolved.contains(value)) {
+						hostnames.add(value);
+					}
+					resolved.add(value);
+				}
+			} catch(Exception e) {
+				logger.warn(hostname, e);
+			} finally {
+				if(attributeEnumeration != null) {
+					try {
+						attributeEnumeration.close();
+					} catch(Exception e) {
+						logger.warn("Failed to close attributeEnumeration. Memory leak possible.", e);
+					}
+					attributeEnumeration = null;
+				}
+			}
+		}
+		return false;
+	}
+
+	private List<String> getLocalIps() throws Exception {
+		List<String> localIps = new ArrayList<String>();
+
+		Enumeration<NetworkInterface> networkInterfacesEnumeration = NetworkInterface.getNetworkInterfaces();
+		while(networkInterfacesEnumeration.hasMoreElements()) {
+			NetworkInterface networkInterface = networkInterfacesEnumeration.nextElement();
+
+			Enumeration<InetAddress> inetAddressesEnumeration = networkInterface.getInetAddresses();
+			while (inetAddressesEnumeration.hasMoreElements()) {
+				InetAddress inetAddress = inetAddressesEnumeration.nextElement();
+				String ip = inetAddress.getHostAddress();
+				if(ip.startsWith("127.")) {
+					continue;
+				}
+				logger.debug("Local ip: "+ip);
+				localIps.add(ip);
+			}
+		}
+		return localIps;
+	}
+}
+
diff --git a/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
new file mode 100644
index 0000000..c401a38
--- /dev/null
+++ b/components/camel-dns/src/main/java/org/apache/camel/component/dns/policy/DnsActivationPolicy.java
@@ -0,0 +1,209 @@
+package org.apache.camel.component.dns.policy;
+
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.Collections;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Route;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.util.ServiceHelper;
+
+import org.apache.camel.spi.RoutePolicy;
+import org.apache.camel.spi.ExceptionHandler;
+
+import org.apache.camel.impl.LoggingExceptionHandler;
+import org.apache.camel.support.RoutePolicySupport;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DnsActivationPolicy extends RoutePolicySupport {
+	private final static Logger logger = LoggerFactory.getLogger(DnsActivationPolicy.class);
+
+	private ExceptionHandler exceptionHandler;
+	
+	private DnsActivation dnsActivation;
+	private long ttl;
+
+	private Map<String,Route> routes = new ConcurrentHashMap<String, Route>();
+	private Timer timer;
+
+	public DnsActivationPolicy() throws Exception
+	{
+		dnsActivation = new DnsActivation();
+	}
+
+	public void onInit(Route route)
+	{
+		logger.debug("onInit "+route.getId());
+		routes.put(route.getId(), route);
+	}
+
+	public void onRemove(Route route)
+	{
+		logger.debug("onRemove "+route.getId());
+		// noop
+	}
+
+	@Override
+	public void onStart(Route route)
+	{
+		logger.debug("onStart "+route.getId());
+		// noop
+	}
+
+	@Override
+	public void onStop(Route route)
+	{
+		logger.debug("onStop "+route.getId());
+		// noop
+	}
+
+	@Override
+	public void onSuspend(Route route)
+	{
+		logger.debug("onSuspend "+route.getId());
+		// noop
+	}
+
+	@Override
+	public void onResume(Route route)
+	{
+		logger.debug("onResume "+route.getId());
+		// noop
+	}
+
+	public void onExchangeBegin(Route route, Exchange exchange)
+	{
+		logger.debug("onExchange start "+route.getId()+"/"+exchange.getExchangeId());
+		// noop
+	}
+
+	public void onExchangeDone(Route route, Exchange exchange)
+	{
+		logger.debug("onExchange end "+route.getId()+"/"+exchange.getExchangeId());
+		// noop
+	}
+
+	@Override
+	protected void doStart() throws Exception
+	{
+		logger.debug("doStart");
+		timer = new Timer();
+		timer.schedule(new DnsActivationTask(), 0, ttl);
+		// noop
+	}
+
+	@Override
+	protected void doStop() throws Exception
+	{
+		logger.debug("doStop");
+		if(timer != null)
+		{
+			timer.cancel();
+			timer = null;
+		}
+
+		// noop
+	}
+
+	public ExceptionHandler getExceptionHandler() {
+		if (exceptionHandler == null) {
+			exceptionHandler = new LoggingExceptionHandler(getClass());
+		}
+		return exceptionHandler;
+	}
+
+	public void setExceptionHandler(ExceptionHandler exceptionHandler) {
+		this.exceptionHandler = exceptionHandler;
+	}
+
+	public void setHostname(String hostname) {
+		dnsActivation.setHostname(hostname);
+	}
+
+	public void setResolvesTo(List<String> resolvesTo) {
+		dnsActivation.setResolvesTo(resolvesTo);
+	}
+
+	public void setResolvesTo(String resolvesTo) {
+		dnsActivation.setResolvesTo(resolvesTo);
+	}
+
+	public void setTtl(String ttl) {
+		this.ttl = Long.parseLong(ttl);
+	}
+
+	private void startRouteImpl(Route route) throws Exception {
+		ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
+
+		if(routeStatus == ServiceStatus.Stopped) {
+			logger.info("Starting "+route.getId());
+			startRoute(route);
+		} else if(routeStatus == ServiceStatus.Suspended) {
+			logger.info("Resuming "+route.getId());
+			startConsumer(route.getConsumer());
+		} else {
+			logger.debug("Nothing to do "+route.getId()+" is "+routeStatus);
+		}
+	}
+
+	private void startRoutes()
+	{
+			for(String routeId : routes.keySet()) {
+				try {
+					Route route = routes.get(routeId);
+					startRouteImpl(route);
+				} catch(Exception e) {
+					logger.warn(routeId, e);
+				}
+			}
+	}
+
+	private void stopRouteImpl(Route route) throws Exception {
+		ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
+
+		if(routeStatus == ServiceStatus.Started) {
+			logger.info("Stopping "+route.getId());
+			stopRoute(route);
+		} else {
+			logger.debug("Nothing to do "+route.getId()+" is "+routeStatus);
+		}
+	}
+
+	private void stopRoutes()
+	{
+			for(String routeId : routes.keySet()) {
+				try {
+					Route route = routes.get(routeId);
+					stopRouteImpl(route);
+				} catch(Exception e) {
+					logger.warn(routeId, e);
+				}
+			}
+	}
+
+ 	class DnsActivationTask extends TimerTask {
+		public void run() {
+			try {
+				if(dnsActivation.isActive()) {
+					startRoutes();
+				} else {
+					stopRoutes();
+				}
+			}
+			catch(Exception e) {
+				logger.warn("DnsActivation TimerTask failed", e);
+			}
+		}
+	}
+}
+
+

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.