1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
149 if (null == ivorn)
150 {
151 throw new IllegalArgumentException(
152 "Null service ivorn"
153 ) ;
154 }
155
156
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
186 if (null == parser)
187 {
188 throw new IllegalArgumentException(
189 "Null ivorn parser"
190 ) ;
191 }
192
193
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
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
223 if (null != endpoint)
224 {
225 log.debug("PASS : Got service endpoint") ;
226 log.debug(" Endpoint : " + endpoint) ;
227
228
229 try {
230 return new URL(
231 endpoint
232 ) ;
233 }
234
235
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
246 else {
247
248
249 throw new FileManagerResolverException(
250 "Registry returned null endpoint address for ivorn",
251 ivorn
252 ) ;
253 }
254 }
255 }
256