Skip to content

Commit b3a7a44

Browse files
authored
Adapt xml-namespace setup to the new Elasticsearch client.
Original Pull Request #2295 Closes #21294
1 parent ccf136c commit b3a7a44

File tree

9 files changed

+231
-21
lines changed

9 files changed

+231
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.elc;
17+
18+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
19+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
20+
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
21+
import org.springframework.beans.factory.xml.ParserContext;
22+
import org.w3c.dom.Element;
23+
24+
/**
25+
* @author Peter-Josef Meisch
26+
* @since 5.0
27+
*/
28+
public class ElasticsearchClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
29+
30+
@Override
31+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
32+
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ElasticsearchClientFactoryBean.class);
33+
setConfigurations(element, builder);
34+
return getSourcedBeanDefinition(builder, element, parserContext);
35+
}
36+
37+
private void setConfigurations(Element element, BeanDefinitionBuilder builder) {
38+
builder.addPropertyValue("hosts", element.getAttribute("hosts"));
39+
}
40+
41+
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
42+
ParserContext context) {
43+
AbstractBeanDefinition definition = builder.getBeanDefinition();
44+
definition.setSource(context.extractSource(source));
45+
return definition;
46+
}
47+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.elc;
17+
18+
import co.elastic.clients.elasticsearch.ElasticsearchClient;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
import org.springframework.beans.factory.DisposableBean;
23+
import org.springframework.beans.factory.FactoryBean;
24+
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
25+
import org.springframework.beans.factory.InitializingBean;
26+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
27+
import org.springframework.lang.Nullable;
28+
import org.springframework.util.Assert;
29+
30+
/**
31+
* ElasticsearchClientFactoryBean
32+
*
33+
* @author Peter-Josef Meisch
34+
* @since 5.0
35+
*/
36+
public class ElasticsearchClientFactoryBean
37+
implements FactoryBean<ElasticsearchClient>, InitializingBean, DisposableBean {
38+
39+
private static final Log LOGGER = LogFactory.getLog(ElasticsearchClientFactoryBean.class);
40+
41+
private @Nullable AutoCloseableElasticsearchClient client;
42+
private String hosts = "http://localhost:9200";
43+
static final String COMMA = ",";
44+
45+
@Override
46+
public void destroy() {
47+
try {
48+
LOGGER.info("Closing elasticSearch client");
49+
if (client != null) {
50+
client.close();
51+
}
52+
} catch (final Exception e) {
53+
LOGGER.error("Error closing ElasticSearch client: ", e);
54+
}
55+
}
56+
57+
@Override
58+
public void afterPropertiesSet() throws Exception {
59+
buildClient();
60+
}
61+
62+
@Override
63+
public ElasticsearchClient getObject() {
64+
65+
if (client == null) {
66+
throw new FactoryBeanNotInitializedException();
67+
}
68+
69+
return client;
70+
}
71+
72+
@Override
73+
public Class<?> getObjectType() {
74+
return ElasticsearchClient.class;
75+
}
76+
77+
@Override
78+
public boolean isSingleton() {
79+
return false;
80+
}
81+
82+
protected void buildClient() throws Exception {
83+
84+
Assert.hasText(hosts, "[Assertion Failed] At least one host must be set.");
85+
86+
var clientConfiguration = ClientConfiguration.builder().connectedTo(hosts).build();
87+
client = (AutoCloseableElasticsearchClient) ElasticsearchClients.createImperative(clientConfiguration);
88+
}
89+
90+
public void setHosts(String hosts) {
91+
this.hosts = hosts;
92+
}
93+
94+
public String getHosts() {
95+
return this.hosts;
96+
}
97+
}

src/main/java/org/springframework/data/elasticsearch/config/RestClientBeanDefinitionParser.java renamed to src/main/java/org/springframework/data/elasticsearch/client/erhlc/RestHighLevelClientBeanDefinitionParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch.config;
16+
package org.springframework.data.elasticsearch.client.erhlc;
1717

1818
import org.springframework.beans.factory.support.AbstractBeanDefinition;
1919
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2020
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
2121
import org.springframework.beans.factory.xml.ParserContext;
22-
import org.springframework.data.elasticsearch.client.erhlc.RestClientFactoryBean;
2322
import org.w3c.dom.Element;
2423

2524
/**
2625
* @author Don Wellington
26+
* @deprecated since 5.0
2727
*/
28-
public class RestClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
28+
@Deprecated
29+
public class RestHighLevelClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
2930

3031
@Override
3132
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
32-
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RestClientFactoryBean.class);
33+
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RestHighLevelClientFactoryBean.class);
3334
setConfigurations(element, builder);
3435
return getSourcedBeanDefinition(builder, element, parserContext);
3536
}

src/main/java/org/springframework/data/elasticsearch/client/erhlc/RestClientFactoryBean.java renamed to src/main/java/org/springframework/data/elasticsearch/client/erhlc/RestHighLevelClientFactoryBean.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
import org.springframework.util.Assert;
3232

3333
/**
34-
* RestClientFactoryBean
34+
* RestHighLevelClientFactoryBean
3535
*
3636
* @author Don Wellington
3737
* @author Peter-Josef Meisch
3838
* @deprecated since 5.0
3939
*/
4040
@Deprecated
41-
public class RestClientFactoryBean implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
41+
public class RestHighLevelClientFactoryBean
42+
implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
4243

43-
private static final Log LOGGER = LogFactory.getLog(RestClientFactoryBean.class);
44+
private static final Log LOGGER = LogFactory.getLog(RestHighLevelClientFactoryBean.class);
4445

4546
private @Nullable RestHighLevelClient client;
4647
private String hosts = "http://localhost:9200";

src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchNamespaceHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.config;
1717

1818
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
19+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClientBeanDefinitionParser;
1920
import org.springframework.data.elasticsearch.repository.config.ElasticsearchRepositoryConfigExtension;
2021
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
2122
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
@@ -35,6 +36,6 @@ public void init() {
3536
RepositoryBeanDefinitionParser parser = new RepositoryBeanDefinitionParser(extension);
3637

3738
registerBeanDefinitionParser("repositories", parser);
38-
registerBeanDefinitionParser("rest-client", new RestClientBeanDefinitionParser());
39+
registerBeanDefinitionParser("elasticsearch-client", new ElasticsearchClientBeanDefinitionParser());
3940
}
4041
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd
22
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-3.2.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-3.2.xsd
33
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-4.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-4.0.xsd
4+
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-5.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-5.0.xsd
45
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-4.0.xsd
56
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd
67
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-3.2.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-3.2.xsd
78
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-4.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-4.0.xsd
8-
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-4.0.xsd
9+
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-5.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-5.0.xsd
10+
https\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-5.0.xsd
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<xsd:schema xmlns="http://www.springframework.org/schema/data/elasticsearch"
3+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4+
xmlns:beans="http://www.springframework.org/schema/beans"
5+
xmlns:tool="http://www.springframework.org/schema/tool"
6+
xmlns:repository="http://www.springframework.org/schema/data/repository"
7+
targetNamespace="http://www.springframework.org/schema/data/elasticsearch"
8+
elementFormDefault="qualified"
9+
attributeFormDefault="unqualified">
10+
11+
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
12+
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
13+
<xsd:import namespace="http://www.springframework.org/schema/data/repository"
14+
schemaLocation="https://www.springframework.org/schema/data/repository/spring-repository.xsd"/>
15+
16+
<xsd:element name="repositories">
17+
<xsd:complexType>
18+
<xsd:complexContent>
19+
<xsd:extension base="repository:repositories">
20+
<xsd:attributeGroup ref="repository:repository-attributes"/>
21+
<xsd:attribute name="elasticsearch-template-ref" type="elasticsearchTemplateRef"
22+
default="elasticsearchTemplate"/>
23+
</xsd:extension>
24+
</xsd:complexContent>
25+
</xsd:complexType>
26+
</xsd:element>
27+
28+
<xsd:simpleType name="elasticsearchTemplateRef">
29+
<xsd:annotation>
30+
<xsd:appinfo>
31+
<tool:annotation kind="ref">
32+
<tool:assignable-to type="org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate"/>
33+
</tool:annotation>
34+
</xsd:appinfo>
35+
</xsd:annotation>
36+
<xsd:union memberTypes="xsd:string"/>
37+
</xsd:simpleType>
38+
39+
<xsd:element name="elasticsearch-client">
40+
<xsd:annotation>
41+
<xsd:documentation/>
42+
<xsd:appinfo>
43+
<tool:assignable-to type="co.elastic.clients.elasticsearch.ElasticsearchClient"/>
44+
</xsd:appinfo>
45+
</xsd:annotation>
46+
<xsd:complexType>
47+
<xsd:complexContent>
48+
<xsd:extension base="beans:identifiedType">
49+
<xsd:attribute name="hosts" type="xsd:string" default="http://127.0.0.1:9200">
50+
<xsd:annotation>
51+
<xsd:documentation>
52+
<![CDATA[The comma delimited list of host:port entries to use for elasticsearch cluster.]]>
53+
</xsd:documentation>
54+
</xsd:annotation>
55+
</xsd:attribute>
56+
</xsd:extension>
57+
</xsd:complexContent>
58+
</xsd:complexType>
59+
</xsd:element>
60+
</xsd:schema>

src/test/java/org/springframework/data/elasticsearch/config/namespace/ElasticsearchNamespaceHandlerTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.data.annotation.Id;
2626
import org.springframework.data.elasticsearch.annotations.Document;
27-
import org.springframework.data.elasticsearch.client.erhlc.RestClientFactoryBean;
27+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClientFactoryBean;
2828
import org.springframework.data.elasticsearch.junit.jupiter.Tags;
2929
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
3030
import org.springframework.test.context.ContextConfiguration;
@@ -50,10 +50,11 @@ public void shouldCreateRepository() {
5050
}
5151

5252
@Test
53-
public void shouldCreateRestClient() {
53+
public void shouldCreateElasticsearchClient() {
5454

55-
assertThat(context.getBean(RestClientFactoryBean.class)).isNotNull();
56-
assertThat(context.getBean(RestClientFactoryBean.class)).isInstanceOf(RestClientFactoryBean.class);
55+
assertThat(context.getBean(ElasticsearchClientFactoryBean.class)).isNotNull();
56+
assertThat(context.getBean(ElasticsearchClientFactoryBean.class))
57+
.isInstanceOf(ElasticsearchClientFactoryBean.class);
5758
}
5859

5960
@Document(indexName = "test-index-config-namespace", createIndex = false)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5-
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
66
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
77

88
<bean name="elasticsearchTemplate"
9-
class="org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate">
10-
<constructor-arg name="client" ref="restClient"/>
9+
class="org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate">
10+
<constructor-arg name="client" ref="elasticsearchClient"/>
1111
</bean>
1212

1313
<elasticsearch:repositories
14-
base-package="org.springframework.data.elasticsearch.config.namespace"
15-
consider-nested-repositories="true"/>
14+
base-package="org.springframework.data.elasticsearch.config.namespace"
15+
consider-nested-repositories="true"/>
1616

17-
<elasticsearch:rest-client id="restClient"/>
17+
<elasticsearch:elasticsearch-client id="elasticsearchClient"/>
1818

1919
</beans>

0 commit comments

Comments
 (0)