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 * http://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 */
018
019 package org.apache.commons.exec.util;
020
021 /**
022 * Helper classes to provide debugging support.
023 *
024 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
025 */
026 public class DebugUtils
027 {
028 /**
029 * System property to determine how to handle exceptions. When
030 * set to "false" we rethrow the otherwise silently catched
031 * exceptions found in the original code. The default value
032 * is "true"
033 */
034 public static final String COMMONS_EXEC_LENIENT = "org.apache.commons.exec.lenient";
035
036 /**
037 * System property to determine how to dump an exception. When
038 * set to "true" we print any exception to stderr. The default
039 * value is "false"
040 */
041 public static final String COMMONS_EXEC_DEBUG = "org.apache.commons.exec.debug";
042
043 /**
044 * Handle an exception based on the system properties.
045 *
046 * @param msg message describing the problem
047 * @param e an exception being handled
048 */
049 public static void handleException(String msg, Exception e) {
050
051 if(isDebugEnabled()) {
052 System.err.println(msg);
053 e.printStackTrace();
054 }
055
056 if(!isLenientEnabled()) {
057 if(e instanceof RuntimeException) {
058 throw (RuntimeException) e;
059 }
060 else {
061 // can't pass root cause since the constructor is not available on JDK 1.3
062 throw new RuntimeException(e.getMessage());
063 }
064 }
065 }
066
067 /**
068 * Determine if debugging is enabled based on the
069 * system property "COMMONS_EXEC_DEBUG".
070 *
071 * @return true if debug mode is enabled
072 */
073 public static boolean isDebugEnabled() {
074 return "true".equalsIgnoreCase(System.getProperty(COMMONS_EXEC_DEBUG, "false"));
075 }
076
077 /**
078 * Determine if lenient mode is enabled.
079 *
080 * @return true if lenient mode is enabled
081 */
082 public static boolean isLenientEnabled() {
083 return "true".equalsIgnoreCase(System.getProperty(COMMONS_EXEC_LENIENT, "true"));
084 }
085
086 }