View Javadoc

1   /*$Id: DefaultMetadataService.java,v 1.7 2004/10/08 20:01:51 pah Exp $
2    * Created on 21-May-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.applications.manager;
12  
13  import org.astrogrid.applications.CeaException;
14  import org.astrogrid.applications.component.ProvidesVODescription;
15  import org.astrogrid.common.bean.v1.Namespaces;
16  import org.astrogrid.component.descriptor.ComponentDescriptor;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.exolab.castor.xml.Marshaller;
21  
22  import java.io.InputStream;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  
26  import javax.xml.transform.Result;
27  import javax.xml.transform.Source;
28  import javax.xml.transform.Templates;
29  import javax.xml.transform.Transformer;
30  import javax.xml.transform.TransformerException;
31  import javax.xml.transform.TransformerFactory;
32  import javax.xml.transform.stream.StreamResult;
33  import javax.xml.transform.stream.StreamSource;
34  
35  import junit.framework.Test;
36  import junit.framework.TestCase;
37  
38  /*** Standard implementation of the {@link org.astrogrid.applications.manager.MetadataService} component
39   * @author Noel Winstanley nw@jb.man.ac.uk 21-May-2004
40   * @TODO need to rationalize with the {@link org.astrogrid.applications.description.registry} classes which is the definitive source....
41   */
42  public class DefaultMetadataService implements MetadataService, ComponentDescriptor {
43      private static final Log logger = LogFactory.getLog(DefaultMetadataService.class);
44      
45      private static final String FORMATTER_XSL = "registryFormatter.xsl";
46         
47      /*** Construct a new StandardCEAMetaData
48       * @param provider a component that provides a VODescription, on which this Metadata component works.
49       * 
50       */
51      public DefaultMetadataService( ProvidesVODescription provider) {
52         this.provider = provider;
53      }
54      //protected final ApplicationDescriptionLibrary library;
55      protected final ProvidesVODescription provider;
56    
57      
58  
59      public String returnRegistryEntry() throws CeaException {
60   
61         StringWriter sw = null, swt = null;
62         InputStream formatterXSL = null;
63         
64         try {
65            sw = new StringWriter();
66            Marshaller mar = new Marshaller(sw);
67            mar.setMarshalExtendedType(true);
68            mar.setSuppressXSIType(false);
69            mar.setMarshalAsDocument(true);
70            //castor will not write the namespace out if it is a top level element - need to do manually
71            mar.setNamespaceMapping("cea", Namespaces.VOCEA); 
72            mar.setNamespaceMapping("ceapd", Namespaces.CEAPD);
73            mar.setNamespaceMapping("ceab", Namespaces.CEAB);
74            mar.setNamespaceMapping("vr", Namespaces.VORESOURCE);
75            mar.marshal(provider.getVODescription());
76            sw.close();           
77            
78            // now transform to make suitable for the registry entry
79            
80            TransformerFactory fac = TransformerFactory.newInstance();
81            String xslpath = this.getClass().getPackage()+FORMATTER_XSL;
82            formatterXSL = this.getClass().getResourceAsStream(FORMATTER_XSL);
83            Source formatter = new StreamSource(formatterXSL);
84            Templates template = fac.newTemplates(formatter);
85            
86            Transformer xformer = template.newTransformer();
87            Source source = new StreamSource(new StringReader(sw.toString()));
88            swt = new StringWriter();
89            Result result = new StreamResult(swt);
90            xformer.transform(source, result);
91            
92            return swt.toString();
93           
94         }
95        catch (Exception e) {
96             logger.debug(e);
97           throw new CeaException("problem returning registry entry", e);
98         }
99      }    
100 
101     /***
102      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
103      */
104     public String getName() {
105         return "Standard CEA Server Description";
106     }
107     /***
108      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
109      */
110     public String getDescription() {
111         return "Standard implementation of the service description component";
112     }
113     /***
114      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
115      */
116     public Test getInstallationTest() {
117         return new InstallationTest("testGetRegistryEntry");
118     }
119     
120     public class InstallationTest extends TestCase {
121 
122         public InstallationTest(String arg0) {
123             super(arg0);
124         }
125         public void testGetRegistryEntry() throws Exception {
126             String entry = returnRegistryEntry();
127             assertNotNull(entry);
128             //@todo need more here really... but can't parse back into VODescripton, as castor fails to round-trip
129         }
130 
131 }
132 }
133 
134 
135 /* 
136 $Log: DefaultMetadataService.java,v $
137 Revision 1.7  2004/10/08 20:01:51  pah
138 make the registry entry more "registry ready" - make sure namespaces are ok
139 
140 Revision 1.6  2004/09/07 13:29:46  pah
141 made sure that the vr namespace is declared
142 
143 Revision 1.5  2004/08/28 07:17:34  pah
144 commandline parameter passing - unit tests ok
145 
146 Revision 1.4  2004/08/17 15:09:20  nw
147 minor improvement on logging
148 
149 Revision 1.3  2004/07/26 12:07:38  nw
150 renamed indirect package to protocol,
151 renamed classes and methods within protocol package
152 javadocs
153 
154 Revision 1.2  2004/07/01 11:16:22  nw
155 merged in branch
156 nww-itn06-componentization
157 
158 Revision 1.1.2.2  2004/07/01 01:42:46  nw
159 final version, before merge
160 
161 Revision 1.1.2.1  2004/06/17 09:21:23  nw
162 finished all major functionality additions to core
163 
164 Revision 1.1.2.1  2004/06/14 08:56:58  nw
165 factored applications into sub-projects,
166 got packaging of wars to work again
167 
168 Revision 1.1.2.1  2004/05/28 10:23:10  nw
169 checked in early, broken version - but it builds and tests (fail)
170  
171 */