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