6017cc292478b852b353367abdac7e4dfb92a44f
[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
18 public final class ConfigProperties {
19
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             System.err.println("ConfigProperties missing a value for " + propertyStr);
50         }
51
52         return value;
53     }
54
55     public static void overrideProperty(String property, String value) {
56         ConfigProperties.OVERRIDES.put(property, value);
57     }
58 }