1 28 29 package com.idaremedia.antx.starters; 30 31 import java.util.Collections ; 32 import java.util.Iterator ; 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.Iteration; 41 import com.idaremedia.antx.apis.Requester; 42 import com.idaremedia.antx.helpers.InnerNameValuePair; 43 import com.idaremedia.antx.helpers.Tk; 44 import com.idaremedia.antx.parameters.FeedbackLevel; 45 46 76 77 public abstract class SimpleManagerTask extends ManagerTask 78 { 79 83 protected SimpleManagerTask(String iam) 84 { 85 super(iam); 86 } 87 88 89 90 97 protected SimpleManagerTask(String iam, String defaultAction) 98 { 99 super(iam); 100 m_actionName = defaultAction; 101 } 102 103 104 105 111 public void setAction(String action) 112 { 113 require_(action!=null,"setAction- nonzro action"); 114 int i = action.indexOf("?"); 115 if (i>0) { 116 String choice = scrub(action.substring(0,i++)); 117 m_actionName = Tk.lowercaseFrom(choice); 118 if (i<action.length()) { 119 m_uriParams = action.substring(i); 120 } 121 } else { 122 m_actionName = Tk.lowercaseFrom(scrub(action)); 123 } 124 } 125 126 127 128 132 protected final String getActionName() 133 { 134 return m_actionName; 135 } 136 137 138 139 146 protected final String getUriParams0() 147 { 148 return m_uriParams; 149 } 150 151 152 153 160 public void addConfiguredParameter(InnerNameValuePair param) 161 { 162 require_(param!=null,"addParam- nonzro param"); 163 param.verifyNamed(); 164 165 if (m_nestedParams==null) { 166 m_nestedParams = AntXFixture.newMap(); 167 } 168 m_nestedParams.put(param.getName(),param.getValue()); 169 } 170 171 172 173 179 protected final void addCheckLaterParameter(InnerNameValuePair param) 180 { 181 require_(param!=null,"addParamPlaceholder- nonzro param"); 182 if (m_nestedUnverifiedParams==null){ 183 m_nestedUnverifiedParams = AntXFixture.newList(); 184 } 185 m_nestedUnverifiedParams.add(param); 186 } 187 188 189 190 194 public void setFeedback(String level) 195 { 196 m_fbLevel = FeedbackLevel.from(level); 197 } 198 199 200 201 206 public final FeedbackLevel getFeedbackLevel() 207 { 208 return m_fbLevel; 209 } 210 211 212 213 219 public void execute() 220 { 221 verifyCanExecute_("exec"); 222 223 if (m_actionName!=null) { 224 Map args = Collections.EMPTY_MAP; 225 if (m_nestedParams!=null) { 226 args = Collections.unmodifiableMap(m_nestedParams); 227 } 228 doAction(m_actionName,args,m_rqlink); 229 } 230 } 231 232 233 234 238 protected void verifyCanExecute_(String calr) 239 { 240 super.verifyCanExecute_(calr); 241 242 if (m_actionName==null) { 243 String message = Iteration.uistrs().get("task.needs.this.attr", 244 getTaskName(), "action"); 245 if (isHaltIfError()) { 246 log(message, Project.MSG_ERR); 247 throw new BuildException(message,getLocation()); 248 } 249 log(message, Project.MSG_WARN); 251 } 252 253 if (m_nestedUnverifiedParams!=null) { 254 Iterator itr = m_nestedUnverifiedParams.iterator(); 255 while (itr.hasNext()) { 256 addConfiguredParameter((InnerNameValuePair)itr.next()); 257 } 258 m_nestedUnverifiedParams = null; } 260 } 261 262 263 264 269 protected final void verifyNotInAntlib() 270 { 271 if (getAntlibClassLoader()!=null) { if (isInvalidAntlibAction()) { 274 String ermsg = uistrs().get 275 ("task.manager.err.illegal.operation",getActionName()); 276 log(ermsg,Project.MSG_ERR); 277 throw new BuildException(ermsg,getLocation()); 278 } 279 } 280 } 281 282 283 284 289 protected boolean isInvalidAntlibAction() 290 { 291 return false; 292 } 293 294 295 296 303 private String scrub(String s) 304 { 305 StringBuffer sb = new StringBuffer (s.length()); 306 int N= s.length(); 307 int i= 0; 308 while (N!=0) { 309 char c = s.charAt(i++); 310 if (i==1) { 311 if (Character.isJavaIdentifierStart(c)) { 312 sb.append(c); 313 } 314 } else if (Character.isJavaIdentifierPart(c)) { 315 sb.append(c); 316 } 317 N--; 318 } 319 return sb!=null ? sb.substring(0) : s; 320 } 321 322 323 private String m_actionName; 324 protected Requester m_rqlink = new Requester.ForComponent(this); 325 private String m_uriParams; 326 private FeedbackLevel m_fbLevel = FeedbackLevel.NORMAL; 327 328 329 protected Map m_nestedParams; 330 private List m_nestedUnverifiedParams; 331 } 332 333 334 | Popular Tags |