8eeeb1e27bf206d76de1f8f9f4b072979c012768
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
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
17 package org.openecomp.core.zusammen.impl;
18
19 import org.apache.commons.lang3.StringUtils;
20
21 import org.openecomp.core.nosqldb.util.CassandraUtils;
22
23 import java.util.function.Supplier;
24
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27
28 public class CassandraConnectionInitializer implements ServletContextListener {
29
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";
44
45   @Override
46   public void contextInitialized(ServletContextEvent servletContextEvent) {
47     setCassandraConnectionPropertiesToSystem();
48   }
49
50   public static void setCassandraConnectionPropertiesToSystem() {
51     DeferredInitializer.init();
52   }
53
54   @Override
55   public void contextDestroyed(ServletContextEvent servletContextEvent) {
56     // no-op, required by the interface
57   }
58
59   private static class DeferredInitializer {
60
61     static {
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);
74     }
75
76     private static void setSystemProperty(String name, Supplier<String> valueSupplier) {
77
78       if (System.getProperty(name) == null) {
79         System.setProperty(name, valueSupplier.get());
80       }
81     }
82
83     private static void setBooleanSystemProperty(String name, Supplier<Boolean> valueSupplier) {
84       setSystemProperty(name, () -> Boolean.toString(valueSupplier.get()));
85     }
86
87     private static void setNullableSystemProperty(String name, Supplier<String> valueSupplier) {
88
89       if ((System.getProperty(name) == null) && (valueSupplier.get() != null)) {
90         System.setProperty(name, valueSupplier.get());
91       }
92     }
93
94     private DeferredInitializer() { }
95
96     public static void init() {
97       // just to ensure static initialization
98     }
99   }
100 }