From 4871bc817fd427c08216cef5623bcf5b54bebf9d Mon Sep 17 00:00:00 2001 From: samskivert Date: Sun, 19 Dec 2010 19:12:10 +0000 Subject: [PATCH] Added requireProperty() and associated MissingPropertyException. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2972 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../util/MissingPropertyException.java | 34 +++++++++++++++++++ .../com/samskivert/util/PropertiesUtil.java | 24 +++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/main/java/com/samskivert/util/MissingPropertyException.java diff --git a/src/main/java/com/samskivert/util/MissingPropertyException.java b/src/main/java/com/samskivert/util/MissingPropertyException.java new file mode 100644 index 00000000..ef2006e8 --- /dev/null +++ b/src/main/java/com/samskivert/util/MissingPropertyException.java @@ -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); + } +} diff --git a/src/main/java/com/samskivert/util/PropertiesUtil.java b/src/main/java/com/samskivert/util/PropertiesUtil.java index b92b8b08..585e0d03 100644 --- a/src/main/java/com/samskivert/util/PropertiesUtil.java +++ b/src/main/java/com/samskivert/util/PropertiesUtil.java @@ -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 + * 'key'." 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; + } }