1
2
3
4
5
6
7
8
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;
64
65 Class coreType = (input.isArray() ? input.getComponentType() : input);
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
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
88 e.printStackTrace();
89 assert false;
90 }
91 }
92 Class output = method.getReturnType();
93 if (!output.equals(Void.TYPE)) {
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
103 e.printStackTrace();
104 assert false;
105 }
106
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
117 ApplicationInterface interf = this.getInterfaces()[0];
118 return new JavaClassApplication(ids,tool,interf,env.getProtocolLib());
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148