Skip to content

Commit

Permalink
fix sonarcloud complaints about code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Akretsch committed Mar 6, 2024
1 parent 092b0ef commit 727069d
Showing 1 changed file with 52 additions and 74 deletions.
126 changes: 52 additions & 74 deletions src/main/java/com/siemens/pki/cmpracomponent/util/ConfigLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,28 @@
*/
public class ConfigLogger {

private static final String EXCEPTION_CALLING = "exception calling ";

private static final String DYNAMIC_CONFIGURATION_EXCEPTION = "DynamicConfigurationException: ";

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLogger.class);

private static <T> void doInnerLogging(
String interfaceName, String accessFunctionName, String certProfile, Integer bodyType, T ret) {
if (LOGGER.isDebugEnabled()) {
final String logMsg = "call {}({}, {}) for \"{}\" -> {}";
if (ret instanceof byte[]) {
LOGGER.debug(
logMsg,
accessFunctionName,
certProfile,
typeToString(bodyType),
interfaceName,
Hex.toHexString((byte[]) ret));
} else if (ret instanceof Collection<?>) {
LOGGER.debug(
logMsg,
accessFunctionName,
certProfile,
typeToString(bodyType),
interfaceName,
Arrays.toString(((Collection<?>) ret).toArray()));
} else if (ret != null && ret.getClass().isArray()) {
LOGGER.debug(
logMsg,
accessFunctionName,
certProfile,
typeToString(bodyType),
interfaceName,
Arrays.toString((Object[]) ret));
} else {
LOGGER.debug(logMsg, accessFunctionName, certProfile, typeToString(bodyType), interfaceName, ret);
}
LOGGER.debug(
"call {}({}, {}) for \"{}\" -> {}",
accessFunctionName,
certProfile,
typeToString(bodyType),
interfaceName,
retValueToString(ret));
}
}

private static <T> void doInnerLogging(String interfaceName, String accessFunctionName, T ret) {
if (LOGGER.isDebugEnabled()) {
final String logMsg = "call {} for \"{}\" -> {}";
if (ret instanceof byte[]) {
LOGGER.debug(logMsg, accessFunctionName, interfaceName, Hex.toHexString((byte[]) ret));
} else if (ret instanceof Collection<?>) {
LOGGER.debug(
logMsg, accessFunctionName, interfaceName, Arrays.toString(((Collection<?>) ret).toArray()));
} else if (ret != null && ret.getClass().isArray()) {
LOGGER.debug(logMsg, accessFunctionName, interfaceName, Arrays.toString((Object[]) ret));
} else {
LOGGER.debug(logMsg, accessFunctionName, interfaceName, ret);
}
LOGGER.debug("call {} for \"{}\" -> {}", accessFunctionName, interfaceName, retValueToString(ret));
}
}

Expand All @@ -90,10 +62,10 @@ private static <T> void doInnerLogging(String interfaceName, String accessFuncti
* @param interfaceName related interface of configuration
* @param accessFunctionName name of access function
* @param accessFunction the access function
* @param certProfile certificate profile extracted from the CMP request header
* generalInfo field or <code>null</code> if no certificate
* profile was specified
* @param bodyType request/response PKI Message Body type
* @param certProfile certificate profile extracted from the CMP request
* header generalInfo field or <code>null</code> if no
* certificate profile was specified
* @param bodyType request/response PKI Message Body type
* @return return value of access function
* @throws NullPointerException if return value was <code>null</code>
*/
Expand All @@ -107,14 +79,9 @@ public static <T> T log(
try {
ret = accessFunction.apply(certProfile, bodyType);
} catch (final Throwable ex) {
final String errorMsg = "exception calling " + accessFunctionName + "(" + certProfile + ", "
final String errorMsg = EXCEPTION_CALLING + accessFunctionName + "(" + certProfile + ", "
+ typeToString(bodyType) + ") for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
logThrowableAndCause(ex, errorMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, certProfile, bodyType, ret);
Expand Down Expand Up @@ -142,13 +109,8 @@ public static <T> T log(String interfaceName, String accessFunctionName, Supplie
try {
ret = accessFunction.get();
} catch (final Throwable ex) {
final String errorMsg = "exception calling " + accessFunctionName + " for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
final String errorMsg = EXCEPTION_CALLING + accessFunctionName + " for \"" + interfaceName + "\"";
logThrowableAndCause(ex, errorMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, ret);
Expand All @@ -168,10 +130,10 @@ public static <T> T log(String interfaceName, String accessFunctionName, Supplie
* @param interfaceName related interface of configuration
* @param accessFunctionName name of access function
* @param accessFunction the access function
* @param certProfile certificate profile extracted from the CMP request header
* generalInfo field or <code>null</code> if no certificate
* profile was specified
* @param bodyType request/response PKI Message Body type
* @param certProfile certificate profile extracted from the CMP request
* header generalInfo field or <code>null</code> if no
* certificate profile was specified
* @param bodyType request/response PKI Message Body type
* @return return value of access function
* @throws NullPointerException if return value of access function was
* <code>null</code>
Expand All @@ -186,14 +148,9 @@ public static <T> T logOptional(
try {
ret = accessFunction.apply(certProfile, bodyType);
} catch (final Throwable ex) {
final String errorMsg = "exception calling " + accessFunctionName + "(" + certProfile + ", "
final String errorMsg = EXCEPTION_CALLING + accessFunctionName + "(" + certProfile + ", "
+ typeToString(bodyType) + ") for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
logThrowableAndCause(ex, errorMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, certProfile, bodyType, ret);
Expand All @@ -217,24 +174,45 @@ public static <T> T logOptional(String interfaceName, String accessFunctionName,
ret = accessFunction.get();
} catch (final Throwable ex) {
final String errorMsg = "exception while calling " + accessFunctionName + " for \"" + interfaceName + "\"";
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
logThrowableAndCause(ex, errorMsg);
throw new RuntimeException(DYNAMIC_CONFIGURATION_EXCEPTION + errorMsg, ex);
}
doInnerLogging(interfaceName, accessFunctionName, ret);
return ret;
}

private static void logThrowableAndCause(final Throwable ex, final String errorMsg) {
LOGGER.error(errorMsg, ex);
Throwable cause = ex.getCause();
while (cause != null) {
LOGGER.error("cause", cause);
cause = cause.getCause();
}
}

private static String retValueToString(Object ret) {
if (ret == null) {
return "<null>";
}
if (ret instanceof byte[]) {
return Hex.toHexString((byte[]) ret);
}
if (ret instanceof Collection<?>) {
return Arrays.toString(((Collection<?>) ret).toArray());
}
if (ret != null && ret.getClass().isArray()) {
return Arrays.toString((Object[]) ret);
}
return ret.toString();
}

private static String typeToString(Integer bodyType) {
if (bodyType == null) {
return "<null>";
}
return MessageDumper.msgTypeAsString(bodyType) + "(" + bodyType + ")";
}

// utility class
private ConfigLogger() {}
}

0 comments on commit 727069d

Please sign in to comment.