| | |
| | | * Portions Copyright 2006-2007 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.util; |
| | | import org.opends.messages.Message; |
| | | |
| | | import static org.opends.server.loggers.debug.DebugLogger.*; |
| | | import org.opends.server.loggers.debug.DebugTracer; |
| | | import org.opends.server.types.ErrorLogCategory; |
| | | import org.opends.server.types.ErrorLogSeverity; |
| | | import org.opends.server.messages.UtilityMessages; |
| | | import org.opends.server.loggers.ErrorLogger; |
| | | |
| | | |
| | | import org.opends.messages.MessageBuilder; |
| | | |
| | | /** |
| | | * This utility class provides static methods that make parameter checking |
| | |
| | | public static boolean ensureNotNull(Object param) |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param == null) throwNull(""); |
| | | if (param == null) throwNull(Message.EMPTY); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | public static boolean ensureNotNull(Object param1, Object param2) |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | | if (param1 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[1])); |
| | | if (param2 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[2])); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | Object param3) |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | | if (param3 == null) throwNull(PARAM_DESCRIPTIONS[3]); |
| | | if (param1 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[1])); |
| | | if (param2 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[2])); |
| | | if (param3 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[3])); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | Object param3, Object param4) |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | | if (param3 == null) throwNull(PARAM_DESCRIPTIONS[3]); |
| | | if (param4 == null) throwNull(PARAM_DESCRIPTIONS[4]); |
| | | if (param1 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[1])); |
| | | if (param2 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[2])); |
| | | if (param3 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[3])); |
| | | if (param4 == null) throwNull(Message.raw(PARAM_DESCRIPTIONS[4])); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (!condition) { |
| | | ensureTrue(condition, ""); |
| | | ensureTrue(condition, Message.EMPTY); |
| | | } |
| | | } |
| | | return true; |
| | |
| | | * |
| | | * @throws AssertionError if condition is false |
| | | */ |
| | | public static boolean ensureTrue(boolean condition, String message) |
| | | public static boolean ensureTrue(boolean condition, Message message) |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (!condition) { |
| | | String fullMessage = generateLineSpecificErrorMessage( |
| | | "The specified condition must be true. " + message); |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append("The specified condition must be true. "); |
| | | mb.append(message); |
| | | Message fullMessage = generateLineSpecificErrorMessage(mb.toMessage()); |
| | | |
| | | logError(fullMessage); |
| | | |
| | |
| | | //////////////////////////////////////////////////////////////////////////// |
| | | |
| | | |
| | | private static String generateLineSpecificErrorMessage(String message) { |
| | | return message + " The error occurred at " + getOriginalCallerLineInfo(); |
| | | private static Message generateLineSpecificErrorMessage(Message message) { |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(message); |
| | | mb.append(" The error occurred at "); |
| | | mb.append(getOriginalCallerLineInfo()); |
| | | return mb.toMessage(); |
| | | } |
| | | |
| | | |
| | | private static void throwNull(String message) |
| | | private static void throwNull(Message message) |
| | | throws AssertionError { |
| | | String fullMessage = generateLineSpecificErrorMessage( |
| | | "The specified parameter must not be null. " + message); |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append("The specified parameter must not be null. "); |
| | | mb.append(message); |
| | | Message fullMessage = generateLineSpecificErrorMessage(mb.toMessage()); |
| | | |
| | | logError(fullMessage); |
| | | |
| | |
| | | |
| | | |
| | | |
| | | private static void logError(String message) { |
| | | private static void logError(Message message) { |
| | | incrementErrorCount(); |
| | | |
| | | String messageWithStack = message + ServerConstants.EOL + getCallingStack(); |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(message); |
| | | mb.append(ServerConstants.EOL); |
| | | mb.append(getCallingStack()); |
| | | Message messageWithStack = mb.toMessage(); |
| | | |
| | | // Log to the debug log. |
| | | if (debugEnabled()) |
| | | { |
| | | TRACER.debugError(messageWithStack); |
| | | TRACER.debugError(messageWithStack.toString()); |
| | | } |
| | | |
| | | // Log to the error log. |
| | | org.opends.server.loggers.ErrorLogger.logError(ErrorLogCategory.CORE_SERVER, |
| | | ErrorLogSeverity.SEVERE_ERROR, |
| | | UtilityMessages.MSGID_VALIDATOR_PRECONDITION_NOT_MET, |
| | | messageWithStack); |
| | | ErrorLogger.logError(messageWithStack); |
| | | } |
| | | |
| | | |