1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.commons.exec.util;
21
22 /**
23 * Provides debugging support.
24 */
25 public class DebugUtils {
26
27 /**
28 * System property to determine how to handle exceptions. When set to "false" we rethrow the otherwise silently catched exceptions found in the original
29 * code. The default value is "true"
30 */
31 public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";
32
33 /**
34 * System property to determine how to dump an exception. When set to "true" we print any exception to stderr. The default value is "false"
35 */
36 public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";
37
38 /**
39 * Handles an exception based on the system properties.
40 *
41 * @param msg message describing the problem.
42 * @param e an exception being handled.
43 */
44 public static void handleException(final String msg, final Exception e) {
45 if (isDebugEnabled()) {
46 System.err.println(msg);
47 e.printStackTrace();
48 }
49 if (!isLenientEnabled()) {
50 if (e instanceof RuntimeException) {
51 throw (RuntimeException) e;
52 }
53 throw new RuntimeException(e);
54 }
55 }
56
57 /**
58 * Determines if debugging is enabled based on the system property "COMMONS_EXEC_DEBUG".
59 *
60 * @return true if debug mode is enabled.
61 */
62 public static boolean isDebugEnabled() {
63 final String debug = System.getProperty(COMMONS_EXEC_DEBUG, Boolean.FALSE.toString());
64 return Boolean.TRUE.toString().equalsIgnoreCase(debug);
65 }
66
67 /**
68 * Determines if lenient mode is enabled.
69 *
70 * @return true if lenient mode is enabled.
71 */
72 public static boolean isLenientEnabled() {
73 final String lenient = System.getProperty(COMMONS_EXEC_LENIENT, Boolean.TRUE.toString());
74 return Boolean.TRUE.toString().equalsIgnoreCase(lenient);
75 }
76
77 /**
78 * Constructs a new instance.
79 *
80 * @deprecated Will be private in the next major version.
81 */
82 @Deprecated
83 public DebugUtils() {
84 // empty
85 }
86 }