1 | package eu.clarin.cmdi.oai.harvester.impl; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.net.URI; |
---|
5 | import java.net.URISyntaxException; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import javax.xml.namespace.QName; |
---|
10 | import javax.xml.stream.XMLInputFactory; |
---|
11 | import javax.xml.stream.XMLStreamException; |
---|
12 | |
---|
13 | import org.codehaus.stax2.XMLInputFactory2; |
---|
14 | import org.codehaus.stax2.XMLStreamReader2; |
---|
15 | |
---|
16 | import com.ctc.wstx.api.WstxInputProperties; |
---|
17 | |
---|
18 | import eu.clarin.cmdi.oai.harvester.HarvestHandler; |
---|
19 | import eu.clarin.cmdi.oai.harvester.HarvestJob; |
---|
20 | import eu.clarin.cmdi.oai.harvester.Harvester; |
---|
21 | import eu.clarin.cmdi.oai.harvester.HarvesterException; |
---|
22 | import eu.clarin.cmdi.oai.harvester.ext.DescriptionParser; |
---|
23 | |
---|
24 | |
---|
25 | abstract class AbstractHarvester implements Harvester { |
---|
26 | private static final int DEFAULT_MAX_NETWORK_RETRY_COUNT = 5; |
---|
27 | protected int maxNetworkRetryCount = DEFAULT_MAX_NETWORK_RETRY_COUNT; |
---|
28 | private final XMLInputFactory2 factory; |
---|
29 | // private final XMLValidationSchema schema; |
---|
30 | private List<DescriptionParser> descriptionParsers = |
---|
31 | new ArrayList<DescriptionParser>(); |
---|
32 | |
---|
33 | |
---|
34 | protected AbstractHarvester() { |
---|
35 | factory = (XMLInputFactory2) XMLInputFactory.newInstance(); |
---|
36 | |
---|
37 | // Stax settings |
---|
38 | factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); |
---|
39 | factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); |
---|
40 | |
---|
41 | // Stax2 settings |
---|
42 | factory.setProperty(XMLInputFactory2.P_AUTO_CLOSE_INPUT, Boolean.FALSE); |
---|
43 | |
---|
44 | // Woodstox settings |
---|
45 | factory.setProperty(WstxInputProperties.P_NORMALIZE_LFS, Boolean.TRUE); |
---|
46 | |
---|
47 | // XMLValidationSchemaFactory schemaFactory = |
---|
48 | // XMLValidationSchemaFactory |
---|
49 | // .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
---|
50 | // URL url = Harvester.class.getClassLoader().getResource( |
---|
51 | // "META-INF/OAI-PMH.xsd"); |
---|
52 | // if (url == null) { |
---|
53 | // throw new NullPointerException("schema not found!"); |
---|
54 | // } |
---|
55 | // schema = schemaFactory.createSchema(url); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | @Override |
---|
60 | public final void registerDescriptionParser(DescriptionParser parser) |
---|
61 | throws HarvesterException { |
---|
62 | if (parser == null) { |
---|
63 | throw new NullPointerException("parser == null"); |
---|
64 | } |
---|
65 | if (findDescriptionParser(parser.getNamespaceURI(), |
---|
66 | parser.getLocalName()) != null) { |
---|
67 | throw new HarvesterException("description parser for '" + |
---|
68 | new QName(parser.getNamespaceURI(), parser.getLocalName()) + |
---|
69 | "' was already registered"); |
---|
70 | } |
---|
71 | if (descriptionParsers == null) { |
---|
72 | descriptionParsers = new ArrayList<DescriptionParser>(); |
---|
73 | } |
---|
74 | descriptionParsers.add(parser); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | @Override |
---|
79 | public abstract HarvestJob createJob(URI repositoryURI, |
---|
80 | HarvestHandler handler) throws HarvesterException; |
---|
81 | |
---|
82 | |
---|
83 | @Override |
---|
84 | public final HarvestJob createJob(URI repositoryURI) |
---|
85 | throws HarvesterException { |
---|
86 | return createJob(repositoryURI, null); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | @Override |
---|
91 | public final HarvestJob createJob(String repositoryURI, |
---|
92 | HarvestHandler handler) throws HarvesterException { |
---|
93 | try { |
---|
94 | return createJob(new URI(repositoryURI), handler); |
---|
95 | } catch (URISyntaxException e) { |
---|
96 | throw new HarvesterException("invalid repositoryURI", e); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | @Override |
---|
102 | public final HarvestJob createJob(String repositoryURI) |
---|
103 | throws HarvesterException { |
---|
104 | return createJob(repositoryURI, null); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | @Override |
---|
109 | public final HarvestJob findJob(long id) throws HarvesterException { |
---|
110 | return doFindJob(id); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | @Override |
---|
115 | public final HarvestJob cancelJob(long id) throws HarvesterException { |
---|
116 | HarvestJob job = doFindJob(id); |
---|
117 | if (job != null) { |
---|
118 | job.cancel(); |
---|
119 | } |
---|
120 | return job; |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | @Override |
---|
125 | public final int getMaxNetworkRetryCount() { |
---|
126 | return maxNetworkRetryCount; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | @Override |
---|
131 | public final void setMaxNetworkRetryCount(int maxNetworkRetryCount) { |
---|
132 | if (maxNetworkRetryCount < 1) { |
---|
133 | throw new IllegalArgumentException( |
---|
134 | "invalid value for maxNetworkRetryCount: " + |
---|
135 | maxNetworkRetryCount); |
---|
136 | } |
---|
137 | this.maxNetworkRetryCount = maxNetworkRetryCount; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | DescriptionParser findDescriptionParser(String namespaceURI, |
---|
142 | String localName) { |
---|
143 | if (descriptionParsers != null) { |
---|
144 | for (DescriptionParser parser : descriptionParsers) { |
---|
145 | if (namespaceURI.equals(parser.getNamespaceURI()) && |
---|
146 | localName.equals(parser.getLocalName())) { |
---|
147 | return parser; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | return null; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | abstract void doRunJob(HarvestJobImpl job) throws HarvesterException; |
---|
156 | |
---|
157 | |
---|
158 | abstract void doCancelJob(HarvestJobImpl job) throws HarvesterException; |
---|
159 | |
---|
160 | |
---|
161 | protected abstract HarvestJob doFindJob(long id) throws HarvesterException; |
---|
162 | |
---|
163 | |
---|
164 | protected final XMLStreamReader2 createReader(InputStream in) |
---|
165 | throws XMLStreamException { |
---|
166 | XMLStreamReader2 reader = |
---|
167 | (XMLStreamReader2) factory.createXMLStreamReader(in); |
---|
168 | // reader.validateAgainst(schema); |
---|
169 | return reader; |
---|
170 | } |
---|
171 | |
---|
172 | } // abstract class AbstractHarvester |
---|