1 package org.collectionspace.services.batch.nuxeo;
3 import org.collectionspace.services.batch.BatchCommon;
4 import org.collectionspace.services.batch.BatchInvocable;
5 import org.collectionspace.services.client.PoxPayloadIn;
6 import org.collectionspace.services.client.PoxPayloadOut;
7 import org.collectionspace.services.common.document.InvalidDocumentException;
8 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 public class BatchValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {
15 final Logger logger = LoggerFactory.getLogger(BatchValidatorHandler.class);
20 private static final String VALIDATION_ERROR = "The batch record payload was invalid. See log file for more details.";
21 private static final String NAME_NULL_ERROR = "The batch record field \"name\" cannot be empty or missing.";
22 private static final String CLASSNAME_NULL_ERROR = "The batch record field \"className\" cannot be empty or missing.";
23 private static final String MISSING_CLASS_ERROR = "The Java class '%s' (fully qualified with package name) for the batch job named '%s' cannot be found.";
26 protected Class<?> getCommonPartClass() {
27 return BatchCommon.class;
31 protected void handleCreate() throws InvalidDocumentException {
33 BatchCommon batchCommon = (BatchCommon) getCommonPart();
34 validateBatchCommon(batchCommon);
35 } catch (AssertionError e) {
36 if (logger.isErrorEnabled() == true) {
37 logger.error(e.getMessage(), e);
39 throw new InvalidDocumentException(VALIDATION_ERROR, e);
44 protected void handleGet() throws InvalidDocumentException {
45 // TODO Auto-generated method stub
50 protected void handleGetAll() throws InvalidDocumentException {
51 // TODO Auto-generated method stub
56 protected void handleUpdate() throws InvalidDocumentException {
58 BatchCommon batchCommon = (BatchCommon) getCommonPart();
59 validateBatchCommon(batchCommon);
60 } catch (AssertionError e) {
61 if (logger.isErrorEnabled() == true) {
62 logger.error(e.getMessage(), e);
64 throw new InvalidDocumentException(VALIDATION_ERROR, e);
69 protected void handleDelete() throws InvalidDocumentException {
70 // TODO Auto-generated method stub
77 private boolean canFindClass(String className) {
78 boolean result = false;
81 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
82 Class<?> c = tccl.loadClass(className);
83 tccl.setClassAssertionStatus(className, true);
84 if (!BatchInvocable.class.isAssignableFrom(c)) {
85 throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
88 } catch (Exception e) {
89 String msg = String.format("Could not find load batch class named '%s'",
97 private void validateBatchCommon(BatchCommon batchCommon) {
98 CS_ASSERT(batchCommon != null);
101 // Ensure a batch name
102 String batchName = batchCommon.getName();
103 CS_ASSERT(batchName != null, NAME_NULL_ERROR);
104 CS_ASSERT(batchName.isEmpty() == false, NAME_NULL_ERROR);
107 // Ensure a batch class
108 String batchClassName = batchCommon.getClassName();
109 CS_ASSERT(batchClassName != null, CLASSNAME_NULL_ERROR);
110 CS_ASSERT(batchClassName.isEmpty() == false, CLASSNAME_NULL_ERROR);
113 // Ensure we can find and load the batch Java class
114 if (canFindClass(batchClassName) == false) {
115 String msg = String.format(MISSING_CLASS_ERROR, batchClassName, batchCommon.getName());
116 CS_ASSERT(false, msg);