Skip to content

Commit

Permalink
Make abstractconfigurable ready to be used in serializable classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Dec 5, 2024
1 parent d516313 commit f2f20c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* Licensed under MIT 2022-2024. */
package edu.kit.kastel.mcse.ardoco.core.configuration;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serial;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
Expand All @@ -14,20 +10,20 @@
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.kit.kastel.mcse.ardoco.core.architecture.Deterministic;

@Deterministic
public abstract class AbstractConfigurable implements IConfigurable {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

public static final String CLASS_ATTRIBUTE_CONNECTOR = "::";
public static final String KEY_VALUE_CONNECTOR = "=";
public static final String LIST_SEPARATOR = ",";

@SuppressWarnings("java:S2065") // The logger is used in the subclasses that are serializable
private transient Logger logger;

private SortedMap<String, String> lastAppliedConfiguration = new TreeMap<>();

@Override
Expand All @@ -50,7 +46,7 @@ private void applyConfiguration(SortedMap<String, String> additionalConfiguratio
}

if (currentClassInHierarchy.getAnnotation(NoConfiguration.class) != null) {
this.logger.debug("Skipping configuration for class {}", currentClassInHierarchy.getSimpleName());
this.getLogger().debug("Skipping configuration for class {}", currentClassInHierarchy.getSimpleName());
return;
}

Expand Down Expand Up @@ -101,7 +97,7 @@ private void setValue(Field field, String value) {
field.setAccessible(true);
field.set(this, parsedValue);
} catch (Exception e) {
this.logger.error(e.getMessage(), e);
this.getLogger().error(e.getMessage(), e);
}
}

Expand Down Expand Up @@ -131,20 +127,10 @@ private Object parse(Field field, Class<?> fieldsClass, String value) {
throw new IllegalArgumentException("Could not find a parse method for fields of type: " + fieldsClass);
}

@Serial
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
}

@Serial
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
try {
var loggerField = Arrays.stream(FieldUtils.getAllFields(this.getClass())).filter(f -> f.getName().equals("logger")).findFirst().orElseThrow();
loggerField.setAccessible(true);
loggerField.set(this, LoggerFactory.getLogger(this.getClass()));
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
protected final Logger getLogger() {
if (this.logger == null) {
this.logger = LoggerFactory.getLogger(this.getClass());
}
return this.logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public boolean wasExecuted() {
public void process() {
this.preparePipelineSteps();
for (var pipelineStep : this.pipelineSteps) {
this.logger.info("Starting {} - {}", this.getId(), pipelineStep.getId());
this.getLogger().info("Starting {} - {}", this.getId(), pipelineStep.getId());
var start = Instant.now();

pipelineStep.run();

if (this.logger.isInfoEnabled()) {
if (this.getLogger().isInfoEnabled()) {
var end = Instant.now();
var duration = Duration.between(start, end);
long minutesPart = duration.toMinutes();
Expand All @@ -92,7 +92,7 @@ public void process() {
durationString = String.format("%01d.%03d s", secondsPart, millisPart);
}

this.logger.info("Finished {} - {} in {}", this.getId(), pipelineStep.getId(), durationString);
this.getLogger().info("Finished {} - {} in {}", this.getId(), pipelineStep.getId(), durationString);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ArDoCoResult runAndSave(File outputDir) {
classLogger.info("Starting {}", this.projectName);

if (!this.hasPipelineSteps()) {
this.logger.error("Pipeline has not been defined and initialized beforehand. Aborting!");
this.getLogger().error("Pipeline has not been defined and initialized beforehand. Aborting!");
return null;
}

Expand All @@ -79,7 +79,7 @@ public ArDoCoResult runAndSave(File outputDir) {
ArDoCoResult arDoCoResult = new ArDoCoResult(this.getDataRepository());
saveOutput(this.projectName, outputDir, arDoCoResult);

if (this.logger.isInfoEnabled()) {
if (this.getLogger().isInfoEnabled()) {
var duration = Duration.between(startTime, endTime);
long minutesPart = duration.toMinutes();
int secondsPart = duration.toSecondsPart();
Expand Down

0 comments on commit f2f20c3

Please sign in to comment.