1
2
3
4
5
6
7 package org.astrogrid.dataservice.metadata;
8
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.net.URLConnection;
14 import javax.xml.parsers.ParserConfigurationException;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.astrogrid.cfg.ConfigException;
18 import org.astrogrid.cfg.ConfigFactory;
19 import org.astrogrid.dataservice.metadata.v0_10.ProxyResourceSupport;
20 import org.astrogrid.xml.DomHelper;
21 import org.w3c.dom.Document;
22 import org.xml.sax.SAXException;
23
24 /***
25 * Serves a metadata resource from a file on disk.
26 * <p>
27 * @author M Hill
28 */
29
30 public class UrlResourcePlugin extends ProxyResourceSupport implements VoResourcePlugin {
31
32
33 /*** Configuration key to where the metadata file is located */
34 public static final String METADATA_URL_LOC_KEY = "datacenter.resource.url";
35
36 /*** Returns the URLs to the metadata given by the configuration properties */
37 public URL[] getResourceUrls() throws IOException {
38
39 Object[] urls = ConfigFactory.getCommonConfig().getProperties(METADATA_URL_LOC_KEY);
40
41 if (urls.length == 0) {
42 throw new ConfigException("Server not configured properly: no url "+METADATA_URL_LOC_KEY+".* keys are given in config ("+ConfigFactory.getCommonConfig().loadedFrom()+") to locate metadata.");
43 }
44
45 URL[] resourceUrls = new URL[urls.length];
46
47 for (int u=0; u<urls.length; u++) {
48 resourceUrls[u] = new URL(urls[u].toString());
49 }
50
51 return resourceUrls;
52 }
53
54 /*** Returns an array of resources by going through the URLs given by the config
55 * key, extracting the resources in each one (as each one may have more than one) and appedning them
56 */
57 public String getVoResource() throws IOException {
58 StringBuffer resources = new StringBuffer();
59
60 URL[] urls = getResourceUrls();
61 for (int u = 0; u < urls.length; u++) {
62
63 URLConnection connection = urls[u].openConnection();
64 InputStream is = connection.getInputStream();
65
66 if (is == null) {
67 throw new FileNotFoundException("Metadata file not found at "+urls[u]);
68 }
69
70 try {
71 resources.append( makeLocal(is));
72
73 }
74 catch (MetadataException me) {
75 log.error("Error in metadata file "+urls[u],me);
76
77 MetadataException me2 = new MetadataException(me.getMessage()+" in file "+urls[u],me.getCause());
78 me2.setStackTrace(me.getStackTrace());
79 throw me2;
80 }
81 }
82 return resources.toString();
83 }
84
85
86
87 }
88
89