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 org.apache.commons.lang3.StringUtils;
21 import org.openecomp.core.nosqldb.util.CassandraUtils;
23 import java.util.function.Supplier;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
28 public class CassandraConnectionInitializer implements ServletContextListener {
30 private static final String CASSANDRA_PREFIX = "cassandra.";
31 private static final String DATA_CENTER_PROPERTY_NAME = CASSANDRA_PREFIX + "datacenter";
32 private static final String CONSISTENCY_LEVEL_PROPERTY_NAME =
33 CASSANDRA_PREFIX + "consistency.level";
34 private static final String NODES_PROPERTY_NAME = CASSANDRA_PREFIX + "nodes";
35 private static final String AUTHENTICATE_PROPERTY_NAME = CASSANDRA_PREFIX + "authenticate";
36 private static final String SSL_PROPERTY_NAME = CASSANDRA_PREFIX + "ssl";
37 private static final String TRUSTSTORE_PROPERTY_NAME = CASSANDRA_PREFIX + "truststore";
38 private static final String TRUSTSTORE_PASSWORD_PROPERTY_NAME =
39 CASSANDRA_PREFIX + "truststore.password";
40 private static final String USER_PROPERTY_NAME = CASSANDRA_PREFIX + "user";
41 private static final String PASSWORD_PROPERTY_NAME = CASSANDRA_PREFIX + "password";
42 private static final String KEYSPACE_PROPERTY_NAME = CASSANDRA_PREFIX + "keyspace";
43 private static final String ZUSAMMEN = "zusammen";
46 public void contextInitialized(ServletContextEvent servletContextEvent) {
47 setCassandraConnectionPropertiesToSystem();
50 public static void setCassandraConnectionPropertiesToSystem() {
51 DeferredInitializer.init();
55 public void contextDestroyed(ServletContextEvent servletContextEvent) {
56 // no-op, required by the interface
59 private static class DeferredInitializer {
62 setSystemProperty(NODES_PROPERTY_NAME, () ->
63 StringUtils.join(CassandraUtils.getAddresses(), ','));
64 setBooleanSystemProperty(AUTHENTICATE_PROPERTY_NAME, CassandraUtils::isAuthenticate);
65 setBooleanSystemProperty(SSL_PROPERTY_NAME, CassandraUtils::isSsl);
66 setSystemProperty(TRUSTSTORE_PROPERTY_NAME, CassandraUtils::getTruststore);
67 setSystemProperty(TRUSTSTORE_PASSWORD_PROPERTY_NAME, CassandraUtils::getTruststorePassword);
68 setSystemProperty(USER_PROPERTY_NAME, CassandraUtils::getUser);
69 setSystemProperty(PASSWORD_PROPERTY_NAME, CassandraUtils::getPassword);
70 setSystemProperty(KEYSPACE_PROPERTY_NAME, () -> ZUSAMMEN);
71 setNullableSystemProperty(DATA_CENTER_PROPERTY_NAME, CassandraUtils::getLocalDataCenter);
72 setNullableSystemProperty(CONSISTENCY_LEVEL_PROPERTY_NAME,
73 CassandraUtils::getConsistencyLevel);
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());
94 private DeferredInitializer() { }
96 public static void init() {
97 // just to ensure static initialization