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 2017/07/30 09:03:55 UTC
[2/5] camel git commit: [CAMEL-11597] Remove hard dependency on XPack
client
[CAMEL-11597] Remove hard dependency on XPack client
If the XPack client exist on the classpath it use the
PreBuiltXPackTransportClient for connection to the instance, otherwise
the PreBuiltTransportClient
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f3b111bf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f3b111bf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f3b111bf
Branch: refs/heads/master
Commit: f3b111bf42f6660d9967ad01519033c278340227
Parents: 811d6f7
Author: fharms <fl...@gmail.com>
Authored: Sat Jul 29 15:18:18 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Jul 30 10:47:26 2017 +0200
----------------------------------------------------------------------
components/camel-elasticsearch5/pom.xml | 19 --------
.../ElasticsearchConfiguration.java | 12 ++---
.../elasticsearch5/ElasticsearchEndpoint.java | 5 +-
.../elasticsearch5/ElasticsearchProducer.java | 49 +++++++++++++-------
.../elasticsearch5/ElasticsearchBaseTest.java | 4 +-
5 files changed, 42 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/f3b111bf/components/camel-elasticsearch5/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch5/pom.xml b/components/camel-elasticsearch5/pom.xml
index e7014cd..8342eaf 100644
--- a/components/camel-elasticsearch5/pom.xml
+++ b/components/camel-elasticsearch5/pom.xml
@@ -38,20 +38,6 @@
<camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=elasticsearch5</camel.osgi.export.service>
</properties>
- <repositories>
- <!-- add the elasticsearch repo -->
- <repository>
- <id>elasticsearch-releases</id>
- <url>https://artifacts.elastic.co/maven</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
-
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
@@ -63,11 +49,6 @@
<version>${elasticsearch5-version}</version>
</dependency>
<dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>x-pack-transport</artifactId>
- <version>${elasticsearch5-version}</version>
- </dependency>
- <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson2-version}</version>
http://git-wip-us.apache.org/repos/asf/camel/blob/f3b111bf/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchConfiguration.java b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchConfiguration.java
index a0c423f..25eddca 100644
--- a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchConfiguration.java
+++ b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchConfiguration.java
@@ -47,11 +47,11 @@ public class ElasticsearchConfiguration {
private int port = ElasticsearchConstants.DEFAULT_PORT;
@UriParam(defaultValue = "false")
private Boolean clientTransportSniff = false;
- @UriParam(defaultValue = "" +ElasticsearchConstants.DEFAULT_PING_SCHEDULE)
+ @UriParam(defaultValue = "" + ElasticsearchConstants.DEFAULT_PING_SCHEDULE)
private String pingSchedule = ElasticsearchConstants.DEFAULT_PING_SCHEDULE;
- @UriParam(defaultValue = "" +ElasticsearchConstants.DEFAULT_PING_TIMEOUT)
+ @UriParam(defaultValue = "" + ElasticsearchConstants.DEFAULT_PING_TIMEOUT)
private String pingTimeout = ElasticsearchConstants.DEFAULT_PING_TIMEOUT;
- @UriParam(defaultValue = "" +ElasticsearchConstants.DEFAULT_TCP_CONNECT_TIMEOUT)
+ @UriParam(defaultValue = "" + ElasticsearchConstants.DEFAULT_TCP_CONNECT_TIMEOUT)
private String tcpConnectTimeout = ElasticsearchConstants.DEFAULT_TCP_CONNECT_TIMEOUT;
@UriParam(defaultValue = "false")
private Boolean tcpCompress = false;
@@ -206,7 +206,7 @@ public class ElasticsearchConfiguration {
/**
* User for authenticate against the cluster. Requires "transport_client" role
- * for accessing the cluster
+ * for accessing the cluster. Require XPack client jar on the classpath
*/
public String getUser() {
return user;
@@ -217,7 +217,7 @@ public class ElasticsearchConfiguration {
}
/**
- * Password for authenticate against the cluster
+ * Password for authenticate against the cluster. Require XPack client jar on the classpath
*/
public String getPassword() {
return password;
@@ -228,7 +228,7 @@ public class ElasticsearchConfiguration {
}
/**
- * Enable SSL
+ * Enable SSL. Require XPack client jar on the classpath
*/
public Boolean getEnableSSL() {
return enableSSL;
http://git-wip-us.apache.org/repos/asf/camel/blob/f3b111bf/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchEndpoint.java b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchEndpoint.java
index a75634b..9618097 100644
--- a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchEndpoint.java
+++ b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchEndpoint.java
@@ -22,9 +22,9 @@ import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
+import org.elasticsearch.client.transport.TransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.elasticsearch.client.transport.TransportClient;
/**
* The elasticsearch component is used for interfacing with ElasticSearch server using 5.x API.
@@ -34,10 +34,11 @@ public class ElasticsearchEndpoint extends DefaultEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchEndpoint.class);
- private TransportClient client;
@UriParam
protected final ElasticsearchConfiguration configuration;
+ private TransportClient client;
+
public ElasticsearchEndpoint(String uri, ElasticsearchComponent component, ElasticsearchConfiguration config, TransportClient client) throws Exception {
super(uri, component);
this.configuration = config;
http://git-wip-us.apache.org/repos/asf/camel/blob/f3b111bf/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchProducer.java b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchProducer.java
index 03390de..a395c39 100644
--- a/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchProducer.java
+++ b/components/camel-elasticsearch5/src/main/java/org/apache/camel/component/elasticsearch5/ElasticsearchProducer.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.elasticsearch5;
+import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
@@ -38,7 +39,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.IndexNotFoundException;
-import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,12 +47,12 @@ import org.slf4j.LoggerFactory;
* Represents an Elasticsearch producer.
*/
public class ElasticsearchProducer extends DefaultProducer {
-
+
private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchProducer.class);
-
+
protected final ElasticsearchConfiguration configuration;
private TransportClient client;
-
+
public ElasticsearchProducer(ElasticsearchEndpoint endpoint, ElasticsearchConfiguration configuration) {
super(endpoint);
this.configuration = configuration;
@@ -177,7 +178,7 @@ public class ElasticsearchProducer extends DefaultProducer {
}
} else if (operation == ElasticsearchOperation.SEARCH) {
SearchRequest searchRequest = message.getBody(SearchRequest.class);
- message.setBody(client.search(searchRequest).actionGet());
+ message.setBody(client.search(searchRequest).actionGet());
} else if (operation == ElasticsearchOperation.MULTISEARCH) {
MultiSearchRequest multiSearchRequest = message.getBody(MultiSearchRequest.class);
message.setBody(client.multiSearch(multiSearchRequest));
@@ -209,7 +210,7 @@ public class ElasticsearchProducer extends DefaultProducer {
}
}
-
+
@Override
@SuppressWarnings("unchecked")
protected void doStart() throws Exception {
@@ -217,37 +218,45 @@ public class ElasticsearchProducer extends DefaultProducer {
if (client == null) {
LOG.info("Connecting to the ElasticSearch cluster: " + configuration.getClusterName());
-
if (configuration.getIp() != null) {
- client = new PreBuiltXPackTransportClient(getSettings())
+ client = createClient()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(configuration.getIp()), configuration.getPort()));
} else if (configuration.getTransportAddressesList() != null
- && !configuration.getTransportAddressesList().isEmpty()) {
+ && !configuration.getTransportAddressesList().isEmpty()) {
List<TransportAddress> addresses = new ArrayList<TransportAddress>(configuration.getTransportAddressesList().size());
for (TransportAddress address : configuration.getTransportAddressesList()) {
addresses.add(address);
}
- client = new PreBuiltXPackTransportClient(getSettings()).addTransportAddresses(addresses.toArray(new TransportAddress[addresses.size()]));
+ client = createClient().addTransportAddresses(addresses.toArray(new TransportAddress[addresses.size()]));
} else {
LOG.info("Incorrect ip address and port parameters settings for ElasticSearch cluster");
}
}
}
- private Settings getSettings() {
+ private TransportClient createClient() {
+ final Settings.Builder settings = getSettings();
+ try {
+ Class clazz = Class.forName("org.elasticsearch.xpack.client.PreBuiltXPackTransportClient");
+ Constructor<?> ctor = clazz.getConstructor(Settings.class, Class[].class);
+ settings.put("xpack.security.user", configuration.getUser() + ":" + configuration.getPassword())
+ .put("xpack.security.transport.ssl.enabled", configuration.getEnableSSL());
+ return (TransportClient) ctor.newInstance(new Object[] {settings.build(), new Class[0]});
+ } catch (Exception e) {
+ LOG.info("XPack Client was not found on the classpath, using the standard client");
+ return new PreBuiltTransportClient(settings.build());
+ }
+ }
+
+ private Settings.Builder getSettings() {
final Settings.Builder settings = Settings.builder()
.put("cluster.name", configuration.getClusterName())
.put("client.transport.sniff", configuration.getClientTransportSniff())
.put("transport.ping_schedule", configuration.getPingSchedule())
.put("client.transport.ping_timeout", configuration.getPingTimeout())
.put("client.transport.sniff", configuration.getClientTransportSniff())
- .put("request.headers.X-Found-Cluster", configuration.getClusterName());//according to the documentation this should be the same as cluster name
-
- if (configuration.getUser() != null && configuration.getPassword() != null) {
- settings.put("xpack.security.user", configuration.getUser() +":"+ configuration.getPassword())
- .put("xpack.security.transport.ssl.enabled", configuration.getEnableSSL());
- }
- return settings.build();
+ .put("request.headers.X-Found-Cluster", configuration.getClusterName());
+ return settings;
}
@Override
@@ -259,4 +268,8 @@ public class ElasticsearchProducer extends DefaultProducer {
}
super.doStop();
}
+
+ public TransportClient getClient() {
+ return client;
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/f3b111bf/components/camel-elasticsearch5/src/test/java/org/apache/camel/component/elasticsearch5/ElasticsearchBaseTest.java
----------------------------------------------------------------------
diff --git a/components/camel-elasticsearch5/src/test/java/org/apache/camel/component/elasticsearch5/ElasticsearchBaseTest.java b/components/camel-elasticsearch5/src/test/java/org/apache/camel/component/elasticsearch5/ElasticsearchBaseTest.java
index 4209393..99885ff 100644
--- a/components/camel-elasticsearch5/src/test/java/org/apache/camel/component/elasticsearch5/ElasticsearchBaseTest.java
+++ b/components/camel-elasticsearch5/src/test/java/org/apache/camel/component/elasticsearch5/ElasticsearchBaseTest.java
@@ -33,7 +33,7 @@ import org.elasticsearch.node.InternalSettingsPreparer;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.transport.Netty4Plugin;
-import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -62,7 +62,7 @@ public class ElasticsearchBaseTest extends CamelTestSupport {
.put("path.home", "target/home")
.put("transport.profiles.default.port", ES_TRANSPORT_PORT)
.build(), Arrays.asList(Netty4Plugin.class)).start();
- client = new PreBuiltXPackTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), ES_TRANSPORT_PORT));
+ client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), ES_TRANSPORT_PORT));
}
@AfterClass