001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.dispatch;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.UnknownElement;
022: import org.apache.tools.ant.Task;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026:
027: /**
028: * Determines and Executes the action method for the task.
029: */
030: public class DispatchUtils {
031: /**
032: * Determines and Executes the action method for the task.
033: * @param task the task to execute.
034: * @throws BuildException on error.
035: */
036: public static final void execute(Object task) throws BuildException {
037: String methodName = "execute";
038: Dispatchable dispatchable = null;
039: try {
040: if (task instanceof Dispatchable) {
041: dispatchable = (Dispatchable) task;
042: } else if (task instanceof UnknownElement) {
043: UnknownElement ue = (UnknownElement) task;
044: Object realThing = ue.getRealThing();
045: if (realThing != null
046: && realThing instanceof Dispatchable
047: && realThing instanceof Task) {
048: dispatchable = (Dispatchable) realThing;
049: }
050: }
051: if (dispatchable != null) {
052: String mName = null;
053: try {
054: final String name = dispatchable
055: .getActionParameterName();
056: if (name != null && name.trim().length() > 0) {
057: mName = "get"
058: + name.trim().substring(0, 1)
059: .toUpperCase();
060: if (name.length() > 1) {
061: mName += name.substring(1);
062: }
063: final Class c = dispatchable.getClass();
064: final Method actionM = c.getMethod(mName,
065: new Class[0]);
066: if (actionM != null) {
067: final Object o = actionM.invoke(
068: dispatchable, (Object[]) null);
069: if (o != null) {
070: final String s = o.toString();
071: if (s != null && s.trim().length() > 0) {
072: methodName = s.trim();
073: Method executeM = null;
074: executeM = dispatchable.getClass()
075: .getMethod(methodName,
076: new Class[0]);
077: if (executeM == null) {
078: throw new BuildException(
079: "No public "
080: + methodName
081: + "() in "
082: + dispatchable
083: .getClass());
084: }
085: executeM.invoke(dispatchable,
086: (Object[]) null);
087: if (task instanceof UnknownElement) {
088: ((UnknownElement) task)
089: .setRealThing(null);
090: }
091: } else {
092: throw new BuildException(
093: "Dispatchable Task attribute '"
094: + name.trim()
095: + "' not set or value is empty.");
096: }
097: } else {
098: throw new BuildException(
099: "Dispatchable Task attribute '"
100: + name.trim()
101: + "' not set or value is empty.");
102: }
103: }
104: } else {
105: throw new BuildException(
106: "Action Parameter Name must not be empty for Dispatchable Task.");
107: }
108: } catch (NoSuchMethodException nsme) {
109: throw new BuildException("No public " + mName
110: + "() in " + task.getClass());
111: }
112: } else {
113: Method executeM = null;
114: executeM = task.getClass().getMethod(methodName,
115: new Class[0]);
116: if (executeM == null) {
117: throw new BuildException("No public " + methodName
118: + "() in " + task.getClass());
119: }
120: executeM.invoke(task, (Object[]) null);
121: if (task instanceof UnknownElement) {
122: ((UnknownElement) task).setRealThing(null);
123: }
124: }
125: } catch (InvocationTargetException ie) {
126: Throwable t = ie.getTargetException();
127: if (t instanceof BuildException) {
128: throw ((BuildException) t);
129: } else {
130: throw new BuildException(t);
131: }
132: } catch (NoSuchMethodException e) {
133: throw new BuildException(e);
134: } catch (IllegalAccessException e) {
135: throw new BuildException(e);
136: }
137: }
138: }
|