Revert "[Minor] Throw exception if policy is missing proper constructor"

This reverts commit abf508c
This commit is contained in:
Robert von Burg 2018-06-18 18:50:14 +02:00
parent abf508cc6b
commit 0c6a8b9ba0
1 changed files with 21 additions and 26 deletions

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch> * Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -37,19 +37,19 @@ import li.strolch.utils.helper.XmlHelper;
/** /**
* <p> * <p>
* This is the default Strolch {@link PolicyHandler} which implements {@link PolicyDefVisitor}. This {@link * This is the default Strolch {@link PolicyHandler} which implements {@link PolicyDefVisitor}. This
* PolicyHandler} parses a policy configuration file to handle the look-up of {@link KeyPolicyDef} * {@link PolicyHandler} parses a policy configuration file to handle the look-up of {@link KeyPolicyDef}
* </p> * </p>
* *
* This {@link StrolchComponent} uses two configuration properties: * This {@link StrolchComponent} uses two configuration properties:
* <ul> * <ul>
* <li>{@link #PROP_READ_POLICY_FILE} - Default is false. If false then no configuration file is * <li>{@link #PROP_READ_POLICY_FILE} - Default is false. If false then no configuration file is read. Useful if all
* read. Useful if all policy definitions are {@link JavaPolicyDef}</li> * policy definitions are {@link JavaPolicyDef}</li>
* <li>{@link #PROP_POLICY_CONFIG} - Default is {@link #DEF_STROLCH_POLICIES_XML}. If {@link * <li>{@link #PROP_POLICY_CONFIG} - Default is {@link #DEF_STROLCH_POLICIES_XML}. If {@link #PROP_READ_POLICY_FILE} is
* #PROP_READ_POLICY_FILE} is true, then this property is used to determine which configuration file to parse for the * true, then this property is used to determine which configuration file to parse for the policy key mappings for
* policy key mappings for {@link KeyPolicyDef}</li> * {@link KeyPolicyDef}</li>
* </ul> * </ul>
* *
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*/ */
public class DefaultPolicyHandler extends StrolchComponent implements PolicyHandler, PolicyDefVisitor { public class DefaultPolicyHandler extends StrolchComponent implements PolicyHandler, PolicyDefVisitor {
@ -84,12 +84,10 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand
Class<T> clazz = policyDef.accept(this); Class<T> clazz = policyDef.accept(this);
Constructor<T> constructor = clazz.getConstructor(ComponentContainer.class, StrolchTransaction.class); Constructor<T> 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); 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( throw new StrolchPolicyException(
MessageFormat.format("Failed to instantiate policy {0} due to {1}", policyDef, e.getMessage()), e); MessageFormat.format("Failed to instantiate policy {0} due to {1}", policyDef, e.getMessage()), e);
} }
@ -148,10 +146,9 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand
// assert API is a Policy // assert API is a Policy
if (!StrolchPolicy.class.isAssignableFrom(implClass)) { if (!StrolchPolicy.class.isAssignableFrom(implClass)) {
throw new StrolchPolicyException( throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY
"Invalid " + StrolchPolicyFileParser.POLICY + " configuration for Type=" + type + " configuration for Type=" + type + " Key=" + key + " as " + className
+ " Key=" + key + " as " + className + " is not a " + StrolchPolicy.class + " is not a " + StrolchPolicy.class.getName());
.getName());
} }
if (!apiClass.isAssignableFrom(implClass)) { if (!apiClass.isAssignableFrom(implClass)) {
@ -165,15 +162,13 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand
this.classByTypeMap.addElement(type, key, (Class<? extends StrolchPolicy>) implClass); this.classByTypeMap.addElement(type, key, (Class<? extends StrolchPolicy>) implClass);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new StrolchPolicyException( throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY
"Invalid " + StrolchPolicyFileParser.POLICY + " configuration for Type=" + type + " configuration for Type=" + type + " Key=" + key + " due to " + e.getMessage(), e);
+ " Key=" + key + " due to " + e.getMessage(), e);
} }
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new StrolchPolicyException( throw new StrolchPolicyException("Invalid " + StrolchPolicyFileParser.POLICY_TYPE
"Invalid " + StrolchPolicyFileParser.POLICY_TYPE + " configuration for Type=" + type + " configuration for Type=" + type + " due to " + e.getMessage(), e);
+ " due to " + e.getMessage(), e);
} }
} }
} }