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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package org.astrogrid.filestore.server ;
55
56 import java.io.File ;
57
58 import java.net.URL ;
59 import java.net.URLConnection ;
60 import java.net.MalformedURLException ;
61
62 import org.apache.commons.logging.Log ;
63 import org.apache.commons.logging.LogFactory ;
64
65 import org.astrogrid.store.Ivorn ;
66
67 import org.astrogrid.config.Config ;
68 import org.astrogrid.config.SimpleConfig ;
69 import org.astrogrid.config.PropertyNotFoundException ;
70
71 import org.astrogrid.filestore.common.ivorn.FileStoreIvornFactory ;
72 import org.astrogrid.filestore.common.exception.FileStoreServiceException ;
73
74 import org.astrogrid.filestore.common.FileStoreConfig ;
75
76 /***
77 * Configuration implementation using the AstroGrid Config.
78 *
79 */
80 public class FileStoreConfigImpl
81 implements FileStoreConfig
82 {
83 /***
84 * Our debug logger.
85 *
86 */
87 private static Log log = LogFactory.getLog(FileStoreConfigImpl.class);
88
89 /***
90 * The config property key for our service name.
91 *
92 */
93 public static final String SERVICE_NAME = "org.astrogrid.filestore.service.name" ;
94
95 /***
96 * Reference to our AstroGrid config.
97 *
98 */
99 private Config config ;
100
101 /***
102 * Public constructor, using the default AstroGrid config.
103 *
104 */
105 public FileStoreConfigImpl()
106 {
107 this(
108 SimpleConfig.getSingleton()
109 ) ;
110 }
111
112 /***
113 * Public constructor, using a specific config.
114 * @param config Reference to the config to use.
115 *
116 */
117 public FileStoreConfigImpl(Config config)
118 {
119 this.config = config ;
120 }
121
122 /***
123 * Access to the local service name.
124 *
125 */
126 public String getServiceName()
127 {
128 String name = (String) config.getProperty(
129 SERVICE_NAME
130 ) ;
131 return name ;
132 }
133
134 /***
135 * Access to a config property, using the service name prefix.
136 * @param name The property name (this is combined with the service name to make the full property key).
137 *
138 */
139 public String getServiceProperty(String name)
140 {
141 log.debug("");
142 log.debug("FileStoreConfigImpl.getServiceProperty()");
143 log.debug(" Name : " + name);
144 String index = getServiceName() + "." + name ;
145 log.debug(" Index : " + index);
146 String value = (String) config.getProperty(
147 index
148 ) ;
149 log.debug(" Value : " + value);
150 return value ;
151 }
152
153 /***
154 * Access to the local service ivorn.
155 * @throws FileStoreServiceException if unable to read the property.
156 *
157 */
158 public Ivorn getServiceIvorn()
159 throws FileStoreServiceException
160 {
161 log.debug("");
162 log.debug("FileStoreConfigImpl.getServiceIvorn()");
163 try {
164 return new Ivorn(
165 getServiceProperty(
166 "service.ivorn"
167 )
168 ) ;
169 }
170 catch (Throwable ouch)
171 {
172 log.error("Unable to read service ivorn from config", ouch);
173 throw new FileStoreServiceException(
174 "Unable to read service ivorn from config.",
175 ouch
176 ) ;
177 }
178 }
179
180 /***
181 * The local service URL.
182 * @throws FileStoreServiceException if unable to read the property.
183 *
184 */
185 public URL getServiceUrl()
186 throws FileStoreServiceException
187 {
188 log.debug("");
189 log.debug("FileStoreConfigImpl.getServiceUrl()");
190 try {
191 return new URL(
192 getServiceProperty(
193 "service.url"
194 )
195 ) ;
196 }
197 catch (Throwable ouch)
198 {
199 throw new FileStoreServiceException(
200 "Unable to generate service URL.",
201 ouch
202 ) ;
203 }
204 }
205
206 /***
207 * Generate a resource ivorn.
208 * @param ident - the resource identifier.
209 * @throws FileStoreServiceException if unable to read the property.
210 *
211 */
212 public Ivorn getResourceIvorn(String ident)
213 throws FileStoreServiceException
214 {
215 log.debug("");
216 log.debug("FileStoreConfigImpl.getResourceIvorn()");
217 log.debug(" Ident : " + ident);
218 try {
219 return FileStoreIvornFactory.createIvorn(
220 getServiceProperty(
221 "service.ivorn"
222 ),
223 ident
224 ) ;
225 }
226 catch (Throwable ouch)
227 {
228 throw new FileStoreServiceException(
229 "Unable to generate resource ivorn.",
230 ouch
231 ) ;
232 }
233 }
234
235 /***
236 * Generate a resource URL.
237 * @param ident - the resource identifier.
238 * @throws FileStoreServiceException if unable to read the property.
239 *
240 */
241 public URL getResourceUrl(String ident)
242 throws FileStoreServiceException
243 {
244 log.debug("");
245 log.debug("FileStoreConfigImpl.getResourceUrl()");
246 log.debug(" Ident : " + ident);
247 try {
248 return new URL(
249 getServiceProperty(
250 "service.url"
251 )
252 + "/" + ident
253 ) ;
254 }
255 catch (Throwable ouch)
256 {
257 throw new FileStoreServiceException(
258 "Unable to generate resource URL.",
259 ouch
260 ) ;
261 }
262 }
263
264 /***
265 * The repository data directory.
266 * @throws FileStoreServiceException if unable to read the property.
267 *
268 */
269 public File getDataDirectory()
270 throws FileStoreServiceException
271 {
272 log.debug("");
273 log.debug("FileStoreConfigImpl.getDataDirectory()");
274 try {
275 return new File(
276 getServiceProperty(
277 "repository"
278 )
279 ) ;
280 }
281 catch (Throwable ouch)
282 {
283 throw new FileStoreServiceException(
284 "Unable to read data directory from config.",
285 ouch
286 ) ;
287 }
288 }
289
290 /***
291 * The repository info directory.
292 * @throws FileStoreServiceException if unable to read the property.
293 *
294 */
295 public File getInfoDirectory()
296 throws FileStoreServiceException
297 {
298 log.debug("");
299 log.debug("FileStoreConfigImpl.getInfoDirectory()");
300 try {
301 return new File(
302 getServiceProperty(
303 "repository"
304 )
305 ) ;
306 }
307 catch (Throwable ouch)
308 {
309 throw new FileStoreServiceException(
310 "Unable to read info directory from config.",
311 ouch
312 ) ;
313 }
314 }
315 }
316
317
318