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