Skip to content

Commit

Permalink
refactor(output): do not rely on subclasses to call initialization (#403
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kschrab authored Aug 16, 2024
1 parent 271ef0d commit a991ae4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ public abstract class OutputGeneratorLoader {
private Collection<String> 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<ImmutableNode> 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.
*
Expand Down Expand Up @@ -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<ImmutableNode> 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<ImmutableNode> config);

/**
* Factory method for creating actual output generator based on the previously
* read configuration ({@link #configure}).
*
* @return the actual output generator
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +71,17 @@ public InteractionFormatter getInteractionFormatter() {
return this.interactionFormatter;
}

@Override
public void configure(HierarchicalConfiguration<ImmutableNode> 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<ImmutableNode> sub)
throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException {
String separatorInput = sub.getString(SEPARATOR);
Expand Down Expand Up @@ -146,18 +156,6 @@ private Write getWrite(HierarchicalConfiguration<ImmutableNode> sub) throws IOEx
return ret;
}

@Override
public void initialize(RtiAmbassador rti, HierarchicalConfiguration<ImmutableNode> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImmutableNode> config, File configurationDirectory) throws Exception {
super.initialize(rti, config, configurationDirectory);
public void configure(HierarchicalConfiguration<ImmutableNode> config) {
port = config.getInt("port");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImmutableNode> config) {
// nothing to do
}

@Override
public AbstractOutputGenerator createOutputGenerator() {
return spy(new AbstractOutputGenerator() {});
Expand Down

0 comments on commit a991ae4

Please sign in to comment.