1 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 ; 25 import java.lang.reflect.Method ; 26 27 30 public class DispatchUtils { 31 36 public static final void execute(Object task) throws BuildException { 37 String 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 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 mName = null; 53 try { 54 final String 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 c = dispatchable.getClass(); 61 final Method actionM = c.getMethod(mName, new Class [0]); 62 if (actionM != null) { 63 final Object o = actionM.invoke(dispatchable, (Object []) null); 64 if (o != null) { 65 final String s = o.toString(); 66 if (s != null && s.trim().length() > 0) { 67 methodName = s.trim(); 68 Method executeM = null; 69 executeM = dispatchable.getClass().getMethod( 70 methodName, new Class [0]); 71 if (executeM == null) { 72 throw new BuildException( 73 "No public " + methodName + "() in " 74 + dispatchable.getClass()); 75 } 76 executeM.invoke(dispatchable, (Object []) 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 nsme) { 96 throw new BuildException("No public " + mName + "() in " + task.getClass()); 97 } 98 } else { 99 Method executeM = null; 100 executeM = task.getClass().getMethod(methodName, new Class [0]); 101 if (executeM == null) { 102 throw new BuildException("No public " + methodName + "() in " 103 + task.getClass()); 104 } 105 executeM.invoke(task, (Object []) null); 106 if (task instanceof UnknownElement) { 107 ((UnknownElement) task).setRealThing(null); 108 } 109 } 110 } catch (InvocationTargetException ie) { 111 Throwable t = ie.getTargetException(); 112 if (t instanceof BuildException) { 113 throw ((BuildException) t); 114 } else { 115 throw new BuildException(t); 116 } 117 } catch (NoSuchMethodException e) { 118 throw new BuildException(e); 119 } catch (IllegalAccessException e) { 120 throw new BuildException(e); 121 } 122 } 123 } 124 | Popular Tags |