001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018
019package org.apache.commons.exec.util;
020
021/**
022 * Helper classes to provide debugging support.
023 *
024 * @version $Id: DebugUtils.java 1557571 2014-01-12 17:51:20Z britter $
025 */
026public class DebugUtils
027{
028    /**
029     * System property to determine how to handle exceptions. When
030     * set to "false" we rethrow the otherwise silently catched
031     * exceptions found in the original code. The default value
032     * is "true"
033     */
034    public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";
035
036    /**
037     * System property to determine how to dump an exception. When
038     * set to "true" we print any exception to stderr. The default
039     * value is "false"
040     */
041    public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";
042
043    /**
044     * Handle an exception based on the system properties.
045     *
046     * @param msg message describing the problem
047     * @param e an exception being handled
048     */
049    public static void handleException(final String msg, final Exception e) {
050
051        if (isDebugEnabled()) {
052            System.err.println(msg);
053            e.printStackTrace();
054        }
055
056        if (!isLenientEnabled()) {
057            if (e instanceof RuntimeException) {
058                throw (RuntimeException) e;
059            }
060            // can't pass root cause since the constructor is not available on JDK 1.3
061            throw new RuntimeException(e.getMessage());
062        }
063    }
064
065    /**
066     * Determine if debugging is enabled based on the
067     * system property "COMMONS_EXEC_DEBUG".
068     *
069     * @return true if debug mode is enabled
070     */
071    public static boolean isDebugEnabled() {
072        final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString());
073        return Boolean.TRUE.toString().equalsIgnoreCase(debug);
074    }
075
076    /**
077     * Determine if lenient mode is enabled.
078     *
079     * @return true if lenient mode is enabled
080     */
081    public static boolean isLenientEnabled() {
082        final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString());
083        return Boolean.TRUE.toString().equalsIgnoreCase(lenient);
084    }
085
086}