From a991ae4e2f688864f3bced52f89db7d88a2ad94a Mon Sep 17 00:00:00 2001 From: Karl Schrab Date: Fri, 16 Aug 2024 16:07:00 +0200 Subject: [PATCH] refactor(output): do not rely on subclasses to call initialization (#403) --- .../ambassador/OutputGeneratorLoader.java | 49 +++++++++++-------- .../generator/file/FileOutputLoader.java | 24 +++++---- .../websocket/WebsocketVisualizerLoader.java | 6 +-- .../ambassador/TestOutputGeneratorLoader.java | 9 +++- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/ambassador/OutputGeneratorLoader.java b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/ambassador/OutputGeneratorLoader.java index f2ceb8a78..f7f6342fc 100644 --- a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/ambassador/OutputGeneratorLoader.java +++ b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/ambassador/OutputGeneratorLoader.java @@ -39,25 +39,6 @@ public abstract class OutputGeneratorLoader { private Collection interactionTypes; private File configurationDirectory; - /** - * this method is called just after a new instance of a derived output generator config was created. - * subclasses should call this method at first, and then proceed with reading custom parameters from the configuration - * - * @param rti the {@link RtiAmbassador} of the federation - * @param config output generator configuration - * @param configurationDirectory output generator configuration directory path - */ - public void initialize(RtiAmbassador rti, HierarchicalConfiguration config, File configurationDirectory) throws Exception { - this.rti = rti; - this.id = ConfigHelper.getId(config); - this.updateInterval = ConfigHelper.getUpdateInterval(config); - this.handleStartTime = ConfigHelper.getHandleStartTime(config); - this.handleEndTime = ConfigHelper.getHandleEndTime(config); - this.interactionTypes = ConfigHelper.getSubscriptions(config); - this.configurationDirectory = configurationDirectory; - } - - /** * Returns the output generator identifier. * @@ -122,7 +103,35 @@ public File getConfigurationDirectory() { } /** - * Factory method for creating actual output generator. + * this method is called just after a new instance of a derived output generator config was created. + * + * @param rti the {@link RtiAmbassador} of the federation + * @param config output generator configuration + * @param configurationDirectory output generator configuration directory path + */ + public final void initialize(RtiAmbassador rti, HierarchicalConfiguration config, File configurationDirectory) throws Exception { + this.rti = rti; + this.id = ConfigHelper.getId(config); + this.updateInterval = ConfigHelper.getUpdateInterval(config); + this.handleStartTime = ConfigHelper.getHandleStartTime(config); + this.handleEndTime = ConfigHelper.getHandleEndTime(config); + this.interactionTypes = ConfigHelper.getSubscriptions(config); + this.configurationDirectory = configurationDirectory; + + configure(config); + } + + /** + * Subclasses implement this method to read additional custom configuration from the + * given {@link HierarchicalConfiguration} tree. + * + * @param config the configuration tree for the output generator + */ + protected abstract void configure(HierarchicalConfiguration config); + + /** + * Factory method for creating actual output generator based on the previously + * read configuration ({@link #configure}). * * @return the actual output generator */ diff --git a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/file/FileOutputLoader.java b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/file/FileOutputLoader.java index 775865b63..0a0b51181 100644 --- a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/file/FileOutputLoader.java +++ b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/file/FileOutputLoader.java @@ -23,7 +23,6 @@ import org.eclipse.mosaic.fed.output.generator.file.write.WriteByFile; import org.eclipse.mosaic.fed.output.generator.file.write.WriteByFileCompress; import org.eclipse.mosaic.fed.output.generator.file.write.WriteByLog; -import org.eclipse.mosaic.rti.api.RtiAmbassador; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.tree.ImmutableNode; @@ -72,6 +71,17 @@ public InteractionFormatter getInteractionFormatter() { return this.interactionFormatter; } + @Override + public void configure(HierarchicalConfiguration config) { + try { + this.writer = this.getWrite(config); + this.interactionFormatter = this.createInteractionFormatter(config); + } catch (Exception e) { + log.error("Exception", e); + throw new RuntimeException("Caused by OutputGenerator " + getId(), e); + } + } + private InteractionFormatter createInteractionFormatter(HierarchicalConfiguration sub) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException { String separatorInput = sub.getString(SEPARATOR); @@ -146,18 +156,6 @@ private Write getWrite(HierarchicalConfiguration sub) throws IOEx return ret; } - @Override - public void initialize(RtiAmbassador rti, HierarchicalConfiguration config, File configurationDirectory) throws Exception { - super.initialize(rti, config, configurationDirectory); - try { - this.writer = this.getWrite(config); - this.interactionFormatter = this.createInteractionFormatter(config); - } catch (Exception e) { - log.error("Exception", e); - throw new Exception("Caused by OutputGenerator " + getId(), e); - } - } - @Override public AbstractOutputGenerator createOutputGenerator() { return new FileOutput(this.writer, this.getInteractionFormatter()); diff --git a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/websocket/WebsocketVisualizerLoader.java b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/websocket/WebsocketVisualizerLoader.java index 97e431b03..620eafdc1 100644 --- a/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/websocket/WebsocketVisualizerLoader.java +++ b/fed/mosaic-output/src/main/java/org/eclipse/mosaic/fed/output/generator/websocket/WebsocketVisualizerLoader.java @@ -17,20 +17,16 @@ import org.eclipse.mosaic.fed.output.ambassador.AbstractOutputGenerator; import org.eclipse.mosaic.fed.output.ambassador.OutputGeneratorLoader; -import org.eclipse.mosaic.rti.api.RtiAmbassador; import org.apache.commons.configuration2.HierarchicalConfiguration; import org.apache.commons.configuration2.tree.ImmutableNode; -import java.io.File; - public class WebsocketVisualizerLoader extends OutputGeneratorLoader { private int port; @Override - public void initialize(RtiAmbassador rti, HierarchicalConfiguration config, File configurationDirectory) throws Exception { - super.initialize(rti, config, configurationDirectory); + public void configure(HierarchicalConfiguration config) { port = config.getInt("port"); } diff --git a/fed/mosaic-output/src/test/java/org/eclipse/mosaic/fed/output/ambassador/TestOutputGeneratorLoader.java b/fed/mosaic-output/src/test/java/org/eclipse/mosaic/fed/output/ambassador/TestOutputGeneratorLoader.java index fae87a36b..d4c790e2d 100644 --- a/fed/mosaic-output/src/test/java/org/eclipse/mosaic/fed/output/ambassador/TestOutputGeneratorLoader.java +++ b/fed/mosaic-output/src/test/java/org/eclipse/mosaic/fed/output/ambassador/TestOutputGeneratorLoader.java @@ -17,14 +17,19 @@ import static org.mockito.Mockito.spy; -import org.eclipse.mosaic.fed.output.ambassador.AbstractOutputGenerator; -import org.eclipse.mosaic.fed.output.ambassador.OutputGeneratorLoader; +import org.apache.commons.configuration2.HierarchicalConfiguration; +import org.apache.commons.configuration2.tree.ImmutableNode; /** * An extension of {@link OutputGeneratorLoader} for testing purposes. */ public class TestOutputGeneratorLoader extends OutputGeneratorLoader { + @Override + protected void configure(HierarchicalConfiguration config) { + // nothing to do + } + @Override public AbstractOutputGenerator createOutputGenerator() { return spy(new AbstractOutputGenerator() {});