EMMA Coverage Report (generated Tue Mar 05 16:36:55 GMT 2013)
[all classes][org.springframework.data.elasticsearch.client]

COVERAGE SUMMARY FOR SOURCE FILE [TransportClientFactoryBean.java]

nameclass, %method, %block, %line, %
TransportClientFactoryBean.java100% (1/1)82%  (9/11)81%  (101/124)72%  (23/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TransportClientFactoryBean100% (1/1)82%  (9/11)81%  (101/124)72%  (23/32)
getObject (): TransportClient 0%   (0/1)0%   (0/3)0%   (0/1)
setProperties (Properties): void 0%   (0/1)0%   (0/4)0%   (0/2)
destroy (): void 100% (1/1)38%  (6/16)29%  (2/7)
settings (): Settings 100% (1/1)60%  (9/15)67%  (2/3)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
TransportClientFactoryBean (): void 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (3/3)100% (2/2)
buildClient (): void 100% (1/1)100% (68/68)100% (11/11)
getObjectType (): Class 100% (1/1)100% (2/2)100% (1/1)
isSingleton (): boolean 100% (1/1)100% (2/2)100% (1/1)
setClusterNodes (String []): void 100% (1/1)100% (4/4)100% (2/2)

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 
17package org.springframework.data.elasticsearch.client;
18 
19import org.elasticsearch.client.transport.TransportClient;
20import org.elasticsearch.common.settings.Settings;
21import org.elasticsearch.common.transport.InetSocketTransportAddress;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24import org.springframework.beans.factory.DisposableBean;
25import org.springframework.beans.factory.FactoryBean;
26import org.springframework.beans.factory.InitializingBean;
27import org.springframework.util.Assert;
28 
29import java.util.Properties;
30 
31import static org.apache.commons.lang.StringUtils.substringAfter;
32import static org.apache.commons.lang.StringUtils.substringBefore;
33import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
34 
35/**
36 *  TransportClientFactoryBean
37 *
38 * @author Rizwan Idrees
39 * @author Mohsin Husen
40 */
41 
42public 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}

[all classes][org.springframework.data.elasticsearch.client]
EMMA 2.0.5312 (C) Vladimir Roubtsov