KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > dispatch > DispatchUtils


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 package org.apache.tools.ant.dispatch;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.UnknownElement;
22 import org.apache.tools.ant.Task;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 /**
28  * Determines and Executes the action method for the task.
29  */

30 public class DispatchUtils {
31     /**
32      * Determines and Executes the action method for the task.
33      * @param task the task to execute.
34      * @throws BuildException on error.
35      */

36     public static final void execute(Object JavaDoc task) throws BuildException {
37         String JavaDoc methodName = "execute";
38         Dispatchable dispatchable = null;
39         try {
40             if (task instanceof Dispatchable) {
41                 dispatchable = (Dispatchable) task;
42             } else if (task instanceof UnknownElement) {
43                 UnknownElement ue = (UnknownElement) task;
44                 Object JavaDoc realThing = ue.getRealThing();
45                 if (realThing != null
46                     && realThing instanceof Dispatchable
47                     && realThing instanceof Task) {
48                     dispatchable = (Dispatchable) realThing;
49                 }
50             }
51             if (dispatchable != null) {
52                 String JavaDoc mName = null;
53                 try {
54                     final String JavaDoc name = dispatchable.getActionParameterName();
55                     if (name != null && name.trim().length() > 0) {
56                         mName = "get" + name.trim().substring(0, 1).toUpperCase();
57                         if (name.length() > 1) {
58                             mName += name.substring(1);
59                         }
60                         final Class JavaDoc c = dispatchable.getClass();
61                         final Method JavaDoc actionM = c.getMethod(mName, new Class JavaDoc[0]);
62                         if (actionM != null) {
63                             final Object JavaDoc o = actionM.invoke(dispatchable, (Object JavaDoc[]) null);
64                             if (o != null) {
65                                 final String JavaDoc s = o.toString();
66                                 if (s != null && s.trim().length() > 0) {
67                                     methodName = s.trim();
68                                     Method JavaDoc executeM = null;
69                                     executeM = dispatchable.getClass().getMethod(
70                                         methodName, new Class JavaDoc[0]);
71                                     if (executeM == null) {
72                                         throw new BuildException(
73                                             "No public " + methodName + "() in "
74                                             + dispatchable.getClass());
75                                     }
76                                     executeM.invoke(dispatchable, (Object JavaDoc[]) null);
77                                     if (task instanceof UnknownElement) {
78                                         ((UnknownElement) task).setRealThing(null);
79                                     }
80                                 } else {
81                                     throw new BuildException(
82                                         "Dispatchable Task attribute '" + name.trim()
83                                         + "' not set or value is empty.");
84                                 }
85                             } else {
86                                     throw new BuildException(
87                                         "Dispatchable Task attribute '" + name.trim()
88                                         + "' not set or value is empty.");
89                             }
90                         }
91                     } else {
92                         throw new BuildException(
93                             "Action Parameter Name must not be empty for Dispatchable Task.");
94                     }
95                 } catch (NoSuchMethodException JavaDoc nsme) {
96                     throw new BuildException("No public " + mName + "() in " + task.getClass());
97                 }
98             } else {
99                 Method JavaDoc executeM = null;
100                 executeM = task.getClass().getMethod(methodName, new Class JavaDoc[0]);
101                 if (executeM == null) {
102                     throw new BuildException("No public " + methodName + "() in "
103                         + task.getClass());
104                 }
105                 executeM.invoke(task, (Object JavaDoc[]) null);
106                 if (task instanceof UnknownElement) {
107                     ((UnknownElement) task).setRealThing(null);
108                 }
109             }
110         } catch (InvocationTargetException JavaDoc ie) {
111             Throwable JavaDoc t = ie.getTargetException();
112             if (t instanceof BuildException) {
113                 throw ((BuildException) t);
114             } else {
115                 throw new BuildException(t);
116             }
117         } catch (NoSuchMethodException JavaDoc e) {
118             throw new BuildException(e);
119         } catch (IllegalAccessException JavaDoc e) {
120             throw new BuildException(e);
121         }
122     }
123 }
124
Popular Tags