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