View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filestore/common/src/java/org/astrogrid/filestore/common/file/FileProperties.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2005/01/28 10:44:01 $</cvs:date>
5    * <cvs:version>$Revision: 1.10 $</cvs:version>
6    * <cvs:log>
7    *   $Log: FileProperties.java,v $
8    *   Revision 1.10  2005/01/28 10:44:01  clq2
9    *   dave_dev_200501141257 (filemanager)
10   *
11   *   Revision 1.9.6.3  2005/01/18 14:54:49  dave
12   *   Refactored properties ..
13   *
14   *   Revision 1.9.6.2  2005/01/15 08:25:50  dave
15   *   Refactored file created and modified date handling ..
16   *
17   *   Revision 1.9.6.1  2005/01/14 21:01:33  dave
18   *   Added FileStoreDateFormat utility to handle dates in ISO format ....
19   *   Fixed the file:/// problem on Unix ...
20   *
21   *   Revision 1.9  2004/12/16 17:25:49  jdt
22   *   merge from dave-dev-200410061224-200412161312
23   *
24   *   Revision 1.7.24.9  2004/12/11 06:00:12  dave
25   *   Updates to support FileManager copy ....
26   *
27   *   Revision 1.7.24.8  2004/12/08 17:54:55  dave
28   *   Added update to FileManager client and server side ...
29   *
30   *   Revision 1.7.24.7  2004/11/17 19:06:30  dave
31   *   Updated server configuration ...
32   *
33   *   Revision 1.7.24.6  2004/11/06 20:02:39  dave
34   *   Allowed for null value ...
35   *
36   *   Revision 1.7.24.5  2004/11/06 19:12:18  dave
37   *   Refactored identifier properties ...
38   *
39   *   Revision 1.7.24.4  2004/11/05 05:23:03  dave
40   *   Added property method for filemanager ...
41   *
42   *   Revision 1.7.24.3  2004/11/04 02:33:03  dave
43   *   Refactored mock delegate and config to make it easier to test filemanager with multiple filstores.
44   *
45   *   Revision 1.7.24.2  2004/11/02 23:20:12  dave
46   *   Added property filter and changed method names to make them FileStore specific.
47   *
48   *   Revision 1.7.24.1  2004/10/15 03:53:04  dave
49   *   Changed WSDD deployment to 'Application' to allow multiple mock instances.
50   *
51   *   Revision 1.7  2004/09/09 01:19:50  dave
52   *   Updated MIME type handling in MySpace.
53   *   Extended test coverage for MIME types in FileStore and MySpace.
54   *   Added VM memory data to community ServiceStatusData.
55   *
56   *   Revision 1.6.6.3  2004/09/08 13:43:37  dave
57   *   Added VoList mime type.
58   *
59   *   Revision 1.6.6.2  2004/09/08 12:41:56  dave
60   *   Added adql mime type.
61   *
62   *   Revision 1.6.6.1  2004/09/08 12:39:18  dave
63   *   Added votable and volist mime types.
64   *
65   *   Revision 1.6  2004/09/02 10:25:41  dave
66   *   Updated FileStore and MySpace to handle mime type and file size.
67   *   Updated Community deployment script.
68   *
69   *   Revision 1.5.2.1  2004/09/01 02:58:07  dave
70   *   Updated to use external mime type for imported files.
71   *
72   *   Revision 1.5  2004/08/27 22:43:15  dave
73   *   Updated filestore and myspace to report file size correctly.
74   *
75   *   Revision 1.4.12.2  2004/08/27 11:48:41  dave
76   *   Added getContentSize to FileProperties.
77   *
78   *   Revision 1.4.12.1  2004/08/26 19:06:50  dave
79   *   Modified filestore to return file size in properties.
80   *
81   *   Revision 1.4  2004/08/18 19:00:01  dave
82   *   Myspace manager modified to use remote filestore.
83   *   Tested before checkin - integration tests at 91%.
84   *
85   *   Revision 1.3.16.3  2004/08/09 13:38:21  dave
86   *   Added getResourceUrl() to properties API.
87   *
88   *   Revision 1.3.16.2  2004/08/06 22:25:06  dave
89   *   Refactored bits and broke a few tests ...
90   *
91   *   Revision 1.3.16.1  2004/08/02 14:54:15  dave
92   *   Trying to get integration tests to run
93   *
94   *   Revision 1.3  2004/07/21 18:11:55  dave
95   *   Merged development branch, dave-dev-200407201059, into HEAD
96   *
97   *   Revision 1.2.6.1  2004/07/21 16:28:16  dave
98   *   Added content properties and tests
99   *
100  *   Revision 1.2  2004/07/14 13:50:29  dave
101  *   Merged development branch, dave-dev-200406301228, into HEAD
102  *
103  *   Revision 1.1.2.2  2004/07/14 10:34:08  dave
104  *   Reafctored server to use array of properties
105  *
106  *   Revision 1.1.2.1  2004/07/13 16:37:29  dave
107  *   Refactored common and client to use an array of FileProperties (more SOAP friendly)
108  *
109  * </cvs:log>
110  *
111  */
112 package org.astrogrid.filestore.common.file ;
113 
114 import java.net.URL ;
115 
116 import java.io.IOException ;
117 import java.io.InputStream ;
118 import java.io.OutputStream ;
119 
120 import java.util.Map ;
121 import java.util.Map.Entry ;
122 import java.util.Date ;
123 import java.util.Iterator ;
124 import java.util.Properties ;
125 import java.util.Enumeration ;
126 
127 import java.text.ParseException ;
128 import org.astrogrid.store.Ivorn ;
129 
130 import org.astrogrid.filestore.common.FileStoreDateFormat;
131 import org.astrogrid.filestore.common.ivorn.FileStoreIvornParser ;
132 import org.astrogrid.filestore.common.ivorn.FileStoreIvornFactory ;
133 import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
134 
135 /***
136  * A wrapper for a set of file properties.
137  *
138  */
139 public class FileProperties
140     {
141     /***
142      * The property key for the resource ivorn.
143      *
144      */
145     public static final String STORE_RESOURCE_IVORN  = "org.astrogrid.filestore.resource.ivorn" ;
146 
147     /***
148      * The property key for the resource URL.
149      *
150      */
151     public static final String STORE_RESOURCE_URL  = "org.astrogrid.filestore.resource.url" ;
152 
153     /***
154      * The property key for the transfer source URL.
155      *
156      */
157     public static final String TRANSFER_SOURCE_URL  = "org.astrogrid.filestore.transfer.source.url" ;
158 
159     /***
160      * The property key for content size.
161      *
162      */
163     public static final String CONTENT_SIZE_PROPERTY  = "org.astrogrid.filestore.content.size" ;
164 
165     /***
166      * The property key for content type.
167      *
168      */
169     public static final String MIME_TYPE_PROPERTY  = "org.astrogrid.filestore.content.type" ;
170 
171     /***
172      * The property key for content encoding.
173      *
174      */
175     public static final String MIME_ENCODING_PROPERTY  = "org.astrogrid.filestore.content.encoding" ;
176 
177     /***
178      * The property key for the file create date.
179      *
180      */
181     public static final String FILE_CREATED_DATE_PROPERTY  = "org.astrogrid.filestore.created.date" ;
182 
183     /***
184      * The property key for the file modify date.
185      *
186      */
187     public static final String FILE_MODIFIED_DATE_PROPERTY  = "org.astrogrid.filestore.modified.date" ;
188 
189     /***
190      * Known MIME type values.
191      *
192      */
193     public static final String MIME_TYPE_TEXT     = "text/raw"  ;
194     public static final String MIME_TYPE_XML      = "text/xml"  ;
195     public static final String MIME_TYPE_HTML     = "text/html"  ;
196 
197     public static final String MIME_TYPE_VOLIST   = "text/xml +org.astrogrid.mime.volist"  ;
198     public static final String MIME_TYPE_VOTABLE  = "text/xml +org.astrogrid.mime.votable" ;
199 
200     public static final String MIME_TYPE_JOB      = "text/xml +org.astrogrid.mime.job"      ;
201     public static final String MIME_TYPE_WORKFLOW = "text/xml +org.astrogrid.mime.workflow" ;
202 
203     public static final String MIME_TYPE_ADQL     = "text/xml +org.astrogrid.mime.adql"     ;
204 
205     /***
206      * Public constructor.
207      *
208      */
209     public FileProperties()
210         {
211         }
212 
213     /***
214      * Public constructor from an array of values.
215      * @param array An array of property values.
216      *
217      */
218     public FileProperties(FileProperty[] array)
219         {
220         if (null != array)
221             {
222             for (int i = 0 ; i < array.length ; i++)
223                 {
224                 this.setProperty(
225                     array[i].getName(),
226                     array[i].getValue()
227                     ) ;
228                 }
229             }
230         }
231 
232     /***
233      * Public constructor from another set of properties.
234      * @param that The other set of properties.
235      *
236      */
237     public FileProperties(FileProperties that)
238         {
239         this(
240             that.toArray()
241             ) ;
242         }
243 
244     /***
245      * Our internal map of properties.
246      *
247      */
248     private Properties properties = new Properties() ;
249 
250     /***
251      * Access to our list of properties.
252      *
253      */
254     protected Map getProperties()
255         {
256         return this.properties ;
257         }
258 
259     /***
260      * Get a property.
261      *
262      */
263     public String getProperty(String key)
264         {
265         return properties.getProperty(key) ;
266         }
267 
268     /***
269      * Set a property.
270      * @param key The property key (name).
271      * @param value The property value.
272      *
273      */
274     public void setProperty(String key, String value)
275         {
276         if (null != key)
277             {
278             if (null != value)
279                 {
280                 properties.setProperty(key, value) ;
281                 }
282             }
283         }
284 
285     /***
286      * Set a property.
287      * @param property The property to set.
288      *
289      */
290     public void setProperty(FileProperty property)
291         {
292         if (null != property)
293             {
294             this.setProperty(
295                 property.getName(),
296                 property.getValue()
297                 ) ;
298             }
299         }
300 
301     /***
302      * Merge the properties from another map.
303      * @param that The set of properties to merge.
304      *
305      */
306     public void merge(FileProperties that)
307         {
308         if (null != that)
309             {
310             Iterator iter = that.properties.entrySet().iterator() ;
311             while (iter.hasNext())
312                 {
313                 Entry  entry = (Entry) iter.next() ;
314                 String key   = (String) entry.getKey();
315                 String value = (String) entry.getValue();
316                 if (null != value)
317                     {
318                     this.setProperty(
319                         key,
320                         value
321                         ) ;
322                     }
323                 }
324             }
325         }
326 
327     /***
328      * Merge the properties from another map, using a filter to exclude specific properties.
329      * @param that   The set of properties to merge.
330      * @param filter The filter for excluded properties.
331      *
332      */
333     public void merge(FileProperties that, PropertyFilter filter)
334         {
335         if (null != that)
336             {
337             Iterator iter = that.properties.entrySet().iterator() ;
338             while (iter.hasNext())
339                 {
340                 Entry entry = (Entry) iter.next() ;
341                 FileProperty property = filter.filter(
342                     new FileProperty(
343                         (String) entry.getKey(),
344                         (String) entry.getValue()
345                         )
346                     ) ;
347                 if (null != property)
348                     {
349                     this.setProperty(
350                         property
351                         ) ;
352                     }
353                 }
354             }
355         }
356 
357     /***
358      * Merge the properties from an array.
359      * @param arrya  The set of properties to merge.
360      * @param filter The filter for excluded properties.
361      *
362      */
363     public void merge(FileProperty[] array, PropertyFilter filter)
364         {
365         if (null != array)
366             {
367             for (int i = 0 ; i < array.length ; i++)
368                 {
369                 FileProperty property = filter.filter(
370                     array[i]
371                     ) ;
372                 if (null != property)
373                     {
374                     this.setProperty(
375                         property
376                         ) ;
377                     }
378                 }
379             }
380         }
381 
382     /***
383      * Convert our properties into an array.
384      *
385      */
386     public FileProperty[] toArray()
387         {
388         //
389         // Create an empty array.
390         FileProperty[] array = new FileProperty[this.properties.size()] ;
391         //
392         // Transfer the values.
393         Iterator iter = this.properties.entrySet().iterator() ;
394         for (int i = 0 ; i < array.length ; i++)
395             {
396             array[i] = new FileProperty(
397                 (Entry) iter.next()
398                 ) ;
399             }
400         return array ;
401         }
402 
403     /***
404      * Load our properties from a file.
405      *
406      */
407     public void load(InputStream stream)
408         throws IOException
409         {
410         this.properties.load(
411             stream
412             ) ;
413         }
414 
415     /***
416      * Save our properties to a file.
417      *
418      */
419     public void save(OutputStream stream)
420         throws IOException
421         {
422         this.properties.store(
423             stream,
424             this.getClass().getName()
425             ) ;
426         }
427 
428     /***
429      * Get the resource ivorn.
430      * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
431      *
432      */
433     public Ivorn getStoreResourceIvorn()
434         throws FileStoreIdentifierException
435         {
436         String ivorn = this.getProperty(
437             STORE_RESOURCE_IVORN
438             ) ;
439         if (null != ivorn)
440             {
441             try {
442                 return new Ivorn(
443                     ivorn
444                     ) ;
445                 }
446             catch (Exception ouch)
447                 {
448                 throw new FileStoreIdentifierException(
449                     ivorn
450                     );
451                 }
452             }
453         else {
454             return null ;
455             }
456         }
457 
458     /***
459      * Set the resource ivorn.
460      * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
461      *
462      */
463     public void setStoreResourceIvorn(Ivorn ivorn)
464         {
465         if (null != ivorn)
466             {
467             this.setProperty(
468                 STORE_RESOURCE_IVORN,
469                 ivorn.toString()
470                 ) ;
471             }
472         else {
473             this.setProperty(
474                 STORE_RESOURCE_IVORN,
475                 null
476                 ) ;
477             }
478         }
479 
480     /***
481      * Get the service ivorn.
482      * @throws FileStoreIdentifierException if the service identifier is invalid.
483      *
484      */
485     public Ivorn getStoreServiceIvorn()
486         throws FileStoreIdentifierException
487         {
488         String ivorn = this.getProperty(
489             STORE_RESOURCE_IVORN
490             ) ;
491         if (null != ivorn)
492             {
493             return new FileStoreIvornParser(
494                 ivorn
495                 ).getServiceIvorn() ;
496             }
497         else {
498             return null ;
499             }
500         }
501 
502     /***
503      * Get the resource identifier.
504      *
505      */
506     public String getStoreResourceIdent()
507         throws FileStoreIdentifierException
508         {
509         String ivorn = this.getProperty(
510             STORE_RESOURCE_IVORN
511             ) ;
512         if (null != ivorn)
513             {
514             return new FileStoreIvornParser(
515                 ivorn
516                 ).getResourceIdent() ;
517             }
518         else {
519             return null ;
520             }
521         }
522 
523     /***
524      * Get the content size.
525      *
526      */
527     public long getContentSize()
528         {
529         String string = getProperty(
530             FileProperties.CONTENT_SIZE_PROPERTY
531             ) ;
532         if (null != string)
533             {
534             try {
535                 return Long.parseLong(string) ;
536                 }
537             catch (NumberFormatException ouch)
538                 {
539                 return -1L ;
540                 }
541             }
542         else {
543             return 0L ;
544             }
545         }
546 
547     /***
548      * Get the content mime type.
549      *
550      */
551     public String getContentType()
552         {
553         return getProperty(
554             FileProperties.MIME_TYPE_PROPERTY
555             ) ;
556         }
557 
558     /***
559      * Get the (external) resource URL.
560      * @throws FileStoreIdentifierException if the service or resource identifiers are invalid.
561      * @deprecated Use exportInit on the parent service to get a transfer URL.
562      *
563      */
564     public URL getStoreResourceUrl()
565         {
566         try {
567             return new URL(
568                 this.getProperty(
569                     STORE_RESOURCE_URL
570                     )
571                 ) ;
572             }
573         catch (Exception ouch)
574             {
575             return null ;
576             }
577         }
578 
579     /***
580      * Get the file create date.
581      * @return The file create date, if the file has stored data, or null if the file does not contains any data.
582      *
583      */
584     public Date getFileCreateDate()
585         {
586         //
587         // Create our ISO date format.
588         FileStoreDateFormat formatter = new FileStoreDateFormat() ;
589         //
590         // Parse the date property.
591         try {
592             return formatter.parse(
593                 this.getProperty(
594                     FileProperties.FILE_CREATED_DATE_PROPERTY
595                     )
596                 );
597             }
598         catch (ParseException ouch)
599             {
600             return null ;
601             }
602         }
603 
604     /***
605      * Get the file modified date.
606      * @return The file modified date, if the file has stored data, or null if the file does not contains any data.
607      *
608      */
609     public Date getFileModifyDate()
610         {
611         //
612         // Create our ISO date format.
613         FileStoreDateFormat formatter = new FileStoreDateFormat() ;
614         //
615         // Parse the date property.
616         try {
617             return formatter.parse(
618                 this.getProperty(
619                     FileProperties.FILE_MODIFIED_DATE_PROPERTY
620                     )
621                 );
622             }
623         catch (ParseException ouch)
624             {
625             return null ;
626             }
627         }
628     }
629