From abf508cc6bc91f74c9d4db4f572b0f8179a1d87d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 18 Jun 2018 18:46:23 +0200 Subject: [PATCH] [Minor] Throw exception if policy is missing proper constructor --- .../strolch/policy/DefaultPolicyHandler.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 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 187939906..a9613d094 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 @@ -1,12 +1,12 @@ /* * Copyright 2015 Robert von Burg - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -37,19 +37,19 @@ import li.strolch.utils.helper.XmlHelper; /** *

- * This is the default Strolch {@link PolicyHandler} which implements {@link PolicyDefVisitor}. This - * {@link PolicyHandler} parses a policy configuration file to handle the look-up of {@link KeyPolicyDef} + * This is the default Strolch {@link PolicyHandler} which implements {@link PolicyDefVisitor}. This {@link + * PolicyHandler} parses a policy configuration file to handle the look-up of {@link KeyPolicyDef} *

- * + * * This {@link StrolchComponent} uses two configuration properties: * - * + * * @author Robert von Burg */ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHandler, PolicyDefVisitor { @@ -84,10 +84,12 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand Class clazz = policyDef.accept(this); Constructor constructor = clazz.getConstructor(ComponentContainer.class, StrolchTransaction.class); + if (constructor == null) + throw new IllegalStateException( + "Class" + clazz.getName() + " is missing constructor (ComponentContainer, StrolchTransaction)"); return constructor.newInstance(getContainer(), tx); - } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) { + } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) { throw new StrolchPolicyException( MessageFormat.format("Failed to instantiate policy {0} due to {1}", policyDef, e.getMessage()), e); } @@ -146,9 +148,10 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand // assert API is a Policy if (!StrolchPolicy.class.isAssignableFrom(implClass)) { - throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY - + " configuration for Type=" + type + " Key=" + key + " as " + className - + " is not a " + StrolchPolicy.class.getName()); + throw new StrolchPolicyException( + "Invalid " + StrolchPolicyFileParser.POLICY + " configuration for Type=" + type + + " Key=" + key + " as " + className + " is not a " + StrolchPolicy.class + .getName()); } if (!apiClass.isAssignableFrom(implClass)) { @@ -162,13 +165,15 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand this.classByTypeMap.addElement(type, key, (Class) implClass); } catch (ClassNotFoundException e) { - throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY - + " configuration for Type=" + type + " Key=" + key + " due to " + e.getMessage(), e); + throw new StrolchPolicyException( + "Invalid " + StrolchPolicyFileParser.POLICY + " configuration for Type=" + type + + " Key=" + key + " due to " + e.getMessage(), e); } } } catch (ClassNotFoundException e) { - throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY_TYPE - + " configuration for Type=" + type + " due to " + e.getMessage(), e); + throw new StrolchPolicyException( + "Invalid " + StrolchPolicyFileParser.POLICY_TYPE + " configuration for Type=" + type + + " due to " + e.getMessage(), e); } } }