1
2
3
4
5
6
7 package gov.noaa.gdsg.xmldbremote.service;
8
9 import org.xmldb.api.DatabaseManager;
10 import org.xmldb.api.base.Database;
11 import org.xmldb.api.base.Collection;
12 import org.xmldb.api.base.Resource;
13 import org.xmldb.api.base.ResourceSet;
14 import org.xmldb.api.base.Service;
15 import org.xmldb.api.modules.XPathQueryService;
16 import org.xmldb.api.base.XMLDBException;
17
18 import gov.noaa.gdsg.xmldbremote.service.transport.CollectionTransport;
19 import gov.noaa.gdsg.xmldbremote.service.transport.ResourceSetTransport;
20 import gov.noaa.gdsg.xmldbremote.service.transport.ResourceTransport;
21 import gov.noaa.gdsg.xmldbremote.service.transport.ServiceTransport;
22
23 import javax.servlet.ServletContext;
24 import javax.xml.rpc.server.ServiceLifecycle;
25 import javax.xml.rpc.server.ServletEndpointContext;
26 import org.apache.log4j.Logger;
27
28 import java.util.Map;
29 import java.util.HashMap;
30
31 /***
32 * This service is meant for session scope. It keeps track of the state
33 * the session is in and is meant as a functional interface to the XML:DB
34 * Database API.
35 *
36 * @version $Id: XmlDbRemoteService.java,v 1.2 2004/11/04 00:06:00 mrxtravis Exp $
37 * @author tns
38 */
39 public class XmlDbRemoteService implements ServiceLifecycle {
40
41 private static Logger log = Logger.getLogger(XmlDbRemoteException.class);
42
43 public final static String databaseKey = "org.xmldb.api.base.Database";
44
45
46 /***
47 * Handler Collectio Object.
48 */
49 protected CollectionHandler collectionHandler = new CollectionHandler();
50 /***
51 * Handler for resrouce object.
52 */
53 protected ResourceHandler resourceHandler = new ResourceHandler();
54 /***
55 * Handler for ResourceSet objects.
56 */
57 protected ResourceSetHandler resourceSetHandler = new ResourceSetHandler();
58 /***
59 * Handler for Service objects.
60 */
61 protected ServiceHandler serviceHandler = new ServiceHandler();
62
63 private ServletContext servletContext = null;
64
65 /***
66 * The database this service wraps.
67 */
68 protected Database database = null;
69
70 /***
71 * Creates a new instance of XmlDbRemoteService
72 * @throws gov.noaa.gdsg.xmldbremote.service.XmlDbRemoteException If it cannot find a database in the database manager.
73 */
74 public XmlDbRemoteService() {
75 }
76
77 public void destroy() {
78
79 this.collectionHandler.closeAllCollections();
80 this.database = null;
81 }
82
83 /*** Grabs the Database from the servlet context with attribute "org.xmldb.api.base.Database".
84 *@param context The ServletEndpiontContext
85 */
86 public void init(Object context) throws javax.xml.rpc.ServiceException {
87 ServletEndpointContext soapContext = (ServletEndpointContext) context;
88 servletContext = soapContext.getServletContext();
89 database = (Database) servletContext.getAttribute(databaseKey);
90 if (database == null){
91 throw new javax.xml.rpc.ServiceException("Database not found " +
92 "using attribute " + databaseKey );
93 }
94 }
95
96
97 /***
98 * Determines if this database will accept the specified URI.
99 * @param uri The URI in which to check.
100 * @throws XmlDbRemoteException Rethrows whatever the DB throws.
101 * @return True if the DB accepts the URI, false otherwise.
102 */
103 public boolean acceptsURI(String uri) throws XmlDbRemoteException {
104 try {
105 return this.database.acceptsURI(uri);
106 } catch (XMLDBException e){
107 throw new XmlDbRemoteException(e);
108 }
109 }
110
111 /***
112 * A wrapper call to the {@see Database} object.
113 * @throws XmlDbRemoteException Throws whatever the DB throws.
114 * @return Whatever the DB returns.
115 */
116 public String getConformanceLevel() throws XmlDbRemoteException {
117 try {
118 return this.database.getConformanceLevel();
119 } catch (XMLDBException e){
120 throw new XmlDbRemoteException(e);
121 }
122
123 }
124
125 /***
126 * A wrapped call to the {@see Database} object.
127 * @throws XmlDbRemoteException Whatever the DB throws.
128 * @return Whatever the DB returns.
129 */
130 public String getName() throws XmlDbRemoteException {
131 try {
132 return this.database.getName();
133 } catch (XMLDBException e){
134 throw new XmlDbRemoteException(e);
135 }
136 }
137
138
139
140 /***
141 * Returns the collection associated
142 * @param uri The uri of the collectioin.
143 * @param userName The user name to log in with
144 * @param password The password used to log in with
145 * @throws XmlDbRemoteException Whatever the wrapped method throws.
146 * @return Whatever the wrapped method returns.
147 */
148 public CollectionTransport getCollection(String uri, String userName,
149 String password) throws XmlDbRemoteException {
150 try {
151 log.debug("Attempting to retrieve collection. URI:" + uri + " userName:" + userName + " password:" + password);
152 Collection collection = this.database.getCollection(uri, userName, password);
153 if (collection == null){
154
155 throw new NullPointerException("Collection not found and exception not thrown");
156 }
157
158 return (CollectionTransport) this.collectionHandler.saveForSession(collection);
159 } catch (XMLDBException e){
160 throw new XmlDbRemoteException(e);
161 }
162 }
163
164
165 /***
166 * Wrapper for the {@see Database#close} method.
167 * @param transport The object representing the collection to close
168 * @throws XmlDbRemoteException Whatever the wrapped method throws.
169 */
170 public void close(CollectionTransport transport) throws XmlDbRemoteException {
171 try {
172 this.collectionHandler.close(transport);
173 } catch (XMLDBException e){
174 throw new XmlDbRemoteException(e);
175 }
176
177 }
178
179 /***
180 * Wrapper for the {@see Database#createId} method.
181 * @param transport The object representing the collection to close
182 * @throws XmlDbRemoteException Whatever the wrapped method throws.
183 * @return Whatever the wrapped method returns.
184 */
185 public String createId(CollectionTransport transport) throws XmlDbRemoteException {
186 try {
187 return this.collectionHandler.createId(transport);
188 } catch (XMLDBException e){
189 throw new XmlDbRemoteException(e);
190 }
191 }
192
193 /***
194 * Calls {@see CollectionHandler#createResource} and the {@see ResourceHandler#saveForSession}
195 * with the resulting {@see Resource}.
196 * @param transport The object representing the collection to close
197 * @param id The id of the new resource
198 * @param type The type of resource to create
199 * @throws XmlDbRemoteException Whatever the wrapped method throws.
200 * @return Whatever the wrapped method returns.
201 */
202 public ResourceTransport createResource(CollectionTransport transport,
203 String id, String type) throws XmlDbRemoteException {
204 try {
205 Resource resource = this.collectionHandler.createResource(transport, id, type);
206 return (ResourceTransport) this.resourceHandler.saveForSession(resource);
207 } catch (XMLDBException e){
208 throw new XmlDbRemoteException(e);
209 }
210
211 }
212
213
214
215 /***
216 * Calls {@see CollectionHander#getChildCollection} and then
217 * {@see CollectionHandler#saveForSession} with the resulting {@see Collection}.
218 * @param transport Pass to wraped method.
219 * @param name Passed olong.
220 * @throws XmlDbRemoteException Whatever the wrapped method throws.
221 * @return Whatever the wrapped method returns.
222 */
223 public CollectionTransport getChildCollection(CollectionTransport transport, String name) throws XmlDbRemoteException {
224 try {
225 Collection child = this.collectionHandler.getChildCollection(transport, name);
226 return (CollectionTransport) this.collectionHandler.saveForSession(child);
227 } catch (XMLDBException e){
228 throw new XmlDbRemoteException(e);
229 }
230
231 }
232
233 /***
234 * Wrapper for {@see Collection#getCildCollectionCount}.
235 * @param transport Pass along.
236 * @throws XmlDbRemoteException Whatever the wrapped method throws.
237 * @return Whatever the wrapped method returns.
238 */
239 public int getChildCollectionCount(CollectionTransport transport) throws XmlDbRemoteException {
240 try {
241 return this.collectionHandler.getChildCollectionCount(transport);
242 } catch (XMLDBException e){
243 throw new XmlDbRemoteException(e);
244 }
245
246 }
247
248
249 /***
250 * Wraps the {@see CollectionHandler#getParentCollection} and the
251 * calls {@see CollectionHandler#saveForSession} with the resulting {@see Collection}.
252 * @return transport The object representing the collection
253 * @param transport Passed along.
254 * @throws XmlDbRemoteException Whatever the wrapped method throws.
255 */
256 public CollectionTransport getParentCollection(CollectionTransport transport) throws XmlDbRemoteException {
257 try {
258 Collection collection = this.collectionHandler.getParentCollection(transport);
259 return (CollectionTransport) this.collectionHandler.saveForSession(collection);
260 } catch (XMLDBException e){
261 throw new XmlDbRemoteException(e);
262 }
263
264 }
265
266 /***
267 * Wraps the {@see CollectionHandler#getResource} method and calls
268 * {@see ResourceHandler#saveForSession} with the resulting {@see Resource}.
269 * @return Whatever the wrapped method returns.
270 * @param transport Passed along
271 * @param id Passed along.
272 * @throws XmlDbRemoteException Whatever the wrapped method throws.
273 */
274 public ResourceTransport getResource(CollectionTransport transport, String id) throws XmlDbRemoteException {
275 try {
276 Resource resource = this.collectionHandler.getResource(transport, id);
277 return (ResourceTransport) this.resourceHandler.saveForSession(resource);
278 } catch (XMLDBException e){
279 throw new XmlDbRemoteException(e);
280 }
281
282 }
283
284 /***
285 * Wrapped the {@see CollectionHandler#getResourcecount} method. Returns the number of resources in the specified collection
286 * @param transport Passed along.
287 * @throws XmlDbRemoteException Whatever the wrapped method throws.
288 * @return Whatever the wrapped method returns.
289 */
290 public int getResourceCount(CollectionTransport transport) throws XmlDbRemoteException {
291 try {
292 return this.collectionHandler.getResourceCount(transport);
293 } catch (XMLDBException e){
294 throw new XmlDbRemoteException(e);
295 }
296
297 }
298
299 /***
300 * Wrap the {@see CollectionHandler#getService} method then
301 * calls {@see ServiceHandler#saveForSession} with the resulting {@see Service}.
302 * @param transport Passed along.
303 * @param version Passed along.
304 * @param name Passed along.
305 * @throws XmlDbRemoteException Whatever the wrapped method throws.
306 * @return Whatever the wrapped method returns.
307 */
308 public ServiceTransport getService(CollectionTransport transport, String name, String version) throws XmlDbRemoteException {
309 try {
310 Service service = this.collectionHandler.getService(transport,name, version);
311 return (ServiceTransport) this.serviceHandler.saveForSession(service);
312 } catch (XMLDBException e){
313 throw new XmlDbRemoteException(e);
314 }
315
316 }
317
318
319 /***
320 * Wraps the {@see CollectionHandler#getServices} method and
321 * the calls {@see SercieHandler#saveForSession} with the resulting
322 * {@see Service} object. Returns the services of the specified collection
323 * @param transport Passed along.
324 * @throws XmlDbRemoteException Whatever the wrapped method throws.
325 * @return Whatever the wrapped method returns.
326 */
327 public ServiceTransport[] getServices(CollectionTransport transport) throws XmlDbRemoteException {
328 try {
329 Service[] services = this.collectionHandler.getServices(transport);
330 ServiceTransport[] transports = new ServiceTransport[services.length];
331 for (int i = 0; i < services.length; i++){
332 transports[i] = (ServiceTransport) this.serviceHandler.saveForSession(services[i]);
333 }
334 return transports;
335 } catch (XMLDBException e){
336 throw new XmlDbRemoteException(e);
337 }
338
339
340 }
341
342 /***
343 * Wraps the {@see CollectionHandler#isOpen} method. Returns true if the Collection is open, false otherwise
344 * @param transport Passed along.
345 * @throws XmlDbRemoteException Whatever the wrapped method throws.
346 * @return Whatever the wrapped method returns.
347 */
348 public boolean isOpen(CollectionTransport transport) throws XmlDbRemoteException {
349 try {
350 return this.collectionHandler.isOpen(transport);
351 } catch (XMLDBException e){
352 throw new XmlDbRemoteException(e);
353 }
354
355 }
356
357 /***
358 * Wrapps the {@see CollectionHander#listChildCollections} method. Returns a list of collection names naming all child collections of the
359 * specified collection
360 * @param transport Passed along.
361 * @throws XmlDbRemoteException Whatever the wrapped method throws.
362 * @return Whatever the wrapped method returns.
363 */
364 public String[] listChildCollections(CollectionTransport transport) throws XmlDbRemoteException {
365 try {
366 return this.collectionHandler.listChildCollections(transport);
367 } catch (XMLDBException e){
368 throw new XmlDbRemoteException(e);
369 }
370
371 }
372
373 /***
374 * Wraps the {@see CollectionHandler#listResources} method. Returns a list of the ids for all resources store in the specified
375 * collection.
376 * @param transport Passed along.
377 * @throws XmlDbRemoteException Whatever the wrapped method throws.
378 * @return Whatever the wrapped method returns.
379 */
380 public String[] listResources(CollectionTransport transport) throws XmlDbRemoteException {
381 try {
382 return this.collectionHandler.listResources(transport);
383 } catch (XMLDBException e){
384 throw new XmlDbRemoteException(e);
385 }
386
387 }
388
389 /***
390 * Wraps the {@see CollectionHandler#storeResource} method. Stores the provided resource.
391 * @param transport Passed along.
392 * @param resourceTransport Passed along.
393 * @throws XmlDbRemoteException Whatever the wrapped method throws.
394 */
395 public void storeResource(CollectionTransport transport, ResourceTransport resourceTransport) throws XmlDbRemoteException {
396 try {
397 Resource resource = (Resource) this.resourceHandler.getObjectFromSession(resourceTransport);
398 this.collectionHandler.storeResource(transport, resource);
399 } catch (XMLDBException e){
400 throw new XmlDbRemoteException(e);
401 }
402
403 }
404
405 /***
406 * Wraps the {@see CollectionHandler#removeResource} method. Removes the resource.
407 * @param transport Passed along.
408 * @param resourceTransport Passed along.
409 * @throws XmlDbRemoteException Whatever the wrapped method throws.
410 */
411 public void removeResource(CollectionTransport transport, ResourceTransport resourceTransport) throws XmlDbRemoteException {
412 try {
413 Resource resource = (Resource) this.resourceHandler.getObjectFromSession(resourceTransport);
414 this.collectionHandler.removeResource(transport, resource);
415 } catch (XMLDBException e){
416 throw new XmlDbRemoteException(e);
417 }
418
419 }
420
421
422
423 /***
424 * Wraps the {@see ResourceSetHandler#addResource} method. Adds a resource instance to the set
425 * @param transport Passed along.
426 * @param resourceTransport Passed along.
427 * @throws XmlDbRemoteException Whatever the wrapped method throws.
428 */
429 public void addResource(ResourceSetTransport transport, ResourceTransport resourceTransport) throws XmlDbRemoteException {
430 try {
431 Resource resource = (Resource) this.resourceHandler.getObjectFromSession(resourceTransport);
432 this.resourceSetHandler.addResource(transport, resource);
433 } catch (XMLDBException e){
434 throw new XmlDbRemoteException(e);
435 }
436
437 }
438
439 /***
440 * Wraps the {@see ResourceSetHandler#clear} method.
441 * @param transport Passed along.
442 * @throws XmlDbRemoteException You always take whats left behind
443 * You never seem to mind
444 * You always take whats left behind
445 * But you're never mine
446 */
447 public void clear(ResourceSetTransport transport) throws XmlDbRemoteException {
448 try {
449 this.resourceSetHandler.clear(transport);
450 } catch (XMLDBException e){
451 throw new XmlDbRemoteException(e);
452 }
453
454 }
455
456 /***
457 * Wraps the {@ResourceGet#getMembersAsResource} method.
458 * Iiterator should be handled on the client
459 * @param transport Passed along.
460 * @throws XmlDbRemoteException Whatever the wrapped method throws.
461 * @return Whatever the wrapped method returns.
462 */
463 public ResourceTransport getMembersAsResource(ResourceSetTransport transport) throws XmlDbRemoteException {
464 try {
465 Resource resource = this.resourceSetHandler.getMembersAsResource(transport);
466 return (ResourceTransport) this.resourceHandler.saveForSession(resource);
467 } catch (XMLDBException e){
468 throw new XmlDbRemoteException(e);
469 }
470
471 }
472
473 /***
474 * Wraps the {@see ResourceSetHandler#getResource} method.
475 * @param transport Passed along.
476 * @param index Passed along.
477 * @throws XmlDbRemoteException Whatever the wrapped method throws.
478 * @return Whatever the wrapped method returns.
479 */
480 public ResourceTransport getResource(ResourceSetTransport transport, long index) throws XmlDbRemoteException {
481 try {
482 Resource resource = this.resourceSetHandler.getResource(transport,index);
483 return (ResourceTransport) this.resourceHandler.saveForSession(resource);
484 } catch (XMLDBException e){
485 throw new XmlDbRemoteException(e);
486 }
487
488 }
489
490 /***
491 * Wraps the {@see ResourceSetHandler@getSize} method.
492 * @param transport Passed along.
493 * @throws XmlDbRemoteException Whatever the wrapped method throws.
494 * @return Whatever the wrapped method thorws.
495 */
496 public long getSize(ResourceSetTransport transport) throws XmlDbRemoteException {
497 try {
498 return this.resourceSetHandler.getSize(transport);
499 } catch (XMLDBException e){
500 throw new XmlDbRemoteException(e);
501 }
502
503 }
504
505 /***
506 * Wraps the {@see ResourceSetHandler#removeResource} method.
507 * @param transport Passed along.
508 * @param index Passed along.
509 * @throws XmlDbRemoteException Whatever the wrapped method throws.
510 */
511 public void removeResource(ResourceSetTransport transport, long index) throws XmlDbRemoteException {
512 try {
513 this.resourceSetHandler.removeResource(transport,index);
514 } catch (XMLDBException e){
515 throw new XmlDbRemoteException(e);
516 }
517
518 }
519
520 /***
521 * Wraps the {@see ResourceHandler#getContentAsDOMText} method.
522 * @param transport Passed along.
523 * @throws XmlDbRemoteException Whatever the wrapped method thorws.
524 * @return Whatever the wrapped method returns.
525 */
526 public String getContentAsDOMText(ResourceTransport transport) throws XmlDbRemoteException {
527 try {
528 return this.resourceHandler.getContentAsDOMText(transport);
529 } catch (XMLDBException e){
530 throw new XmlDbRemoteException(e);
531 }
532
533 }
534
535 /***
536 * Wraps the {@see ResourceHandler#getDocumentId) method.
537 * @param transport Passed along.
538 * @throws XmlDbRemoteException Whatever the wrapped method throws.
539 * @return Whatever the wrapped method returns.
540 */
541
542 public String getDocumentId(ResourceTransport transport) throws XmlDbRemoteException {
543 try {
544 return this.resourceHandler.getDocumentId(transport);
545 } catch (XMLDBException e){
546 throw new XmlDbRemoteException(e);
547 }
548
549 }
550
551
552
553 /***
554 * Wraps the {@see ResourceHandler#setContentAsDOMText}.
555 * @param transport Passed along.
556 * @param xml Passed along.
557 * @throws XmlDbRemoteException Whatever the wrapped method throws.
558 */
559 public void setContentAsDOMText(ResourceTransport transport,String xml) throws XmlDbRemoteException {
560 try {
561 this.resourceHandler.setContentAsDOMText(transport,xml);
562 } catch (XMLDBException e){
563 throw new XmlDbRemoteException(e);
564 }
565
566 }
567
568
569
570
571
572
573
574 /***
575 * Wraps the {@see Resource#getParentCollection} method.
576 * @param transport Passed along.
577 * @throws XmlDbRemoteException Whatever the wrapped method throws.
578 * @return Whatever the wrapped method returns.
579 */
580 public CollectionTransport getParentCollection(ResourceTransport transport) throws XmlDbRemoteException {
581 try {
582 Collection collection = this.resourceHandler.getParentCollection(transport);
583 return (CollectionTransport) this.collectionHandler.saveForSession(collection);
584 } catch (XMLDBException e){
585 throw new XmlDbRemoteException(e);
586 }
587
588 }
589
590 /***
591 * Wrapper to {@see ServiceHandler#clearNamespaces} method.
592 * @param transport Passed along.
593 * @throws XmlDbRemoteException Whatever the wrapped method throws.
594 */
595 public void clearNamespaces(ServiceTransport transport) throws XmlDbRemoteException {
596 try {
597 this.serviceHandler.clearNamespaces(transport);
598 } catch (XMLDBException e){
599 throw new XmlDbRemoteException(e);
600 }
601
602 }
603
604 /***
605 * Wraps the {@see ServiceHandler#getNamespace} method.
606 * @param transport Passed along.
607 * @param prefix Passed along.
608 * @throws XmlDbRemoteException Whatever the wrapped method throws.
609 * @return Whatever the wrapped method returns.
610 */
611 public String getNamespace(ServiceTransport transport, String prefix) throws XmlDbRemoteException {
612 try {
613 return this.serviceHandler.getNamespace(transport,prefix);
614 } catch (XMLDBException e){
615 throw new XmlDbRemoteException(e);
616 }
617
618 }
619
620 /***
621 * Wraps the {@see ServiceHandler#query} method.
622 * @param transport Passed along.
623 * @param query Passed along.
624 * @throws XmlDbRemoteException Whatever the wrapped method throws.
625 * @return Whatever the wrapped method returns.
626 */
627 public ResourceSetTransport query(ServiceTransport transport, String query) throws XmlDbRemoteException {
628 try {
629 ResourceSet resourceSet = this.serviceHandler.query(transport,query);
630 return (ResourceSetTransport) this.resourceSetHandler.saveForSession(resourceSet);
631 } catch (XMLDBException e){
632 throw new XmlDbRemoteException(e);
633 }
634 }
635
636 /***
637 * Wraps the {@see ServiceHandler#queyResource} method.
638 * @param transport Passed along.
639 * @param id Passed along.
640 * @param query Passed along.
641 * @throws XmlDbRemoteException Whatever the wrapped method throws.
642 * @return Whatever the wrapped method returns.
643 */
644 public ResourceSetTransport queryResource(ServiceTransport transport, String id, String query) throws XmlDbRemoteException {
645 try {
646 ResourceSet resourceSet = this.serviceHandler.queryResource(transport,id,query);
647 return (ResourceSetTransport) this.resourceSetHandler.saveForSession(resourceSet);
648 } catch (XMLDBException e){
649 throw new XmlDbRemoteException(e);
650 }
651
652 }
653
654 /***
655 * Wraps the {@see ServiceHandler#removeNamespace} method.
656 * @param transport Passed along.
657 * @param prefix Passed along.
658 * @throws XmlDbRemoteException Whatever the wrapped method throws.
659 */
660 public void removeNamespace(ServiceTransport transport, String prefix) throws XmlDbRemoteException {
661 try {
662 this.serviceHandler.removeNamespace(transport,prefix);
663 } catch (XMLDBException e){
664 throw new XmlDbRemoteException(e);
665 }
666
667 }
668
669 /***
670 * Wraps the {@see ServiceHandler#setNamespace} method.
671 * @param transport Passed along.
672 * @param prefix Passed along.
673 * @param uri Passed along.
674 * @throws XmlDbRemoteException Whatever the wrapped method throws.
675 */
676 public void setNamespace(ServiceTransport transport, String prefix, String uri) throws XmlDbRemoteException {
677 try {
678 this.serviceHandler.setNamespace(transport,prefix,uri);
679 } catch (XMLDBException e){
680 throw new XmlDbRemoteException(e);
681 }
682
683 }
684
685
686
687
688 /***
689 * Wraps the {@see ServiceHandler#setCollection} method.
690 * @param transport Passed along.
691 * @param collectionTransport Passed along.
692 * @throws XmlDbRemoteException Whatever the wrapped method thorws.
693 */
694 public void setCollection(ServiceTransport transport, CollectionTransport collectionTransport) throws XmlDbRemoteException {
695 try {
696 Collection collection = (Collection) this.collectionHandler.getObjectFromSession(collectionTransport);
697 this.serviceHandler.setCollection(transport,collection);
698 } catch (XMLDBException e){
699 throw new XmlDbRemoteException(e);
700 }
701
702 }
703
704
705 }