1
2
3
4
5
6
7
8
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228