View Javadoc

1   /*$Id: TableManipulatorApplicationDescription.java,v 1.2 2004/11/27 13:20:02 pah Exp $
2    * Created on 11-Aug-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.apps;
12  
13  import org.astrogrid.applications.AbstractApplication;
14  import org.astrogrid.applications.Application;
15  import org.astrogrid.applications.CeaException;
16  import org.astrogrid.applications.DefaultIDs;
17  import org.astrogrid.applications.Status;
18  import org.astrogrid.applications.apps.tables.TableConverter;
19  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
20  import org.astrogrid.applications.beans.v1.parameters.types.ParameterTypes;
21  import org.astrogrid.applications.description.ApplicationInterface;
22  import org.astrogrid.applications.description.ParameterDescription;
23  import org.astrogrid.applications.description.base.AbstractApplicationDescription;
24  import org.astrogrid.applications.description.base.ApplicationDescriptionEnvironment;
25  import org.astrogrid.applications.description.base.BaseApplicationInterface;
26  import org.astrogrid.applications.description.base.BaseParameterDescription;
27  import org.astrogrid.applications.description.exception.ParameterDescriptionNotFoundException;
28  import org.astrogrid.applications.parameter.DefaultParameterAdapter;
29  import org.astrogrid.applications.parameter.ParameterAdapter;
30  import org.astrogrid.applications.parameter.StreamParameterAdapter;
31  import org.astrogrid.applications.parameter.protocol.ExternalValue;
32  import org.astrogrid.applications.parameter.protocol.ProtocolLibrary;
33  import org.astrogrid.community.User;
34  import org.astrogrid.component.descriptor.ComponentDescriptor;
35  import org.astrogrid.workflow.beans.v1.Tool;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import uk.ac.starlink.table.StarTableFactory;
41  
42  import java.io.InputStream;
43  import java.util.HashMap;
44  import java.util.Iterator;
45  import java.util.Map;
46  
47  import javax.mail.Message;
48  import javax.mail.MessagingException;
49  import javax.mail.Session;
50  import javax.mail.Transport;
51  import javax.mail.internet.InternetAddress;
52  import javax.mail.internet.MimeMessage;
53  import javax.naming.Context;
54  import javax.naming.InitialContext;
55  import javax.naming.NamingException;
56  
57  import junit.framework.Test;
58  import junit.framework.TestCase;
59  import junit.framework.TestSuite;
60  
61  /*** Description of a generic table Manipulation application.
62   * <P>
63   * this application can
64   * <ul>
65   * <li> Transform tables from one representation to another - e.g. CSV to VOTable
66   * <li> Manipulation of table columns within a single table
67   * <li> limited cross matching facility
68   * </ul>
69   * <p> it is based on the Starlink STIL package for generic table manipulations.
70   * @author Paul Harrison (pah@jb.man.ac.uk)
71   *
72   */
73  public class TableManipulatorApplicationDescription extends AbstractApplicationDescription implements ComponentDescriptor {
74      private static final String INPUTTABLENAME = "INTABLE";
75     private static final String INPUT2TABLENAME = "INTABLE2";
76     private static final String OUTPUTTABLENAME = "OUTTABLE";
77     private static final String CONVERT = "convert";
78     private static final String OUTPUTFORMATNAME = "FORMAT";
79  
80  
81  
82     /*** Construct a new Description
83       * @param env
84       */
85      public TableManipulatorApplicationDescription(ApplicationDescriptionEnvironment env) {
86          super(env);
87          this.setMetaData();
88      }
89      
90      /*** set up metadata for this instance */
91      private final void setMetaData() {
92         StringBuffer thename = new StringBuffer(env.getAuthIDResolver().getAuthorityID());
93         thename.append("/TableManipulator");
94         setName(thename.toString()); 
95         BaseParameterDescription inputTable = new BaseParameterDescription();
96         inputTable.setName(INPUTTABLENAME);
97         inputTable.setDisplayName("Input table");
98         inputTable.setDisplayDescription("The table to be manipulated");
99         inputTable.setType(ParameterTypes.BINARY);
100        this.addParameterDescription(inputTable);
101        
102        BaseParameterDescription inputTable2 = new BaseParameterDescription();
103        inputTable2.setName(INPUT2TABLENAME);
104        inputTable2.setDisplayName("2nd Input table");
105        inputTable2.setDisplayDescription("The table");
106        inputTable2.setType(ParameterTypes.BINARY);
107        this.addParameterDescription(inputTable);
108        BaseParameterDescription outputTable = new BaseParameterDescription();
109        outputTable.setName(OUTPUTTABLENAME);
110        outputTable.setDisplayName("Output table");
111        outputTable.setDisplayDescription("The output table");
112        outputTable.setType(ParameterTypes.BINARY);
113        this.addParameterDescription(inputTable);	
114        BaseParameterDescription outputFormat = new BaseParameterDescription();
115        outputFormat.setName(OUTPUTFORMATNAME);
116        outputFormat.setDisplayName("format");
117        outputFormat.setDisplayDescription("the format to output the table in");
118        outputFormat.setType(ParameterTypes.TEXT);
119        this.addParameterDescription(outputFormat);
120  
121         BaseApplicationInterface convertInterface = new BaseApplicationInterface(CONVERT,this);
122         try {
123             convertInterface.addInputParameter(inputTable.getName());
124             convertInterface.addInputParameter(outputFormat.getName());
125             convertInterface.addOutputParameter(outputTable.getName());
126         } catch (ParameterDescriptionNotFoundException e) {
127             logger.fatal("Programming error",e);// really shouldn't happen.
128             throw new RuntimeException("Programming error",e);
129         }
130         this.addInterface(convertInterface);
131     }
132 
133     /***
134      * Commons Logger for this class
135      */
136     private static final Log logger = LogFactory.getLog(TableManipulatorApplicationDescription.class);
137 
138 
139 
140     /***
141      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
142      */
143     public String getDescription() {
144         return "Table Maipulation application\n" + this.toString();
145     }
146 
147     /*** installation test verifies mailer session object is present in jndi.
148      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
149      */
150     public Test getInstallationTest() {        
151         return null;
152     }
153     
154     /***
155      * @see org.astrogrid.applications.description.ApplicationDescription#initializeApplication(java.lang.String, org.astrogrid.community.User, org.astrogrid.workflow.beans.v1.Tool)
156      */
157     public Application initializeApplication(String callerAssignedID, User user, Tool tool) throws Exception {
158         String newID = env.getIdGen().getNewID();
159         final DefaultIDs ids = new DefaultIDs(callerAssignedID,newID,user);
160         ApplicationInterface iface = this.getInterface(tool.getInterface());
161         return new TableManipulatorApplication(ids,tool,iface,env.getProtocolLib());
162     }
163 
164     /***The actual table manipulator application.
165      * @TODO perhaps this needs to be in its own file.
166     * @author Paul Harrison (pah@jb.man.ac.uk) 09-Nov-2004
167     * @version $Name:  $
168     * @since iteration6
169     */
170    public static class TableManipulatorApplication extends AbstractApplication {
171 
172         /*** Construct a new SendMailApplication
173          * @param ids
174          * @param tool
175          * @param applicationInterface
176          * @param lib
177          */
178         public TableManipulatorApplication(IDs ids, Tool tool, ApplicationInterface applicationInterface, ProtocolLibrary lib) {
179             super(ids, tool, applicationInterface, lib);
180         }
181 
182       protected ParameterAdapter instantiateAdapter(ParameterValue pval,
183             ParameterDescription descr, ExternalValue indirectVal) {
184          if (pval.getName().equals(OUTPUTFORMATNAME)) {
185             return new DefaultParameterAdapter(pval, descr, indirectVal);
186          }
187          else {
188             return new StreamParameterAdapter(pval, descr, indirectVal);
189          }
190       }
191         public Runnable createExecutionTask() throws CeaException {
192 
193             createAdapters();
194             setStatus(Status.INITIALIZED);
195             return new Runnable() {
196                     public void run() {
197                         final Map args = new HashMap();
198                         try {
199                         for (Iterator i = inputParameterAdapters(); i.hasNext();) {
200                             ParameterAdapter a = (ParameterAdapter)i.next();
201                             args.put(a.getWrappedParameter().getName(),a.process());
202                         }                        
203                         setStatus(Status.RUNNING);
204                         // convert the table here
205                         if(getApplicationInterface().getName().equals(CONVERT)){
206                         TableConverter tableconverter = new TableConverter((InputStream)args.get(INPUTTABLENAME), (String)args.get(OUTPUTFORMATNAME));
207                         }
208                        // table = new StarTableFactory().makeStarTable(trans);
209                          } catch (Throwable t) {
210                             reportError("Something went wrong",t);
211                         }
212                     }
213             };
214 
215             
216         }
217     }
218     
219 }
220 
221 
222 /* 
223 $Log: TableManipulatorApplicationDescription.java,v $
224 Revision 1.2  2004/11/27 13:20:02  pah
225 result of merge of pah_cea_bz561 branch
226 
227 Revision 1.1.2.1  2004/11/15 16:55:05  pah
228 initial thoughts on a table manipulator - stalled because want to make changes to STIL
229 
230 Revision 1.5  2004/09/17 01:21:12  nw
231 altered to work with new threadpool
232 
233 Revision 1.4.12.1  2004/09/14 13:46:04  nw
234 upgraded to new threading practice.
235 
236 Revision 1.4  2004/09/03 13:19:14  nw
237 added some progress messages
238 
239 Revision 1.3  2004/08/28 11:25:10  nw
240 tried to improve error trapping - seems to fail to complete at the moment.
241 
242 Revision 1.2  2004/08/16 11:03:46  nw
243 first stab at a cat application
244 
245 Revision 1.1  2004/08/11 17:40:49  nw
246 implemented send mail application
247  
248 */