]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d7486adf3eecf33b623f2d28f805091d6baf700b
[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 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.";
24
25         @Override
26         protected Class<?> getCommonPartClass() {
27                 return BatchCommon.class;
28         }
29
30         @Override
31         protected void handleCreate() throws InvalidDocumentException {
32         try {
33                 BatchCommon batchCommon = (BatchCommon) getCommonPart();
34                 validateBatchCommon(batchCommon);                        
35         } catch (AssertionError e) {
36                 if (logger.isErrorEnabled() == true) {
37                         logger.error(e.getMessage(), e);
38                 }
39                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
40         }
41     }
42
43         @Override
44         protected void handleGet() throws InvalidDocumentException {
45                 // TODO Auto-generated method stub
46                 
47         }
48
49         @Override
50         protected void handleGetAll() throws InvalidDocumentException {
51                 // TODO Auto-generated method stub
52                 
53         }
54
55         @Override
56         protected void handleUpdate() throws InvalidDocumentException {
57         try {
58                 BatchCommon batchCommon = (BatchCommon) getCommonPart();
59                 validateBatchCommon(batchCommon);                        
60         } catch (AssertionError e) {
61                 if (logger.isErrorEnabled() == true) {
62                         logger.error(e.getMessage(), e);
63                 }
64                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
65         }
66     }
67
68         @Override
69         protected void handleDelete() throws InvalidDocumentException {
70                 // TODO Auto-generated method stub
71                 
72         }
73
74     //
75     // Private Methods
76     //
77         private boolean canFindClass(String className) {
78                 boolean result = false;
79                 
80                 try {
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!");
86                         }
87                         result = true;
88                 } catch (Exception e) {
89                         String msg = String.format("Could not find load batch class named '%s'",
90                                         className);
91                         logger.debug(msg, e);
92                 }
93
94                 return result;
95         }
96         
97         private void validateBatchCommon(BatchCommon batchCommon) {
98         CS_ASSERT(batchCommon != null);
99         
100         //
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);
105         
106         //
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);
111         
112         //
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);
117         }
118     }
119         
120 }