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:16 UTC

[camel] 01/04: Added dns activation policy

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.