]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c864a662ede4a7daadaa0fe068bf4fa942384c00
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.batch.nuxeo;
2
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;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 public class BatchValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {
14
15     final Logger logger = LoggerFactory.getLogger(BatchValidatorHandler.class);
16
17     //
18     // Error Strings
19     //
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 MISSING_CLASS_ERROR = "The Java class '%s' (fully qualified with package name) for the batch job named '%s' cannot be found.";
23
24         @Override
25         protected Class<?> getCommonPartClass() {
26                 return BatchCommon.class;
27         }
28
29         @Override
30         protected void handleCreate() throws InvalidDocumentException {
31         try {
32                 BatchCommon batchCommon = (BatchCommon) getCommonPart();
33                 validateBatchCommon(batchCommon);                        
34         } catch (AssertionError e) {
35                 if (logger.isErrorEnabled() == true) {
36                         logger.error(e.getMessage(), e);
37                 }
38                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
39         }
40     }
41
42         @Override
43         protected void handleGet() throws InvalidDocumentException {
44                 // TODO Auto-generated method stub
45                 
46         }
47
48         @Override
49         protected void handleGetAll() throws InvalidDocumentException {
50                 // TODO Auto-generated method stub
51                 
52         }
53
54         @Override
55         protected void handleUpdate() throws InvalidDocumentException {
56         try {
57                 BatchCommon batchCommon = (BatchCommon) getCommonPart();
58                 validateBatchCommon(batchCommon);                        
59         } catch (AssertionError e) {
60                 if (logger.isErrorEnabled() == true) {
61                         logger.error(e.getMessage(), e);
62                 }
63                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
64         }
65     }
66
67         @Override
68         protected void handleDelete() throws InvalidDocumentException {
69                 // TODO Auto-generated method stub
70                 
71         }
72
73     //
74     // Private Methods
75     //
76         private boolean canFindClass(String className) {
77                 boolean result = false;
78                 
79                 try {
80                         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
81                         Class<?> c = tccl.loadClass(className);
82                         tccl.setClassAssertionStatus(className, true);
83                         if (!BatchInvocable.class.isAssignableFrom(c)) {
84                                 throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
85                         }
86                         result = true;
87                 } catch (Exception e) {
88                         String msg = String.format("Could not find load batch class named '%s'",
89                                         className);
90                         logger.debug(msg, e);
91                 }
92
93                 return result;
94         }
95         
96         private void validateBatchCommon(BatchCommon batchCommon) {
97         CS_ASSERT(batchCommon != null);
98         
99         //
100         // Ensure a batch name
101         String batchName = batchCommon.getName();
102         CS_ASSERT(batchName != null, NAME_NULL_ERROR);
103         CS_ASSERT(batchName.isEmpty() == false, NAME_NULL_ERROR);
104         
105         //
106         // Ensure a batch class
107         String batchClassName = batchCommon.getClassName();
108         CS_ASSERT(batchName != null, NAME_NULL_ERROR);
109         CS_ASSERT(batchName.isEmpty() == false, NAME_NULL_ERROR);
110         
111         //
112         // Ensure we can find and load the batch Java class
113         if (canFindClass(batchClassName) == false) {
114                 String msg = String.format(MISSING_CLASS_ERROR, batchClassName, batchCommon.getName());
115                 CS_ASSERT(false, batchClassName);
116         }
117     }
118         
119 }