2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.listen;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
27 import java.util.jar.Attributes;
28 import java.util.jar.Manifest;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
33 import org.apache.commons.io.filefilter.FileFilterUtils;
34 import org.apache.commons.io.filefilter.IOFileFilter;
35 import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
36 import org.apache.commons.io.monitor.FileAlterationMonitor;
37 import org.apache.commons.io.monitor.FileAlterationObserver;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
40 import org.openecomp.sdc.be.monitoring.BeMonitoringService;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.openecomp.sdc.common.impl.ExternalConfiguration;
43 import org.openecomp.sdc.common.listener.AppContextListener;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
46 public class BEAppContextListener extends AppContextListener implements ServletContextListener {
48 private static final String MANIFEST_FILE_NAME = "/META-INF/MANIFEST.MF";
49 private static final Logger log = Logger.getLogger(BEAppContextListener.class);
51 public void contextInitialized(ServletContextEvent context) {
52 super.contextInitialized(context);
53 ConfigurationManager configurationManager = new ConfigurationManager(ExternalConfiguration.getConfigurationSource());
54 log.debug("loading configuration from configDir: {} appName: {}", ExternalConfiguration.getConfigDir(), ExternalConfiguration.getAppName());
55 context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
56 WebAppContextWrapper webAppContextWrapper = new WebAppContextWrapper();
57 context.getServletContext().setAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR, webAppContextWrapper);
58 context.getServletContext().setAttribute(Constants.ASDC_RELEASE_VERSION_ATTR, getVersionFromManifest(context));
60 BeMonitoringService bms = new BeMonitoringService(context.getServletContext());
61 bms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(15));
62 initTlsFileMonitoring();
63 log.debug("After executing {}", this.getClass());
66 private String getVersionFromManifest(ServletContextEvent context) {
67 ServletContext servletContext = context.getServletContext();
68 InputStream inputStream = servletContext.getResourceAsStream(MANIFEST_FILE_NAME);
69 String version = null;
71 Manifest mf = new Manifest(inputStream);
72 Attributes atts = mf.getMainAttributes();
73 version = atts.getValue(Constants.ASDC_RELEASE_VERSION_ATTR);
74 if (version == null || version.isEmpty()) {
75 log.warn("failed to read ASDC version from MANIFEST.");
77 log.info("ASDC version from MANIFEST is {}", version);
79 } catch (IOException e) {
80 log.warn("failed to read ASDC version from MANIFEST", e);
85 private void initTlsFileMonitoring() {
86 final Map<String, IOFileFilter> tlsFileFilters = createTlsFileFilters();
87 if (!tlsFileFilters.isEmpty()) {
88 final TlsFileChangeHandler tlsFileChangeHandler = new TlsFileChangeHandler();
89 tlsFileFilters.entrySet().stream().forEach(entry -> listenForChanges(entry.getKey(), tlsFileChangeHandler, entry.getValue()));
93 private Map<String, IOFileFilter> createTlsFileFilters() {
94 final Map<String, IOFileFilter> filters = new HashMap<>();
95 addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getTlsCert());
96 addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getTlsKey());
97 addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getCaCert());
101 private void addFilter(final Map<String, IOFileFilter> filters, final String path) {
103 final File file = new File(path);
104 final IOFileFilter caCertFileFilter =
105 FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.nameFileFilter(file.getName()));
107 if (filters.containsKey(file.getParent())) {
108 filters.put(file.getParent(), FileFilterUtils.or(filters.get(file.getParent()), caCertFileFilter));
110 filters.put(file.getParent(), caCertFileFilter);
115 private void listenForChanges(String path, FileAlterationListenerAdaptor changeListener, IOFileFilter ioFileFilter) {
116 FileAlterationMonitor monitor = new FileAlterationMonitor();
117 final FileAlterationObserver observer = new FileAlterationObserver(path, ioFileFilter);
118 observer.addListener(changeListener);
119 monitor.addObserver(observer);
122 } catch (final Exception exception) {
123 log.error("Error starting monitoring of TLS files", exception);