View Javadoc

1   /*
2    * $Id: CeaResourceServer.java,v 1.9 2004/11/12 10:44:54 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.datacenter.metadata;
8   import java.io.IOException;
9   import java.io.StringWriter;
10  import java.util.Date;
11  import javax.xml.parsers.ParserConfigurationException;
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.astrogrid.applications.CeaException;
15  import org.astrogrid.applications.component.CEAComponentManagerFactory;
16  import org.astrogrid.config.SimpleConfig;
17  import org.astrogrid.datacenter.metadata.VoDescriptionServer;
18  import org.astrogrid.io.xml.XmlPrinter;
19  import org.astrogrid.io.xml.XmlTagPrinter;
20  import org.astrogrid.util.DomHelper;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.NodeList;
24  import org.xml.sax.SAXException;
25  
26  /***
27   * Serves the CEA resources.  Bit of a mangled fudge at the moment to get
28   * the right stuff from the right bit into the right bit.
29   * <p>
30   * @author M Hill
31   */
32  
33  public class CeaResourceServer implements VoResourcePlugin {
34  
35     protected static Log log = LogFactory.getLog(VoDescriptionServer.class);
36     
37     /***
38      * Returns an array of VOResource elements of the metadata.  Returns a string (rather than
39      * DOM element)
40      * so that we can combine them easily; some DOMs do not mix well.
41      */
42     public String[] getVoResources() throws IOException {
43  
44        //add the voresource for the CEA access to the datacenter
45        try {
46           String ceaVoDescription = CEAComponentManagerFactory.getInstance().getMetadataService().returnRegistryEntry();
47           //extract the resource elements
48           Document ceaDoc = DomHelper.newDocument(ceaVoDescription);
49           NodeList ceaResources = ceaDoc.getElementsByTagName("Resource");
50           if (ceaResources.getLength() ==0) {
51              ceaResources = ceaDoc.getElementsByTagName("vr:Resource");
52           }
53  
54           String[] returns = new String[ceaResources.getLength()];
55  
56           //each resource will need a little tweaking using the PALs configuration.
57           for (int i = 0; i < ceaResources.getLength(); i++) {
58              
59              Element resource = (Element) ceaResources.item(i);
60  
61              String type = resource.getAttribute("xsi:type");
62              resource.setAttribute("status", "active");
63              resource.setAttribute("update", VoDescriptionServer.REGISTRY_DATEFORMAT.format(new Date()));
64  
65              if (type.indexOf("CeaApplicationType")>-1) {
66                 returns[i] = getApplicationType(resource);
67              }
68              else if (type.indexOf("CeaServiceType")>-1) {
69                 returns[i] = getServiceType(resource);
70              }
71              else {
72                 returns[i] = DomHelper.ElementToString(resource);
73              }
74           }
75  
76           return returns;
77           
78        } catch (SAXException th) {
79          log.error(th+" getting CEA resources",th);
80        }
81        catch (ParserConfigurationException th) {
82          log.error(th+" getting CEA resources",th);
83        }
84        catch (CeaException th) {
85          log.error(th+" getting CEA resources",th);
86        }
87        return new String[] {} ;
88     }
89  
90     /*** Application type seems to be some of the authority stuff plus some
91      * application definition stuff.  the authority stuff is cut and pasted in
92      * from the autority plugin :-( and the application stuff lifted from the given reosurce */
93     public String getApplicationType(Element resource) throws IOException {
94        StringWriter sw = new StringWriter();
95        XmlPrinter printer = new XmlPrinter(sw, false);
96        XmlTagPrinter appType = printer.newTag("Resource", new String[] {
97                 "xsi:type='CeaApplicationType'",
98                 "status='active'",
99                 "updated='"+VoDescriptionServer.REGISTRY_DATEFORMAT.format(new Date())+"'"
100             });
101       VoDescriptionServer.writeIdentifier(appType, "/ceaApplication");
102 
103       VoDescriptionServer.writeSummary(appType);
104       VoDescriptionServer.writeCuration(appType);
105 
106       appType.writeTag("Type", "Other"); //for the workflow builder
107 
108       NodeList appDefs = resource.getElementsByTagName("cea:ApplicationDefinition");
109       if (appDefs.getLength() == 0) {
110          throw new MetadataException("No cea:ApplicationDefinition in CEA resource "+resource);
111       }
112       for (int i=0;i<appDefs.getLength();i++) {
113          appType.writeTag( (Element) appDefs.item(i));
114       }
115       appType.close();
116       sw.flush();
117       sw.close();
118       
119       return sw.toString();
120    }
121 
122    public String getServiceType(Element resource) throws IOException {
123       StringWriter sw = new StringWriter();
124       XmlPrinter printer = new XmlPrinter(sw, false);
125       XmlTagPrinter servType = printer.newTag("Resource", new String[] {
126                "xsi:type='CeaServiceType'",
127                "status='active'",
128                "updated='"+VoDescriptionServer.REGISTRY_DATEFORMAT.format(new Date())+"'"
129             });
130       VoDescriptionServer.writeIdentifier(servType , "/ceaService");
131 
132       VoDescriptionServer.writeSummary(servType );
133       VoDescriptionServer.writeCuration(servType );
134 
135       servType.writeTag("Type", "Other"); //for the workflow builder
136       
137       XmlTagPrinter capabilityTag = servType.newTag("vr:Capability");
138       capabilityTag.writeTag("vr:StandardURL",  "http://www.astrogrid.org/maven/build/applications/");
139       XmlTagPrinter sidTag = capabilityTag.newTag("vr:StandardID");
140       sidTag.writeTag("vr:AuthorityID", "astrogrid.org");
141       sidTag.writeTag("vr:ResourceKey", "CommonExecutionArchitucture");
142 
143       XmlTagPrinter intTag = servType.newTag("vr:Interface");
144       intTag.writeTag("vr:Invocation", "WebService");
145       intTag.writeTag("vr:AccessURL",  new String[] { "use='base'"},
146 //         ServletHelper.getUrlStem()+"services/CommonExecutionConnectorService"
147             SimpleConfig.getSingleton().getString("datacenter.url")+"services/CommonExecutionConnectorService"
148          );
149 
150       //reference to CeaApplication resource
151       XmlTagPrinter manappTag = servType.newTag("cea:ManagedApplications");
152       XmlTagPrinter appRefTag = manappTag.newTag("cea:ApplicationReference");
153       appRefTag.writeTag("AuthorityID", SimpleConfig.getSingleton().getString("datacenter.authorityId"));
154       appRefTag.writeTag("ResourceKey", SimpleConfig.getSingleton().getString("datacenter.resourceKey")+"/ceaApplication");
155 
156       /*
157       <vr:Capability>
158          <vr:StandardURL>http://www.astrogrid.org/maven/build/applications/</vr:StandardURL>
159          <vr:StandardID>
160             <vr:AuthorityID>astrogrid.og</vr:AuthorityID>
161             <vr:ResourceKey>CommonExecutionArchitucture</vr:ResourceKey>
162          </vr:StandardID>
163       </vr:Capability>
164       <vr:Interface>
165          <vr:Invocation>WebService</vr:Invocation>
166          <vr:AccessURL use="base">http://localhost:8080/astrogrid-cea-server/services/CommonExecutionConnectorService</vr:AccessURL>
167       </vr:Interface>
168 
169       <cea:ManagedApplications>
170          <cea:ApplicationReference>
171             <vr:AuthorityID>org.astrogrid.localhost</vr:AuthorityID>
172             <vr:ResourceKey>testdsa</vr:ResourceKey>
173          </cea:ApplicationReference>
174       </cea:ManagedApplications>
175     */
176 
177       servType.close();
178       sw.flush();
179       sw.close();
180       
181       return sw.toString();
182 
183    }
184 }
185 
186 
187 
188 
189 
190 
191