add modernizer-maven-plugin to infrautils' parent
[serviceutils.git] / metrics / impl / src / main / java / org / opendaylight / infrautils / metrics / internal / Configuration.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.infrautils.metrics.internal;
9
10 import com.google.common.base.MoreObjects;
11 import java.util.Map;
12 import java.util.function.Consumer;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * Configuration properties for the metrics implementation.
18  *
19  * <p>Karaf's OSGi ConfigAdmin service, via the cm blueprint extension, sets this
20  * from the etc/org.opendaylight.infrautils.metrics.cfg configuration file.
21  *
22  * @author Michael Vorburger.ch
23  */
24 public final class Configuration {
25
26     private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);
27
28     private final MetricProviderImpl metricProvider;
29
30     // Apply any change to these defaults also to org.opendaylight.infrautils.metrics.cfg
31     // (Just for clarity; they are commented out there, so these are the real defaults.)
32     private int threadsWatcherIntervalMS = 500;
33     private int maxThreads = 1000;
34     private int fileReporterIntervalSecs = 0;
35     private int maxThreadsMaxLogIntervalSecs = 60;
36     private int deadlockedThreadsMaxLogIntervalSecs = 60;
37
38     public Configuration(MetricProviderImpl metricProvider, Map<String, String> initialProperties) {
39         this(metricProvider);
40         updateProperties(initialProperties);
41     }
42
43     public Configuration(MetricProviderImpl metricProvider) {
44         this.metricProvider = metricProvider;
45     }
46
47     public void updateProperties(Map<String, String> properties) {
48         LOG.info("updateProperties({})", properties);
49         doIfIntPropertyIsPresent(properties, "threadsWatcherIntervalMS", this::setThreadsWatcherIntervalMS);
50         doIfIntPropertyIsPresent(properties, "maxThreads", this::setMaxThreads);
51         doIfIntPropertyIsPresent(properties, "fileReporterIntervalSecs", this::setFileReporterIntervalSecs);
52         doIfIntPropertyIsPresent(properties, "maxThreadsMaxLogIntervalSecs", this::setMaxThreadsMaxLogIntervalSecs);
53         doIfIntPropertyIsPresent(properties, "deadlockedThreadsMaxLogIntervalSecs",
54                 this::setDeadlockedThreadsMaxLogIntervalSecs);
55
56         metricProvider.updateConfiguration(this);
57     }
58
59     public void setFileReporterIntervalSecs(int fileReporterIntervalSecs) {
60         this.fileReporterIntervalSecs = fileReporterIntervalSecs;
61     }
62
63     public int getFileReporterIntervalSecs() {
64         return fileReporterIntervalSecs;
65     }
66
67     public void setThreadsWatcherIntervalMS(int ms) {
68         this.threadsWatcherIntervalMS = ms;
69     }
70
71     public int getThreadsWatcherIntervalMS() {
72         return this.threadsWatcherIntervalMS;
73     }
74
75     public void setMaxThreads(int maxThreads) {
76         this.maxThreads = maxThreads;
77     }
78
79     public int getMaxThreads() {
80         return this.maxThreads;
81     }
82
83     public void setMaxThreadsMaxLogIntervalSecs(int maxThreadsMaxLogIntervalSecs) {
84         this.maxThreadsMaxLogIntervalSecs = maxThreadsMaxLogIntervalSecs;
85     }
86
87     public int getMaxThreadsMaxLogIntervalSecs() {
88         return maxThreadsMaxLogIntervalSecs;
89     }
90
91     public void setDeadlockedThreadsMaxLogIntervalSecs(int deadlockedThreadsMaxLogIntervalSecs) {
92         this.deadlockedThreadsMaxLogIntervalSecs = deadlockedThreadsMaxLogIntervalSecs;
93     }
94
95     public int getDeadlockedThreadsMaxLogIntervalSecs() {
96         return deadlockedThreadsMaxLogIntervalSecs;
97     }
98
99     @Override
100     public String toString() {
101         return MoreObjects.toStringHelper(this)
102                 .add("threadsWatcherIntervalMS", threadsWatcherIntervalMS)
103                 .add("maxThreads", maxThreads)
104                 .add("maxThreadsMaxLogIntervalSecs", maxThreadsMaxLogIntervalSecs)
105                 .add("deadlockedThreadsMaxLogIntervalSecs", deadlockedThreadsMaxLogIntervalSecs)
106                 .add("fileReporterIntervalSecs", fileReporterIntervalSecs)
107                 .toString();
108     }
109
110     // When any other project want to deal with Configuration like this, perhaps this could be moved somewhere re-usable
111     private static void doIfIntPropertyIsPresent(
112             Map<String, String> properties, String propertyName, Consumer<Integer> consumer) {
113         String propertyValueAsString = properties.get(propertyName);
114         if (propertyValueAsString != null) {
115             try {
116                 Integer propertyValueAsInt = Integer.parseInt(propertyValueAsString);
117                 consumer.accept(propertyValueAsInt);
118             } catch (NumberFormatException nfe) {
119                 LOG.warn("Ignored property '{}' that was expected to be an Integer but was not: {}", propertyName,
120                         propertyValueAsString, nfe);
121             }
122         }
123     }
124 }