View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filestore/common/src/java/org/astrogrid/filestore/common/ivorn/FileStoreIvornParser.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2004/11/25 00:19:20 $</cvs:date>
5    * <cvs:version>$Revision: 1.5 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: FileStoreIvornParser.java,v $
9    *   Revision 1.5  2004/11/25 00:19:20  jdt
10   *   Merge from dave-dev-200410061224-200411221626
11   *
12   *   Revision 1.4.14.2  2004/11/06 19:12:18  dave
13   *   Refactored identifier properties ...
14   *
15   *   Revision 1.4.14.1  2004/11/06 12:17:35  dave
16   *   Modified getServiceIdent() to return full ivorn string.
17   *
18   *   Revision 1.4  2004/09/17 06:57:10  dave
19   *   Added commons logging to FileStore.
20   *   Updated logging properties in Community.
21   *   Fixed bug in AGINAB deployment.
22   *   Removed MySpace tests with hard coded grendel address.
23   *
24   *   Revision 1.3.32.1  2004/09/17 01:08:36  dave
25   *   Updated debug to use commons logging API ....
26   *
27   *   Revision 1.3  2004/08/18 19:00:01  dave
28   *   Myspace manager modified to use remote filestore.
29   *   Tested before checkin - integration tests at 91%.
30   *
31   *   Revision 1.2.10.2  2004/08/09 15:43:00  dave
32   *   Fixed bug in ivorn parser
33   *
34   *   Revision 1.2.10.1  2004/07/28 03:00:17  dave
35   *   Refactored resolver constructors and added mock ivorn
36   *
37   *   Revision 1.2  2004/07/23 09:11:16  dave
38   *   Merged development branch, dave-dev-200407221513, into HEAD
39   *
40   *   Revision 1.1.2.4  2004/07/23 03:55:38  dave
41   *   Added getServiceIvorn to parser
42   *
43   *   Revision 1.1.2.3  2004/07/23 03:37:06  dave
44   *   Debugged tests and parser bugs
45   *
46   *   Revision 1.1.2.2  2004/07/23 03:08:37  dave
47   *   Updated parser tests
48   *
49   *   Revision 1.1.2.1  2004/07/23 02:10:58  dave
50   *   Added IvornFactory and IvornParser
51   *
52   * </cvs:log>
53   *
54   */
55  package org.astrogrid.filestore.common.ivorn ;
56  
57  import org.apache.commons.logging.Log ;
58  import org.apache.commons.logging.LogFactory ;
59  
60  import java.net.URI ;
61  import java.net.URISyntaxException ;
62  
63  import java.util.regex.Matcher ;
64  import java.util.regex.Pattern ;
65  
66  import org.astrogrid.store.Ivorn ;
67  
68  import org.astrogrid.config.Config ;
69  import org.astrogrid.config.SimpleConfig ;
70  import org.astrogrid.config.PropertyNotFoundException ;
71  
72  import org.astrogrid.filestore.common.exception.FileStoreServiceException ;
73  import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
74  
75  /***
76   * A parser for handling Filestore identifiers.
77   *
78   */
79  public class FileStoreIvornParser
80      {
81      /***
82       * Our debug logger.
83       *
84       */
85      private static Log log = LogFactory.getLog(FileStoreIvornParser.class);
86  
87  	/***
88  	 * The mock service identifier.
89  	 * Used to create mock ivorns in JUnit tests.
90  	 *
91  	 */
92  	public static String MOCK_SERVICE_IDENT = "ivo://org.astrogrid.mock" ;
93  
94      /***
95       * Our AstroGrid configuration.
96       *
97       */
98      protected static Config config = SimpleConfig.getSingleton() ;
99  
100     /***
101      * Public constructor for a specific Ivorn.
102      * @param ivorn A vaild Ivorn identifier.
103      * @throws FileStoreIdentifierException If the identifier is not valid.
104      *
105      */
106     public FileStoreIvornParser(Ivorn ivorn)
107         throws FileStoreIdentifierException
108         {
109         this.setIvorn(ivorn) ;
110         }
111 
112     /***
113      * Public constructor for a specific identifier.
114      * @param ident A vaild identifier.
115      * @throws FileStoreIdentifierException If the identifier is not valid.
116      *
117      */
118     public FileStoreIvornParser(String ident)
119         throws FileStoreIdentifierException
120         {
121         this.setIvorn(
122             parse(ident)
123             ) ;
124         }
125 
126     /***
127      * Our target Ivorn.
128      *
129      */
130     private Ivorn ivorn ;
131 
132     /***
133      * Our corresponding URI.
134      *
135      */
136     private URI uri ;
137 
138     /***
139      * The service ident.
140      *
141      */
142     private String service ;
143 
144     /***
145      * The resource ident.
146      *
147      */
148     private String resource ;
149 
150     /***
151      * Get our target Ivorn.
152      *
153      */
154     public Ivorn getIvorn()
155         {
156         return this.ivorn ;
157         }
158 
159     /***
160      * Convert a string identifier into an Ivorn.
161      * @param ident The identifier.
162      * @return a new Ivorn containing the identifier.
163      * @throws FileStoreIdentifierException If the identifier is not a valid ivorn.
164      *
165      */
166     protected static Ivorn parse(String ident)
167         throws FileStoreIdentifierException
168         {
169         if (null == ident)
170             {
171             throw new FileStoreIdentifierException(
172                 "Null identifier"
173                 ) ;
174             }
175         try {
176             return new Ivorn(ident) ;
177             }
178         catch (URISyntaxException ouch)
179             {
180             throw new FileStoreIdentifierException(
181                 ouch
182                 ) ;
183             }
184         }
185 
186     /***
187      * Set our target Ivorn.
188      * @param A vaild Ivorn identifier.
189      * @throws FileStoreIdentifierException If the identifier is not valid.
190      *
191      */
192     protected void setIvorn(Ivorn ivorn)
193         throws FileStoreIdentifierException
194         {
195         log.debug("") ;
196         log.debug("----\"----") ;
197         log.debug("FilestoreIvornParser.setIvorn()") ;
198         log.debug("  Ivorn : " + ivorn) ;
199         //
200         // Check for null param.
201         if (null == ivorn)
202             {
203             throw new FileStoreIdentifierException(
204                 "Null identifier"
205                 ) ;
206             }
207         //
208         // Save the ivorn.
209         this.ivorn    = ivorn ;
210         //
211         // Reset everything else.
212         this.uri      = null  ;
213         this.service  = null ;
214         this.resource = null ;
215         //
216         // Try convert it into a URI.
217         try {
218             this.uri = new URI(ivorn.toString()) ;
219             //
220             // Parse the filestore ident.
221             this.parseServiceIdent() ;
222             //
223             // Parse resource ident.
224             this.parseResourceIdent() ;
225             }
226         //
227         // All Ivorn should be valid URI.
228         catch (URISyntaxException ouch)
229             {
230             throw new FileStoreIdentifierException(
231                 ouch
232                 ) ;
233             }
234         }
235 
236     /***
237      * Parse the filestore ident.
238      * @throws FileStoreIdentifierException If the identifier is not a valid ivorn.
239      *
240      */
241     protected void parseServiceIdent()
242 		throws FileStoreIdentifierException
243         {
244         log.debug("") ;
245         log.debug("----\"----") ;
246         log.debug("FilestoreIvornParser.parseServiceIdent()") ;
247         log.debug("  Ivorn    : " + ivorn) ;
248         if (null != uri)
249             {
250 			String auth = uri.getAuthority() ;
251 			String path = uri.getPath() ;
252 			log.debug("  Auth     : " + auth) ;
253 			log.debug("  Path     : " + path) ;
254 			StringBuffer buffer = new StringBuffer() ;
255 			buffer.append(Ivorn.SCHEME) ;
256 			buffer.append("://") ;
257 			//
258 			// If the URI has an authority ident.
259 			if (null != auth)
260 				{
261 				//
262 				// If the authority ident isn't empty.
263 				if (auth.length() > 0)
264 					{
265 					//
266 					// Start with the URI authority.
267 					buffer.append(auth) ;
268 					//
269 					// If we have a path.
270 					if (null != path)
271 						{
272 						if (path.length() > 1)
273 							{
274 							buffer.append(path) ;
275 							}
276 						}
277 					}
278 				//
279 				// If the authority ident is empty.
280 				else {
281 					throw new FileStoreIdentifierException(
282 						"Invalid identifier",
283 						uri.toString()
284 						) ;
285 					}
286 				}
287 			//
288 			// If the URI does not have an authority ident.
289 			else {
290 				throw new FileStoreIdentifierException(
291 					"Invalid identifier",
292 					uri.toString()
293 					) ;
294 				}
295 			this.service = buffer.toString() ;
296 			}
297 		log.debug("  Service  : " + this.service) ;
298 		}
299 
300     /***
301      * Parse the resource ident.
302      *
303      */
304     protected void parseResourceIdent()
305         {
306         log.debug("") ;
307         log.debug("----\"----") ;
308         log.debug("FilestoreIvornParser.parseResourceIdent()") ;
309         log.debug("  Ivorn    : " + ivorn) ;
310         if (null != uri)
311             {
312             this.resource = uri.getFragment() ;
313             }
314         log.debug("  Resource : " + this.resource) ;
315         }
316 
317     /***
318      * Get the filestore ident as a string.
319      * @return The filestore ident, or null if no match was found.
320      *
321      */
322     public String getServiceIdent()
323         {
324         return this.service ;
325         }
326 
327     /***
328      * Get the filestore ident as an ivorn.
329      * @return The filestore ivorn, or null if no match was found.
330 	 * @throws FileStoreIdentifierException If the service identifier is not valid.
331      *
332      */
333     public Ivorn getServiceIvorn()
334 		throws FileStoreIdentifierException
335         {
336         try {
337         	return new Ivorn(
338         		this.service
339         		) ;
340 			}
341 		catch (URISyntaxException ouch)
342 			{
343 			throw new FileStoreIdentifierException(
344 				ouch
345 				) ;
346 			}
347         }
348 
349     /***
350      * Get the resource ident as a string.
351      * @return The resource ident, or null if no match was found.
352      *
353      */
354     public String getResourceIdent()
355         {
356         return this.resource ;
357         }
358 
359     /***
360      * Convert the parser to a String.
361      *
362      */
363     public String toString()
364         {
365         return "FilestoreIvornParser : " + ((null != ivorn) ? ivorn.toString() : null) ;
366         }
367 
368 	/***
369 	 * Check if this is a mock ivorn.
370 	 * @return true if this is a mock ivorn.
371 	 *
372 	 */
373 	public boolean isMock()
374 		{
375 		if (null != this.service)
376 			{
377 			return this.service.startsWith(
378 				MOCK_SERVICE_IDENT
379 				) ;
380 			}
381 		else {
382 			return false ;
383 			}
384 		}
385     }