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
18 package org.apache.commons.exec.util;
19
20 /**
21 * Provides debugging support.
22 */
23 public class DebugUtils {
24
25 /**
26 * System property to determine how to handle exceptions. When set to "false" we rethrow the otherwise silently catched exceptions found in the original
27 * code. The default value is "true"
28 */
29 public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";
30
31 /**
32 * System property to determine how to dump an exception. When set to "true" we print any exception to stderr. The default value is "false"
33 */
34 public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";
35
36 /**
37 * Handles an exception based on the system properties.
38 *
39 * @param msg message describing the problem.
40 * @param e an exception being handled.
41 */
42 public static void handleException(final String msg, final Exception e) {
43 if (isDebugEnabled()) {
44 System.err.println(msg);
45 e.printStackTrace();
46 }
47 if (!isLenientEnabled()) {
48 if (e instanceof RuntimeException) {
49 throw (RuntimeException) e;
50 }
51 throw new RuntimeException(e);
52 }
53 }
54
55 /**
56 * Determines if debugging is enabled based on the system property "COMMONS_EXEC_DEBUG".
57 *
58 * @return true if debug mode is enabled.
59 */
60 public static boolean isDebugEnabled() {
61 final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString());
62 return Boolean.TRUE.toString().equalsIgnoreCase(debug);
63 }
64
65 /**
66 * Determines if lenient mode is enabled.
67 *
68 * @return true if lenient mode is enabled.
69 */
70 public static boolean isLenientEnabled() {
71 final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString());
72 return Boolean.TRUE.toString().equalsIgnoreCase(lenient);
73 }
74
75 /**
76 * Constructs a new instance.
77 *
78 * @deprecated Will be private in the next major version.
79 */
80 @Deprecated
81 public DebugUtils() {
82 // empty
83 }
84 }