1 28 29 package com.idaremedia.antx.starters; 30 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 39 import com.idaremedia.antx.AntXFixture; 40 import com.idaremedia.antx.AssertableLibDefinition; 41 import com.idaremedia.antx.Iteration; 42 import com.idaremedia.antx.apis.Requester; 43 import com.idaremedia.antx.helpers.Tk; 44 import com.idaremedia.antx.ownhelpers.LocalTk; 45 import com.idaremedia.antx.parameters.RecoveryEnabled; 46 47 85 86 public abstract class ManagerTask extends AssertableLibDefinition 87 implements RecoveryEnabled 88 { 89 93 protected ManagerTask(String iam) 94 { 95 super(iam); 96 } 97 98 99 100 107 private static class ActionEntry 108 { 109 Method method; 110 String name; 111 112 ActionEntry(String name, Method method) { 113 this.name = name; 114 this.method = method; 115 } 116 boolean matches(String othername) { 117 return this.name.equals(othername); 118 } 119 void call(ManagerTask target, Map params) { 120 try { 121 method.invoke(target, new Object []{params}); 122 } catch(Exception anyX) { 123 if (anyX instanceof BuildException) { 124 throw ((BuildException)anyX); 125 } 126 throw new BuildException(anyX,target.getLocation()); 127 } 128 } 129 public String toString() { 130 return this.name; 131 } 132 } 133 134 135 136 142 private void newActionMethod(String name, Method method) 143 { 144 if (m_actors==null) { 145 m_actors = AntXFixture.newList(); 146 } 147 m_actors.add(new ActionEntry(name,method)); 148 } 149 150 151 152 158 private ActionEntry findActionMethod(String name) 159 { 160 if (m_actors!=null) { 161 name = Tk.lowercaseFrom(name); 162 for (int i=0,N=m_actors.size();i<N;i++) { 163 ActionEntry e = (ActionEntry)m_actors.get(i); 164 if (e.matches(name)) { 165 return e; 166 } 167 } 168 } 169 return null; 170 } 171 172 173 174 181 private void buildActionMethods() 182 { 183 if (m_actors==null) { 184 verify_(Modifier.isPublic(getClass().getModifiers()), "init- public class"); 185 Method [] methods = getClass().getMethods(); 186 String loc = LocalTk.purtyLocation(getLocation()); 187 final boolean logit = getProject()!=null; 188 for (int i=0;i<methods.length;i++) { 189 Method m = methods[i]; 190 String name = m.getName(); 191 Class returnType = m.getReturnType(); 192 Class [] args = m.getParameterTypes(); 193 194 if (name.startsWith("do") && !name.equals("do") && 195 java.lang.Void.TYPE.equals(returnType) && 196 args.length == 1 && 197 Map .class.equals(args[0])) { 198 199 String actionname = Tk.lowercaseFrom(name.substring(2)); 200 newActionMethod(actionname, m); 201 if (logit) { 202 log("Recording action '"+actionname+"' @ "+loc, Project.MSG_DEBUG); 203 } 204 } 205 } 206 } 207 } 208 209 210 211 216 public int actionMethodCount() 217 { 218 return m_actors==null ? 0 : m_actors.size(); 219 } 220 221 222 223 226 protected void initonce() 227 { 228 super.initonce(); 229 buildActionMethods(); 230 } 231 232 233 234 244 protected void doAction(String action, Map params, Requester clnt) 245 { 246 require_(action!=null,"do- nonzro action"); 247 ActionEntry e = findActionMethod(action); 248 if (e!=null) { 249 e.call(this,params); 250 } 251 else { 252 if (clnt==null) { 253 clnt = new Requester.ForComponent(this); 254 } 255 String error = Iteration.uistrs().get("task.manager.err.unknown.operation",action); 256 if (isHaltIfError()) { 257 clnt.problem(error, Project.MSG_ERR); 258 throw new BuildException(error, clnt.getLocation()); 259 } 260 clnt.problem(error, Project.MSG_WARN); 261 } 262 } 263 264 265 266 272 protected void verifyCanExecute_(String calr) 273 { 274 super.verifyCanExecute_(calr); 275 buildActionMethods(); 276 } 277 278 279 280 289 public final String validActionNames(String separator) 290 { 291 if (m_actors!=null) { 292 StringBuffer sb = AntXFixture.newStringBuffer(); 293 if (separator==null) { 294 separator = "|"; 295 } 296 for (int i=0,N=m_actors.size();i<N;i++) { 297 ActionEntry e = (ActionEntry)m_actors.get(i); 298 if (i>0) { 299 sb.append(separator); 300 } 301 sb.append(e.name); 302 } 303 return sb.substring(0); 304 } 305 return ""; 306 } 307 308 309 310 316 public void setHaltIfError(boolean halt) 317 { 318 m_willHalt = halt; 319 } 320 321 322 323 329 public boolean isHaltIfError() 330 { 331 return m_willHalt; 332 } 333 334 335 336 private List m_actors; 337 private boolean m_willHalt=true;} 339 340 | Popular Tags |