DebugUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.commons.exec.util;

  18. /**
  19.  * Provides debugging support.
  20.  */
  21. public class DebugUtils {

  22.     /**
  23.      * System property to determine how to handle exceptions. When set to "false" we rethrow the otherwise silently catched exceptions found in the original
  24.      * code. The default value is "true"
  25.      */
  26.     public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";

  27.     /**
  28.      * System property to determine how to dump an exception. When set to "true" we print any exception to stderr. The default value is "false"
  29.      */
  30.     public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";

  31.     /**
  32.      * Handles an exception based on the system properties.
  33.      *
  34.      * @param msg message describing the problem.
  35.      * @param e   an exception being handled.
  36.      */
  37.     public static void handleException(final String msg, final Exception e) {
  38.         if (isDebugEnabled()) {
  39.             System.err.println(msg);
  40.             e.printStackTrace();
  41.         }
  42.         if (!isLenientEnabled()) {
  43.             if (e instanceof RuntimeException) {
  44.                 throw (RuntimeException) e;
  45.             }
  46.             throw new RuntimeException(e);
  47.         }
  48.     }

  49.     /**
  50.      * Determines if debugging is enabled based on the system property "COMMONS_EXEC_DEBUG".
  51.      *
  52.      * @return true if debug mode is enabled.
  53.      */
  54.     public static boolean isDebugEnabled() {
  55.         final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString());
  56.         return Boolean.TRUE.toString().equalsIgnoreCase(debug);
  57.     }

  58.     /**
  59.      * Determines if lenient mode is enabled.
  60.      *
  61.      * @return true if lenient mode is enabled.
  62.      */
  63.     public static boolean isLenientEnabled() {
  64.         final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString());
  65.         return Boolean.TRUE.toString().equalsIgnoreCase(lenient);
  66.     }

  67.     /**
  68.      * Constructs a new instance.
  69.      *
  70.      * @deprecated Will be private in the next major version.
  71.      */
  72.     @Deprecated
  73.     public DebugUtils() {
  74.         // empty
  75.     }
  76. }