    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 *      https://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
    018package org.apache.commons.exec.util;
    019
    020/**
    021 * Provides debugging support.
    022 */
    023public class DebugUtils {
    024
    025    /**
    026     * System property to determine how to handle exceptions. When set to "false" we rethrow the otherwise silently catched exceptions found in the original
    027     * code. The default value is "true"
    028     */
    029    public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";
    030
    031    /**
    032     * System property to determine how to dump an exception. When set to "true" we print any exception to stderr. The default value is "false"
    033     */
    034    public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";
    035
    036    /**
    037     * Handles an exception based on the system properties.
    038     *
    039     * @param msg message describing the problem.
    040     * @param e   an exception being handled.
    041     */
    042    public static void handleException(final String msg, final Exception e) {
    043        if (isDebugEnabled()) {
    044            System.err.println(msg);
    045            e.printStackTrace();
    046        }
    047        if (!isLenientEnabled()) {
    048            if (e instanceof RuntimeException) {
    049                throw (RuntimeException) e;
    050            }
    051            throw new RuntimeException(e);
    052        }
    053    }
    054
    055    /**
    056     * Determines if debugging is enabled based on the system property "COMMONS_EXEC_DEBUG".
    057     *
    058     * @return true if debug mode is enabled.
    059     */
    060    public static boolean isDebugEnabled() {
    061        final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString());
    062        return Boolean.TRUE.toString().equalsIgnoreCase(debug);
    063    }
    064
    065    /**
    066     * Determines if lenient mode is enabled.
    067     *
    068     * @return true if lenient mode is enabled.
    069     */
    070    public static boolean isLenientEnabled() {
    071        final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString());
    072        return Boolean.TRUE.toString().equalsIgnoreCase(lenient);
    073    }
    074
    075    /**
    076     * Constructs a new instance.
    077     *
    078     * @deprecated Will be private in the next major version.
    079     */
    080    @Deprecated
    081    public DebugUtils() {
    082        // empty
    083    }
    084}