[New] Allow to use constructor new StrolchPolicy(tx)
This commit is contained in:
parent
c87b3c3719
commit
5d47fdb7ec
|
@ -78,7 +78,10 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand
|
|||
try {
|
||||
|
||||
Class<T> clazz = policyDef.accept(this);
|
||||
Constructor<T> constructor = clazz.getConstructor(ComponentContainer.class, StrolchTransaction.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
Constructor<T> constructor = (Constructor<T>) 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue