Added requireProperty() and associated MissingPropertyException.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2972 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-12-19 19:12:10 +00:00
parent df7df145e6
commit 4871bc817f
2 changed files with 58 additions and 0 deletions
@@ -0,0 +1,34 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 Michael Bayne, et al.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util;
/**
* An exception thrown when a required property was not found.
*
* @see PropertiesUtil#requireProperty
*/
public class MissingPropertyException extends RuntimeException
{
public MissingPropertyException (String message)
{
super(message);
}
}
@@ -218,4 +218,28 @@ public class PropertiesUtil
return null;
}
}
/**
* Returns the specified property from the supplied properties object.
* @throws MissingPropertyException with the text "Missing required property
* '<code>key</code>'." if the property does not exist or is the empty string.
*/
public static String requireProperty (Properties props, String key)
{
return requireProperty(props, key, "Missing required property '" + key + "'.");
}
/**
* Returns the specified property from the supplied properties object.
* @throws MissingPropertyException with the supplied message if the property does not exist or
* is the empty string.
*/
public static String requireProperty (Properties props, String key, String missingMessage)
{
String value = props.getProperty(key);
if (StringUtil.isBlank(value)) {
throw new MissingPropertyException(missingMessage);
}
return value;
}
}