1 | /* |
2 | * Copyright 2013 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 | * http://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 | |
17 | package org.springframework.data.elasticsearch.client; |
18 | |
19 | import org.elasticsearch.client.transport.TransportClient; |
20 | import org.elasticsearch.common.settings.Settings; |
21 | import org.elasticsearch.common.transport.InetSocketTransportAddress; |
22 | import org.slf4j.Logger; |
23 | import org.slf4j.LoggerFactory; |
24 | import org.springframework.beans.factory.DisposableBean; |
25 | import org.springframework.beans.factory.FactoryBean; |
26 | import org.springframework.beans.factory.InitializingBean; |
27 | import org.springframework.util.Assert; |
28 | |
29 | import java.util.Properties; |
30 | |
31 | import static org.apache.commons.lang.StringUtils.substringAfter; |
32 | import static org.apache.commons.lang.StringUtils.substringBefore; |
33 | import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; |
34 | |
35 | /** |
36 | * TransportClientFactoryBean |
37 | * |
38 | * @author Rizwan Idrees |
39 | * @author Mohsin Husen |
40 | */ |
41 | |
42 | public class TransportClientFactoryBean implements FactoryBean<TransportClient>, InitializingBean, DisposableBean { |
43 | |
44 | private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); |
45 | private String[] clusterNodes; |
46 | private TransportClient client; |
47 | private Properties properties; |
48 | static final String COLON = ":"; |
49 | |
50 | @Override |
51 | public void destroy() throws Exception { |
52 | try { |
53 | logger.info("Closing elasticSearch client"); |
54 | if (client != null) { |
55 | client.close(); |
56 | } |
57 | } catch (final Exception e) { |
58 | logger.error("Error closing ElasticSearch client: ", e); |
59 | } |
60 | } |
61 | |
62 | @Override |
63 | public TransportClient getObject() throws Exception { |
64 | return client; |
65 | } |
66 | |
67 | @Override |
68 | public Class<TransportClient> getObjectType() { |
69 | return TransportClient.class; |
70 | } |
71 | |
72 | @Override |
73 | public boolean isSingleton() { |
74 | return false; |
75 | } |
76 | |
77 | @Override |
78 | public void afterPropertiesSet() throws Exception { |
79 | buildClient(); |
80 | } |
81 | |
82 | protected void buildClient() throws Exception { |
83 | client = new TransportClient(settings()); |
84 | Assert.notEmpty(clusterNodes,"[Assertion failed] clusterNodes settings missing."); |
85 | for (String clusterNode : clusterNodes) { |
86 | String hostName = substringBefore(clusterNode, COLON); |
87 | String port = substringAfter(clusterNode, COLON); |
88 | Assert.hasText(hostName,"[Assertion failed] missing host name in 'clusterNodes'"); |
89 | Assert.hasText(port,"[Assertion failed] missing port in 'clusterNodes'"); |
90 | logger.info("adding transport node : " + clusterNode); |
91 | client.addTransportAddress(new InetSocketTransportAddress(hostName, Integer.valueOf(port))); |
92 | } |
93 | client.connectedNodes(); |
94 | } |
95 | |
96 | private Settings settings(){ |
97 | if(properties != null){ |
98 | return settingsBuilder().put(properties).build(); |
99 | } |
100 | return settingsBuilder() |
101 | .put("client.transport.sniff",true).build(); |
102 | } |
103 | |
104 | public void setClusterNodes(String[] clusterNodes) { |
105 | this.clusterNodes = clusterNodes; |
106 | } |
107 | |
108 | public void setProperties(Properties properties) { |
109 | this.properties = properties; |
110 | } |
111 | } |