1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.portal.workflow.impl;
12
13 import org.astrogrid.applications.beans.v1.ApplicationBase;
14 import org.astrogrid.applications.beans.v1.InterfacesType;
15 import org.astrogrid.applications.beans.v1.Parameters;
16 import org.astrogrid.applications.beans.v1.parameters.BaseParameterDefinition;
17 import org.astrogrid.portal.workflow.intf.ApplicationDescription;
18 import org.astrogrid.portal.workflow.intf.ApplicationDescriptionSummary;
19 import org.astrogrid.portal.workflow.intf.ApplicationRegistry;
20 import org.astrogrid.portal.workflow.intf.WorkflowInterfaceException;
21 import org.astrogrid.registry.RegistryException;
22 import org.astrogrid.registry.beans.cea.ApplicationDefinition;
23 import org.astrogrid.registry.beans.resource.IdentifierType;
24 import org.astrogrid.registry.beans.resource.ResourceType;
25 import org.astrogrid.registry.beans.resource.VODescription;
26 import org.astrogrid.registry.client.RegistryDelegateFactory;
27 import org.astrogrid.registry.client.query.RegistryService;
28
29 import org.apache.axis.utils.XMLUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.exolab.castor.xml.CastorException;
33 import org.exolab.castor.xml.Unmarshaller;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.NodeList;
38
39 import java.io.File;
40 import java.io.FileWriter;
41 import java.io.IOException;
42 import java.net.URL;
43
44 /*** Implementation of ApplicationRegistry that resolves lookups using an astrogrid registry
45 * <p>
46 * @todo hacked at the moment to use the DOM-based queries. later use castor-object model ones.
47 * @author Noel Winstanley nw@jb.man.ac.uk 09-Mar-2004
48 *
49 */
50 public class RegistryApplicationRegistry implements ApplicationRegistry {
51 private static final Log logger = LogFactory.getLog(RegistryApplicationRegistry.class);
52
53 /*** construct a new RegistryApplicationRegistry
54 * let registry-delegate self-configure */
55 public RegistryApplicationRegistry() {
56 logger.info("Creating an astrogrid-backed application registry");
57 logger.info("Letting delegate determine own endpoint");
58 service = RegistryDelegateFactory.createQuery();
59 assert service != null;
60 }
61
62 /*** Construct a new RegistryApplicationRegistry
63 * @param endpoint endpoint for the astrogrid registry web service
64 */
65 public RegistryApplicationRegistry(URL endpoint) {
66 logger.info("Creating an astrogrid-backed application registry");
67 service = RegistryDelegateFactory.createQuery(endpoint);
68 assert service != null;
69 }
70 protected final RegistryService service;
71 /*** string query to to pass to registry to get list of tools back
72 */
73 public final static String LIST_QUERY_STRING= "<query><selectionSequence>" +
74 "<selection item='searchElements' itemOp='EQ' value='vr:Resource'/>" +
75 "<selectionOp op='$and$'/>" +
76 "<selection item='@xsi:type' itemOp='EQ' value='CeaApplicationType'/>" +
77 "<selectionOp op='OR'/>" +
78 "<selection item='@xsi:type' itemOp='EQ' value='cea:CeaApplicationType'/>" +
79 "<selectionOp op='OR'/>" +
80 "<selection item='@xsi:type' itemOp='EQ' value='CeaHttpApplicationType'/>" +
81 "<selectionOp op='OR'/>" +
82 "<selection item='@xsi:type' itemOp='EQ' value='cea:CeaHttpApplicationType'/>" +
83 "</selectionSequence></query>";
84
85
86
87 public String[] listApplications() throws WorkflowInterfaceException {
88 try {
89 Document doc = service.submitQuery(LIST_QUERY_STRING);
90 assert doc != null;
91 NodeList nl = doc.getElementsByTagNameNS("*","Identifier");
92
93 String[] names = new String[nl.getLength()];
94 for (int i = 0; i < nl.getLength(); i++) {
95 IdentifierType it = (IdentifierType)Unmarshaller.unmarshal(IdentifierType.class,nl.item(i));
96 names[i] = it.getAuthorityID() + "/" + it.getResourceKey();
97 }
98 return names;
99 } catch (CastorException e) {
100 logger.error("list applications failed - castor failed to parse result",e);
101 throw new WorkflowInterfaceException(e);
102 } catch (RegistryException e) {
103 logger.error("listApplications Failed with exception from registry",e);
104 throw new WorkflowInterfaceException(e);
105 }
106 }
107
108 /***
109 * @see org.astrogrid.portal.workflow.intf.ApplicationRegistry#listUIApplications()
110 */
111 public ApplicationDescriptionSummary[] listUIApplications() throws WorkflowInterfaceException {
112 try {
113 Document doc = service.submitQuery(LIST_QUERY_STRING);
114 assert doc != null;
115 NodeList nl = doc.getElementsByTagNameNS("*","Resource");
116
117 ApplicationDescriptionSummary[] descs = new ApplicationDescriptionSummary[nl.getLength()];
118 for (int i = 0; i < nl.getLength(); i++) {
119 Element resource = (Element)nl.item(i);
120 String authId = resource.getElementsByTagNameNS("*","AuthorityID").item(0).getFirstChild().getNodeValue();
121 String resourceKey = resource.getElementsByTagNameNS("*","ResourceKey").item(0).getFirstChild().getNodeValue();
122 String title = resource.getElementsByTagNameNS("*","Title").item(0).getFirstChild().getNodeValue();
123
124
125
126
127
128
129
130
131
132
133 NodeList interfaces = resource.getElementsByTagNameNS("*","Interface");
134 String[] interfaceNames = new String[interfaces.getLength()];
135 for (int j = 0; j < interfaces.getLength(); j++) {
136 interfaceNames[j] = ((Element)interfaces.item(j)).getAttribute("name");
137 }
138 descs[i] = new ApplicationDescriptionSummary(authId + "/" + resourceKey,title,interfaceNames);
139
140 }
141 return descs;
142 } catch (RegistryException e){
143 logger.error("listUIApplications failed with exception from registry",e);
144 throw new WorkflowInterfaceException(e);
145 }
146 }
147
148 /***
149 * @see org.astrogrid.portal.workflow.intf.ApplicationRegistry#getDescriptionFor(java.lang.String)
150 * @todo make namespace aware.
151 */
152 public ApplicationDescription getDescriptionFor(String applicationName) throws WorkflowInterfaceException {
153 try {
154 Document doc = service.getResourceByIdentifier(applicationName);
155 assert doc != null;
156
157 NodeList nl = doc.getElementsByTagNameNS("*","ApplicationDefinition");
158 if (nl.getLength() == 0) {
159 throw new WorkflowInterfaceException("Registry entry for '" + applicationName + "' has no ApplicationDefinition Element");
160 }
161 Element n = (Element)nl.item(0);
162
163 n.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); // bug-fix work around.
164 ApplicationDefinition def = (ApplicationDefinition) Unmarshaller.unmarshal(ApplicationDefinition.class,n);
165
166 ApplicationBase appBase = new ApplicationBase();
167 appBase.setName(applicationName);
168 appBase.setInterfaces(def.getInterfaces());
169 BaseParameterDefinition[] paramdef = def.getParameters().getParameterDefinition();
170 Parameters params = new Parameters();
171 appBase.setParameters(params);
172 params.setParameter(paramdef);
173
174 nl = doc.getElementsByTagNameNS("*","VODescription");
175 Element voDesc = null;
176 if (nl.getLength() > 0) {
177 voDesc = (Element)nl.item(0);
178 } else {
179 logger.warn("Odd - can't seem to find a VODescription for " + applicationName + " setting to null");
180 }
181 return new ApplicationDescription(appBase,voDesc);
182 } catch (CastorException e) {
183 logger.error("getDescriptionFor " + applicationName + " - castor failed to parse result",e);
184 throw new WorkflowInterfaceException(e);
185 } catch (RegistryException e) {
186 logger.error("getDescriptionFor " + applicationName + " - exception from registry",e);
187 throw new WorkflowInterfaceException(e);
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