From 5d47fdb7ec2be8ce9833fc5893fa9fb4a02f9b77 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 30 May 2019 17:15:19 +0200 Subject: [PATCH] [New] Allow to use constructor new StrolchPolicy(tx) --- .../strolch/policy/DefaultPolicyHandler.java | 32 +++++++++++++++++-- .../java/li/strolch/policy/StrolchPolicy.java | 11 +++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java b/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java index e199ab8b6..fe8099752 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java @@ -78,7 +78,10 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand try { Class clazz = policyDef.accept(this); - Constructor constructor = clazz.getConstructor(ComponentContainer.class, StrolchTransaction.class); + @SuppressWarnings("unchecked") + Constructor constructor = (Constructor) getConstructorForPolicy(clazz); + if (constructor.getParameterCount() == 1) + return constructor.newInstance(tx); return constructor.newInstance(getContainer(), tx); } catch (Exception e) { @@ -190,12 +193,12 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand Constructor constructor; try { - constructor = implClass.getConstructor(ComponentContainer.class, StrolchTransaction.class); + constructor = getConstructorForPolicy(implClass); } catch (NoSuchMethodException e) { throw new IllegalStateException( "Invalid " + StrolchPolicyFileParser.POLICY + " configuration for Type=" + type + " Key=" + key - + " as constructor (ComponentContainer, StrolchTransaction) does not exist!"); + + " as constructor (StrolchTransaction) or (ComponentContainer, StrolchTransaction) does not exist!"); } if (Modifier.isAbstract(constructor.getModifiers()) || Modifier @@ -221,4 +224,27 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand } } } + + private Constructor getConstructorForPolicy(Class implClass) throws NoSuchMethodException { + Constructor constructor = null; + Constructor[] constructors = implClass.getConstructors(); + for (Constructor c : constructors) { + Class[] parameterTypes = c.getParameterTypes(); + if (parameterTypes.length == 1 && parameterTypes[0] == StrolchTransaction.class) { + constructor = c; + break; + } + + if (parameterTypes.length == 2 && parameterTypes[0] == ComponentContainer.class + && parameterTypes[1] == StrolchTransaction.class) { + constructor = c; + break; + } + } + + if (constructor == null) + throw new NoSuchMethodException(); + + return constructor; + } } diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/StrolchPolicy.java b/li.strolch.agent/src/main/java/li/strolch/policy/StrolchPolicy.java index 74e656c1a..e2d986840 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/StrolchPolicy.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/StrolchPolicy.java @@ -54,6 +54,17 @@ public abstract class StrolchPolicy { this.tx = tx; } + /** + * Instantiate a new {@link StrolchPolicy} + * + * @param tx + * the transaction for this policy + */ + public StrolchPolicy(StrolchTransaction tx) { + this.container = tx.getContainer(); + this.tx = tx; + } + /** * Allows the concrete {@link Command} implementation access to {@link StrolchComponent StrolchComponents} at * runtime