View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filestore/client/src/java/org/astrogrid/filestore/resolver/FileStoreEndpointResolverImpl.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2004/11/25 00:19:21 $</cvs:date>
5    * <cvs:version>$Revision: 1.2 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: FileStoreEndpointResolverImpl.java,v $
9    *   Revision 1.2  2004/11/25 00:19:21  jdt
10   *   Merge from dave-dev-200410061224-200411221626
11   *
12   *   Revision 1.1.2.2  2004/11/09 17:41:36  dave
13   *   Added file:// URL handling to allow server URLs to be tested.
14   *   Added importInit and exportInit to server implementation.
15   *   Moved remaining tests out of extended test abd removed it.
16   *
17   *   Revision 1.1.2.1  2004/10/19 14:56:15  dave
18   *   Refactored config and resolver to enable multiple instances of mock implementation.
19   *   Required to implement handling of multiple FileStore(s) in FileManager.
20   *
21   * </cvs:log>
22   *
23   */
24  package org.astrogrid.filestore.resolver ;
25  
26  import org.apache.commons.logging.Log ;
27  import org.apache.commons.logging.LogFactory ;
28  
29  import java.net.URL ;
30  import java.net.MalformedURLException ;
31  
32  import org.astrogrid.store.Ivorn ;
33  import org.astrogrid.registry.RegistryException;
34  import org.astrogrid.registry.client.RegistryDelegateFactory ;
35  import org.astrogrid.registry.client.query.RegistryService ;
36  
37  import org.astrogrid.filestore.common.ivorn.FileStoreIvornParser ;
38  import org.astrogrid.filestore.common.ivorn.FileStoreIvornFactory ;
39  
40  import org.astrogrid.filestore.common.exception.FileStoreIdentifierException ;
41  
42  
43  /***
44   * A helper class to resolve an Ivron into a service endpoint.
45   *
46   */
47  public class FileStoreEndpointResolverImpl
48  	implements FileStoreEndpointResolver
49      {
50      /***
51       * Our debug logger.
52       *
53       */
54      private static Log log = LogFactory.getLog(FileStoreEndpointResolverImpl.class);
55  
56      /***
57       * Public constructor, using the default Registry service.
58       *
59       */
60      public FileStoreEndpointResolverImpl()
61          {
62  		this(
63  			(URL) null
64  			) ;
65          }
66  
67      /***
68       * Public constructor, for a specific Registry service.
69       * @param endpoint The endpoint address for our RegistryDelegate.
70       *
71       */
72      public FileStoreEndpointResolverImpl(URL registry)
73  		{
74  		this(
75  			registry,
76  			new RegistryDelegateFactory()
77  			) ;
78  		}
79  
80      /***
81       * Public constructor, for a specific Registry service.
82       * @param registry The endpoint address for our registry service.
83       * @param factory A factory to create our registry delegate.
84       *
85       */
86      public FileStoreEndpointResolverImpl(URL registry, RegistryDelegateFactory factory)
87          {
88          log.debug("") ;
89          log.debug("----\"----") ;
90          log.debug("FileStoreEndpointResolverImpl()") ;
91          log.debug("  Registry : " + registry) ;
92  		if (null == factory)
93  			{
94  			throw new IllegalArgumentException(
95  				"Null registry delegate factory"
96  				) ;
97  			}
98  		if (null == registry)
99  			{
100 	        this.registry = factory.createQuery() ;
101 			}
102 		else {
103 	        this.registry = factory.createQuery(registry) ;
104 			}
105         }
106 
107     /***
108      * Public constructor, using a specific registry delegate.
109      * @param registry The registry delegate.
110      *
111      */
112     public FileStoreEndpointResolverImpl(RegistryService registry)
113         {
114 		if (null == registry)
115 			{
116 			throw new IllegalArgumentException(
117 				"Null registry delegate"
118 				) ;
119 			}
120 		this.registry = registry ;
121 		}
122 
123     /***
124      * Our Registry delegate.
125      *
126      */
127     private RegistryService registry ;
128 
129     /***
130      * Resolve an Ivorn into a service endpoint.
131      * @param ivorn An Ivorn containing a filestore identifier.
132      * @return The endpoint address for the service.
133      * @throws FileStoreIdentifierException If the identifier is not valid.
134      * @throws FileStoreResolverException If unable to resolve the identifier.
135      *
136      */
137     public URL resolve(Ivorn ivorn)
138         throws FileStoreIdentifierException, FileStoreResolverException
139         {
140         log.debug("") ;
141         log.debug("----\"----") ;
142         log.debug("FileStoreEndpointResolverImpl.resolve()") ;
143         log.debug("  Ivorn : " + ivorn) ;
144         //
145         // Check for null ivorn.
146         if (null == ivorn)
147             {
148             throw new FileStoreIdentifierException(
149                 "Null identifier"
150                 ) ;
151             }
152         //
153         // Parse the ivorn and resolve it.
154     	return this.resolve(
155             new FileStoreIvornParser(ivorn)
156             ) ;
157         }
158 
159     /***
160      * Resolve an Ivorn parser into a service endpoint.
161      * @param parser A FileStoreIvornParser containing the Filestore identifier.
162      * @return The endpoint address for the service.
163      * @throws FileStoreIdentifierException If the identifier is not valid.
164      * @throws FileStoreResolverException If unable to resolve the identifier.
165      *
166      */
167     public URL resolve(FileStoreIvornParser parser)
168         throws FileStoreIdentifierException, FileStoreResolverException
169         {
170         log.debug("") ;
171         log.debug("----\"----") ;
172         log.debug("FileStoreEndpointResolverImpl.resolve()") ;
173         log.debug("  Ivorn : " + ((null != parser) ? parser.getIvorn() : null)) ;
174         //
175         // Check for null parser.
176         if (null == parser)
177             {
178             throw new FileStoreIdentifierException(
179                 "Null identifier"
180                 ) ;
181             }
182         //
183         // Check for null service.
184         if (null == parser.getServiceIdent())
185             {
186             throw new FileStoreIdentifierException(
187                 "Null filestore identifier"
188                 ) ;
189             }
190         //
191         // Get our service Ivorn.
192         Ivorn ivorn  = parser.getServiceIvorn() ;
193         //
194         // Lookup the service in the registry.
195         String endpoint = null ;
196         try {
197             endpoint = registry.getEndPointByIdentifier(
198                 ivorn
199                 ) ;
200             }
201         catch (Throwable ouch)
202             {
203             log.debug("FAIL : Registry lookup failed")  ;
204             log.debug("  Exception : " + ouch)  ;
205             throw new FileStoreResolverException(
206                 "Registry lookup failed",
207                 ouch
208                 ) ;
209             }
210         //
211         // If we found an entry in the Registry.
212         if (null != endpoint)
213             {
214             log.debug("PASS : Got service endpoint")  ;
215             log.debug("  Endpoint : " + endpoint)  ;
216             //
217             // Convert it into an endpoint URL.
218             try {
219                 return new URL(endpoint) ;
220                 }
221             //
222             // Report the problem in a Exception.
223             catch (MalformedURLException ouch)
224                 {
225                 throw new FileStoreResolverException(
226                     "Unable to parse Registry response into endpoint URL",
227                     ivorn
228                     ) ;
229                 }
230             }
231         //
232         // If we didn't get a service endpoint.
233         else {
234             //
235             // Report the problem in a Exception.
236             throw new FileStoreResolverException(
237                 "Registry returned null endpoint address for ivorn",
238                 ivorn
239                 ) ;
240             }
241         }
242     }
243