View Javadoc

1   /*$Id: JavaClassApplicationDescription.java,v 1.4 2004/07/30 14:54:47 jdt 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.Application;
14  import org.astrogrid.applications.DefaultIDs;
15  import org.astrogrid.applications.beans.v1.parameters.types.ParameterTypes;
16  import org.astrogrid.applications.description.ApplicationInterface;
17  import org.astrogrid.applications.description.base.AbstractApplicationDescription;
18  import org.astrogrid.applications.description.base.ApplicationDescriptionEnvironment;
19  import org.astrogrid.applications.description.base.BaseApplicationInterface;
20  import org.astrogrid.applications.description.base.BaseParameterDescription;
21  import org.astrogrid.applications.description.exception.ParameterDescriptionNotFoundException;
22  import org.astrogrid.community.User;
23  import org.astrogrid.workflow.beans.v1.Tool;
24  
25  import java.lang.reflect.Method;
26  
27  /*** A description for an application that is implemented as a static java method.
28   * <p>
29   * For a method foo(), this class will create an {@link org.astrogrid.applications.description.ApplicationDescription} for <tt><i>authorityName</i>/foo</tt>,
30   * where the authority name is specified in the constructor.
31   * <p>
32   * Constructs all the metadata for the application via reflection on the static method.
33   * @todo add support for attributes, or something, so that other metadata can be specified (e.g. documentation, UCD).
34   * @todo improve definition of types
35   * @author Noel Winstanley nw@jb.man.ac.uk 08-Jun-2004
36   *
37   */
38  public class JavaClassApplicationDescription extends AbstractApplicationDescription {
39      /*** Construct a new JavaClassApplicationDescription
40       * @param method the method that is to be the implementation of this application
41       * @param authorityName the name of the authority under which to add this application
42       * @param env standard container for helper objects.
43       * 
44       */
45      public JavaClassApplicationDescription(Method method,String authorityName,ApplicationDescriptionEnvironment env) {
46          super(env);
47          this.method = method;
48          createMetadata(authorityName);
49      }
50      protected final Method method;
51      
52      /*** populates this object with parameterDescriptions by reflecting on application method
53       * @todo improve handling to parameter types
54       *  */
55      protected final void createMetadata(String communityName){
56          setName(communityName + "/" + method.getName());
57          Class[] inputs = method.getParameterTypes();
58          BaseApplicationInterface singleInterface =new BaseApplicationInterface(method.getName(),this);        
59          for (int i = 0; i < inputs.length; i++) {
60              Class input = inputs[i];
61              JavaClassParameterDescription param = new JavaClassParameterDescription();
62              param.setName("parameter-" + i);
63              ParameterTypes targetType = ParameterTypes.TEXT; // suitable default.
64              // special cases.
65              Class coreType = (input.isArray() ? input.getComponentType() : input); // get the base type, if this is an array.
66              if (coreType.equals(Boolean.class) || coreType.equals(Boolean.TYPE)) {
67                  targetType = ParameterTypes.BOOLEAN;
68              } else if (coreType.equals(Byte.TYPE) || coreType.equals(Byte.class)) {
69                  targetType = ParameterTypes.BINARY;
70              } else if (coreType.equals(Integer.TYPE) || coreType.equals(Integer.class)) {
71                  targetType = ParameterTypes.INTEGER;
72              } else if (coreType.equals(Float.TYPE) || coreType.equals(Float.class)) {
73                  targetType = ParameterTypes.REAL;
74              } else if (coreType.equals(Double.TYPE) || coreType.equals(Double.class)) {
75                  targetType = ParameterTypes.DOUBLE;
76              }
77              // URI, Document, Node?
78              
79              param.setType(targetType); 
80              param.setSubType(input.getName());
81              param.setTargetClass(input);
82              this.addParameterDescription(param);
83              try {
84                  singleInterface.addInputParameter(param.getName());
85              }
86              catch (ParameterDescriptionNotFoundException e) {
87                  // impossible.
88                  e.printStackTrace();
89                  assert false; //are you sure?
90              }
91          }
92          Class output = method.getReturnType();
93          if (!output.equals(Void.TYPE)) { // only add a parameter description if the method doesn't return void.            
94              BaseParameterDescription result = new BaseParameterDescription();
95              result.setName("result");
96              result.setType(ParameterTypes.TEXT);
97              result.setSubType(output.getName());            
98              this.addParameterDescription(result);
99              try {
100                 singleInterface.addOutputParameter(result.getName());
101             } catch (ParameterDescriptionNotFoundException e) {
102                 // can't happen.
103                 e.printStackTrace();
104                 assert false; //never say never
105             }        
106             // add parameters to interface.
107             this.addInterface(singleInterface);
108         }
109     }
110     /***
111      * @see org.astrogrid.applications.description.ApplicationDescription#initializeApplication(java.lang.String, org.astrogrid.community.User, org.astrogrid.workflow.beans.v1.Tool)
112      */
113     public Application initializeApplication(String jobStepID, User user, Tool tool) throws Exception {
114         String newID = env.getIdGen().getNewID();
115         final DefaultIDs ids = new DefaultIDs(jobStepID,newID,user);
116         // we know there's only one interface supported for each application
117         ApplicationInterface interf = this.getInterfaces()[0];
118         return new JavaClassApplication(ids,tool,interf,env.getProtocolLib());
119     }
120 }
121 
122 
123 /* 
124 $Log: JavaClassApplicationDescription.java,v $
125 Revision 1.4  2004/07/30 14:54:47  jdt
126 merges in from case3 branch
127 
128 Revision 1.3.4.1  2004/07/29 09:11:54  jdt
129 can't be too careful
130 
131 Revision 1.3  2004/07/26 10:21:47  nw
132 javadoc
133 
134 Revision 1.2  2004/07/01 11:16:22  nw
135 merged in branch
136 nww-itn06-componentization
137 
138 Revision 1.1.2.3  2004/07/01 01:42:46  nw
139 final version, before merge
140 
141 Revision 1.1.2.2  2004/06/17 09:21:23  nw
142 finished all major functionality additions to core
143 
144 Revision 1.1.2.1  2004/06/14 08:56:58  nw
145 factored applications into sub-projects,
146 got packaging of wars to work again
147  
148 */