Description
Returns the property with the given key from the given Properties .
License
Apache License
Parameter
Parameter | Description |
---|
properties | the Properties from which to retrieve the property |
context | The context should be the name of the caller, so that stack trace gives enough detail as to which class expected the configuration property if no property was found |
key | the key of the property to return |
def | the default value, if null, then an exception will be thrown if the property is not set |
Exception
Parameter | Description |
---|
RuntimeException | if the property is not set and def is null |
Return
the property under the given key
Declaration
public static String getProperty(Properties properties, String context, String key, String def)
throws RuntimeException
Method Source Code
//package com.java2s;
/*/* www .java2 s . com*/
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.MessageFormat;
import java.util.Properties;
public class Main {
/**
* Returns the property with the given key from the given {@link Properties}. If def is null, and the property is
* not set, then a {@link RuntimeException} is thrown
*
* @param properties
* the {@link Properties} from which to retrieve the property
* @param context
* The context should be the name of the caller, so that stack trace gives enough detail as to which
* class expected the configuration property if no property was found
* @param key
* the key of the property to return
* @param def
* the default value, if null, then an exception will be thrown if the property is not set
*
* @return the property under the given key
*
* @throws RuntimeException
* if the property is not set and def is null
*/
public static String getProperty(Properties properties, String context, String key, String def)
throws RuntimeException {
String property = properties.getProperty(key, def);
if (property == null) {
String msg = "[{0}] Property {1} is not set, and no default was given!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, context, key);
throw new RuntimeException(msg);
}
return property;
}
/**
* Returns the {@link System#getProperty(String)} with the given key. If def is null, and the property is not set,
* then a {@link RuntimeException} is thrown
*
* @param context
* The context should be the name of the caller, so that stack trace gives enough detail as to which
* class expected the configuration property if no property was found
* @param key
* the key of the property to return
* @param def
* the default value, if null, then an exception will be thrown if the property is not set
*
* @return the property under the given key
*
* @throws RuntimeException
* if the property is not set and def is null
*/
public static String getProperty(String context, String key, String def) throws RuntimeException {
return getProperty(System.getProperties(), context, key, def);
}
}
Related
- getLong(Properties props, String string, long defaultV)
- getNestedProperties(String prefix, Properties properties)
- getProp(Properties props, String name)
- getPropertiesForBloomFilter( int requiredFieldsSize, int matchKeySize)
- getPropertiesWithPrefix(Properties pro, String prefix)
- getProperty(Properties props, String keyword)
- getProperty(Properties props, String prefix, String key)
- getProperty(Properties props, String propertyName)
- getProperty(Properties props, String propname)