Home » apache-ant-1.7.1-src » org.apache.tools » ant » dispatch » [javadoc | source]
    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;
   25   import java.lang.reflect.Method;
   26   
   27   /**
   28    * Determines and Executes the action method for the task.
   29    */
   30   // CheckStyle:HideUtilityClassConstructorCheck OFF - (bc)
   31   public class DispatchUtils {
   32       /**
   33        * Determines and Executes the action method for the task.
   34        * @param task the task to execute.
   35        * @throws BuildException on error.
   36        */
   37       public static final void execute(Object task) throws BuildException {
   38           String methodName = "execute";
   39           Dispatchable dispatchable = null;
   40           try {
   41               if (task instanceof Dispatchable) {
   42                   dispatchable = (Dispatchable) task;
   43               } else if (task instanceof UnknownElement) {
   44                   UnknownElement ue = (UnknownElement) task;
   45                   Object realThing = ue.getRealThing();
   46                   if (realThing != null
   47                       && realThing instanceof Dispatchable
   48                       && realThing instanceof Task) {
   49                       dispatchable = (Dispatchable) realThing;
   50                   }
   51               }
   52               if (dispatchable != null) {
   53                   String mName = null;
   54                   try {
   55                       final String name = dispatchable.getActionParameterName();
   56                       if (name != null && name.trim().length() > 0) {
   57                           mName = "get" + name.trim().substring(0, 1).toUpperCase();
   58                           if (name.length() > 1) {
   59                               mName += name.substring(1);
   60                           }
   61                           final Class c = dispatchable.getClass();
   62                           final Method actionM = c.getMethod(mName, new Class[0]);
   63                           if (actionM != null) {
   64                               final Object o = actionM.invoke(dispatchable, (Object[]) null);
   65                               if (o != null) {
   66                                   final String s = o.toString();
   67                                   if (s != null && s.trim().length() > 0) {
   68                                       methodName = s.trim();
   69                                       Method executeM = null;
   70                                       executeM = dispatchable.getClass().getMethod(
   71                                           methodName, new Class[0]);
   72                                       if (executeM == null) {
   73                                           throw new BuildException(
   74                                               "No public " + methodName + "() in "
   75                                               + dispatchable.getClass());
   76                                       }
   77                                       executeM.invoke(dispatchable, (Object[]) null);
   78                                       if (task instanceof UnknownElement) {
   79                                           ((UnknownElement) task).setRealThing(null);
   80                                       }
   81                                   } else {
   82                                       throw new BuildException(
   83                                           "Dispatchable Task attribute '" + name.trim()
   84                                           + "' not set or value is empty.");
   85                                   }
   86                               } else {
   87                                       throw new BuildException(
   88                                           "Dispatchable Task attribute '" + name.trim()
   89                                           + "' not set or value is empty.");
   90                               }
   91                           }
   92                       } else {
   93                           throw new BuildException(
   94                               "Action Parameter Name must not be empty for Dispatchable Task.");
   95                       }
   96                   } catch (NoSuchMethodException nsme) {
   97                       throw new BuildException("No public " + mName + "() in " + task.getClass());
   98                   }
   99               } else {
  100                   Method executeM = null;
  101                   executeM = task.getClass().getMethod(methodName, new Class[0]);
  102                   if (executeM == null) {
  103                       throw new BuildException("No public " + methodName + "() in "
  104                           + task.getClass());
  105                   }
  106                   executeM.invoke(task, (Object[]) null);
  107                   if (task instanceof UnknownElement) {
  108                       ((UnknownElement) task).setRealThing(null);
  109                   }
  110               }
  111           } catch (InvocationTargetException ie) {
  112               Throwable t = ie.getTargetException();
  113               if (t instanceof BuildException) {
  114                   throw ((BuildException) t);
  115               } else {
  116                   throw new BuildException(t);
  117               }
  118           } catch (NoSuchMethodException e) {
  119               throw new BuildException(e);
  120           } catch (IllegalAccessException e) {
  121               throw new BuildException(e);
  122           }
  123       }
  124   }

Save This Page
Home » apache-ant-1.7.1-src » org.apache.tools » ant » dispatch » [javadoc | source]