summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bentele2021-04-15 14:01:47 +0200
committerManuel Bentele2021-04-15 14:01:47 +0200
commit90d94a479e5af0d884c63643f1266dc0a6596318 (patch)
treea84ee0f2c9bf1bce9abc43cfacdb87cd52592fcf
parentAdd representation of used virtualization systems (diff)
downloadmaster-sync-shared-90d94a479e5af0d884c63643f1266dc0a6596318.tar.gz
master-sync-shared-90d94a479e5af0d884c63643f1266dc0a6596318.tar.xz
master-sync-shared-90d94a479e5af0d884c63643f1266dc0a6596318.zip
Add generic transformation library to transform configs
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java65
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java25
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java39
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java25
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java146
-rw-r--r--src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java43
6 files changed, 343 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java b/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java
new file mode 100644
index 0000000..af0e181
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/Transformation.java
@@ -0,0 +1,65 @@
+package org.openslx.virtualization.configuration.transformation;
+
+/**
+ * Represents a transformation that transforms (alters) a given configuration with specified input
+ * arguments.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ *
+ * @param <T> type of the configuration which will be transformed.
+ * @param <R> type of input arguments for the transformation.
+ */
+public abstract class Transformation<T, R> implements TransformationFunction<T, R>
+{
+ /**
+ * Name of the transformation.
+ */
+ private final String name;
+
+ /**
+ * State of the transformation.
+ */
+ private boolean enabled;
+
+ /**
+ * Creates a transformation.
+ *
+ * @param name comprehensible name for the transformation.
+ */
+ public Transformation( String name )
+ {
+ this.name = name;
+ this.setEnabled( true );
+ }
+
+ /**
+ * Returns the name of the transformation.
+ *
+ * @return name of the transformation.
+ */
+ public String getName()
+ {
+ return this.name;
+ }
+
+ /**
+ * Returns the state of the transformation.
+ *
+ * @return state of the transformation.
+ */
+ public boolean isEnabled()
+ {
+ return this.enabled;
+ }
+
+ /**
+ * Sets the state for the transformation.
+ *
+ * @param enabled state for the transformation.
+ */
+ public void setEnabled( boolean enabled )
+ {
+ this.enabled = enabled;
+ }
+}
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java
new file mode 100644
index 0000000..72f320d
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationException.java
@@ -0,0 +1,25 @@
+package org.openslx.virtualization.configuration.transformation;
+
+/**
+ * An exception of a transformation error.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ */
+public class TransformationException extends Exception
+{
+ /**
+ * Version for serialization.
+ */
+ private static final long serialVersionUID = 7293420658901349154L;
+
+ /**
+ * Creates a transformation exception including an error message.
+ *
+ * @param errorMsg message to describe the exception.
+ */
+ public TransformationException( String errorMsg )
+ {
+ super( errorMsg );
+ }
+}
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java
new file mode 100644
index 0000000..b5be7a0
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationFunction.java
@@ -0,0 +1,39 @@
+package org.openslx.virtualization.configuration.transformation;
+
+/**
+ * Represents a transformation operation that transforms (alters) a given configuration with
+ * specified input arguments and returns no result.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ *
+ * @param <T> type of the configuration which will be transformed.
+ * @param <R> type of input arguments for the transformation.
+ */
+@FunctionalInterface
+public interface TransformationFunction<T, R>
+{
+ /**
+ * Transforms a given configuration with the specified input arguments.
+ *
+ * @param config configuration which will be transformed.
+ * @param args input arguments for the transformation.
+ *
+ * @throws TransformationException transformation of the configuration failed.
+ */
+ public void transform( T config, R args ) throws TransformationException;
+
+ /**
+ * Applies the transformation function {@link #transform(Object, Object)} to the given
+ * configuration and specified input arguments.
+ *
+ * @param config configuration which will be transformed.
+ * @param args input arguments for the transformation.
+ *
+ * @throws TransformationException transformation of the configuration failed.
+ */
+ public default void apply( T config, R args ) throws TransformationException
+ {
+ this.transform( config, args );
+ }
+}
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java
new file mode 100644
index 0000000..4ad36c7
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationGeneric.java
@@ -0,0 +1,25 @@
+package org.openslx.virtualization.configuration.transformation;
+
+/**
+ * Represents a generic transformation that transforms (alters) a given configuration with specified
+ * input arguments. The generic transformation does not depend on any external states of a
+ * virtualizer.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ *
+ * @param <T> type of the configuration which will be transformed.
+ * @param <R> type of input arguments for the transformation.
+ */
+public abstract class TransformationGeneric<T, R> extends Transformation<T, R>
+{
+ /**
+ * Create a generic transformation.
+ *
+ * @param name comprehensible name for the transformation.
+ */
+ public TransformationGeneric( String name )
+ {
+ super( name );
+ }
+}
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java
new file mode 100644
index 0000000..ce0ad96
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationManager.java
@@ -0,0 +1,146 @@
+package org.openslx.virtualization.configuration.transformation;
+
+import java.util.ArrayList;
+
+/**
+ * A transformation manager is a class to manage several transformations and their application.
+ *
+ * Transformations can be registered at the transformation manager. The transformation manager has
+ * the ability to apply all registered transformations on a given configuration and specified input
+ * arguments.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ *
+ * @param <T> type of the configuration which will be transformed by all transformations.
+ * @param <R> type of input arguments for all transformations.
+ */
+public class TransformationManager<T, R>
+{
+ /**
+ * List of registered transformations.
+ */
+ private ArrayList<Transformation<T, R>> transformations;
+
+ /**
+ * Reference to the configuration that will be transformed (altered).
+ */
+ private T config;
+
+ /**
+ * Reference to the input arguments for all registered transformations.
+ */
+ private R args;
+
+ /**
+ * Create a transformation manager.
+ *
+ * @param config configuration which will be transformed.
+ * @param args input arguments for all registered transformations.
+ */
+ public TransformationManager( T config, R args )
+ {
+ this.transformations = new ArrayList<Transformation<T, R>>();
+ this.config = config;
+ this.args = args;
+ }
+
+ /**
+ * Registers and enables a transformation.
+ *
+ * @param transformation existing transformation that will be registered and enabled.
+ */
+ public void register( Transformation<T, R> transformation )
+ {
+ this.register( transformation, true );
+ }
+
+ /**
+ * Registers a transformation and sets its state.
+ *
+ * @param transformation existing transformation that will be registered.
+ * @param enabled state for the existing transformation that will be set.
+ */
+ public void register( Transformation<T, R> transformation, boolean enabled )
+ {
+ transformation.setEnabled( enabled );
+ this.transformations.add( transformation );
+ }
+
+ /**
+ * Registers a transformation function as a new transformation and enables the registered
+ * transformation.
+ *
+ * @param name comprehensible name for the transformation.
+ * @param function transformation operation for the transformation.
+ */
+ public void register( String name, TransformationFunction<T, R> function )
+ {
+ this.register( name, function, true );
+ }
+
+ /**
+ * Registers a transformation function as a new transformation and sets the state of the
+ * registered transformation.
+ *
+ * @param name comprehensible name for the transformation.
+ * @param function transformation operation for the transformation.
+ * @param enabled state for the transformation.
+ */
+ public void register( String name, TransformationFunction<T, R> function, boolean enabled )
+ {
+ this.register( new Transformation<T, R>( name ) {
+ @Override
+ public void transform( T document, R args ) throws TransformationException
+ {
+ function.apply( document, args );
+ }
+ }, enabled );
+ }
+
+ /**
+ * Applies all registered transformations, whose state is set to <code>enabled</code>, to the
+ * referenced configuration and input arguments.
+ *
+ * @throws TransformationException transformation of the configuration failed.
+ */
+ public void transform() throws TransformationException
+ {
+ for ( Transformation<T, R> transformation : this.transformations ) {
+ try {
+ transformation.apply( this.config, this.args );
+ } catch ( TransformationException e ) {
+ final String errorMsg = new String(
+ "Error in configuration filter '" + transformation.getName() + "':" + e.getLocalizedMessage() );
+ throw new TransformationException( errorMsg );
+ }
+ }
+ }
+
+ /**
+ * Returns a human readable summary of all registered transformations.
+ *
+ * @return human readable summary of all registered transformations.
+ */
+ private String showTransformations()
+ {
+ String transformationSummary = new String();
+ final int maxFilterNumCharacters = ( this.transformations.size() + 1 ) / 10;
+
+ for ( int i = 0; i < this.transformations.size(); i++ ) {
+ final Transformation<T, R> transformation = this.transformations.get( i );
+ final String paddedNumber = String.format( "%-" + maxFilterNumCharacters + "s", i + 1 );
+ final String transformationState = transformation.isEnabled() ? "[ active ]" : "[inactive]";
+ transformationSummary += paddedNumber + ": " + transformationState + " ";
+ transformationSummary += transformation.getName() + System.lineSeparator();
+ }
+
+ return transformationSummary;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.showTransformations();
+ }
+}
diff --git a/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java
new file mode 100644
index 0000000..5b13f45
--- /dev/null
+++ b/src/main/java/org/openslx/virtualization/configuration/transformation/TransformationSpecific.java
@@ -0,0 +1,43 @@
+package org.openslx.virtualization.configuration.transformation;
+
+/**
+ * Represents a specific transformation that transforms (alters) a given configuration with
+ * specified input arguments. The specific transformation depends on external states of a
+ * specified virtualizer.
+ *
+ * @author Manuel Bentele
+ * @version 1.0
+ *
+ * @param <T> type of the configuration which will be transformed.
+ * @param <R> type of input arguments for the transformation.
+ * @param <H> type of the external input source.
+ */
+public abstract class TransformationSpecific<T, R, H> extends Transformation<T, R>
+{
+ /**
+ * Reference to virtualizer to query external states.
+ */
+ private final H virtualizer;
+
+ /**
+ * Create a specific transformation.
+ *
+ * @param name comprehensible name for the transformation.
+ */
+ public TransformationSpecific( String name, H virtualizer )
+ {
+ super( name );
+
+ this.virtualizer = virtualizer;
+ }
+
+ /**
+ * Returns the referenced virtualizer of the transformation.
+ *
+ * @return referenced virtualizer of the transformation.
+ */
+ public H getVirtualizer()
+ {
+ return this.virtualizer;
+ }
+}