View Javadoc

1   /*$Id: XMLFileLocator.java,v 1.4 2004/03/15 23:45:07 nw Exp $
2    * Created on 25-Feb-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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              // try creating another of self, see if barfs.
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 $Log: XMLFileLocator.java,v $
103 Revision 1.4  2004/03/15 23:45:07  nw
104 improved javadoc
105 
106 Revision 1.3  2004/03/07 21:04:38  nw
107 merged in nww-itn05-pico - adds picocontainer
108 
109 Revision 1.2.4.1  2004/03/07 20:41:06  nw
110 added component descriptor interface impl,
111 refactored any primitive types passed into constructor
112 
113 Revision 1.2  2004/02/27 00:46:03  nw
114 merged branch nww-itn05-bz#91
115 
116 Revision 1.1.2.1  2004/02/27 00:27:21  nw
117 rearranging code
118  
119 */