View Javadoc

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