• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Commonjava / indy-tracking-service / 11453122149

22 Oct 2024 04:14AM UTC coverage: 66.667% (+0.04%) from 66.629%
11453122149

push

github

yma96
Remove one time closed flag since might need to reconnect as necessary

0 of 1 new or added line in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

1178 of 1767 relevant lines covered (66.67%)

1.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.35
/src/main/java/org/commonjava/indy/service/tracking/data/cassandra/CassandraClient.java
1
/**
2
 * Copyright (C) 2022-2023 Red Hat, Inc. (https://github.com/Commonjava/indy-tracking-service)
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
package org.commonjava.indy.service.tracking.data.cassandra;
17

18
import com.datastax.driver.core.Cluster;
19
import com.datastax.driver.core.Session;
20
import com.datastax.driver.core.SocketOptions;
21
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
22
import jakarta.annotation.PostConstruct;
23
import jakarta.enterprise.context.ApplicationScoped;
24
import jakarta.inject.Inject;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

28
import java.util.Map;
29
import java.util.concurrent.ConcurrentHashMap;
30

31
import static org.apache.commons.lang3.StringUtils.isNotBlank;
32

33
@ApplicationScoped
34
public class CassandraClient
35
{
36
    private final Logger logger = LoggerFactory.getLogger( getClass() );
2✔
37

38
    final private Map<String, Session> sessions = new ConcurrentHashMap<>();
2✔
39

40
    @Inject
41
    CassandraConfiguration config;
42

43
    private String host;
44

45
    private int port;
46

47
    private String username;
48

49
    private Cluster cluster;
50

51
    public CassandraClient()
52
    {
2✔
53
    }
2✔
54

55
    public CassandraClient( CassandraConfiguration config )
56
    {
×
57
        this.config = config;
×
58
        init();
×
59
    }
×
60

61
    @PostConstruct
62
    public void init()
63
    {
64
        if ( !config.isEnabled() )
2✔
65
        {
66
            logger.info( "Cassandra client not enabled" );
×
67
            return;
×
68
        }
69

70
        host = config.getCassandraHost();
2✔
71
        port = config.getCassandraPort();
2✔
72
        SocketOptions socketOptions = new SocketOptions();
2✔
73
        socketOptions.setConnectTimeoutMillis( config.getConnectTimeoutMillis() );
2✔
74
        socketOptions.setReadTimeoutMillis( config.getReadTimeoutMillis() );
2✔
75
        Cluster.Builder builder = Cluster.builder()
2✔
76
                                         .withoutJMXReporting()
2✔
77
                                         .withReconnectionPolicy(
2✔
78
                                                 new ConstantReconnectionPolicy( config.getConstantDelayMs() ) )
2✔
79
                                         .withRetryPolicy( new ConfigurableRetryPolicy( config.getReadRetries(),
2✔
80
                                                                                        config.getWriteRetries() ) )
2✔
81
                                         .addContactPoint( host )
2✔
82
                                         .withPort( port )
2✔
83
                                         .withSocketOptions( socketOptions );
2✔
84
        username = config.getCassandraUser();
2✔
85
        String password = config.getCassandraPass();
2✔
86
        if ( isNotBlank( username ) && isNotBlank( password ) )
2✔
87
        {
88
            logger.info( "Build with credentials, user: {}, pass: ****", username );
2✔
89
            builder.withCredentials( username, password );
2✔
90
        }
91
        cluster = builder.build();
2✔
92
    }
2✔
93

94
    public Session getSession( String keyspace )
95
    {
96
        if ( !config.isEnabled() )
2✔
97
        {
98
            logger.info( "Cassandra client not enabled" );
×
99
            return null;
×
100
        }
101

102
        return sessions.computeIfAbsent( keyspace, key -> {
2✔
103
            logger.info( "Connect to Cassandra, host: {}, port: {}, user: {}, keyspace: {}", host, port, username,
2✔
104
                         key );
105
            try
106
            {
107
                return cluster.connect();
2✔
108
            }
109
            catch ( Exception e )
2✔
110
            {
111
                logger.error( "Connecting to Cassandra failed", e );
2✔
112
            }
113
            return null;
2✔
114
        } );
115
    }
116

117
    public void close()
118
    {
NEW
119
        if ( cluster != null )
×
120
        {
121
            logger.info( "Close cassandra client" );
×
122
            sessions.forEach( ( key, value ) -> value.close() );
×
123
            sessions.clear();
×
124
            cluster.close();
×
125
            cluster = null;
×
126
        }
UNCOV
127
    }
×
128

129
    public Map<String, Session> getSessions()
130
    {
131
        return sessions;
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc