]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ce825c5a0cf1261dc712e767d470e3db61266fea
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.batch.nuxeo;
25
26 import java.util.List;
27
28 import javax.ws.rs.core.Response;
29
30 import org.collectionspace.services.BatchJAXBSchema;
31 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
32 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
33 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
34 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
35 import org.collectionspace.services.batch.BatchCommon;
36 import org.collectionspace.services.batch.BatchInvocable;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.common.ResourceMap;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.document.BadRequestException;
42 import org.collectionspace.services.common.document.DocumentException;
43 import org.collectionspace.services.common.document.DocumentWrapper;
44 import org.collectionspace.services.common.invocable.Invocable;
45 import org.collectionspace.services.common.invocable.InvocationContext;
46 import org.collectionspace.services.common.invocable.InvocationResults;
47 import org.collectionspace.services.common.invocable.Invocable.InvocationError;
48
49 import org.jboss.resteasy.spi.ResteasyProviderFactory;
50
51 import org.nuxeo.ecm.core.api.DocumentModel;
52 import org.nuxeo.ecm.core.api.model.PropertyException;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler<BatchCommon> {
58         private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
59
60         protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
61
62         public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
63                         ResourceMap resourceMap, InvocationContext invContext) throws Exception {
64
65                 CoreSessionInterface repoSession = null;
66                 boolean releaseRepoSession = false;
67
68                 String invocationMode = invContext.getMode();
69                 String modeProperty = null;
70                 boolean checkDocType = true;
71                 if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
72                         modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
73                 } else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
74                         modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
75                 } else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
76                         modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
77                 } else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
78                         modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
79                         checkDocType = false;
80                 } else {
81                         throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode);
82                 }
83
84                 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl) this.getRepositoryClient(ctx);
85                 repoSession = this.getRepositorySession();
86                 if (repoSession == null) {
87                         repoSession = repoClient.getRepositorySession(ctx);
88                         releaseRepoSession = true;
89                 }
90
91                 String className = null;
92                 // Get properties from the batch docModel, and release the session
93                 try {
94                         DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, csid);
95                         DocumentModel docModel = wrapper.getWrappedObject();
96                         Boolean supports = (Boolean) docModel.getPropertyValue(modeProperty);
97                         if (!supports) {
98                                 throw new BadRequestException("BatchResource: This Batch Job does not support Invocation Mode: "
99                                                 + invocationMode);
100                         }
101                         if (checkDocType) {
102                                 List<String> forDocTypeList = (List<String>) docModel.getPropertyValue(BatchJAXBSchema.FOR_DOC_TYPES);
103                                 if (forDocTypeList == null || !forDocTypeList.contains(invContext.getDocType())) {
104                                         throw new BadRequestException("BatchResource: Invoked with unsupported document type: "
105                                                         + invContext.getDocType());
106                                 }
107                         }
108                         className = (String) docModel.getPropertyValue(BatchJAXBSchema.BATCH_CLASS_NAME);
109                 } catch (PropertyException pe) {
110                         if (logger.isDebugEnabled()) {
111                                 logger.debug("Property exception getting batch values: ", pe);
112                         }
113                         throw pe;
114                 } catch (DocumentException de) {
115                         if (logger.isDebugEnabled()) {
116                                 logger.debug("Problem getting batch doc: ", de);
117                         }
118                         throw de;
119                 } catch (Exception e) {
120                         if (logger.isDebugEnabled()) {
121                                 logger.debug("Caught exception ", e);
122                         }
123                         throw new DocumentException(e);
124                 } finally {
125                         if (releaseRepoSession && repoSession != null) {
126                                 repoClient.releaseRepositorySession(ctx, repoSession);
127                         }
128                 }
129
130                 className = className.trim();
131                 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
132                 Class<?> c = tccl.loadClass(className);
133                 // enable validation assertions
134                 tccl.setClassAssertionStatus(className, true);
135                 if (!BatchInvocable.class.isAssignableFrom(c)) {
136                         throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
137                 }
138
139                 BatchInvocable batchInstance = (BatchInvocable) c.newInstance();
140                 List<String> modes = batchInstance.getSupportedInvocationModes();
141                 if (!modes.contains(invocationMode)) {
142                         throw new BadRequestException("BatchResource: Invoked with unsupported context mode: " + invocationMode);
143                 }
144
145                 batchInstance.setInvocationContext(invContext);
146                 
147                 if (resourceMap != null) {
148                         batchInstance.setResourceMap(resourceMap);
149                 } else {
150                         resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
151                         if (resourceMap != null) {
152                                 batchInstance.setResourceMap(resourceMap);
153                         } else {
154                                 logger.warn("BatchResource.invoke did not get a resourceMapHolder in Context!");
155                         }
156                 }
157
158                 batchInstance.run();
159                 int status = batchInstance.getCompletionStatus();
160                 if (status == Invocable.STATUS_ERROR) {
161                         InvocationError error = batchInstance.getErrorInfo();
162                         if (error.getResponseCode() == BAD_REQUEST_STATUS) {
163                                 throw new BadRequestException("BatchResouce: batchProcess encountered error: "
164                                                 + batchInstance.getErrorInfo());
165                         } else {
166                                 throw new RuntimeException("BatchResouce: batchProcess encountered error: "
167                                                 + batchInstance.getErrorInfo());
168
169                         }
170                 }
171
172                 InvocationResults results = batchInstance.getResults();
173                 return results;
174         }
175 }