1 | package eu.clarin.sru.client; |
---|
2 | |
---|
3 | import java.util.concurrent.CountDownLatch; |
---|
4 | |
---|
5 | import org.slf4j.Logger; |
---|
6 | import org.slf4j.LoggerFactory; |
---|
7 | |
---|
8 | import eu.clarin.sru.fcs.ClarinFederatedContentSearchRecordData; |
---|
9 | import eu.clarin.sru.fcs.ClarinFederatedContentSearchRecordParser; |
---|
10 | |
---|
11 | |
---|
12 | public class TestThreadedClientCallback { |
---|
13 | private static final Logger logger = |
---|
14 | LoggerFactory.getLogger(TestThreadedClientCallback.class); |
---|
15 | |
---|
16 | public static void main(String[] args) { |
---|
17 | if (args.length > 0) { |
---|
18 | logger.info("initializing client ..."); |
---|
19 | SRUThreadedClient client = new SRUThreadedClient(); |
---|
20 | |
---|
21 | try { |
---|
22 | client.registerRecordParser(new ClarinFederatedContentSearchRecordParser()); |
---|
23 | } catch (SRUClientException e) { |
---|
24 | logger.error("error adding record parser", e); |
---|
25 | System.exit(1); |
---|
26 | } |
---|
27 | |
---|
28 | try { |
---|
29 | final CountDownLatch latch = new CountDownLatch(3); |
---|
30 | |
---|
31 | /* |
---|
32 | * the following requests will be run asynchronously and |
---|
33 | * concurrently |
---|
34 | * Invoke requests and supply a callback, that |
---|
35 | * a) prints the results |
---|
36 | * b) downs a latch (which is used to make the main-thread wait |
---|
37 | * on the requests to be completed) |
---|
38 | */ |
---|
39 | logger.info("submitting 'explain' request ..."); |
---|
40 | SRUExplainRequest request1 = new SRUExplainRequest(args[0]); |
---|
41 | client.explain(request1, new SRUCallback<SRUExplainRequest, SRUExplainResponse>() { |
---|
42 | @Override |
---|
43 | public void onSuccess(SRUExplainResponse response) { |
---|
44 | printExplainResponse(response); |
---|
45 | latch.countDown(); |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public void onError(SRUExplainRequest request, |
---|
50 | SRUClientException error) { |
---|
51 | logger.error("error while performing request", error); |
---|
52 | latch.countDown(); |
---|
53 | } |
---|
54 | }); |
---|
55 | |
---|
56 | logger.info("submitting 'scan' request ..."); |
---|
57 | SRUScanRequest request2 = new SRUScanRequest(args[0]); |
---|
58 | request2.setScanClause("fcs.resource"); |
---|
59 | client.scan(request2, new SRUCallback<SRUScanRequest, SRUScanResponse>() { |
---|
60 | @Override |
---|
61 | public void onSuccess(SRUScanResponse response) { |
---|
62 | printScanResponse(response); |
---|
63 | latch.countDown(); |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public void onError(SRUScanRequest request, |
---|
68 | SRUClientException error) { |
---|
69 | logger.error("error while performing request", error); |
---|
70 | latch.countDown(); |
---|
71 | } |
---|
72 | }); |
---|
73 | |
---|
74 | logger.info("submitting 'searchRetrieve' request ..."); |
---|
75 | SRUSearchRetrieveRequest request3 = |
---|
76 | new SRUSearchRetrieveRequest(args[0]); |
---|
77 | request3.setQuery("Faustus"); |
---|
78 | request3.setRecordSchema(ClarinFederatedContentSearchRecordData.RECORD_SCHEMA); |
---|
79 | request3.setMaximumRecords(5); |
---|
80 | client.searchRetrieve(request3, new SRUCallback<SRUSearchRetrieveRequest, SRUSearchRetrieveResponse>() { |
---|
81 | @Override |
---|
82 | public void onSuccess(SRUSearchRetrieveResponse response) { |
---|
83 | printSearchResponse(response); |
---|
84 | latch.countDown(); |
---|
85 | } |
---|
86 | |
---|
87 | @Override |
---|
88 | public void onError(SRUSearchRetrieveRequest request, |
---|
89 | SRUClientException error) { |
---|
90 | logger.error("error while performing request", error); |
---|
91 | latch.countDown(); |
---|
92 | } |
---|
93 | }); |
---|
94 | |
---|
95 | latch.await(); |
---|
96 | } catch (Exception e) { |
---|
97 | logger.error("a fatal error occured while performing request", e); |
---|
98 | } |
---|
99 | |
---|
100 | client.shutdown(); |
---|
101 | logger.info("done"); |
---|
102 | } else { |
---|
103 | System.err.println("missing args"); |
---|
104 | System.exit(64); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | private static void printExplainResponse(SRUExplainResponse response) { |
---|
110 | logger.info("displaying results of 'explain' request ..."); |
---|
111 | if (response.hasDiagnostics()) { |
---|
112 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
113 | logger.info("uri={}, message={}, detail={}", |
---|
114 | new Object[] { diagnostic.getURI(), |
---|
115 | diagnostic.getMessage(), |
---|
116 | diagnostic.getDetails() }); |
---|
117 | } |
---|
118 | } |
---|
119 | if (response.hasRecord()) { |
---|
120 | SRURecord record = response.getRecord(); |
---|
121 | logger.info("schema = {}", record.getRecordSchema()); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | private static void printScanResponse(SRUScanResponse response) { |
---|
127 | logger.info("displaying results of 'scan' request ..."); |
---|
128 | if (response.hasDiagnostics()) { |
---|
129 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
130 | logger.info("uri={}, message={}, detail={}", |
---|
131 | new Object[] { |
---|
132 | diagnostic.getURI(), |
---|
133 | diagnostic.getMessage(), |
---|
134 | diagnostic.getDetails() |
---|
135 | }); |
---|
136 | } |
---|
137 | } |
---|
138 | if (response.hasTerms()) { |
---|
139 | for (SRUTerm term : response.getTerms()) { |
---|
140 | logger.info( |
---|
141 | "value={}, numberOfRecords={}, displayTerm={}", |
---|
142 | new Object[] { term.getValue(), |
---|
143 | term.getNumberOfRecords(), |
---|
144 | term.getDisplayTerm() }); |
---|
145 | } |
---|
146 | } else { |
---|
147 | logger.info("no terms"); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | private static void printSearchResponse(SRUSearchRetrieveResponse response) { |
---|
153 | logger.info("displaying results of 'searchRetrieve' request ..."); |
---|
154 | logger.info("numberOfRecords = {}, nextResultPosition = {}", |
---|
155 | new Object[] { response.getNumberOfRecords(), |
---|
156 | response.getNextRecordPosition() }); |
---|
157 | if (response.hasDiagnostics()) { |
---|
158 | for (SRUDiagnostic diagnostic : response.getDiagnostics()) { |
---|
159 | logger.info("uri={}, message={}, detail={}", |
---|
160 | new Object[] { diagnostic.getURI(), |
---|
161 | diagnostic.getMessage(), |
---|
162 | diagnostic.getDetails() }); |
---|
163 | } |
---|
164 | } |
---|
165 | if (response.hasRecords()) { |
---|
166 | for (SRURecord record : response.getRecords()) { |
---|
167 | logger.info("schema = {}, identifier = {}, position = {}", |
---|
168 | new Object[] { record.getRecordSchema(), |
---|
169 | record.getRecordIdentifier(), |
---|
170 | record.getRecordPosition() }); |
---|
171 | if (record.isRecordSchema(ClarinFederatedContentSearchRecordData.RECORD_SCHEMA)) { |
---|
172 | ClarinFederatedContentSearchRecordData r = |
---|
173 | (ClarinFederatedContentSearchRecordData) record.getRecordData(); |
---|
174 | logger.info("CLARIN-FCS: \"{}\"/\"{}\"/\"{}\"", |
---|
175 | new Object[] { r.getLeft(), r.getKeyword(), r.getRight() }); |
---|
176 | } else if (record.isRecordSchema(SRUSurrogateRecordData.RECORD_SCHEMA)) { |
---|
177 | SRUSurrogateRecordData r = |
---|
178 | (SRUSurrogateRecordData) record.getRecordData(); |
---|
179 | logger.info("SURROGATE DIAGNOSTIC: uri={}, message={}, detail={}", |
---|
180 | new Object[] { r.getURI(), r.getMessage(), |
---|
181 | r.getDetails() }); |
---|
182 | } else { |
---|
183 | logger.info("UNKNOWN RECORD SCHEMA"); |
---|
184 | } |
---|
185 | } |
---|
186 | } else { |
---|
187 | logger.info("no results"); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | static { |
---|
193 | org.apache.log4j.BasicConfigurator.configure( |
---|
194 | new org.apache.log4j.ConsoleAppender( |
---|
195 | new org.apache.log4j.PatternLayout("%-5p [%t] %m%n"), |
---|
196 | org.apache.log4j.ConsoleAppender.SYSTEM_ERR)); |
---|
197 | org.apache.log4j.Logger logger = |
---|
198 | org.apache.log4j.Logger.getRootLogger(); |
---|
199 | logger.setLevel(org.apache.log4j.Level.INFO); |
---|
200 | logger.getLoggerRepository().getLogger("eu.clarin").setLevel( |
---|
201 | org.apache.log4j.Level.DEBUG); |
---|
202 | } |
---|
203 | |
---|
204 | } |
---|