Skip to content

Commit 9febd4a

Browse files
committed
added compability with elasticsearch TransportClient from v2 to v7
- call a Builder.put() method compatible with elasticsearch v2 and v5 - create TransportAddress through introspection Also added profiles to help working with elasticsearch jars from v2 to v7
1 parent 844fe70 commit 9febd4a

6 files changed

Lines changed: 195 additions & 26 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ install:
1717
- if [[ $TRAVIS_PULL_REQUEST = false ]] && [[ $TRAVIS_BRANCH = master ]] || [[ $TRAVIS_TAG = v* ]]; then GOAL=deploy; else GOAL=install; fi
1818
- if [[ $TRAVIS_TAG = v* ]]; then ADDITIONAL_PROFILES=release; mvn -q -U org.seedstack:seedstack-maven-plugin:release; else ADDITIONAL_PROFILES=snapshots; fi
1919

20-
script: mvn -q -U -T 2 -Pbuild-number,compatibility,bintray,javadoc,$ADDITIONAL_PROFILES $GOAL jacoco:report
20+
script: mvn -q -U -T 2 -Pbuild-number,compatibility,bintray,javadoc,v2,$ADDITIONAL_PROFILES $GOAL jacoco:report
2121

2222
after_success: mvn -q coveralls:report -DrepoToken=$COVERALLS_TOKEN

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Version 3.1.0 (2020-10-01)
1+
# Version 3.1.0 (2020-10-29)
22

33
* [chg] Update for SeedStack 20.7
4+
* [chg] Compatibility with transport client from elasticsearch v2 to v7
45

56
# Version 3.0.0 (2017-01-13)
67

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
ElasticSearch official persistence integration for SeedStack.
66

7+
This addon provides a transport client compatible with elasticsearch from v2 to v7.
8+
9+
It has been tested against following elasticsearch servers:
10+
11+
- 2.4.6
12+
- 5.6.2
13+
- 6.8.10
14+
- 7.9.1
15+
16+
Profiles have added in order to help build the addon with various elasticsearch jar versions.
17+
By default, Profile v2 is used in travis build configuration.
18+
719
# Copyright and license
820

921
This source code is copyrighted by [The SeedStack Authors](https://github.com/seedstack/seedstack/blob/master/AUTHORS) and

pom.xml

Lines changed: 148 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@
7575
<version>${seed.version}</version>
7676
</dependency>
7777
<dependency>
78-
<groupId>org.elasticsearch</groupId>
79-
<artifactId>elasticsearch</artifactId>
80-
<version>2.4.3</version>
81-
<scope>provided</scope>
82-
</dependency>
83-
84-
<dependency>
85-
<groupId>org.hibernate</groupId>
78+
<groupId>org.hibernate.validator</groupId>
8679
<artifactId>hibernate-validator</artifactId>
8780
<version>${hibernate-validator.version}</version>
8881
</dependency>
82+
<dependency>
83+
<groupId>org.yaml</groupId>
84+
<artifactId>snakeyaml</artifactId>
85+
<version>1.26</version>
86+
</dependency>
8987

9088
<dependency>
9189
<groupId>org.skyscreamer</groupId>
@@ -164,5 +162,147 @@
164162
</repository>
165163
</repositories>
166164
</profile>
165+
<profile>
166+
<id>v2</id>
167+
<activation>
168+
<activeByDefault>false</activeByDefault>
169+
</activation>
170+
<dependencies>
171+
<dependency>
172+
<groupId>org.elasticsearch</groupId>
173+
<artifactId>elasticsearch</artifactId>
174+
<version>2.4.3</version>
175+
<scope>provided</scope>
176+
</dependency>
177+
</dependencies>
178+
</profile>
179+
<profile>
180+
<id>v5</id>
181+
<activation>
182+
<activeByDefault>false</activeByDefault>
183+
</activation>
184+
<dependencies>
185+
<dependency>
186+
<groupId>org.elasticsearch</groupId>
187+
<artifactId>elasticsearch</artifactId>
188+
<version>5.6.16</version>
189+
<scope>provided</scope>
190+
</dependency>
191+
<dependency>
192+
<groupId>org.elasticsearch.client</groupId>
193+
<artifactId>transport</artifactId>
194+
<version>5.6.16</version>
195+
<scope>provided</scope>
196+
</dependency>
197+
<dependency>
198+
<groupId>org.apache.logging.log4j</groupId>
199+
<artifactId>log4j-api</artifactId>
200+
<version>2.11.1</version>
201+
<scope>provided</scope>
202+
</dependency>
203+
<dependency>
204+
<groupId>org.apache.logging.log4j</groupId>
205+
<artifactId>log4j-core</artifactId>
206+
<version>2.11.1</version>
207+
<scope>provided</scope>
208+
</dependency>
209+
</dependencies>
210+
</profile>
211+
<profile>
212+
<id>v60</id>
213+
<activation>
214+
<activeByDefault>false</activeByDefault>
215+
</activation>
216+
<dependencies>
217+
<dependency>
218+
<groupId>org.elasticsearch</groupId>
219+
<artifactId>elasticsearch</artifactId>
220+
<version>6.0.1</version>
221+
<scope>provided</scope>
222+
</dependency>
223+
<dependency>
224+
<groupId>org.elasticsearch.client</groupId>
225+
<artifactId>transport</artifactId>
226+
<version>6.0.1</version>
227+
<scope>provided</scope>
228+
</dependency>
229+
<dependency>
230+
<groupId>org.apache.logging.log4j</groupId>
231+
<artifactId>log4j-api</artifactId>
232+
<version>2.11.1</version>
233+
<scope>provided</scope>
234+
</dependency>
235+
<dependency>
236+
<groupId>org.apache.logging.log4j</groupId>
237+
<artifactId>log4j-core</artifactId>
238+
<version>2.11.1</version>
239+
<scope>provided</scope>
240+
</dependency>
241+
</dependencies>
242+
</profile>
243+
<profile>
244+
<id>v68</id>
245+
<activation>
246+
<activeByDefault>false</activeByDefault>
247+
</activation>
248+
<dependencies>
249+
<dependency>
250+
<groupId>org.elasticsearch</groupId>
251+
<artifactId>elasticsearch</artifactId>
252+
<version>6.8.10</version>
253+
<scope>provided</scope>
254+
</dependency>
255+
<dependency>
256+
<groupId>org.elasticsearch.client</groupId>
257+
<artifactId>transport</artifactId>
258+
<version>6.8.10</version>
259+
<scope>provided</scope>
260+
</dependency>
261+
<dependency>
262+
<groupId>org.apache.logging.log4j</groupId>
263+
<artifactId>log4j-api</artifactId>
264+
<version>2.11.1</version>
265+
<scope>provided</scope>
266+
</dependency>
267+
<dependency>
268+
<groupId>org.apache.logging.log4j</groupId>
269+
<artifactId>log4j-core</artifactId>
270+
<version>2.11.1</version>
271+
<scope>provided</scope>
272+
</dependency>
273+
</dependencies>
274+
</profile>
275+
<profile>
276+
<id>v79</id>
277+
<activation>
278+
<activeByDefault>false</activeByDefault>
279+
</activation>
280+
<dependencies>
281+
<dependency>
282+
<groupId>org.elasticsearch</groupId>
283+
<artifactId>elasticsearch</artifactId>
284+
<version>7.9.0</version>
285+
<scope>provided</scope>
286+
</dependency>
287+
<dependency>
288+
<groupId>org.elasticsearch.client</groupId>
289+
<artifactId>transport</artifactId>
290+
<version>7.9.0</version>
291+
<scope>provided</scope>
292+
</dependency>
293+
<dependency>
294+
<groupId>org.apache.logging.log4j</groupId>
295+
<artifactId>log4j-api</artifactId>
296+
<version>2.11.1</version>
297+
<scope>provided</scope>
298+
</dependency>
299+
<dependency>
300+
<groupId>org.apache.logging.log4j</groupId>
301+
<artifactId>log4j-core</artifactId>
302+
<version>2.11.1</version>
303+
<scope>provided</scope>
304+
</dependency>
305+
</dependencies>
306+
</profile>
167307
</profiles>
168308
</project>

src/main/java/org/seedstack/elasticsearch/internal/ElasticSearchErrorCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ enum ElasticSearchErrorCode implements ErrorCode {
1616
FORBIDDEN_CLIENT_CLOSE,
1717
INVALID_HOST,
1818
INVALID_PORT,
19-
CANNOT_CREATE_CLIENT, UNKNOWN_HOST
19+
CANNOT_CREATE_CLIENT, UNKNOWN_HOST,
20+
NO_TRANSPORT_CLIENT_FOUND,
21+
CANNOT_CREATE_ADDRESS;
2022
}

src/main/java/org/seedstack/elasticsearch/internal/ElasticSearchPlugin.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.client.Client;
1313
import org.elasticsearch.client.transport.TransportClient;
1414
import org.elasticsearch.common.settings.Settings;
15-
import org.elasticsearch.common.transport.InetSocketTransportAddress;
1615
import org.elasticsearch.plugins.Plugin;
1716
import org.seedstack.elasticsearch.ElasticSearchConfig;
1817
import org.seedstack.seed.SeedException;
@@ -24,7 +23,6 @@
2423
import java.lang.reflect.Constructor;
2524
import java.lang.reflect.Method;
2625
import java.net.InetAddress;
27-
import java.net.UnknownHostException;
2826
import java.util.Collection;
2927
import java.util.HashMap;
3028
import java.util.List;
@@ -83,11 +81,19 @@ public void stop() {
8381

8482
private Client buildRemoteClient(String clientName, ElasticSearchConfig.ClientConfig clientConfig) {
8583
Settings.Builder settingsBuilder = Settings.builder();
86-
settingsBuilder.put(clientConfig.getProperties());
84+
for ( Entry<Object, Object> entry: clientConfig.getProperties().entrySet() ) {
85+
settingsBuilder.put((String) entry.getKey(), (String) entry.getValue());
86+
}
8787

88-
TransportClient transportClient = Classes.optional("org.elasticsearch.transport.client.PreBuiltTransportClient")
89-
.map(preBuiltTransportClientClass -> createV5TransportClient(clientName, clientConfig, settingsBuilder.build()))
90-
.orElseGet(() -> createV2TransportClient(clientName, clientConfig, settingsBuilder.build()));
88+
TransportClient transportClient = null;
89+
if ( Classes.optional("org.elasticsearch.client.transport.TransportClient$Builder").isPresent() ) {
90+
transportClient = createV2TransportClient(clientName, clientConfig, settingsBuilder.build());
91+
} else if (Classes.optional("org.elasticsearch.transport.client.PreBuiltTransportClient").isPresent() ) {
92+
transportClient = createPreBuiltTransportClient(clientName, clientConfig, settingsBuilder.build());
93+
} else {
94+
throw SeedException.createNew(ElasticSearchErrorCode.NO_TRANSPORT_CLIENT_FOUND)
95+
.put("clientName", clientName);
96+
}
9197

9298
for (String host : clientConfig.getHosts()) {
9399
String[] hostInfo = host.split(":");
@@ -100,20 +106,15 @@ private Client buildRemoteClient(String clientName, ElasticSearchConfig.ClientCo
100106
int port = DEFAULT_ELASTIC_SEARCH_PORT;
101107
try {
102108
if (hostInfo.length > 1) {
103-
port = Integer.valueOf(hostInfo[1]);
109+
port = Integer.parseInt(hostInfo[1]);
104110
}
105111
} catch (NumberFormatException e) {
106112
throw SeedException.wrap(e, ElasticSearchErrorCode.INVALID_PORT)
107113
.put("clientName", clientName)
108114
.put("host", host)
109115
.put("port", hostInfo[1]);
110116
}
111-
try {
112-
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), port));
113-
} catch (UnknownHostException e) {
114-
throw SeedException.wrap(e, ElasticSearchErrorCode.UNKNOWN_HOST)
115-
.put("host", address);
116-
}
117+
addTransportAddress(transportClient, address, port);
117118
}
118119

119120
return transportClient;
@@ -133,10 +134,9 @@ private TransportClient createV2TransportClient(String clientName, ElasticSearch
133134
throw SeedException.wrap(e, ElasticSearchErrorCode.CANNOT_CREATE_CLIENT)
134135
.put("clientName", clientName);
135136
}
136-
137137
}
138138

139-
private TransportClient createV5TransportClient(String clientName, ElasticSearchConfig.ClientConfig clientConfig, Settings settings) {
139+
private TransportClient createPreBuiltTransportClient(String clientName, ElasticSearchConfig.ClientConfig clientConfig, Settings settings) {
140140
try {
141141
Class<?> preBuiltTransportClientClass = Class.forName("org.elasticsearch.transport.client.PreBuiltTransportClient");
142142
Constructor<?> constructor = preBuiltTransportClientClass.getConstructor(Settings.class, Collection.class);
@@ -146,4 +146,18 @@ private TransportClient createV5TransportClient(String clientName, ElasticSearch
146146
.put("clientName", clientName);
147147
}
148148
}
149+
150+
private void addTransportAddress(TransportClient transportClient, String address, int port) throws SeedException {
151+
try {
152+
Class<?> transportAddressClass = Classes.optional("org.elasticsearch.common.transport.InetSocketTransportAddress")
153+
.orElse((Class<Object>) Class.forName("org.elasticsearch.common.transport.TransportAddress"));
154+
155+
Constructor<?> transportAddressClassConstructor = transportAddressClass.getConstructor(InetAddress.class, Integer.TYPE);
156+
Method addTransportAddressMethod = transportClient.getClass().getMethod("addTransportAddress", Class.forName("org.elasticsearch.common.transport.TransportAddress"));
157+
addTransportAddressMethod.invoke(transportClient, transportAddressClassConstructor.newInstance(InetAddress.getByName(address), port));
158+
} catch (Exception e) {
159+
throw SeedException.wrap(e, ElasticSearchErrorCode.CANNOT_CREATE_ADDRESS)
160+
.put("AddressType", "InetSocketTransportAddress");
161+
}
162+
}
149163
}

0 commit comments

Comments
 (0)