NIHVIVO-3628 Clean up the logic that instantiates the postprocessor. Log a more specific error message if there is a problem.
This commit is contained in:
parent
28c06329f7
commit
1f96d551f5
2 changed files with 56 additions and 45 deletions
|
@ -338,9 +338,9 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
||||||
/* NB The default post-processor is not the same as the post-processor for the default view. The latter
|
/* NB The default post-processor is not the same as the post-processor for the default view. The latter
|
||||||
* actually defines its own post-processor, whereas the default post-processor is used for custom views
|
* actually defines its own post-processor, whereas the default post-processor is used for custom views
|
||||||
* that don't define a post-processor, to ensure that the standard post-processing applies.
|
* that don't define a post-processor, to ensure that the standard post-processing applies.
|
||||||
|
*
|
||||||
|
* edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostProcessor
|
||||||
*/
|
*/
|
||||||
private static final String DEFAULT_POSTPROCESSOR =
|
|
||||||
"edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostProcessor";
|
|
||||||
|
|
||||||
private boolean isDefaultConfig;
|
private boolean isDefaultConfig;
|
||||||
private Set<String> constructQueries;
|
private Set<String> constructQueries;
|
||||||
|
@ -440,33 +440,48 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
||||||
constructQueries = configFileContents.getConstructQueries();
|
constructQueries = configFileContents.getConstructQueries();
|
||||||
|
|
||||||
String postprocessorName = configFileContents.getPostprocessorName();
|
String postprocessorName = configFileContents.getPostprocessorName();
|
||||||
|
postprocessor = getPostProcessor(postprocessorName, ObjectPropertyTemplateModel.this, wdf, configFilePath);
|
||||||
if (StringUtils.isBlank(postprocessorName)) {
|
|
||||||
log.debug("No postprocessor specified for property "
|
|
||||||
+ propertyUri + ". Using default postprocessor.");
|
|
||||||
postprocessorName = DEFAULT_POSTPROCESSOR;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
getPostProcessor(postprocessorName, wdf);
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (!postprocessorName.equals(DEFAULT_POSTPROCESSOR)) {
|
|
||||||
log.debug("Cannot find postprocessor specified for property "
|
|
||||||
+ propertyUri
|
|
||||||
+ ". Using default postprocessor.");
|
|
||||||
postprocessorName = DEFAULT_POSTPROCESSOR;
|
|
||||||
getPostProcessor(postprocessorName, wdf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error processing config file " + configFilePath, e);
|
log.error("Error processing config file " + configFilePath, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getPostProcessor(String name, WebappDaoFactory wdf) throws Exception {
|
private ObjectPropertyDataPostProcessor getPostProcessor(
|
||||||
Class<?> postprocessorClass = Class.forName(name);
|
String className,
|
||||||
Constructor<?> constructor = postprocessorClass.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
ObjectPropertyTemplateModel optm,
|
||||||
postprocessor = (ObjectPropertyDataPostProcessor) constructor.newInstance(ObjectPropertyTemplateModel.this, wdf);
|
WebappDaoFactory wdf, String configFilePath) {
|
||||||
|
try {
|
||||||
|
if (StringUtils.isBlank(className)) {
|
||||||
|
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> clazz = Class.forName(className);
|
||||||
|
Constructor<?> constructor = clazz.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
||||||
|
return (ObjectPropertyDataPostProcessor) constructor.newInstance(optm, wdf);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
log.error("Error processing config file '" + configFilePath
|
||||||
|
+ "': can't load postprocessor class '" + className
|
||||||
|
+ "'. " + "Using default postprocessor.", e);
|
||||||
|
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
log.error("Error processing config file '" + configFilePath
|
||||||
|
+ "': postprocessor class '" + className
|
||||||
|
+ "' does not have a constructor that takes "
|
||||||
|
+ "ObjectPropertyTemplateModel and WebappDaoFactory. "
|
||||||
|
+ "Using default postprocessor.", e);
|
||||||
|
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||||
|
} catch (ClassCastException e) {
|
||||||
|
log.error("Error processing config file '" + configFilePath
|
||||||
|
+ "': postprocessor class '" + className + "' does "
|
||||||
|
+ "not implement ObjectPropertyDataPostProcessor. "
|
||||||
|
+ "Using default postprocessor.", e);
|
||||||
|
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error processing config file '" + configFilePath
|
||||||
|
+ "': can't create postprocessor instance of class '"
|
||||||
|
+ className + "'. " + "Using default postprocessor.", e);
|
||||||
|
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getConfigFilePath(String filename) {
|
private String getConfigFilePath(String filename) {
|
||||||
|
|
|
@ -281,8 +281,6 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void templateNodeIsEmpty() throws InvalidConfigurationException {
|
public void templateNodeIsEmpty() throws InvalidConfigurationException {
|
||||||
// TODO fix this so it doesn't throw a NullPointerException - use
|
|
||||||
// textValue() or something
|
|
||||||
captureLogsFromOPTM();
|
captureLogsFromOPTM();
|
||||||
|
|
||||||
op = buildOperation("templateNodeIsEmpty");
|
op = buildOperation("templateNodeIsEmpty");
|
||||||
|
@ -403,13 +401,9 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void postProcessorNameEmpty() throws InvalidConfigurationException {
|
public void postProcessorNameEmpty() throws InvalidConfigurationException {
|
||||||
captureLogsFromOPTM();
|
|
||||||
|
|
||||||
op = buildOperation("postProcessorNameEmpty");
|
op = buildOperation("postProcessorNameEmpty");
|
||||||
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
||||||
|
|
||||||
// TODO This should not cause an error. If it did, it should not swallow
|
|
||||||
// the exception. It should use the default PP.
|
|
||||||
assertPostProcessorClass("pp name empty",
|
assertPostProcessorClass("pp name empty",
|
||||||
DefaultObjectPropertyDataPostProcessor.class);
|
DefaultObjectPropertyDataPostProcessor.class);
|
||||||
}
|
}
|
||||||
|
@ -418,14 +412,12 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
public void postProcessorClassNotFound()
|
public void postProcessorClassNotFound()
|
||||||
throws InvalidConfigurationException {
|
throws InvalidConfigurationException {
|
||||||
captureLogsFromOPTM();
|
captureLogsFromOPTM();
|
||||||
setLoggerLevel(ObjectPropertyTemplateModel.class, Level.DEBUG);
|
|
||||||
|
|
||||||
op = buildOperation("postProcessorClassNotFound");
|
op = buildOperation("postProcessorClassNotFound");
|
||||||
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
||||||
|
|
||||||
// TODO this should log an error.
|
|
||||||
assertLogMessagesContains("pp class not found",
|
assertLogMessagesContains("pp class not found",
|
||||||
"Cannot find postprocessor specified");
|
"java.lang.ClassNotFoundException");
|
||||||
assertPostProcessorClass("pp class not found",
|
assertPostProcessorClass("pp class not found",
|
||||||
DefaultObjectPropertyDataPostProcessor.class);
|
DefaultObjectPropertyDataPostProcessor.class);
|
||||||
}
|
}
|
||||||
|
@ -434,14 +426,12 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
public void postProcessorClassIsNotSuitable()
|
public void postProcessorClassIsNotSuitable()
|
||||||
throws InvalidConfigurationException {
|
throws InvalidConfigurationException {
|
||||||
captureLogsFromOPTM();
|
captureLogsFromOPTM();
|
||||||
setLoggerLevel(ObjectPropertyTemplateModel.class, Level.DEBUG);
|
|
||||||
|
|
||||||
op = buildOperation("postProcessorClassNotSuitable");
|
op = buildOperation("postProcessorClassNotSuitable");
|
||||||
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
||||||
|
|
||||||
// TODO this should log an error.
|
|
||||||
assertLogMessagesContains("pp doesn't implement required interface",
|
assertLogMessagesContains("pp doesn't implement required interface",
|
||||||
"Cannot find postprocessor specified");
|
"java.lang.ClassCastException");
|
||||||
assertPostProcessorClass("pp doesn't implement required interface",
|
assertPostProcessorClass("pp doesn't implement required interface",
|
||||||
DefaultObjectPropertyDataPostProcessor.class);
|
DefaultObjectPropertyDataPostProcessor.class);
|
||||||
}
|
}
|
||||||
|
@ -450,14 +440,12 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
public void postProcessorClassHasWrongConstructor()
|
public void postProcessorClassHasWrongConstructor()
|
||||||
throws InvalidConfigurationException {
|
throws InvalidConfigurationException {
|
||||||
captureLogsFromOPTM();
|
captureLogsFromOPTM();
|
||||||
setLoggerLevel(ObjectPropertyTemplateModel.class, Level.DEBUG);
|
|
||||||
|
|
||||||
op = buildOperation("postProcessorWrongConstructor");
|
op = buildOperation("postProcessorWrongConstructor");
|
||||||
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
||||||
|
|
||||||
// TODO this should log an error.
|
|
||||||
assertLogMessagesContains("pp has wrong constructor",
|
assertLogMessagesContains("pp has wrong constructor",
|
||||||
"Cannot find postprocessor specified");
|
"java.lang.NoSuchMethodException");
|
||||||
assertPostProcessorClass("pp has wrong constructor",
|
assertPostProcessorClass("pp has wrong constructor",
|
||||||
DefaultObjectPropertyDataPostProcessor.class);
|
DefaultObjectPropertyDataPostProcessor.class);
|
||||||
}
|
}
|
||||||
|
@ -466,14 +454,12 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
public void postProcessorConstructorThrowsAnException()
|
public void postProcessorConstructorThrowsAnException()
|
||||||
throws InvalidConfigurationException {
|
throws InvalidConfigurationException {
|
||||||
captureLogsFromOPTM();
|
captureLogsFromOPTM();
|
||||||
setLoggerLevel(ObjectPropertyTemplateModel.class, Level.DEBUG);
|
|
||||||
|
|
||||||
op = buildOperation("postProcessorConstructorThrowsException");
|
op = buildOperation("postProcessorConstructorThrowsException");
|
||||||
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
optm = new NonCollatingOPTM(op, subject, vreq, false);
|
||||||
|
|
||||||
// TODO this should log an error.
|
|
||||||
assertLogMessagesContains("pp throws an exception",
|
assertLogMessagesContains("pp throws an exception",
|
||||||
"Cannot find postprocessor specified");
|
"java.lang.reflect.InvocationTargetException");
|
||||||
assertPostProcessorClass("pp throws an exception",
|
assertPostProcessorClass("pp throws an exception",
|
||||||
DefaultObjectPropertyDataPostProcessor.class);
|
DefaultObjectPropertyDataPostProcessor.class);
|
||||||
}
|
}
|
||||||
|
@ -636,8 +622,18 @@ public class ObjectPropertyTemplateModel_PropertyListConfigTest extends
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Does not implement the required interface. */
|
||||||
|
public static class ClassNotSuitable {
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public ClassNotSuitable(ObjectPropertyTemplateModel optm,
|
||||||
|
WebappDaoFactory wadf) {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/** Does not have a constructor with the correct arguments */
|
/** Does not have a constructor with the correct arguments */
|
||||||
public static class PostProcessorHasWrongConstructor implements
|
public static class PostProcessorWrongConstructor implements
|
||||||
ObjectPropertyDataPostProcessor {
|
ObjectPropertyDataPostProcessor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue