aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* FROM mail.jar */ aoqi@0: package com.sun.xml.internal.org.jvnet.mimepull; aoqi@0: aoqi@0: import java.util.*; aoqi@0: aoqi@0: /** aoqi@0: * Utilities to make it easier to get property values. aoqi@0: * Properties can be strings or type-specific value objects. aoqi@0: * aoqi@0: * @author Bill Shannon aoqi@0: */ aoqi@0: final class PropUtil { aoqi@0: aoqi@0: // No one should instantiate this class. aoqi@0: private PropUtil() { aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get a boolean valued System property. aoqi@0: */ aoqi@0: public static boolean getBooleanSystemProperty(String name, boolean def) { aoqi@0: try { aoqi@0: return getBoolean(getProp(System.getProperties(), name), def); aoqi@0: } catch (SecurityException sex) { aoqi@0: // fall through... aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * If we can't get the entire System Properties object because aoqi@0: * of a SecurityException, just ask for the specific property. aoqi@0: */ aoqi@0: try { aoqi@0: String value = System.getProperty(name); aoqi@0: if (value == null) { aoqi@0: return def; aoqi@0: } aoqi@0: if (def) { aoqi@0: return !value.equalsIgnoreCase("false"); aoqi@0: } else { aoqi@0: return value.equalsIgnoreCase("true"); aoqi@0: } aoqi@0: } catch (SecurityException sex) { aoqi@0: return def; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the value of the specified property. aoqi@0: * If the "get" method returns null, use the getProperty method, aoqi@0: * which might cascade to a default Properties object. aoqi@0: */ aoqi@0: private static Object getProp(Properties props, String name) { aoqi@0: Object val = props.get(name); aoqi@0: if (val != null) { aoqi@0: return val; aoqi@0: } else { aoqi@0: return props.getProperty(name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Interpret the value object as a boolean, aoqi@0: * returning def if unable. aoqi@0: */ aoqi@0: private static boolean getBoolean(Object value, boolean def) { aoqi@0: if (value == null) { aoqi@0: return def; aoqi@0: } aoqi@0: if (value instanceof String) { aoqi@0: /* aoqi@0: * If the default is true, only "false" turns it off. aoqi@0: * If the default is false, only "true" turns it on. aoqi@0: */ aoqi@0: if (def) { aoqi@0: return !((String)value).equalsIgnoreCase("false"); aoqi@0: } else { aoqi@0: return ((String)value).equalsIgnoreCase("true"); aoqi@0: } aoqi@0: } aoqi@0: if (value instanceof Boolean) { aoqi@0: return ((Boolean)value).booleanValue(); aoqi@0: } aoqi@0: return def; aoqi@0: } aoqi@0: }