1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
146 if (null == ivorn)
147 {
148 throw new FileStoreIdentifierException(
149 "Null identifier"
150 ) ;
151 }
152
153
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
176 if (null == parser)
177 {
178 throw new FileStoreIdentifierException(
179 "Null identifier"
180 ) ;
181 }
182
183
184 if (null == parser.getServiceIdent())
185 {
186 throw new FileStoreIdentifierException(
187 "Null filestore identifier"
188 ) ;
189 }
190
191
192 Ivorn ivorn = parser.getServiceIvorn() ;
193
194
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
212 if (null != endpoint)
213 {
214 log.debug("PASS : Got service endpoint") ;
215 log.debug(" Endpoint : " + endpoint) ;
216
217
218 try {
219 return new URL(endpoint) ;
220 }
221
222
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
233 else {
234
235
236 throw new FileStoreResolverException(
237 "Registry returned null endpoint address for ivorn",
238 ivorn
239 ) ;
240 }
241 }
242 }
243