2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.core.zusammen.impl;
19 import java.util.function.Supplier;
20 import org.apache.commons.lang3.StringUtils;
21 import org.openecomp.core.nosqldb.util.CassandraUtils;
23 public class CassandraConnectionInitializer {
25 private static final String CASSANDRA_PREFIX = "cassandra.";
26 private static final String DATA_CENTER_PROPERTY_NAME = CASSANDRA_PREFIX + "datacenter";
27 private static final String CONSISTENCY_LEVEL_PROPERTY_NAME =
28 CASSANDRA_PREFIX + "consistency.level";
29 private static final String CASSANDRA_RECONNECT_TIMEOUT = CASSANDRA_PREFIX + "reconnection.delay";
30 private static final String NODES_PROPERTY_NAME = CASSANDRA_PREFIX + "nodes";
31 private static final String CASSANDRA_PORT_PROPERTY_NAME = CASSANDRA_PREFIX + "port";
32 private static final String AUTHENTICATE_PROPERTY_NAME = CASSANDRA_PREFIX + "authenticate";
33 private static final String SSL_PROPERTY_NAME = CASSANDRA_PREFIX + "ssl";
34 private static final String TRUSTSTORE_PROPERTY_NAME = CASSANDRA_PREFIX + "truststore";
35 private static final String TRUSTSTORE_PASSWORD_PROPERTY_NAME =
36 CASSANDRA_PREFIX + "truststore.password";
37 private static final String USER_PROPERTY_NAME = CASSANDRA_PREFIX + "user";
38 private static final String PASSWORD_PROPERTY_NAME = CASSANDRA_PREFIX + "password";
39 private static final String KEYSPACE_PROPERTY_NAME = CASSANDRA_PREFIX + "keyspace";
40 private static final String ZUSAMMEN = "zusammen";
42 private CassandraConnectionInitializer() {
43 // static utility class, prevent instantiation
46 public static void setCassandraConnectionPropertiesToSystem() {
47 DeferredInitializer.init();
50 private static class DeferredInitializer {
53 setSystemProperty(NODES_PROPERTY_NAME, () ->
54 StringUtils.join(CassandraUtils.getAddresses(), ','));
55 setSystemProperty(CASSANDRA_PORT_PROPERTY_NAME, () -> Integer.toString(CassandraUtils.getCassandraPort()));
56 setBooleanSystemProperty(AUTHENTICATE_PROPERTY_NAME, CassandraUtils::isAuthenticate);
57 setBooleanSystemProperty(SSL_PROPERTY_NAME, CassandraUtils::isSsl);
58 setNullableSystemProperty(TRUSTSTORE_PROPERTY_NAME, CassandraUtils::getTruststore);
59 setNullableSystemProperty(TRUSTSTORE_PASSWORD_PROPERTY_NAME, CassandraUtils::getTruststorePassword);
60 setSystemProperty(USER_PROPERTY_NAME, CassandraUtils::getUser);
61 setSystemProperty(PASSWORD_PROPERTY_NAME, CassandraUtils::getPassword);
62 setSystemProperty(KEYSPACE_PROPERTY_NAME, () -> ZUSAMMEN);
63 setNullableSystemProperty(DATA_CENTER_PROPERTY_NAME, CassandraUtils::getLocalDataCenter);
64 setNullableSystemProperty(CONSISTENCY_LEVEL_PROPERTY_NAME,
65 CassandraUtils::getConsistencyLevel);
66 setSystemProperty(CASSANDRA_RECONNECT_TIMEOUT, () -> Long.toString(CassandraUtils.getReconnectTimeout()));
69 private DeferredInitializer() { }
71 @SuppressWarnings("EmptyMethod")
73 // no-op, just to ensure static initialization
76 private static void setSystemProperty(String name, Supplier<String> valueSupplier) {
78 if (System.getProperty(name) == null) {
79 System.setProperty(name, valueSupplier.get());
83 private static void setBooleanSystemProperty(String name, Supplier<Boolean> valueSupplier) {
84 setSystemProperty(name, () -> Boolean.toString(valueSupplier.get()));
87 private static void setNullableSystemProperty(String name, Supplier<String> valueSupplier) {
89 if ((System.getProperty(name) == null) && (valueSupplier.get() != null)) {
90 System.setProperty(name, valueSupplier.get());