|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.client.xmldb.xmlrpc; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.client.xmldb.ResourceSetImpl; |
|
25 |
| import org.apache.xindice.client.xmldb.XindiceCollection; |
|
26 |
| import org.apache.xindice.client.xmldb.resources.BinaryResourceImpl; |
|
27 |
| import org.apache.xindice.client.xmldb.resources.XMLResourceImpl; |
|
28 |
| import org.apache.xindice.core.FaultCodes; |
|
29 |
| import org.apache.xindice.core.meta.MetaData; |
|
30 |
| import org.apache.xindice.server.rpc.RPCDefaultMessage; |
|
31 |
| import org.apache.xindice.server.rpc.RPCMessageInterface; |
|
32 |
| import org.apache.xindice.util.SymbolDeserializer; |
|
33 |
| import org.apache.xindice.xml.TextWriter; |
|
34 |
| import org.apache.xindice.xml.SymbolTable; |
|
35 |
| import org.apache.xindice.xml.dom.DOMParser; |
|
36 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
|
37 |
| import org.apache.xmlrpc.XmlRpcException; |
|
38 |
| import org.apache.xmlrpc.client.XmlRpcClient; |
|
39 |
| import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; |
|
40 |
| |
|
41 |
| import org.w3c.dom.Document; |
|
42 |
| import org.xml.sax.InputSource; |
|
43 |
| import org.xmldb.api.base.Collection; |
|
44 |
| import org.xmldb.api.base.ErrorCodes; |
|
45 |
| import org.xmldb.api.base.Resource; |
|
46 |
| import org.xmldb.api.base.ResourceSet; |
|
47 |
| import org.xmldb.api.base.XMLDBException; |
|
48 |
| import org.xmldb.api.modules.XMLResource; |
|
49 |
| |
|
50 |
| import javax.xml.parsers.DocumentBuilderFactory; |
|
51 |
| |
|
52 |
| import java.io.StringReader; |
|
53 |
| import java.net.URL; |
|
54 |
| import java.util.HashMap; |
|
55 |
| import java.util.Hashtable; |
|
56 |
| import java.util.Map; |
|
57 |
| import java.util.StringTokenizer; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| public class CollectionImpl extends XindiceCollection { |
|
68 |
| |
|
69 |
| private static final Log log = LogFactory.getLog(CollectionImpl.class); |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| private XmlRpcClient client; |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| private SymbolDeserializer symsDeserializer; |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
1128
| public CollectionImpl(XmlRpcClient client, String collPath) throws XMLDBException {
|
|
92 |
1128
| super(collPath);
|
|
93 |
1128
| this.client = client;
|
|
94 |
| |
|
95 |
| |
|
96 |
1128
| Map params = new HashMap(3);
|
|
97 |
1128
| params.put(RPCDefaultMessage.COLLECTION, collPath);
|
|
98 |
1128
| String exists = (String) runRemoteCommand("GetCollectionConfiguration", params);
|
|
99 |
| |
|
100 |
1127
| if (!"yes".equals(exists)) {
|
|
101 |
1
| throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION,
|
|
102 |
| FaultCodes.COL_COLLECTION_NOT_FOUND, |
|
103 |
| "Collection not found: " + collPath); |
|
104 |
| } |
|
105 |
| |
|
106 |
1126
| symsDeserializer = new SymbolDeserializer();
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
1
| private URL getURL() {
|
|
115 |
1
| return ((XmlRpcClientConfigImpl) client.getClientConfig()).getServerURL();
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
2357
| private Object runRemoteCommand(String cmdName, Map params) throws XMLDBException {
|
|
129 |
2357
| try {
|
|
130 |
2357
| params.put(RPCMessageInterface.MESSAGE_PARAM, cmdName);
|
|
131 |
2357
| return ((Map) client.execute("run", new Object[]{ params })).get(RPCDefaultMessage.RESULT);
|
|
132 |
| } catch (XmlRpcException e) { |
|
133 |
15
| if (log.isDebugEnabled()) {
|
|
134 |
0
| log.debug("Got XmlRpc exception running command " + cmdName + ", code: " + e.code + ", msg: " + e.getMessage());
|
|
135 |
| } |
|
136 |
| |
|
137 |
15
| if (e.code != 0) {
|
|
138 |
14
| throw new XMLDBException(e.code / FaultCodes.MAX_CODE,
|
|
139 |
| e.code % FaultCodes.MAX_CODE, |
|
140 |
| e.getMessage()); |
|
141 |
| } |
|
142 |
| |
|
143 |
1
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, FaultCodes.GEN_GENERAL_ERROR,
|
|
144 |
| "Failed to execute command '" + cmdName + "' on server: " + getURL() + ", message: " + e.getMessage(), e); |
|
145 |
| } |
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
175
| public Resource getResource(String id) throws XMLDBException {
|
|
162 |
175
| checkOpen();
|
|
163 |
| |
|
164 |
175
| try {
|
|
165 |
175
| if (id == null) {
|
|
166 |
2
| return null;
|
|
167 |
| } |
|
168 |
| |
|
169 |
173
| Map params = new HashMap();
|
|
170 |
173
| params.put(RPCDefaultMessage.COLLECTION, collPath);
|
|
171 |
173
| params.put(RPCDefaultMessage.NAME, id);
|
|
172 |
173
| params.put(RPCDefaultMessage.COMPRESSED, "true");
|
|
173 |
173
| params.put(RPCDefaultMessage.TIMESTAMP, Long.toString(symsDeserializer.getLastModified()));
|
|
174 |
| |
|
175 |
173
| Object result = runRemoteCommand("GetResource", params);
|
|
176 |
| |
|
177 |
173
| if (result == null) {
|
|
178 |
| |
|
179 |
5
| return null;
|
|
180 |
| |
|
181 |
168
| } else if (result instanceof Map) {
|
|
182 |
| |
|
183 |
166
| Map compressed = (Map) result;
|
|
184 |
166
| SymbolTable syms = symsDeserializer.getSymbols(compressed);
|
|
185 |
166
| return new XMLResourceImpl(id, id, this, syms, (byte[]) compressed.get("document"));
|
|
186 |
| |
|
187 |
2
| } else if (result instanceof byte[]) {
|
|
188 |
| |
|
189 |
2
| return new BinaryResourceImpl(id, this, (byte[]) result);
|
|
190 |
| |
|
191 |
| } else { |
|
192 |
| |
|
193 |
0
| return new XMLResourceImpl(id, this, (String) result);
|
|
194 |
| } |
|
195 |
| |
|
196 |
| } catch (XMLDBException x) { |
|
197 |
| |
|
198 |
0
| throw x;
|
|
199 |
| } catch (Exception e) { |
|
200 |
| |
|
201 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
202 |
| } |
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
21
| public int getResourceCount() throws XMLDBException {
|
|
217 |
| |
|
218 |
21
| checkOpen();
|
|
219 |
21
| try {
|
|
220 |
| |
|
221 |
21
| Map params = new HashMap();
|
|
222 |
21
| params.put(RPCDefaultMessage.COLLECTION, collPath);
|
|
223 |
21
| return ((Integer) runRemoteCommand("GetDocumentCount", params)).intValue();
|
|
224 |
| } catch (XMLDBException x) { |
|
225 |
| |
|
226 |
0
| throw x;
|
|
227 |
| } catch (Exception e) { |
|
228 |
| |
|
229 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
230 |
| } |
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
406
| public void storeResource(Resource res) throws XMLDBException {
|
|
248 |
| |
|
249 |
406
| if (res.getContent() == null) {
|
|
250 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data");
|
|
251 |
| } |
|
252 |
| |
|
253 |
406
| checkOpen();
|
|
254 |
406
| try {
|
|
255 |
406
| Map params = createParams(res);
|
|
256 |
406
| String name = (String) runRemoteCommand("SetResource", params);
|
|
257 |
406
| seResourcetId(res, name);
|
|
258 |
| } catch (XMLDBException x) { |
|
259 |
0
| throw x;
|
|
260 |
| } catch (Exception e) { |
|
261 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
262 |
| } |
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
2
| public void insertResource(Resource res) throws XMLDBException {
|
|
278 |
| |
|
279 |
2
| if (res.getContent() == null) {
|
|
280 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data");
|
|
281 |
| } |
|
282 |
| |
|
283 |
2
| checkOpen();
|
|
284 |
2
| try {
|
|
285 |
2
| Map params = createParams(res);
|
|
286 |
2
| String name = (String) runRemoteCommand("InsertResource", params);
|
|
287 |
1
| seResourcetId(res, name);
|
|
288 |
| } catch (XMLDBException x) { |
|
289 |
1
| throw x;
|
|
290 |
| } catch (Exception e) { |
|
291 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
292 |
| } |
|
293 |
| } |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
2
| public void updateResource(Resource res) throws XMLDBException {
|
|
308 |
| |
|
309 |
2
| if (res.getContent() == null) {
|
|
310 |
0
| throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data");
|
|
311 |
| } |
|
312 |
| |
|
313 |
2
| checkOpen();
|
|
314 |
2
| try {
|
|
315 |
2
| Map params = createParams(res);
|
|
316 |
2
| String name = (String) runRemoteCommand("UpdateResource", params);
|
|
317 |
1
| seResourcetId(res, name);
|
|
318 |
| } catch (XMLDBException x) { |
|
319 |
1
| throw x;
|
|
320 |
| } catch (Exception e) { |
|
321 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
322 |
| } |
|
323 |
| } |
|
324 |
| |
|
325 |
410
| private Map createParams(Resource res) throws XMLDBException {
|
|
326 |
410
| Map params = new HashMap();
|
|
327 |
410
| params.put(RPCDefaultMessage.COLLECTION, collPath);
|
|
328 |
410
| params.put(RPCDefaultMessage.NAME, res.getId());
|
|
329 |
410
| params.put(RPCDefaultMessage.DOCUMENT, res.getContent());
|
|
330 |
410
| return params;
|
|
331 |
| } |
|
332 |
| |
|
333 |
408
| private void seResourcetId(Resource res, String name) {
|
|
334 |
408
| if (res instanceof XMLResource) {
|
|
335 |
404
| ((XMLResourceImpl) res).setId(name);
|
|
336 |
| } else { |
|
337 |
4
| ((BinaryResourceImpl) res).setId(name);
|
|
338 |
| } |
|
339 |
| } |
|
340 |
| |
|
341 |
| |
|
342 |
1622
| public boolean isOpen() {
|
|
343 |
| |
|
344 |
1622
| return (client != null);
|
|
345 |
| } |
|
346 |
| |
|
347 |
| |
|
348 |
0
| public String getURI() {
|
|
349 |
| |
|
350 |
0
| return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://" +
|
|
351 |
| getURL().getHost() + ':' + getURL().getPort() + |
|
352 |
| collPath; |
|
353 |
| } |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
127
| public Collection getChildCollection(String name) throws XMLDBException {
|
|
368 |
| |
|
369 |
127
| if (name.indexOf('/') != -1) {
|
|
370 |
1
| throw new XMLDBException(ErrorCodes.INVALID_COLLECTION);
|
|
371 |
| } |
|
372 |
| |
|
373 |
126
| try {
|
|
374 |
126
| return new CollectionImpl(client, collPath + "/" + name);
|
|
375 |
| } catch (XMLDBException e) { |
|
376 |
| |
|
377 |
0
| if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) {
|
|
378 |
| |
|
379 |
0
| return null;
|
|
380 |
| } |
|
381 |
| |
|
382 |
0
| throw e;
|
|
383 |
| } |
|
384 |
| } |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
4
| public String createId() throws XMLDBException {
|
|
397 |
| |
|
398 |
4
| checkOpen();
|
|
399 |
4
| try {
|
|
400 |
| |
|
401 |
4
| Map params = new HashMap();
|
|
402 |
4
| params.put(RPCDefaultMessage.COLLECTION, collPath);
|
|
403 |
4
| return (String) runRemoteCommand("CreateNewOID", params);
|
|
404 |
| } catch (XMLDBException x) { |
|
405 |
| |
|
406 |
0
| throw x;
|
|
407 |
| } catch (Exception e) { |
|
408 |
| |
|
409 |
0
| throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e);
|
|
410 |
| } |
|
411 |
| } |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
1
| public void close() throws XMLDBException {
|
|
425 |
1
| client = null;
|
|
426 |
| } |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
4
| public Collection getParentCollection() throws XMLDBException {
|
|
440 |
| |
|
441 |
| |
|
442 |
4
| if (collPath.lastIndexOf("/") == 0) {
|
|
443 |
1
| return null;
|
|
444 |
| } |
|
445 |
| |
|
446 |
3
| try {
|
|
447 |
3
| return new CollectionImpl(client, collPath.substring(0, collPath.lastIndexOf('/')));
|
|
448 |
| } catch (XMLDBException e) { |
|
449 |
0
| if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) {
|
|
450 |
| |
|
451 |
0
| return null;
|
|
452 |
| } |
|
453 |
0
| throw e;
|
|
454 |
| } |
|
455 |
| } |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
153
| public void removeResource(Resource res) throws XMLDBException {
|
|
472 |
| |
|
473 |
|