[KARAF-4236] Cellar DNS nodes discovery
diff --git a/assembly/src/main/resources/features.xml b/assembly/src/main/resources/features.xml
index 692c1e9..1316c49 100644
--- a/assembly/src/main/resources/features.xml
+++ b/assembly/src/main/resources/features.xml
@@ -116,5 +116,9 @@
<bundle>mvn:org.apache.karaf.cellar.http/org.apache.karaf.cellar.http.balancer/${project.version}</bundle>
</feature>
+ <feature name="cellar-dns" description="Cellar DNS support in clusters" version="${project.version}">
+ <bundle>mvn:org.apache.karaf.cellar/org.apache.karaf.cellar.dns/${project.version}</bundle>
+ </feature>
+
</features>
diff --git a/dns/NOTICE b/dns/NOTICE
new file mode 100644
index 0000000..3f7fae7
--- /dev/null
+++ b/dns/NOTICE
@@ -0,0 +1,39 @@
+Apache Karaf Cellar
+Copyright 2011-2015 The Apache Software Foundation
+
+I. Used Software
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+The OSGi Alliance (http://www.osgi.org/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+Hazelcast (http://www.hazelcast.com/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+OPS4J (http://www.ops4j.org/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+FUSE Source (http://www.fusesource.org/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+JClouds (http://www.jclouds.org/).
+Licensed under the Apache License 2.0.
+
+This product uses software developed at
+SLF4J (http://www.slf4j.org/).
+Licensed under the MIT License.
+
+This product includes software from http://www.json.org.
+Copyright (c) 2002 JSON.org
+
+II. License Summary
+- Apache License 2.0
+- MIT License
diff --git a/dns/pom.xml b/dns/pom.xml
new file mode 100644
index 0000000..024cd4a
--- /dev/null
+++ b/dns/pom.xml
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <!--
+
+ 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.
+ -->
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.karaf</groupId>
+ <artifactId>cellar</artifactId>
+ <version>4.0.1-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <groupId>org.apache.karaf.cellar</groupId>
+ <artifactId>org.apache.karaf.cellar.dns</artifactId>
+ <packaging>bundle</packaging>
+ <name>Apache Karaf :: Cellar :: DNS</name>
+
+ <dependencies>
+
+ <!-- Internal Dependencies -->
+ <dependency>
+ <groupId>org.apache.karaf.cellar</groupId>
+ <artifactId>org.apache.karaf.cellar.core</artifactId>
+ </dependency>
+
+ <!-- OSGi -->
+ <dependency>
+ <groupId>org.apache.karaf</groupId>
+ <artifactId>org.apache.karaf.util</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Logging Dependencies -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- Testing Dependencies -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.karaf.tooling</groupId>
+ <artifactId>karaf-services-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+ <Export-Package>
+ !org.apache.karaf.cellar.dns.internal.osgi,
+ org.apache.karaf.cellar.dns.*
+ </Export-Package>
+ <Import-Package>
+ org.slf4j;version="[1.6,2)";resolution:=optional,
+ *
+ </Import-Package>
+ <Private-Package>
+ org.apache.karaf.cellar.dns.internal.osgi,
+ org.apache.karaf.util.tracker;-split-package:=merge-first
+ </Private-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
\ No newline at end of file
diff --git a/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryService.java b/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryService.java
new file mode 100644
index 0000000..de8f29b
--- /dev/null
+++ b/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryService.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed 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.karaf.cellar.dns;
+
+import org.apache.karaf.cellar.core.discovery.DiscoveryService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import java.util.*;
+
+/**
+ * Discovery service that uses the DNS SRV Record to discover Cellar nodes.
+ */
+public class DnsDiscoveryService implements DiscoveryService {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DnsDiscoveryService.class);
+
+ private final Hashtable<String, String> dnsEnv;
+ private String dnsService;
+
+ public DnsDiscoveryService() {
+ this.dnsEnv = new Hashtable<String, String>();
+ this.dnsEnv.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
+ this.dnsEnv.put("java.naming.provider.url", "dns:");
+
+ this.dnsService = null;
+ }
+
+ @Override
+ public Set<String> discoverMembers() {
+ LOGGER.debug("CELLAR DNS: query services with name [{}]", dnsService);
+ Set<String> members = new HashSet<String>();
+ try {
+ DirContext ctx = new InitialDirContext(this.dnsEnv);
+ Attributes attrs = ctx.getAttributes(this.dnsService, new String[]{"SRV"});
+ NamingEnumeration<?> servers = attrs.get("srv").getAll();
+ while (servers.hasMore()) {
+ String dns = (String)servers.next();
+ String[] split = dns.split(" ");
+
+ members.add(split[3] + ":" + split[2]);
+ }
+ } catch (Exception e) {
+ LOGGER.error("CELLAR DNS: can't get service", e);
+ }
+
+ return members;
+ }
+
+ @Override
+ public void signIn() {
+ }
+
+ @Override
+ public void refresh() {
+ }
+
+ @Override
+ public void signOut() {
+ }
+
+ public void setDnsService(String dnsService) {
+ this.dnsService = dnsService;
+ }
+}
diff --git a/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceFactory.java b/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceFactory.java
new file mode 100644
index 0000000..f3f79b2
--- /dev/null
+++ b/dns/src/main/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceFactory.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed 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.karaf.cellar.dns;
+
+import org.apache.karaf.cellar.core.discovery.DiscoveryService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A factory for DNS SRV discovery services.
+ */
+public class DnsDiscoveryServiceFactory implements ManagedServiceFactory {
+
+ private static String getEnvOrDefault(String var, String def) {
+ final String val = System.getenv(var);
+ return val == null ? def : val;
+ }
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DnsDiscoveryServiceFactory.class);
+
+ private static final String DNS_SERVICE = "dns.service";
+
+ private final Map<String, ServiceRegistration> registrations = new ConcurrentHashMap<String, ServiceRegistration>();
+
+ private final BundleContext bundleContext;
+
+ public DnsDiscoveryServiceFactory(BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ }
+
+ @Override
+ public String getName() {
+ return "CELLAR DNS: discovery service factory";
+ }
+
+ @Override
+ public void updated(String pid, Dictionary properties) throws ConfigurationException {
+
+ ServiceRegistration newServiceRegistration = null;
+ try {
+ if (properties != null) {
+
+ LOGGER.info("CELLAR DNS: creating the discovery service ...");
+
+ Properties serviceProperties = new Properties();
+ Enumeration propKeys = properties.keys();
+ while (propKeys.hasMoreElements()) {
+ Object key = propKeys.nextElement();
+ Object value = properties.get(key);
+ serviceProperties.put(key, value);
+ }
+
+ DnsDiscoveryService dnsDiscoveryService = new DnsDiscoveryService();
+ String dnsService = (String) properties.get(DNS_SERVICE);
+ if (dnsService == null) {
+ dnsService = getEnvOrDefault("DNS_RO_SERVICE_NAME", "_karaf._tcp.");
+ }
+
+ dnsDiscoveryService.setDnsService(dnsService);
+
+ newServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(), dnsDiscoveryService, (Dictionary) serviceProperties);
+ }
+ } finally {
+ ServiceRegistration oldServiceRegistration = (newServiceRegistration == null) ? registrations.remove(pid) : registrations.put(pid, newServiceRegistration);
+ if (oldServiceRegistration != null) {
+ oldServiceRegistration.unregister();
+ }
+ }
+ }
+
+ @Override
+ public void deleted(String pid) {
+ LOGGER.debug("CELLAR DNS: delete discovery service {}", pid);
+ ServiceRegistration oldServiceRegistration = registrations.remove(pid);
+ if (oldServiceRegistration != null) {
+ oldServiceRegistration.unregister();
+ }
+ }
+
+}
diff --git a/dns/src/main/java/org/apache/karaf/cellar/dns/internal/osgi/Activator.java b/dns/src/main/java/org/apache/karaf/cellar/dns/internal/osgi/Activator.java
new file mode 100644
index 0000000..3c57293
--- /dev/null
+++ b/dns/src/main/java/org/apache/karaf/cellar/dns/internal/osgi/Activator.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed 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.karaf.cellar.dns.internal.osgi;
+
+import org.apache.karaf.cellar.dns.DnsDiscoveryServiceFactory;
+import org.apache.karaf.util.tracker.BaseActivator;
+import org.apache.karaf.util.tracker.annotation.ProvideService;
+import org.apache.karaf.util.tracker.annotation.Services;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ManagedServiceFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Hashtable;
+
+@Services(
+ provides = {
+ @ProvideService(ManagedServiceFactory.class)
+ }
+)
+public class Activator extends BaseActivator {
+
+ private final static Logger LOGGER = LoggerFactory.getLogger(Activator.class);
+
+ @Override
+ public void doStart() throws Exception {
+ LOGGER.debug("CELLAR DNS: init discovery service factory");
+ Hashtable props = new Hashtable();
+ props.put(Constants.SERVICE_PID, "org.apache.karaf.cellar.dns");
+ DnsDiscoveryServiceFactory factory = new DnsDiscoveryServiceFactory(bundleContext);
+ register(ManagedServiceFactory.class, factory, props);
+ }
+
+ @Override
+ public void doStop() {
+ super.doStop();
+ }
+}
diff --git a/dns/src/test/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceTest.java b/dns/src/test/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceTest.java
new file mode 100644
index 0000000..9b32c03
--- /dev/null
+++ b/dns/src/test/java/org/apache/karaf/cellar/dns/DnsDiscoveryServiceTest.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed 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.karaf.cellar.dns;
+
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.Assert.assertFalse;
+
+public class DnsDiscoveryServiceTest {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DnsDiscoveryServiceTest.class);
+
+ @Ignore
+ @Test
+ public void testDiscovery() {
+ DnsDiscoveryService discovery = new DnsDiscoveryService();
+ discovery.setDnsService("_xmpp-server._tcp.gmail.com");
+
+ assertFalse(discovery.discoverMembers().isEmpty());
+ }
+}
diff --git a/pom.xml b/pom.xml
index 5b3bd1b..f8f159c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -66,6 +66,7 @@
<module>http</module>
<module>cloud</module>
<module>kubernetes</module>
+ <module>dns</module>
<module>webconsole</module>
<module>assembly</module>
<module>samples</module>