View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filemanager/client/src/java/org/astrogrid/filemanager/client/store/StoreClientWrapper.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2005/01/28 10:43:58 $</cvs:date>
5    * <cvs:version>$Revision: 1.2 $</cvs:version>
6    * <cvs:log>
7    *   $Log: StoreClientWrapper.java,v $
8    *   Revision 1.2  2005/01/28 10:43:58  clq2
9    *   dave_dev_200501141257 (filemanager)
10   *
11   *   Revision 1.1.2.2  2005/01/26 12:24:22  dave
12   *   Removed type from add(), replaced with addNode() and addFile() ...
13   *
14   *   Revision 1.1.2.1  2005/01/22 07:54:16  dave
15   *   Refactored delegate into a separate package ....
16   *
17   *   Revision 1.2.2.2  2005/01/21 13:07:37  dave
18   *   Added exportURL to the node API ...
19   *
20   *   Revision 1.2.2.1  2005/01/21 12:18:54  dave
21   *   Changed input() and output() to exportStream() and importStream() ...
22   *
23   *   Revision 1.2  2005/01/13 17:23:15  jdt
24   *   merges from dave-dev-200412201250
25   *
26   *   Revision 1.1.2.4  2005/01/12 14:20:57  dave
27   *   Replaced tabs with spaces ....
28   *
29   *   Revision 1.1.2.3  2005/01/07 12:49:28  dave
30   *   dded initial get path impl ...
31   *
32   *   Revision 1.1.2.2  2005/01/07 12:18:00  dave
33   *   Added StoreClientWrapperTest
34   *   Added StoreFileWrapper
35   *
36   *   Revision 1.1.2.1  2004/12/22 07:38:36  dave
37   *   Started to move towards StoreClient API ...
38   *
39   * </cvs:log>
40   *
41   */
42  package org.astrogrid.filemanager.client.store;
43  
44  import java.net.URL;
45  
46  import java.io.IOException;
47  import java.io.InputStream;
48  import java.io.OutputStream;
49  import java.io.OutputStreamWriter;
50  import java.io.FileNotFoundException;
51  
52  import org.apache.commons.logging.Log ;
53  import org.apache.commons.logging.LogFactory ;
54  
55  import org.astrogrid.community.User;
56  
57  import org.astrogrid.store.Agsl;
58  import org.astrogrid.store.Msrl;
59  import org.astrogrid.store.Ivorn;
60  import org.astrogrid.store.delegate.StoreFile;
61  import org.astrogrid.store.delegate.StoreClient;
62  
63  import org.astrogrid.filemanager.common.ivorn.FileManagerIvornFactory;
64  import org.astrogrid.filemanager.common.exception.NodeNotFoundException;
65  import org.astrogrid.filemanager.common.exception.DuplicateNodeException;
66  import org.astrogrid.filemanager.common.exception.FileManagerServiceException;
67  import org.astrogrid.filemanager.common.exception.FileManagerIdentifierException;
68  
69  import org.astrogrid.filemanager.client.FileManagerNode;
70  import org.astrogrid.filemanager.client.delegate.FileManagerDelegate;
71  
72  /***
73   * A wrapper for the FileManager delegate to implement the StoreClient API.
74   *
75   */
76  public class StoreClientWrapper
77      implements StoreClient
78      {
79  
80      /***
81       * Our debug logger.
82       *
83       */
84      protected static Log log = LogFactory.getLog(StoreClientWrapper.class);
85  
86      /***
87       * Our Ivorn factory.
88       *
89       */
90      private FileManagerIvornFactory factory = new FileManagerIvornFactory();
91  
92      /***
93       * The FileManager delegate this wrapper is for.
94       *
95       */
96      private FileManagerDelegate delegate ;
97  
98      /***
99       * Public constructor.
100      *
101      */
102     public StoreClientWrapper(FileManagerDelegate delegate)
103         {
104         if (null == delegate)
105             {
106             throw new IllegalArgumentException(
107                 "Null FileManager delegate"
108                 );
109             }
110         this.delegate = delegate ;
111         }
112 
113     /***
114      * The service ivorn.
115      *
116      */
117     private Ivorn ivorn ;
118 
119     /***
120      * Get the service ivorn.
121      *
122      */
123     public Ivorn getIvorn()
124         {
125         log.debug("StoreClientWrapper.getIvorn()");
126         if (null == this.ivorn)
127             {
128             try {
129                 this.ivorn = delegate.getServiceIvorn();
130                 }
131             catch(FileManagerServiceException ouch)
132                 {
133                 log.debug("Failed to get service Ivorn");
134                 }
135             }
136         log.debug("  Ivorn : " + this.ivorn.toString());
137         return this.ivorn ;
138         }
139 
140     /***
141      * The current user ** Not used **.
142      *
143      */
144     private User user ;
145 
146     /***
147      * Returns the user of this delegate.
148      * Not supported, throws UnsupportedOperationException.
149      * @throws UnsupportedOperationException
150      *
151      */
152     public User getOperator()
153         {
154         log.warn("StoreClientWrapper.getOperator()");
155         return this.user ;
156         }
157 
158     /***
159      * Returns the Agsl to the service this client is connected to.
160      * @return An Agsl based on the FileManager Ivorn.
161      * @todo Do something sensible if it can't get the FileManager Ivorn.
162      *
163      */
164     public Agsl getEndpoint()
165         {
166         log.debug("StoreClientWrapper.getEndpoint()");
167         return new Agsl(
168             new Msrl(
169                 this.getIvorn(),
170                 null
171                 )
172             );
173         }
174 
175     /***
176      * Returns the Agsl for the given source path. This should be sufficient
177      * that we can reach the file for writing to as well; if this requires a
178      * delegate, then the agsl should include the delegate endpoint (eg myspace)
179      * rather than a read-only URL
180      * @param path The path of the file to access.
181      *
182      */
183     public Agsl getAgsl(String path)
184         throws IOException
185         {
186         log.debug("StoreClientWrapper.getAgsl()");
187         try {
188             return new Agsl(
189                 new Msrl(
190                     factory.ivorn(
191                         this.getIvorn(),
192                         path
193                         ),
194                     null
195                     )
196                 );
197             }
198         catch (FileManagerIdentifierException ouch)
199             {
200             throw new IOException(
201                 "Unable to parse ivorn address"
202                 );
203             }
204         }
205 
206     /***
207      * Returns a tree representation of the files that match the expression.
208      * Not supported.
209      *
210      */
211     public StoreFile getFiles(String filter)
212         throws IOException
213         {
214         throw new UnsupportedOperationException(
215             "Wildcard path is not supported"
216             );
217         }
218 
219     /***
220      * Returns the StoreFile representation of the file at the given path.
221      *
222      */
223     public StoreFile getFile(String path)
224         throws IOException
225         {
226         log.debug("StoreClientWrapper.getFile()");
227         log.debug("  Path : " + path);
228         try {
229             Ivorn ivorn = factory.ivorn(
230                 this.getIvorn(),
231                 path
232                 );
233             log.debug("  Ivorn : " + ivorn.toString());
234             return new StoreFileWrapper(
235                 delegate.getNode(
236                     ivorn
237                     )
238                 );
239             }
240         catch (NodeNotFoundException ouch)
241             {
242             throw new FileNotFoundException(
243                 ouch.getMessage()
244                 );
245             }
246         catch (FileManagerIdentifierException ouch)
247             {
248             throw new IOException(
249                 ouch.getMessage()
250                 );
251             }
252         catch (FileManagerServiceException ouch)
253             {
254             throw new IOException(
255                 ouch.getMessage()
256                 );
257             }
258         }
259 
260     /***
261      * Get the parent from a path.
262      * @param path The path to parse.
263      * @return The parent path.
264      *
265      */
266     protected String getFilePath(String path)
267         {
268         int index  = path.lastIndexOf('/');
269         //
270         // If there is no path separator.
271         if (index == -1)
272             {
273             return null ;
274             }
275         //
276         // If the '/' is at the start
277         if (index == 0)
278             {
279             return null ;
280             }
281         //
282         // Return the parent path.
283         return path.substring(
284             0,
285             index
286             );
287         }
288 
289     /***
290      * Get the name from a path.
291      * @param path The path to parse.
292      * @return The filename.
293      *
294      */
295     protected String getFileName(String path)
296         {
297         //
298         // If the path separator is at the end.
299         if (path.endsWith("/"))
300             {
301             return null ;
302             }
303         //
304         // If there is no path separator.
305         int index = path.lastIndexOf('/');
306         if (index == -1)
307             {
308             return path ;
309             }
310         //
311         // Return the file name.
312         return path.substring(
313             index + 1
314             );
315         }
316 
317     /***
318      * Create a new container.
319      * @param target The path of the new folder.
320      *
321      */
322     public void newFolder(String target)
323         throws IOException
324         {
325         log.debug("StoreClientWrapper.newFolder(String)");
326         log.debug("  Path  : " + target);
327         //
328         // Separate the path into path and name.
329         String name = getFileName(target);
330         String path = getFilePath(target);
331         if (null == path)
332             {
333             throw new FileNotFoundException(
334                 "Unable to parse file path"
335                 );
336             }
337         if (null == name)
338             {
339             throw new FileNotFoundException(
340                 "Unable to parse file name"
341                 );
342             }
343         try {
344             //
345             // Generate the parent ivorn.
346             Ivorn ivorn = factory.ivorn(
347                 this.getIvorn(),
348                 path
349                 );
350             log.debug("  Ivorn : " + ivorn.toString());
351             //
352             // Find the parent node.
353             FileManagerNode parent = delegate.getNode(
354                 ivorn
355                 );
356             //
357             // Check it is a container.
358             if (parent.isContainer() == false)
359                 {
360                 throw new FileNotFoundException(
361                     "Parent node is not a container"
362                     );
363                 }
364             //
365             // Create the new node.
366             parent.addNode(
367                 name
368                 );
369             }
370         catch (NodeNotFoundException ouch)
371             {
372             throw new FileNotFoundException(
373                 ouch.getMessage()
374                 );
375             }
376         catch (DuplicateNodeException ouch)
377             {
378             throw new IOException(
379                 ouch.getMessage()
380                 );
381             }
382         catch (FileManagerIdentifierException ouch)
383             {
384             throw new IOException(
385                 ouch.getMessage()
386                 );
387             }
388         catch (FileManagerServiceException ouch)
389             {
390             throw new IOException(
391                 ouch.getMessage()
392                 );
393             }
394         }
395 
396     /***
397      * Create a new file.
398      * @param string The path of the new file.
399      *
400      */
401     public FileManagerNode newFile(String target)
402         throws IOException
403         {
404         log.debug("StoreClientWrapper.newFile(String)");
405         log.debug("  Path  : " + target);
406         //
407         // Separate the path into path and name.
408         String name = getFileName(target);
409         String path = getFilePath(target);
410         if (null == path)
411             {
412             throw new FileNotFoundException(
413                 "Unable to parse file path"
414                 );
415             }
416         if (null == name)
417             {
418             throw new FileNotFoundException(
419                 "Unable to parse file name"
420                 );
421             }
422         try {
423             //
424             // Generate the parent ivorn.
425             Ivorn ivorn = factory.ivorn(
426                 this.getIvorn(),
427                 path
428                 );
429             log.debug("  Ivorn : " + ivorn.toString());
430             //
431             // Find the parent node.
432             FileManagerNode parent = delegate.getNode(
433                 ivorn
434                 );
435             //
436             // Check it is a container.
437             if (parent.isContainer() == false)
438                 {
439                 throw new FileNotFoundException(
440                     "Parent node is not a container"
441                     );
442                 }
443             //
444             // Create the new node.
445             return parent.addFile(
446                 name
447                 );
448             }
449         catch (NodeNotFoundException ouch)
450             {
451             throw new FileNotFoundException(
452                 ouch.getMessage()
453                 );
454             }
455         catch (DuplicateNodeException ouch)
456             {
457             throw new IOException(
458                 ouch.getMessage()
459                 );
460             }
461         catch (FileManagerIdentifierException ouch)
462             {
463             throw new IOException(
464                 ouch.getMessage()
465                 );
466             }
467         catch (FileManagerServiceException ouch)
468             {
469             throw new IOException(
470                 ouch.getMessage()
471                 );
472             }
473         }
474 
475     /***
476      * Create a new node.
477      * @param target The node path.
478      * @param type   The node type.
479      *
480     protected FileManagerNode addNode(String target, String type)
481         throws IOException
482         {
483         log.debug("StoreClientWrapper.addNode(String, String)");
484         }
485      */
486 
487     /***
488      * Puts the given byte buffer from offset of length bytes, to the given target.
489      * @param bytes  The byte array to store.
490      * @param offset The starting offset within the array.
491      * @param length The the number of bytes to transfer from the array.
492      * @param path   The path of the file to store the data.
493      * @param append Not supported.
494      *
495      */
496     public void putBytes(byte[] bytes, int offset, int length, String path, boolean append)
497         throws IOException
498         {
499         if (append)
500             {
501             throw new UnsupportedOperationException(
502                 "Append is not supported"
503                 );
504             }
505         //
506         // Open an output stream to the target node.
507         OutputStream stream = putStream(
508             path,
509             append
510             );
511         //
512         // Transfer the data.
513         stream.write(
514             bytes,
515             offset,
516             length
517             );
518         //
519         // Close the stream.
520         stream.close();
521         }
522 
523     /***
524      * Puts the given string into the given location.
525      * @deprecated - use putBytes() or stream to putStream()
526      * @param contents The string to store.
527      * @param path   The path of the file to store the data.
528      * @param append Not supported.
529      *
530      */
531     public void putString(String contents, String path, boolean append)
532         throws IOException
533         {
534         if (append)
535             {
536             throw new UnsupportedOperationException(
537                 "Append is not supported"
538                 );
539             }
540         //
541         // Open an output stream to the target node.
542         OutputStream stream = putStream(
543             path,
544             append
545             );
546         //
547         // Wrap it in a writer.
548         OutputStreamWriter writer = new OutputStreamWriter(
549             stream
550             );
551         //
552         // Transfer the data.
553         writer.write(
554             contents
555             );
556         //
557         // Close the stream.
558         writer.close();
559         }
560 
561     /***
562      * Copies the contents of the file at the given source url to the given location.
563      * @param source The source URL of the data to transfer.
564      * @param path   The path of the file to store the data.
565      * @param append Not supported.
566      *
567      */
568     public void putUrl(URL source, String path, boolean append)
569         throws IOException
570         {
571         if (append)
572             {
573             throw new UnsupportedOperationException(
574                 "Append is not supported"
575                 );
576             }
577         throw new UnsupportedOperationException(
578             "Put URL is not supported (yet)"
579             );
580         }
581 
582     /***
583      * Streaming output - returns a stream that can be used to output to the given location.
584      * @param path The path of the file to send the data to.
585      * @param flag Append flag (not supported).
586      *
587      */
588     public OutputStream putStream(String path, boolean flag)
589         throws IOException
590         {
591         log.debug("StoreClientWrapper.putStream(String, boolean)");
592         log.debug("  Path : " + path);
593         log.debug("  Flag : " + flag);
594         if (flag)
595             {
596             throw new UnsupportedOperationException(
597                 "Append is not supported"
598                 );
599             }
600         try {
601             //
602             // Create the node ivorn.
603             Ivorn ivorn = factory.ivorn(
604                 this.getIvorn(),
605                 path
606                 );
607             log.debug("  Ivorn : " + ivorn.toString());
608             //
609             // Try to find the target node.
610             FileManagerNode node = null ;
611             try {
612                 node = delegate.getNode(
613                     ivorn
614                     );
615                 }
616             //
617             // Create the node if it does not exist.
618             catch (NodeNotFoundException ouch)
619                 {
620                 node = this.newFile(
621                     path
622                     );
623                 }
624             //
625             // Return an output stream to the node.
626             return node.importStream();
627             }
628         catch (NodeNotFoundException ouch)
629             {
630             throw new FileNotFoundException(
631                 ouch.getMessage()
632                 );
633             }
634         catch (FileManagerIdentifierException ouch)
635             {
636             throw new IOException(
637                 ouch.getMessage()
638                 );
639             }
640         catch (FileManagerServiceException ouch)
641             {
642             throw new IOException(
643                 ouch.getMessage()
644                 );
645             }
646         }
647 
648     /***
649      * Gets a file's contents as a stream.
650      * @param path  The path of the file to read the data from.
651      *
652      */
653     public InputStream getStream(String path)
654         throws IOException
655         {
656         log.debug("StoreClientWrapper.getStream(String)");
657         log.debug("  Path : " + path);
658         try {
659             //
660             // Create the node ivorn.
661             Ivorn ivorn = factory.ivorn(
662                 this.getIvorn(),
663                 path
664                 );
665             log.debug("  Ivorn : " + ivorn.toString());
666             //
667             // Try to find the target node.
668             FileManagerNode node = 
669                 delegate.getNode(
670                     ivorn
671                     );
672             //
673             // Return an output stream to the node.
674             return node.exportStream();
675             }
676         catch (NodeNotFoundException ouch)
677             {
678             throw new FileNotFoundException(
679                 ouch.getMessage()
680                 );
681             }
682         catch (FileManagerIdentifierException ouch)
683             {
684             throw new IOException(
685                 ouch.getMessage()
686                 );
687             }
688         catch (FileManagerServiceException ouch)
689             {
690             throw new IOException(
691                 ouch.getMessage()
692                 );
693             }
694         }
695 
696     /***
697      * Gets the url to the given source file.
698      * @deprecated? don't think we should always publish files as URLs... mch
699      * @param path The path of the file to access.
700      *
701      */
702     public URL getUrl(String path)
703         throws IOException
704         {
705         log.debug("StoreClientWrapper.getUrl(String)");
706         log.debug("  Path : " + path);
707         throw new UnsupportedOperationException(
708             "Get URL is not supported (yet)"
709             );
710         }
711 
712     /***
713      * Delete a file.
714      * @param path The path of the file to delete.
715      *
716      */
717     public void delete(String path)
718         throws IOException
719         {
720         throw new UnsupportedOperationException(
721             "Delete not implemented (yet)"
722             );
723         }
724 
725     /***
726      * Copy a file to a target Agsl.
727      * @param path   The path of the data to copy.
728      * @param target The destination to copy the data to.
729      *
730      */
731     public void copy(String path, Agsl target)
732         throws IOException
733         {
734         throw new UnsupportedOperationException(
735             "Copy not implemented (yet)"
736             );
737         }
738 
739     /***
740      * Copy a file from a source Agsl
741      * @param source The Agsl of the data to copy.
742      * @param path   The path of the file to copy the data to.
743      *
744      */
745     public void copy(Agsl source, String path)
746         throws IOException
747         {
748         throw new UnsupportedOperationException(
749             "Copy not implemented (yet)"
750             );
751         }
752 
753     /***
754      * Move a file to a target Agsl.
755      * @param path   The path of the data to move.
756      * @param target The destination to move the data to.
757      *
758      */
759     public void move(String path, Agsl target)
760         throws IOException
761         {
762         throw new UnsupportedOperationException(
763             "Move not implemented (yet)"
764             );
765         }
766 
767     /***
768      * Move a file from a source Agsl.
769      * @param source The source of the data to move.
770      * @param path   The destination to move the data to.
771      *
772      */
773     public void move(Agsl source, String path)
774         throws IOException
775         {
776         throw new UnsupportedOperationException(
777             "Move not implemented (yet)"
778             );
779         }
780 
781 
782 
783     }
784