View Javadoc

1   /*$Id: RegistryV10ApplicationRegistry.java,v 1.5 2006/06/17 18:49:25 clq2 Exp $
2    * Created on 15-Apr-2005
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.Parameters;
15  import org.astrogrid.applications.beans.v1.parameters.BaseParameterDefinition;
16  import org.astrogrid.portal.workflow.intf.ApplicationDescription;
17  import org.astrogrid.portal.workflow.intf.ApplicationDescriptionSummary;
18  import org.astrogrid.portal.workflow.intf.ApplicationRegistry;
19  import org.astrogrid.portal.workflow.intf.WorkflowInterfaceException;
20  import org.astrogrid.oldquery.sql.Sql2Adql;
21  import org.astrogrid.registry.RegistryException;
22  import org.astrogrid.registry.beans.cea.ApplicationDefinition;
23  import org.astrogrid.registry.client.RegistryDelegateFactory;
24  import org.astrogrid.registry.client.query.RegistryService;
25  
26  import org.apache.axis.utils.XMLUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.exolab.castor.xml.CastorException;
30  import org.exolab.castor.xml.Unmarshaller;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.Element;
33  import org.w3c.dom.NodeList;
34  
35  import java.io.IOException;
36  import java.net.URL;
37  
38  /*** implementaiton of ApplicationRegistry against a v10 registry schema.
39   * @todo handle namespaces better.
40   * @todo rewrite to not use castor.
41   * @author Noel Winstanley nw@jb.man.ac.uk 15-Apr-2005
42   *
43   */
44  public class RegistryV10ApplicationRegistry implements ApplicationRegistry {
45      /***
46       * Commons Logger for this class
47       */
48      private static final Log logger = LogFactory.getLog(RegistryV10ApplicationRegistry.class);
49  
50      /*** Construct a new RegistryV10ApplicationRegistry
51       * 
52       */
53      public RegistryV10ApplicationRegistry() {
54          logger.info("Creating ApplicationRegistry for v10 registry entries");
55          logger.info("Creating an astrogrid-backed application registry");
56          logger.info("Letting delegate determine own endpoint");
57          service = RegistryDelegateFactory.createQuery();
58          assert service != null;
59      }
60  public RegistryV10ApplicationRegistry(URL endpoint) {
61      logger.info("Creating ApplicationRegistry for v10 registry entries");
62      logger.info("Creating an astrogrid-backed application registry");
63      logger.info("Letting delegate determine own endpoint");
64      service = RegistryDelegateFactory.createQuery();
65      assert service != null;
66  }
67  
68  protected final RegistryService service;
69  
70  public final static String LIST_QUERY_STRING= "Select * from Registry where " +
71  " (@xsi:type = 'cea:CeaApplicationType' or " +
72  " @xsi:type = 'cea:CeaHttpApplicationType')" +
73  " and @status = 'active'";
74      /***
75       * 
76       * @see org.astrogrid.portal.workflow.intf.ApplicationRegistry#listApplications()
77       */
78      public String[] listApplications() throws WorkflowInterfaceException {
79          try {
80              String adqlString = Sql2Adql.translateToAdql074(LIST_QUERY_STRING);
81              logger.info("ADQL String in PORTAL for REGISTRY = " + adqlString);
82  
83              Document doc = service.search(adqlString);
84              assert doc != null;
85              NodeList nl = doc.getElementsByTagNameNS("*","identifier");
86              
87              String[] names = new String[nl.getLength()];
88              for (int i = 0; i < nl.getLength(); i++) {
89                  names[i] =  nl.item(i).getFirstChild().getNodeValue();
90              }
91              return names;                     
92          } catch (RegistryException e) {
93              logger.error("listApplications Failed with exception from registry",e);
94              throw new WorkflowInterfaceException(e);
95          } catch(IOException e) {
96              logger.error("IOException Failure probable cause is the sql to adql translator",e);
97              throw new WorkflowInterfaceException(e);
98          }
99      }
100 
101     /***
102      * @see org.astrogrid.portal.workflow.intf.ApplicationRegistry#getDescriptionFor(java.lang.String)
103      */
104     public ApplicationDescription getDescriptionFor(String applicationName)
105             throws WorkflowInterfaceException {
106         try {
107             Document doc = service.getResourceByIdentifier(applicationName);
108             if (doc == null) {
109                 throw new WorkflowInterfaceException("Null documented returned");
110             }
111             logger.debug(XMLUtils.DocumentToString(doc));
112             
113             // navigate down to the bit we're interested in.
114             NodeList nl = doc.getElementsByTagNameNS("*","ApplicationDefinition");
115             logger.debug("Found " + nl.getLength() + " Application definitions");
116             if (nl.getLength() == 0) {                
117                 throw new WorkflowInterfaceException("Registry entry for '" + applicationName + "' has no ApplicationDefinition Element");
118             }
119             Element n = (Element)nl.item(0);            
120             n.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); // bug-fix work around.
121             logger.debug(XMLUtils.ElementToString(n));
122             
123             //@todo rewrite this to not use castor.
124             
125             ApplicationDefinition def = (ApplicationDefinition) Unmarshaller.unmarshal(ApplicationDefinition.class,n);
126            
127             logger.debug("Castor-Parsed the Application Definition");
128             // now mangle across to the required types.
129          
130             ApplicationBase appBase = new ApplicationBase();
131             appBase.setName(applicationName);
132             appBase.setInterfaces(def.getInterfaces());
133             BaseParameterDefinition[] paramdef = def.getParameters().getParameterDefinition();
134             Parameters params = new Parameters();
135            appBase.setParameters(params);
136            params.setParameter(paramdef);
137            logger.debug("Now finding resource");
138            // quickly find the vodescription..
139            //v0.9 - nl = doc.getElementsByTagNameNS("*","VODescription");
140            // is the equivalent of this Resource now??
141            nl = doc.getElementsByTagNameNS("*","Resource");
142            Element voDesc = null;
143            if (nl.getLength() > 0) {
144                voDesc = (Element)nl.item(0);
145            } else {
146                logger.warn("Odd - can't seem to find a Resource " + applicationName + " setting to null");
147            }
148            logger.debug("Completed building application description");
149             return new ApplicationDescription(appBase,voDesc);   
150         } catch (CastorException e) {
151             logger.error("getDescriptionFor " + applicationName + " - castor failed to parse result",e);
152             throw new WorkflowInterfaceException(e);         
153         } catch (RegistryException e) {
154             logger.error("getDescriptionFor " + applicationName + " - exception from registry",e);
155             throw new WorkflowInterfaceException(e);
156         }
157     }
158 
159     public ApplicationDescriptionSummary[] listUIApplications() throws WorkflowInterfaceException {
160         try {
161             String adqlString = Sql2Adql.translateToAdql074(LIST_QUERY_STRING);
162             logger.info("ADQL String in PORTAL for REGISTRY = " + adqlString);
163             Document doc = service.search(adqlString);
164 
165         assert doc != null;
166         NodeList nl = doc.getElementsByTagNameNS("*","Resource");
167 
168         ApplicationDescriptionSummary[] descs = new ApplicationDescriptionSummary[nl.getLength()];
169         for (int i = 0; i < nl.getLength(); i++) {
170             Element resource = (Element)nl.item(i);
171             String identifier = resource.getElementsByTagNameNS("*","identifier").item(0).getFirstChild().getNodeValue();
172             String title = resource.getElementsByTagNameNS("*","title").item(0).getFirstChild().getNodeValue();
173 
174             NodeList interfaces = resource.getElementsByTagNameNS("*","Interface");
175             String[] interfaceNames = new String[interfaces.getLength()];
176             for (int j = 0; j < interfaces.getLength(); j++) {
177                 interfaceNames[j] = ((Element)interfaces.item(j)).getAttribute("name");
178             }
179             descs[i] = new ApplicationDescriptionSummary(identifier,title,interfaceNames);
180             
181         }
182         return descs;
183         } catch (RegistryException e){
184             logger.error("listUIApplications failed with exception from registry",e);
185             throw new WorkflowInterfaceException(e);
186         } catch(IOException e) {
187             logger.error("IOException Failure probable cause is the sql to adql translator",e);
188             throw new WorkflowInterfaceException(e);
189         }
190     }
191 
192 }
193 
194 
195 /* 
196 $Log: RegistryV10ApplicationRegistry.java,v $
197 Revision 1.5  2006/06/17 18:49:25  clq2
198 to match KEA_PAL_1612 change of package name of org.astrogrid.query.sql.Sql2Adql to org.astrogrid.oldquery.sql.Sql2Adql
199 
200 Revision 1.4  2005/06/23 08:00:32  nw
201 fixed error on v10 list applications query - just needed ( )
202 
203 Revision 1.3  2005/06/08 14:52:24  clq2
204 1111
205 
206 Revision 1.2.2.1  2005/06/02 15:06:02  nw
207 improved logging
208 
209 Revision 1.2  2005/04/27 13:43:17  clq2
210 1082
211 
212 Revision 1.1.2.1  2005/04/15 17:30:06  nw
213 added registry implementation for v10.
214  
215 */