]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1dc994666c061925312bc356fdab8ad04a6eadf0
[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.RepositoryClientImpl;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
36 import org.collectionspace.services.batch.BatchCommon;
37 import org.collectionspace.services.batch.BatchInvocable;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.ResourceMap;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.document.BadRequestException;
43 import org.collectionspace.services.common.document.DocumentException;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.invocable.Invocable;
46 import org.collectionspace.services.common.invocable.InvocationContext;
47 import org.collectionspace.services.common.invocable.InvocationResults;
48 import org.collectionspace.services.common.invocable.Invocable.InvocationError;
49 import org.jboss.resteasy.spi.ResteasyProviderFactory;
50 import org.nuxeo.ecm.core.api.DocumentModel;
51 import org.nuxeo.ecm.core.api.model.PropertyException;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler<BatchCommon> {
56         private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
57
58         protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
59
60         public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
61                         ResourceMap resourceMap, InvocationContext invContext) throws Exception {
62
63                 CoreSessionInterface repoSession = null;
64                 boolean releaseRepoSession = false;
65
66                 String invocationMode = invContext.getMode();
67                 String modeProperty = null;
68                 boolean checkDocType = true;
69                 if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
70                         modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
71                 } else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
72                         modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
73                 } else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
74                         modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
75                 } else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
76                         modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
77                         checkDocType = false;
78                 } else {
79                         throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode);
80                 }
81
82                 RepositoryClientImpl repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx);
83                 repoSession = this.getRepositorySession();
84                 if (repoSession == null) {
85                         repoSession = repoClient.getRepositorySession(ctx);
86                         releaseRepoSession = true;
87                 }
88
89                 String className = null;
90                 // Get properties from the batch docModel, and release the session
91                 try {
92                         DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, csid);
93                         DocumentModel docModel = wrapper.getWrappedObject();
94                         Boolean supports = (Boolean) NuxeoUtils.getProperyValue(docModel, modeProperty);
95                         if (!supports) {
96                                 throw new BadRequestException("BatchResource: This Batch Job does not support Invocation Mode: "
97                                                 + invocationMode);
98                         }
99                         if (checkDocType) {
100                                 List<String> forDocTypeList = (List<String>) NuxeoUtils.getProperyValue(docModel, BatchJAXBSchema.FOR_DOC_TYPES); //docModel.getPropertyValue(BatchJAXBSchema.FOR_DOC_TYPES);
101                                 if (forDocTypeList == null || !forDocTypeList.contains(invContext.getDocType())) {
102                                         throw new BadRequestException("BatchResource: Invoked with unsupported document type: "
103                                                         + invContext.getDocType());
104                                 }
105                         }
106                         className = (String) NuxeoUtils.getProperyValue(docModel, BatchJAXBSchema.BATCH_CLASS_NAME); //docModel.getPropertyValue(BatchJAXBSchema.BATCH_CLASS_NAME);
107                 } catch (PropertyException pe) {
108                         if (logger.isDebugEnabled()) {
109                                 logger.debug("Property exception getting batch values: ", pe);
110                         }
111                         throw pe;
112                 } catch (DocumentException de) {
113                         if (logger.isDebugEnabled()) {
114                                 logger.debug("Problem getting batch doc: ", de);
115                         }
116                         throw de;
117                 } catch (Exception e) {
118                         if (logger.isDebugEnabled()) {
119                                 logger.debug("Caught exception ", e);
120                         }
121                         throw new DocumentException(e);
122                 } finally {
123                         if (releaseRepoSession && repoSession != null) {
124                                 repoClient.releaseRepositorySession(ctx, repoSession);
125                         }
126                 }
127
128                 className = className.trim();
129                 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
130                 Class<?> c = tccl.loadClass(className);
131                 // enable validation assertions
132                 tccl.setClassAssertionStatus(className, true);
133                 if (!BatchInvocable.class.isAssignableFrom(c)) {
134                         throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
135                 }
136
137                 BatchInvocable batchInstance = (BatchInvocable) c.newInstance();
138                 List<String> modes = batchInstance.getSupportedInvocationModes();
139                 if (!modes.contains(invocationMode)) {
140                         throw new BadRequestException("BatchResource: Invoked with unsupported context mode: " + invocationMode);
141                 }
142
143                 batchInstance.setInvocationContext(invContext);
144                 
145                 if (resourceMap != null) {
146                         batchInstance.setResourceMap(resourceMap);
147                 } else {
148                         resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
149                         if (resourceMap != null) {
150                                 batchInstance.setResourceMap(resourceMap);
151                         } else {
152                                 logger.warn("BatchResource.invoke did not get a resourceMapHolder in Context!");
153                         }
154                 }
155
156                 batchInstance.run();
157                 int status = batchInstance.getCompletionStatus();
158                 if (status == Invocable.STATUS_ERROR) {
159                         InvocationError error = batchInstance.getErrorInfo();
160                         if (error.getResponseCode() == BAD_REQUEST_STATUS) {
161                                 throw new BadRequestException("BatchResouce: batchProcess encountered error: "
162                                                 + batchInstance.getErrorInfo());
163                         } else {
164                                 throw new RuntimeException("BatchResouce: batchProcess encountered error: "
165                                                 + batchInstance.getErrorInfo());
166
167                         }
168                 }
169
170                 InvocationResults results = batchInstance.getResults();
171                 return results;
172         }
173 }