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