KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > TaskAdapter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant;
20
21 import java.lang.reflect.Method JavaDoc;
22 import org.apache.tools.ant.dispatch.Dispatchable;
23 import org.apache.tools.ant.dispatch.DispatchUtils;
24
25 /**
26  * Uses introspection to "adapt" an arbitrary Bean which doesn't
27  * itself extend Task, but still contains an execute method and optionally
28  * a setProject method.
29  *
30  */

31 public class TaskAdapter extends Task implements TypeAdapter {
32
33     /** Object to act as a proxy for. */
34     private Object JavaDoc proxy;
35
36     /**
37      * Checks whether or not a class is suitable to be adapted by TaskAdapter.
38      * If the class is of type Dispatchable, the check is not performed because
39      * the method that will be executed will be determined only at runtime of
40      * the actual task and not during parse time.
41      *
42      * This only checks conditions which are additionally required for
43      * tasks adapted by TaskAdapter. Thus, this method should be called by
44      * Project.checkTaskClass.
45      *
46      * Throws a BuildException and logs as Project.MSG_ERR for
47      * conditions that will cause the task execution to fail.
48      * Logs other suspicious conditions with Project.MSG_WARN.
49      *
50      * @param taskClass Class to test for suitability.
51      * Must not be <code>null</code>.
52      * @param project Project to log warnings/errors to.
53      * Must not be <code>null</code>.
54      *
55      * @see Project#checkTaskClass(Class)
56      */

57     public static void checkTaskClass(final Class JavaDoc taskClass,
58                                       final Project project) {
59         if (!Dispatchable.class.isAssignableFrom(taskClass)) {
60             // don't have to check for interface, since then
61
// taskClass would be abstract too.
62
try {
63                 final Method JavaDoc executeM = taskClass.getMethod("execute", (Class JavaDoc[]) null);
64                 // don't have to check for public, since
65
// getMethod finds public method only.
66
// don't have to check for abstract, since then
67
// taskClass would be abstract too.
68
if (!Void.TYPE.equals(executeM.getReturnType())) {
69                     final String JavaDoc message = "return type of execute() should be "
70                         + "void but was \"" + executeM.getReturnType() + "\" in "
71                         + taskClass;
72                     project.log(message, Project.MSG_WARN);
73                 }
74             } catch (NoSuchMethodException JavaDoc e) {
75                 final String JavaDoc message = "No public execute() in " + taskClass;
76                 project.log(message, Project.MSG_ERR);
77                 throw new BuildException(message);
78             } catch (LinkageError JavaDoc e) {
79                 String JavaDoc message = "Could not load " + taskClass + ": " + e;
80                 project.log(message, Project.MSG_ERR);
81                 throw new BuildException(message, e);
82             }
83         }
84     }
85
86     /**
87      * Check if the proxy class is a valid class to use
88      * with this adapter.
89      * The class must have a public no-arg "execute()" method.
90      * @param proxyClass the class to check.
91      */

92     public void checkProxyClass(Class JavaDoc proxyClass) {
93         checkTaskClass(proxyClass, getProject());
94     }
95
96     /**
97      * Executes the proxied task.
98      *
99      * @exception BuildException if the project could not be set
100      * or the method could not be executed.
101      */

102     public void execute() throws BuildException {
103         try {
104             Method JavaDoc setLocationM = proxy.getClass().getMethod(
105                 "setLocation", new Class JavaDoc[] {Location.class});
106             if (setLocationM != null) {
107                 setLocationM.invoke(proxy, new Object JavaDoc[] {getLocation()});
108             }
109         } catch (NoSuchMethodException JavaDoc e) {
110             // ignore this if the class being used as a task does not have
111
// a set location method.
112
} catch (Exception JavaDoc ex) {
113             log("Error setting location in " + proxy.getClass(),
114                 Project.MSG_ERR);
115             throw new BuildException(ex);
116         }
117
118         try {
119             Method JavaDoc setProjectM = proxy.getClass().getMethod(
120                 "setProject", new Class JavaDoc[] {Project.class});
121             if (setProjectM != null) {
122                 setProjectM.invoke(proxy, new Object JavaDoc[] {getProject()});
123             }
124         } catch (NoSuchMethodException JavaDoc e) {
125             // ignore this if the class being used as a task does not have
126
// a set project method.
127
} catch (Exception JavaDoc ex) {
128             log("Error setting project in " + proxy.getClass(),
129                 Project.MSG_ERR);
130             throw new BuildException(ex);
131         }
132
133         try {
134             DispatchUtils.execute(proxy);
135         } catch (BuildException be) {
136             throw be;
137         } catch (Exception JavaDoc ex) {
138             log("Error in " + proxy.getClass(), Project.MSG_VERBOSE);
139             throw new BuildException(ex);
140         }
141     }
142
143     /**
144      * Sets the target object to proxy for.
145      *
146      * @param o The target object. Must not be <code>null</code>.
147      */

148     public void setProxy(Object JavaDoc o) {
149         this.proxy = o;
150     }
151
152     /**
153      * Returns the target object being proxied.
154      *
155      * @return the target proxy object.
156      */

157     public Object JavaDoc getProxy() {
158         return proxy;
159     }
160
161 }
162
Popular Tags