View Javadoc

1   /*$Id: JavaClassApplication.java,v 1.8 2006/06/13 20:33:13 clq2 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         // CeaUser.setUser(ids.getUser()); // Makes the User object available as ThreadLocal - but doesn't work.
53      }
54      
55      /*** Starts the application executing.
56       * standard pattern - processes all input parameters, then starts a background thread to perform the execution itself.
57       * @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.
58       * @see org.astrogrid.applications.Application#execute(org.astrogrid.applications.ApplicationExitMonitor)
59       */
60      public Runnable createExecutionTask() throws CeaException {
61          createAdapters();
62  
63         JavaClassApplicationDescription jappDesc = (JavaClassApplicationDescription)getApplicationDescription();            
64         Runnable task = new Worker(jappDesc.method);
65         setStatus(Status.INITIALIZED);
66         return task;
67     
68      }
69       /*** A Worker thread, that performs the computation after {@link JavaClassApplication#execute() } returns */
70       protected class Worker implements Runnable {
71           /*** Construct a new Worker
72           * @param args the arguments to the call 
73           * @param m the method to call
74           */
75          public Worker(Method m) {
76               this.method =m;
77           }
78           protected final Method method;
79           /*** 'executes' the application by calling {@link Method#invoke(java.lang.Object, java.lang.Object[])}*/
80           public void run() {   
81               try {
82                   List args = new ArrayList();
83                   for (Iterator i = inputParameterAdapters(); i.hasNext(); ) {
84                       ParameterAdapter a = (ParameterAdapter)i.next();
85                       args.add( a.process());
86                   }             
87                   setStatus(Status.RUNNING);
88                   Object resultVal = null;
89  
90                  resultVal = method.invoke(null,args.toArray());
91                  
92                  // we can do this, as we know there's only ever going to be one interface, and one output parameter.
93                  setStatus(Status.WRITINGBACK);
94                  ParameterAdapter result = (ParameterAdapter)outputParameterAdapters().next();
95                  result.writeBack(resultVal);
96                  setStatus(Status.COMPLETED);                
97              } catch (IllegalArgumentException e) {
98                  reportError("Illegal Argument passed to  java 'application'",e);
99              } catch (IllegalAccessException e) {
100                 reportError("Could not access java 'application'",e);
101             } catch (InvocationTargetException e) {
102                 reportError("Invoked java 'application' raised an exception",e.getTargetException());
103             } catch (CeaException e) {
104                 reportError("Failed to write back parameter values",e);
105             } catch (Throwable t) {
106                 reportError("Something else gone wrong",t);
107             }
108          }
109     }
110     
111     /*** overridden to return a {@link JavaClassParameterAdapter}
112      * @see org.astrogrid.applications.AbstractApplication#instantiateAdapter(org.astrogrid.applications.beans.v1.parameters.ParameterValue, org.astrogrid.applications.description.ParameterDescription, org.astrogrid.applications.parameter.indirect.IndirectParameterValue)
113      */
114     protected ParameterAdapter instantiateAdapter(ParameterValue pval,
115             ParameterDescription descr, ExternalValue indirectVal) {
116         return new JavaClassParameterAdapter(pval, descr, indirectVal);
117     }
118 
119 }
120 
121 
122 /* 
123 $Log: JavaClassApplication.java,v $
124 Revision 1.8  2006/06/13 20:33:13  clq2
125 pal_gtr_1671
126 
127 Revision 1.7.162.1  2006/06/09 17:49:07  gtr
128 I added security features.
129 
130 Revision 1.7  2004/09/17 01:21:20  nw
131 altered to work with new threadpool
132 
133 Revision 1.6.40.1  2004/09/14 13:46:04  nw
134 upgraded to new threading practice.
135 
136 Revision 1.6  2004/08/11 16:53:32  nw
137 added @todo for bug
138 
139 Revision 1.5  2004/07/26 12:07:38  nw
140 renamed indirect package to protocol,
141 renamed classes and methods within protocol package
142 javadocs
143 
144 Revision 1.4  2004/07/26 10:21:47  nw
145 javadoc
146 
147 Revision 1.3  2004/07/22 16:32:54  nw
148 cleaned up application / parameter adapter interface.
149 
150 Revision 1.2  2004/07/01 11:16:22  nw
151 merged in branch
152 nww-itn06-componentization
153 
154 Revision 1.1.2.3  2004/07/01 01:42:46  nw
155 final version, before merge
156 
157 Revision 1.1.2.2  2004/06/17 09:21:23  nw
158 finished all major functionality additions to core
159 
160 Revision 1.1.2.1  2004/06/14 08:56:58  nw
161 factored applications into sub-projects,
162 got packaging of wars to work again
163  
164 */