View Javadoc

1   /*$Id: JavaClassApplication.java,v 1.7 2004/09/17 01:21:20 nw Exp $
2    * Created on 08-Jun-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.javaclass;
12  
13  import org.astrogrid.applications.AbstractApplication;
14  import org.astrogrid.applications.CeaException;
15  import org.astrogrid.applications.Status;
16  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
17  import org.astrogrid.applications.description.ApplicationInterface;
18  import org.astrogrid.applications.description.ParameterDescription;
19  import org.astrogrid.applications.parameter.ParameterAdapter;
20  import org.astrogrid.applications.parameter.protocol.ExternalValue;
21  import org.astrogrid.applications.parameter.protocol.ProtocolLibrary;
22  import org.astrogrid.workflow.beans.v1.Tool;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import java.lang.reflect.InvocationTargetException;
28  import java.lang.reflect.Method;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /*** An application that executes by calling a static java method
34   * @see org.astrogrid.applications.javaclass.JavaClassApplicationDescription
35   * @see java.lang.reflect.Method
36   * @author Noel Winstanley nw@jb.man.ac.uk 08-Jun-2004
37   */
38  public class JavaClassApplication extends AbstractApplication {
39      /***
40       * Commons Logger for this class
41       */
42      private static final Log logger = LogFactory.getLog(JavaClassApplication.class);
43  
44      /*** Construct a new JavaClassApplication
45       * @param ids
46       * @param user
47       * @param tool
48       * @param description
49       */
50      public JavaClassApplication(IDs ids, Tool tool, ApplicationInterface interf, ProtocolLibrary lib) {
51          super(ids, tool, interf,lib);
52      }
53      
54      /*** Starts the application executing.
55       * standard pattern - processes all input parameters, then starts a background thread to perform the execution itself.
56       * @todo bug here - we assume our parameters are in the correct order to pass to the java method. should sort them into correct order first.
57       * @see org.astrogrid.applications.Application#execute(org.astrogrid.applications.ApplicationExitMonitor)
58       */
59      public Runnable createExecutionTask() throws CeaException {
60          createAdapters();
61  
62         JavaClassApplicationDescription jappDesc = (JavaClassApplicationDescription)getApplicationDescription();            
63         Runnable task = new Worker(jappDesc.method);
64         setStatus(Status.INITIALIZED);
65         return task;
66     
67      }
68       /*** A Worker thread, that performs the computation after {@link JavaClassApplication#execute() } returns */
69       protected class Worker implements Runnable {
70           /*** Construct a new Worker
71           * @param args the arguments to the call 
72           * @param m the method to call
73           */
74          public Worker(Method m) {
75               this.method =m;
76           }
77           protected final Method method;
78           /*** 'executes' the application by calling {@link Method#invoke(java.lang.Object, java.lang.Object[])}*/
79           public void run() {   
80               try {
81                   List args = new ArrayList();
82                   for (Iterator i = inputParameterAdapters(); i.hasNext(); ) {
83                       ParameterAdapter a = (ParameterAdapter)i.next();
84                       args.add( a.process());
85                   }             
86                   setStatus(Status.RUNNING);
87                   Object resultVal = null;
88  
89                  resultVal = method.invoke(null,args.toArray());
90                  
91                  // we can do this, as we know there's only ever going to be one interface, and one output parameter.
92                  setStatus(Status.WRITINGBACK);
93                  ParameterAdapter result = (ParameterAdapter)outputParameterAdapters().next();
94                  result.writeBack(resultVal);
95                  setStatus(Status.COMPLETED);                
96              } catch (IllegalArgumentException e) {
97                  reportError("Illegal Argument passed to  java 'application'",e);
98              } catch (IllegalAccessException e) {
99                  reportError("Could not access java 'application'",e);
100             } catch (InvocationTargetException e) {
101                 reportError("Invoked java 'application' raised an exception",e.getTargetException());
102             } catch (CeaException e) {
103                 reportError("Failed to write back parameter values",e);
104             } catch (Throwable t) {
105                 reportError("Something else gone wrong",t);
106             }
107          }
108     }
109     
110     /*** overridden to return a {@link JavaClassParameterAdapter}
111      * @see org.astrogrid.applications.AbstractApplication#instantiateAdapter(org.astrogrid.applications.beans.v1.parameters.ParameterValue, org.astrogrid.applications.description.ParameterDescription, org.astrogrid.applications.parameter.indirect.IndirectParameterValue)
112      */
113     protected ParameterAdapter instantiateAdapter(ParameterValue pval,
114             ParameterDescription descr, ExternalValue indirectVal) {
115         return new JavaClassParameterAdapter(pval, descr, indirectVal);
116     }
117 
118 }
119 
120 
121 /* 
122 $Log: JavaClassApplication.java,v $
123 Revision 1.7  2004/09/17 01:21:20  nw
124 altered to work with new threadpool
125 
126 Revision 1.6.40.1  2004/09/14 13:46:04  nw
127 upgraded to new threading practice.
128 
129 Revision 1.6  2004/08/11 16:53:32  nw
130 added @todo for bug
131 
132 Revision 1.5  2004/07/26 12:07:38  nw
133 renamed indirect package to protocol,
134 renamed classes and methods within protocol package
135 javadocs
136 
137 Revision 1.4  2004/07/26 10:21:47  nw
138 javadoc
139 
140 Revision 1.3  2004/07/22 16:32:54  nw
141 cleaned up application / parameter adapter interface.
142 
143 Revision 1.2  2004/07/01 11:16:22  nw
144 merged in branch
145 nww-itn06-componentization
146 
147 Revision 1.1.2.3  2004/07/01 01:42:46  nw
148 final version, before merge
149 
150 Revision 1.1.2.2  2004/06/17 09:21:23  nw
151 finished all major functionality additions to core
152 
153 Revision 1.1.2.1  2004/06/14 08:56:58  nw
154 factored applications into sub-projects,
155 got packaging of wars to work again
156  
157 */