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 package org.astrogrid.mySpace.mySpaceManager ;
91
92 import org.astrogrid.mySpace.mySpaceStatus.Logger ;
93
94 import java.net.URL ;
95 import java.net.URISyntaxException ;
96 import java.net.MalformedURLException ;
97
98 import org.astrogrid.store.Ivorn ;
99
100 import org.astrogrid.filestore.client.FileStoreDelegate ;
101
102 import org.astrogrid.filestore.common.file.FileProperty ;
103 import org.astrogrid.filestore.common.file.FileProperties ;
104
105 import org.astrogrid.filestore.common.transfer.UrlGetTransfer ;
106
107 import org.astrogrid.filestore.resolver.FileStoreDelegateResolver ;
108 import org.astrogrid.filestore.resolver.FileStoreDelegateResolverImpl ;
109 import org.astrogrid.filestore.resolver.FileStoreResolverException ;
110
111 import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
112
113 import org.astrogrid.filestore.common.exception.FileStoreException ;
114 import org.astrogrid.filestore.common.exception.FileStoreNotFoundException ;
115 import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
116 import org.astrogrid.filestore.common.exception.FileStoreServiceException ;
117 import org.astrogrid.filestore.common.exception.FileStoreTransferException ;
118
119
120 /***
121 * A wrapper class to integrate the FileStore delegate into the MySpaceManager.
122 *
123 */
124 public class FileStoreDriver
125 {
126 /***
127 * Switch for our debug statements.
128 *
129 */
130 protected static final boolean DEBUG_FLAG = true ;
131
132 /***
133 * Property key for the myspace file name.
134 *
135 */
136 public static final String MYSPACE_NAME_PROPERTY = "org.astrogrid.myspace.name" ;
137
138 /***
139 * Property key for the myspace file ident.
140 *
141 */
142 public static final String MYSPACE_IDENT_PROPERTY = "org.astrogrid.myspace.ident" ;
143
144 /***
145 * A reference to our FileStore delegate.
146 *
147 */
148 private FileStoreDelegate filestore ;
149
150 /***
151 * Public constructor using a specific filestore delegate.
152 * @paran filestore Our filestore delegate.
153 *
154 */
155 public FileStoreDriver(FileStoreDelegate filestore)
156 {
157 if (null == filestore)
158 {
159 throw new IllegalArgumentException(
160 "Null filestore delegate"
161 ) ;
162 }
163 this.filestore = filestore ;
164 }
165
166 /***
167 * A factory to create a new FileStoreDriver using the default registry and filestore.
168 * This uses the astrogrid configuration to lookup the default registry URL.
169 * This uses the MMC configurator to lookup the default filestore ivorn.
170 * @throws FileStoreIdentifierException If the identifier is not valid.
171 * @throws FileStoreResolverException If unable to resolve the identifier.
172 *
173 */
174 public static FileStoreDriver create()
175 throws FileStoreResolverException, FileStoreIdentifierException
176 {
177 if (DEBUG_FLAG) System.out.println("") ;
178 if (DEBUG_FLAG) System.out.println("----\"----") ;
179 if (DEBUG_FLAG) System.out.println("FileStoreDriver.create()") ;
180 try {
181 String property = MMC.getProperty(
182 "MYSPACESERVERURL",
183 "MYSPACEMANAGER"
184 ) ;
185 if (DEBUG_FLAG) System.out.println(" property : " + property) ;
186 return create(
187 new Ivorn(
188 property
189 )
190 ) ;
191 }
192 catch(FileStoreResolverException ouch)
193 {
194 throw ouch ;
195 }
196 catch(FileStoreIdentifierException ouch)
197 {
198 throw ouch ;
199 }
200 catch(Throwable ouch)
201 {
202 throw new FileStoreResolverException(
203 "Failed to resolve filestore delegate",
204 ouch
205 ) ;
206 }
207 }
208
209 /***
210 * A factory to create a new FileStoreDriver using the default registry.
211 * This uses the astrogrid configuration to lookup the default registry URL.
212 * @param ivorn The ivorn of the filestore service.
213 * @throws FileStoreIdentifierException If the identifier is not valid.
214 * @throws FileStoreResolverException If unable to resolve the identifier.
215 *
216 */
217 public static FileStoreDriver create(String ivorn)
218 throws FileStoreResolverException, FileStoreIdentifierException
219 {
220 if (DEBUG_FLAG) System.out.println("") ;
221 if (DEBUG_FLAG) System.out.println("----\"----") ;
222 if (DEBUG_FLAG) System.out.println("FileStoreDriver.create()") ;
223 if (DEBUG_FLAG) System.out.println(" Ivorn : " + ivorn) ;
224 try {
225 return create(
226 new Ivorn(
227 ivorn
228 )
229 ) ;
230 }
231 catch (URISyntaxException ouch)
232 {
233 throw new FileStoreIdentifierException(
234 ouch
235 ) ;
236 }
237 }
238
239 /***
240 * A factory to create a new FileStoreDriver using the default registry.
241 * This uses the astrogrid configuration to lookup the default registry URL.
242 * @param ivorn The ivorn of the filestore service.
243 * @throws FileStoreIdentifierException If the identifier is not valid.
244 * @throws FileStoreResolverException If unable to resolve the identifier.
245 *
246 */
247 public static FileStoreDriver create(Ivorn ivorn)
248 throws FileStoreResolverException, FileStoreIdentifierException
249 {
250 if (DEBUG_FLAG) System.out.println("") ;
251 if (DEBUG_FLAG) System.out.println("----\"----") ;
252 if (DEBUG_FLAG) System.out.println("FileStoreDriver.create()") ;
253 if (DEBUG_FLAG) System.out.println(" Ivorn : " + ivorn) ;
254
255
256 FileStoreDelegateResolver resolver =
257 new FileStoreDelegateResolverImpl() ;
258
259
260 return new FileStoreDriver(
261 resolver.resolve(
262 ivorn
263 )
264 ) ;
265 }
266
267 /***
268 * A factory to create a new FileStoreDriver.
269 * @paran ivorn The ivorn for our FileStore service.
270 * @paran registry The endpoint URL for our registry service.
271 * @throws FileStoreIdentifierException If the identifier is not valid.
272 * @throws FileStoreResolverException If unable to resolve the identifier.
273 *
274 */
275 public static FileStoreDriver create(Ivorn ivorn, URL registry)
276 throws FileStoreResolverException, FileStoreIdentifierException
277 {
278 if (DEBUG_FLAG) System.out.println("") ;
279 if (DEBUG_FLAG) System.out.println("----\"----") ;
280 if (DEBUG_FLAG) System.out.println("FileStoreDriver.create()") ;
281 if (DEBUG_FLAG) System.out.println(" Registry : " + registry) ;
282 if (DEBUG_FLAG) System.out.println(" Ivorn : " + ivorn) ;
283
284
285 FileStoreDelegateResolver resolver =
286 new FileStoreDelegateResolverImpl(
287 registry
288 ) ;
289
290
291 return new FileStoreDriver(
292 resolver.resolve(
293 ivorn
294 )
295 ) ;
296 }
297
298 /***
299 * Import (store) a string in a new file.
300 * @param item The data item record for the data.
301 * @param data The string of data to store.
302 * @throws FileStoreException if the data string is null.
303 * @throws FileStoreServiceException if unable handle the request.
304 *
305 */
306 public void importString(DataItemRecord item, String data)
307 throws FileStoreServiceException, FileStoreException
308 {
309 if (DEBUG_FLAG) System.out.println("") ;
310 if (DEBUG_FLAG) System.out.println("----\"----") ;
311 if (DEBUG_FLAG) System.out.println("FileStoreDriver.importString()") ;
312
313
314 updateDataItem(
315 item,
316 filestore.importString(
317 initProperties(item),
318 data
319 )
320 ) ;
321 }
322
323 /***
324 * Import (store) an array of bytes in a new file.
325 * @param item The data item record for the data.
326 * @param data The data to store.
327 * @throws FileStoreException if the data string is null.
328 * @throws FileStoreServiceException if unable handle the request.
329 *
330 */
331 public void importBytes(DataItemRecord item, byte[] data)
332 throws FileStoreServiceException, FileStoreException
333 {
334 if (DEBUG_FLAG) System.out.println("") ;
335 if (DEBUG_FLAG) System.out.println("----\"----") ;
336 if (DEBUG_FLAG) System.out.println("FileStoreDriver.importBytes()") ;
337
338
339 updateDataItem(
340 item,
341 filestore.importBytes(
342 initProperties(item),
343 data
344 )
345 ) ;
346 }
347
348 /***
349 * Append (store) a string to an existing file.
350 * @param item The data item record for the data.
351 * @param data The string of data to store.
352 * @throws FileStoreIdentifierException if the identifier is null or not valid.
353 * @throws FileStoreNotFoundException if unable to locate the file.
354 * @throws FileStoreException if the string is null.
355 * @throws FileStoreServiceException if unable handle the request.
356 *
357 */
358 public void appendString(DataItemRecord item, String data)
359 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException, FileStoreException
360 {
361 if (DEBUG_FLAG) System.out.println("") ;
362 if (DEBUG_FLAG) System.out.println("----\"----") ;
363 if (DEBUG_FLAG) System.out.println("FileStoreDriver.appendString()") ;
364
365
366 updateDataItem(
367 item,
368 filestore.appendString(
369 item.getDataItemFile(),
370 data
371 )
372 ) ;
373 }
374
375 /***
376 * Append (store) an array of bytes to an existing file.
377 * @param item The data item record for the data.
378 * @param data The data to store.
379 * @throws FileStoreIdentifierException if the identifier is null or not valid.
380 * @throws FileStoreNotFoundException if unable to locate the file.
381 * @throws FileStoreException if the string is null.
382 * @throws FileStoreServiceException if unable handle the request.
383 *
384 */
385 public void appendBytes(DataItemRecord item, byte[] data)
386 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException, FileStoreException
387 {
388 if (DEBUG_FLAG) System.out.println("") ;
389 if (DEBUG_FLAG) System.out.println("----\"----") ;
390 if (DEBUG_FLAG) System.out.println("FileStoreDriver.appendBytes()") ;
391
392
393 updateDataItem(
394 item,
395 filestore.appendBytes(
396 item.getDataItemFile(),
397 data
398 )
399 ) ;
400 }
401
402 /***
403 * Import (store) the contents of a URL in a new file.
404 * @param item The data item record for the data.
405 * @param source The source URL.
406 * @throws FileStoreServiceException if unable handle the request.
407 * @throws FileStoreTransferException if the transfer properties not valid.
408 *
409 */
410 public void importUrl(DataItemRecord item, URL source)
411 throws FileStoreServiceException, FileStoreTransferException, FileStoreIdentifierException
412 {
413 if (DEBUG_FLAG) System.out.println("") ;
414 if (DEBUG_FLAG) System.out.println("----\"----") ;
415 if (DEBUG_FLAG) System.out.println("FileStoreDriver.importUrl()") ;
416
417
418 updateDataItem(
419 item,
420 filestore.importData(
421 new UrlGetTransfer(
422 source,
423 initProperties(item)
424 )
425 )
426 .getFileProperties()
427 ) ;
428 }
429
430 /***
431 * Get a file contents as a string.
432 * @param item The data item record for the data.
433 * @throws FileStoreIdentifierException if the identifier is null or not valid.
434 * @throws FileStoreNotFoundException if unable to locate the file.
435 * @throws FileStoreServiceException if unable handle the request.
436 *
437 */
438 public String exportString(DataItemRecord item)
439 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException
440 {
441 if (DEBUG_FLAG) System.out.println("") ;
442 if (DEBUG_FLAG) System.out.println("----\"----") ;
443 if (DEBUG_FLAG) System.out.println("FileStoreDriver.exportString()") ;
444 return filestore.exportString(
445 item.getDataItemFile()
446 ) ;
447 }
448
449 /***
450 * Get a file contents as a byte array.
451 * @param item The data item record for the data.
452 * @throws FileStoreIdentifierException if the identifier is null or not valid.
453 * @throws FileStoreNotFoundException if unable to locate the file.
454 * @throws FileStoreServiceException if unable handle the request.
455 *
456 */
457 public byte[] exportBytes(DataItemRecord item)
458 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException
459 {
460 if (DEBUG_FLAG) System.out.println("") ;
461 if (DEBUG_FLAG) System.out.println("----\"----") ;
462 if (DEBUG_FLAG) System.out.println("FileStoreDriver.exportBytes()") ;
463 return filestore.exportBytes(
464 item.getDataItemFile()
465 ) ;
466 }
467
468 /***
469 * Delete data from the store.
470 * @param item The data item record for the data.
471 * @throws FileStoreIdentifierException if the identifier is null or not valid.
472 * @throws FileStoreNotFoundException if unable to locate the file.
473 * @throws FileStoreServiceException if unable handle the request.
474 *
475 *
476 */
477 public void delete(DataItemRecord item)
478 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException
479 {
480 if (DEBUG_FLAG) System.out.println("") ;
481 if (DEBUG_FLAG) System.out.println("----\"----") ;
482 if (DEBUG_FLAG) System.out.println("FileStoreDriver.delete()") ;
483 updateDataItem(
484 item,
485 filestore.delete(
486 item.getDataItemFile()
487 )
488 ) ;
489 }
490
491 /***
492 * Copy a file within the server.
493 * @param source - the data item to copy from.
494 * @param destination - the data item to copy into.
495 * @throws FileStoreTransferException if unable to transfer the data.
496 * @throws FileStoreIdentifierException if the source identifier is null or not valid.
497 * @throws FileStoreNotFoundException if unable to locate the file.
498 * @throws FileStoreServiceException if unable handle the request.
499 *
500 */
501 public void duplicate(DataItemRecord source, DataItemRecord destination)
502 throws FileStoreServiceException, FileStoreIdentifierException, FileStoreNotFoundException, FileStoreTransferException
503 {
504 if (DEBUG_FLAG) System.out.println("") ;
505 if (DEBUG_FLAG) System.out.println("----\"----") ;
506 if (DEBUG_FLAG) System.out.println("FileStoreDriver.duplicate()") ;
507 updateDataItem(
508 destination,
509 filestore.duplicate(
510 source.getDataItemFile(),
511 null
512 )
513 ) ;
514 }
515
516 /***
517 * Update a DataItemRecord from data in an array of FileProperty.
518 * @param item - the myspace data item to update.
519 * @param properties - the original properties from the file store.
520 *
521 */
522 private void updateDataItem(DataItemRecord item, FileProperty[] properties)
523 throws FileStoreIdentifierException
524 {
525 updateDataItem(
526 item,
527 new FileProperties(
528 properties
529 )
530 ) ;
531 }
532
533 /***
534 * Update a DataItemRecord from data in a FileProperties.
535 * @param item - the myspace data item to update.
536 * @param properties - the original properties from the file store.
537 *
538 */
539 private void updateDataItem(DataItemRecord item, FileProperties properties)
540 throws FileStoreIdentifierException
541 {
542
543
544 item.setDataItemFile(
545 properties.getStoreResourceIdent()
546 ) ;
547 item.setDataItemIvorn(
548 properties.getStoreResourceIvorn().toString()
549 ) ;
550 item.setDataItemUri(
551 properties.getStoreResourceUrl().toString()
552 ) ;
553 item.setSize(
554 properties.getContentSize()
555 ) ;
556 item.setDataItemMime(
557 properties.getContentType()
558 ) ;
559 }
560
561 /***
562 * Create an initial set of properties for an item.
563 * This includes the guess for the mime type based on the file name.
564 * @param item The data item record.
565 * @return An array of properties for the item.
566 *
567 */
568 private FileProperty[] initProperties(DataItemRecord item)
569 {
570 if (DEBUG_FLAG) System.out.println("") ;
571 if (DEBUG_FLAG) System.out.println("----\"----") ;
572 if (DEBUG_FLAG) System.out.println("FileStoreDriver.initProperties()") ;
573 if (DEBUG_FLAG) System.out.println(" Name : " + item.getDataItemName()) ;
574 if (DEBUG_FLAG) System.out.println(" Mime : " + item.getDataItemMime()) ;
575
576
577 FileProperties properties = new FileProperties() ;
578
579
580 properties.setProperty(
581 MYSPACE_NAME_PROPERTY,
582 item.getDataItemName()
583 ) ;
584 properties.setProperty(
585 MYSPACE_IDENT_PROPERTY,
586 String.valueOf(item.getDataItemID())
587 ) ;
588
589
590 String mime = item.getDataItemMime() ;
591
592
593 if ("null".equals(mime))
594 {
595 if (DEBUG_FLAG) System.out.println(" Fake null in data item mime type.") ;
596 mime = null ;
597 }
598
599
600 if (null == mime)
601 {
602 if (DEBUG_FLAG) System.out.println(" Null mime type in data item.") ;
603 if (DEBUG_FLAG) System.out.println(" Generating mime type from name.") ;
604
605
606 String name = item.getDataItemName() ;
607
608
609 if (null != name)
610 {
611
612
613 int index = name.lastIndexOf('.') ;
614
615
616 if (index != -1)
617 {
618
619
620 String type = name.substring(
621 index
622 ).toLowerCase() ;
623
624
625 if (".xml".equals(type))
626 {
627 mime = FileProperties.MIME_TYPE_XML ;
628 }
629
630
631 if (".xsl".equals(type))
632 {
633 mime = FileProperties.MIME_TYPE_XML ;
634 }
635
636
637 if (".vot".equals(type))
638 {
639 mime = FileProperties.MIME_TYPE_VOTABLE ;
640 }
641 if (".votable".equals(type))
642 {
643 mime = FileProperties.MIME_TYPE_VOTABLE ;
644 }
645
646
647 if (".vol".equals(type))
648 {
649 mime = FileProperties.MIME_TYPE_VOLIST ;
650 }
651 if (".volist".equals(type))
652 {
653 mime = FileProperties.MIME_TYPE_VOLIST ;
654 }
655
656
657 if (".job".equals(type))
658 {
659 mime = FileProperties.MIME_TYPE_JOB ;
660 }
661
662
663 if (".work".equals(type))
664 {
665 mime = FileProperties.MIME_TYPE_WORKFLOW ;
666 }
667 if (".flow".equals(type))
668 {
669 mime = FileProperties.MIME_TYPE_WORKFLOW ;
670 }
671 if (".workflow".equals(type))
672 {
673 mime = FileProperties.MIME_TYPE_WORKFLOW ;
674 }
675
676
677 if (".adql".equals(type))
678 {
679 mime = FileProperties.MIME_TYPE_ADQL ;
680 }
681 }
682 }
683 }
684
685
686 if (null != mime)
687 {
688
689
690 properties.setProperty(
691 FileProperties.MIME_TYPE_PROPERTY,
692 mime
693 ) ;
694 }
695
696
697 return properties.toArray() ;
698 }
699 }
700