1
2
3
4
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
45 try {
46 String ceaVoDescription = CEAComponentManagerFactory.getInstance().getMetadataService().returnRegistryEntry();
47
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
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");
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");
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
147 SimpleConfig.getSingleton().getString("datacenter.url")+"services/CommonExecutionConnectorService"
148 );
149
150
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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