1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.jobscheduler.locator;
12
13 import org.apache.commons.digester.Digester;
14 import org.xml.sax.SAXException;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.URL;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 /*** Extension of map locator that uses digester to read in an xml configuration file giving tool details.
25 * @see tools.xml for examplpe configuration file
26 * @author Noel Winstanley nw@jb.man.ac.uk 25-Feb-2004
27 *
28 */
29 public class XMLFileLocator extends MapLocator {
30 /*** configuration interface - url of tool list document */
31 public static interface ToolList {
32 public URL getURL();
33 }
34
35 /*** Construct a new XMLFileLocator, read config from parameter URL
36 * @param url url to gain config from
37 */
38 public XMLFileLocator(ToolList t) throws IOException, SAXException{
39 this.url = t.getURL();
40 assert url != null;
41 InputStream is = url.openStream();
42 Digester dig = new Digester();
43 dig.push(this);
44 dig.addObjectCreate("tools/tool",MapLocator.ToolInfo.class);
45 dig.addSetProperties("tools/tool");
46 dig.addSetNext("tools/tool","addTool");
47 dig.parse(is);
48 }
49
50 protected final URL url;
51 /***
52 * @see org.astrogrid.jes.component.ComponentDescriptor#getDescription()
53 */
54 public String getDescription() {
55 return "Tool locator that populates internal map from an xml document at a URL\n"
56 + "Current Settings\n"
57 + "file location :" + url.toString()
58 + "\nmap contents :\n"
59 + m.toString();
60 }
61
62 /***
63 * @see org.astrogrid.jes.component.ComponentDescriptor#getInstallationTest()
64 */
65 public Test getInstallationTest() {
66 TestSuite suite = new TestSuite("Tests for XML File Locator");
67 suite.addTest(new InstallationTest("testCanReadFromURL"));
68 suite.addTest(super.getInstallationTest());
69 return suite;
70 }
71
72 /***
73 * @see org.astrogrid.jes.component.ComponentDescriptor#getName()
74 */
75 public String getName() {
76 return "XML File Locator";
77 }
78
79 protected class InstallationTest extends TestCase {
80 public InstallationTest(String s) {
81 super(s);
82 }
83
84 public void testCanReadFromURL() throws Exception{
85 InputStream is = url.openStream();
86 assertNotNull(is);
87
88 MapLocator loc = new XMLFileLocator(new ToolList() {
89
90 public URL getURL() {
91 return url;
92 }
93 });
94 assertNotNull(loc);
95 }
96 }
97
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119