1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container;
import com.yahoo.api.annotations.Beta;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.docproc.DocprocExecutor;
import com.yahoo.docproc.DocprocService;
import com.yahoo.docproc.DocumentProcessor;
import com.yahoo.docproc.jdisc.DocumentProcessingHandler;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.processing.execution.chain.ChainRegistry;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* For doing document processing with {@link JDisc}.
*
* @author Einar M R Rosenvinge
*/
@Beta
public final class DocumentProcessing {
private final DocumentProcessingHandler handler;
private final Map<String, DocumentType> documentTypes;
DocumentProcessing(DocumentProcessingHandler handler) {
this.handler = handler;
documentTypes = retrieveDocumentTypes(handler.getDocumentTypeManager());
}
private static Map<String, DocumentType> retrieveDocumentTypes(DocumentTypeManager documentTypeManager) {
Map<String, DocumentType> documentTypes = new HashMap<>() ;
for (Iterator<DocumentType> i = documentTypeManager.documentTypeIterator(); i.hasNext();) {
DocumentType type = i.next();
documentTypes.put(type.getName(), type);
}
return Collections.unmodifiableMap(documentTypes);
}
/**
* Processes the given Processing through the specified chain. Note that if one
* {@link com.yahoo.docproc.DocumentProcessor DocumentProcessor} in the
* chain returns a {@link com.yahoo.docproc.DocumentProcessor.LaterProgress DocumentProcessor.LaterProgress},
* the calling thread will sleep for the duration
* specified in {@link com.yahoo.docproc.DocumentProcessor.LaterProgress#getDelay() DocumentProcessor.LaterProgress#getDelay()},
* and then run again. This method will hence return when a document processor returns
* {@link com.yahoo.docproc.DocumentProcessor.Progress#DONE DocumentProcessor.Progress#DONE} or
* {@link com.yahoo.docproc.DocumentProcessor.Progress#FAILED DocumentProcessor.Progress#FAILED}, throws an exception,
* or if the calling thread is interrupted. This method will never return a
* {@link com.yahoo.docproc.DocumentProcessor.LaterProgress DocumentProcessor.LaterProgress}.
*
* @param chain the specification of the chain to execute
* @param processing the Processing to process
* @return Progress.DONE or Progress.FAILED
* @throws RuntimeException if one of the document processors in the chain throws, or if the calling thread is interrupted
*/
public DocumentProcessor.Progress process(ComponentSpecification chain, com.yahoo.docproc.Processing processing) {
DocprocExecutor executor = getExecutor(chain);
processing.setDocprocServiceRegistry(handler.getDocprocServiceRegistry());
return executor.processUntilDone(processing);
}
/**
* Processes the given Processing through the specified chain. Note that if one
* {@link com.yahoo.docproc.DocumentProcessor DocumentProcessor} in the
* chain returns a {@link com.yahoo.docproc.DocumentProcessor.LaterProgress DocumentProcessor.LaterProgress},
* it will be returned by this method. This method will hence return whenever a document processor returns any
* {@link com.yahoo.docproc.DocumentProcessor.Progress DocumentProcessor.Progress}, or
* throws an exception.
*
* @param chain the specification of the chain to execute
* @param processing the Processing to process
* @return any Progress
* @throws RuntimeException if one of the document processors in the chain throws
*/
public DocumentProcessor.Progress processOnce(ComponentSpecification chain, com.yahoo.docproc.Processing processing) {
DocprocExecutor executor = getExecutor(chain);
processing.setDocprocServiceRegistry(handler.getDocprocServiceRegistry());
return executor.process(processing);
}
private DocprocExecutor getExecutor(ComponentSpecification chain) {
DocprocService service = handler.getDocprocServiceRegistry().getComponent(chain);
if (service == null) {
throw new IllegalArgumentException("No such chain: " + chain);
}
return service.getExecutor();
}
/**
* Returns a registry of configured docproc chains.
*
* @return a registry of configured docproc chains
*/
public ChainRegistry<DocumentProcessor> getChains() {
return handler.getChains();
}
public Map<String, DocumentType> getDocumentTypes() {
return documentTypes;
}
public Map<String, AnnotationType> getAnnotationTypes() {
return handler.getDocumentTypeManager().getAnnotationTypeRegistry().getTypes();
}
}
|