View Javadoc

1   /*$Id: RegistryApplicationRegistry.java,v 1.11 2004/12/13 13:55:05 pjn3 Exp $
2    * Created on 09-Mar-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.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             /* crap - was extracting the wrong thing. wanted title instead
124             NodeList uiElement = resource.getElementsByTagNameNS("*","UI_Name");
125             String uiName = "unavailable";
126             if(uiElement.getLength() > 0) {
127                 Node n = uiElement.item(0).getFirstChild();
128                 if (n != null) {
129                     uiName = n.getNodeValue();
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             // navigate down to the bit we're interested in.
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             // now mangle across to the required types.
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            // quickly find the vodescription..
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 $Log: RegistryApplicationRegistry.java,v $
198 Revision 1.11  2004/12/13 13:55:05  pjn3
199 Bug #827 - change by RTP to query string to include cea namespace
200 
201 Revision 1.10  2004/11/12 18:14:43  clq2
202 nww-itn07-590b again.
203 
204 Revision 1.8.4.2  2004/11/11 14:37:16  nw
205 bugfix. was exracting the wrong field
206 
207 Revision 1.8.4.1  2004/11/10 13:33:32  nw
208 added new method to ApplicationRegistry - listUIApplications
209 
210 Revision 1.8  2004/11/08 18:05:15  jdt
211 Merges from branch nww-bz#590
212 
213 Revision 1.7.2.1  2004/10/28 14:53:50  nw
214 added method getOriginalVODescription() to ApplicationDescription,
215 adjusted RegistryApplicationRegistry to populate this field.
216 
217 Revision 1.7  2004/10/12 11:24:21  pah
218 used explicit namespaces in query for 200x speedup!
219 
220 Revision 1.6  2004/10/08 20:04:39  pah
221 optimize the tool query to use namespaces - better performance than using wildcard
222 
223 Revision 1.5  2004/09/02 13:03:53  jdt
224 merge from SIAP case3
225 
226 */