6cef8f5753a26620ae7045f7912a547f78deca23
[ovsdb.git] / utils / config / src / main / java / org / opendaylight / ovsdb / utils / config / ConfigProperties.java
1 /*
2  * Copyright (c) 2014, 2016 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
9 package org.opendaylight.ovsdb.utils.config;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.osgi.framework.Bundle;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.FrameworkUtil;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public final class ConfigProperties {
21     private static final Logger LOG = LoggerFactory.getLogger(ConfigProperties.class);
22     private static final Map<String, String> OVERRIDES = new HashMap<>();
23
24     private ConfigProperties() {
25         // empty
26     }
27
28     public static String getProperty(Class<?> classParam, final String propertyStr) {
29         return getProperty(classParam, propertyStr, null);
30     }
31
32     public static String getProperty(Class<?> classParam, final String propertyStr, final String defaultValue) {
33         String value = ConfigProperties.OVERRIDES.get(propertyStr);
34         if (value != null) {
35             return value;
36         }
37
38         Bundle bundle = FrameworkUtil.getBundle(classParam);
39
40         if (bundle != null) {
41             BundleContext bundleContext = bundle.getBundleContext();
42             if (bundleContext != null) {
43                 value = bundleContext.getProperty(propertyStr);
44             }
45         }
46         if (value == null) {
47             value = System.getProperty(propertyStr, defaultValue);
48         }
49
50         if (value == null) {
51             LOG.debug("ConfigProperties missing a value for {}, default {}", propertyStr, defaultValue);
52         }
53
54         return value;
55     }
56
57     public static void overrideProperty(String property, String value) {
58         ConfigProperties.OVERRIDES.put(property, value);
59     }
60 }