View Javadoc

1   /*$Id: MapLocator.java,v 1.12 2005/03/13 07:13:39 clq2 Exp $
2    * Created on 19-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.astrogrid.component.descriptor.ComponentDescriptor;
14  import org.astrogrid.jes.JesException;
15  import org.astrogrid.jes.job.NotFoundException;
16  import org.astrogrid.jes.jobscheduler.Locator;
17  import org.astrogrid.workflow.beans.v1.Tool;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.IOException;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.net.URLConnection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import junit.framework.Test;
31  import junit.framework.TestCase;
32  import junit.framework.TestSuite;
33  
34  /***Tool locator that accepts a map of tool information.
35   * Interim solution - eventually will find this information from the registry. 
36   * @author Noel Winstanley nw@jb.man.ac.uk 19-Feb-2004
37   *
38   */
39  public class MapLocator implements Locator, ComponentDescriptor {
40      protected static final Log logger = LogFactory.getLog(MapLocator.class);
41      /*** Construct a new MapToolLocator
42       * 
43       */
44      public MapLocator() {
45          m = new HashMap();
46      }
47  
48      protected final Map m;
49  
50      /*** add a tool to the map */
51      public void addTool(ToolInfo info) {
52          m.put(info.getName(),info);
53      }
54      /***
55       * @see org.astrogrid.jes.jobscheduler.ToolLocator#locateTool(org.astrogrid.jes.job.JobStep)
56       */
57      public String[] locateTool(Tool tool) throws JesException{
58          ToolInfo nfo = getToolInfo(tool);
59          logger.debug("tool location for " + tool.getName() + " :=" + nfo.getLocation());
60          return new String[]{nfo.getLocation()};
61          
62      }
63      /*** return tool for this job step, or throw not found excepton */
64      private ToolInfo getToolInfo(Tool tool) throws JesException {
65          if (tool == null) {
66              throw new JesException("Job Step contains no tool");
67          }
68          String toolName = tool.getName();
69          ToolInfo nfo = (ToolInfo)m.get(toolName);
70          if (nfo == null) {
71              throw new NotFoundException("No information for tool named " + toolName);
72          }
73          return nfo;
74      }
75  
76      
77      public static class ToolInfo {
78          private String location;
79          private String interfaceName;
80          private String name;
81  
82          public void setLocation(String endpoint) {
83              this.location = endpoint;
84          }
85  
86          public String getLocation() {
87              return location;
88          }
89  
90          public void setInterface(String interfaceName) {
91              this.interfaceName = interfaceName;
92          }
93  
94          public String getInterface() {
95              return interfaceName;
96          }
97          /***
98           * @return
99           */
100         public String getName() {
101             return name;
102         }
103 
104         /***
105          * @param string
106          */
107         public void setName(String string) {
108             name = string;
109         }
110 
111     public String toString() {
112         StringBuffer buffer = new StringBuffer();
113         buffer.append("[ToolInfo:");
114         buffer.append(" location: ");
115         buffer.append(location);
116         buffer.append(" interfaceName: ");
117         buffer.append(interfaceName);
118         buffer.append(" name: ");
119         buffer.append(name);
120         buffer.append("]");
121         return buffer.toString();
122     }
123     }
124     /***
125      * @see org.astrogrid.jes.component.ComponentDescriptor#getName()
126      */
127     public String getName() {
128         return "MapToolLocator";
129     }
130     /***
131      * @see org.astrogrid.jes.component.ComponentDescriptor#getDescription()
132      */
133     public String getDescription() {
134         return "Empty map of tool locations, that can be extended prorammatically\n" + 
135         "current contents:\n" + m.toString();
136     }
137     /***
138      * @see org.astrogrid.jes.component.ComponentDescriptor#getInstallationTest()
139      */
140     public Test getInstallationTest() {        
141         TestSuite suite  = new TestSuite("Tests for Map Locator");
142         suite.addTest(new InstallationTest("testCanResolveToolEndpoints"));
143         return suite;    
144     }
145 
146     
147     protected class InstallationTest extends TestCase {
148         public InstallationTest(String s) {
149             super(s);
150         }
151         /*** @todo more checking of url endpoint here.. */
152         public void testCanResolveToolEndpoints() {
153             Iterator i = m.values().iterator();
154             while (i.hasNext()) {
155                 ToolInfo nfo = (ToolInfo)i.next();
156                 assertNotNull(nfo);
157                 try {                 
158                     URL url = new URL(nfo.getLocation());
159                     URLConnection conn = url.openConnection();
160                     assertNotNull(conn);
161                     conn.connect();
162                 } catch (MalformedURLException e) {
163                     fail(nfo.getName() + " " + e.getMessage());
164                 } catch(IOException e) {
165                     fail("could not connect to " + nfo.getName() + ": " + e.getMessage());
166                 }
167             }
168         }
169     }
170 
171 }
172 
173 
174 /* 
175 $Log: MapLocator.java,v $
176 Revision 1.12  2005/03/13 07:13:39  clq2
177 merging jes-nww-686 common-nww-686 workflow-nww-996 scripting-nww-995 cea-nww-994
178 
179 Revision 1.11.92.1  2005/03/11 15:21:35  nw
180 adjusted locator so that it returns a list of endpoints to connect to.
181 we can get round-robin by shuffling the list.
182 dispatcher tries each endpoint in the list until can connect to one wihout throwing an exception.
183 
184 Revision 1.11  2004/08/13 09:07:26  nw
185 tidied imports
186 
187 Revision 1.10  2004/08/03 16:31:25  nw
188 simplified interface to dispatcher and locator components.
189 removed redundant implementations.
190 
191 Revision 1.9  2004/07/01 21:15:00  nw
192 added results-listener interface to jes
193 
194 Revision 1.8  2004/03/16 01:21:12  nw
195 fixed bug
196 
197 Revision 1.7  2004/03/15 23:45:07  nw
198 improved javadoc
199 
200 Revision 1.6  2004/03/15 01:30:45  nw
201 factored component descriptor out into separate package
202 
203 Revision 1.5  2004/03/12 15:32:14  nw
204 improved logging
205 
206 Revision 1.4  2004/03/07 21:04:38  nw
207 merged in nww-itn05-pico - adds picocontainer
208 
209 Revision 1.3.4.1  2004/03/07 20:41:06  nw
210 added component descriptor interface impl,
211 refactored any primitive types passed into constructor
212 
213 Revision 1.3  2004/03/04 01:57:35  nw
214 major refactor.
215 upgraded to latest workflow object model.
216 removed internal facade
217 replaced community snippet with objects
218 
219 Revision 1.2  2004/02/27 00:46:03  nw
220 merged branch nww-itn05-bz#91
221 
222 Revision 1.1.2.1  2004/02/27 00:27:21  nw
223 rearranging code
224 
225 Revision 1.1.2.1  2004/02/19 13:38:17  nw
226 started implementation of an alternative tool locator
227  
228 */