1 28 29 package com.idaremedia.antx.solo; 30 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.PropertyHelper; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.ExportedProperties; 40 import com.idaremedia.antx.FixtureExaminer; 41 import com.idaremedia.antx.FixtureIds; 42 import com.idaremedia.antx.FixtureInitializer; 43 import com.idaremedia.antx.Iteration; 44 import com.idaremedia.antx.NoiseLevel; 45 import com.idaremedia.antx.ValueURIHandler; 46 import com.idaremedia.antx.apis.BuildError; 47 import com.idaremedia.antx.parameters.FeedbackLevel; 48 import com.idaremedia.antx.starters.SimpleManagerTask; 49 50 93 94 public class ValueURIManagerTask extends SimpleManagerTask 95 implements FixtureInitializer 96 { 97 private static final String REFID= FixtureIds.VALUEURI_INTERPRETER; 98 private static final String LABEL = "ValueUriInterpreter"; 99 100 101 106 public ValueURIManagerTask() 107 { 108 super(AntX.fixture+"ValueURIManagerTask:","enable"); 109 } 110 111 112 113 119 private void verifyNoneInChain(PropertyHelper ph) 120 { 121 do { 122 if (ph.getNext() instanceof Interpreter) { 123 String error = Iteration.uistrs().get 124 ("fixture.supPh.stack.corrupted", LABEL); 125 log(error, Project.MSG_ERR); 126 throw new BuildError(error); 127 } 128 ph = ph.getNext(); 129 } while(ph!=null); 130 } 131 132 133 134 142 private Class loadHandlerClass(Map.Entry e) 143 { 144 String classname = (String )e.getValue(); 145 try { 146 Class c = Class.forName(classname); 147 if (!ValueURIHandler.class.isAssignableFrom(c)) { 148 throw new ClassCastException (); 149 } 150 return c; 151 } catch (Exception anyX) { 152 String error = Iteration.uistrs().get("task.bad.custimpl.class1", 153 classname, ValueURIHandler.class.getName()); 154 log(error, Project.MSG_ERR); 155 throw new BuildException(error, anyX, getLocation()); 156 } 157 } 158 159 160 161 168 public void doInstall(Map args) 169 { 170 if (!args.isEmpty()) { 171 Iterator itr= args.entrySet().iterator(); 172 Class oldc, newc; 173 boolean warnreplace = isVerbose(); 174 175 while (itr.hasNext()) { 176 Map.Entry e = (Map.Entry )itr.next(); 177 String prefix = (String )e.getKey(); 178 179 if (!FixtureExaminer.isBuiltinValueURIScheme(prefix)) { 180 newc = loadHandlerClass(e); 181 oldc = Iteration.replaceValueURIHandler(prefix,newc); 182 if (warnreplace && oldc!=null && oldc!=newc) { 183 String warning = Iteration.uistrs().get("valueuri.repl.handlr", 184 prefix, oldc.getName(), newc.getName()); 185 log(warning, Project.MSG_WARN); 186 } else { 187 log("For ("+prefix+") installed value URI handler "+newc.getName(), 188 Project.MSG_VERBOSE); 189 } 190 } else { 191 String warning = Iteration.uistrs().get("valueuri.resrved.handlr",prefix); 192 log(warning, Project.MSG_WARN); 193 } 194 } 195 } 196 } 197 198 199 200 207 public void doEnable(Map args) 208 { 209 Project p = getProject(); 210 Map ht = p.getReferences(); 211 boolean warn = false; 212 213 synchronized(ht) { Object o = ht.get(REFID); 215 if (o==null) { 216 Interpreter interpreter; 217 PropertyHelper ph = PropertyHelper.getPropertyHelper(p); 218 synchronized(ph) { 219 verifyNoneInChain(ph); 220 interpreter = new Interpreter(p); 221 interpreter.setNext(ph.getNext()); 222 ph.setNext(interpreter); 223 } 224 ht.put(REFID,interpreter); 225 } else { 226 warn = true; 227 } 228 } 229 if (!args.isEmpty()) { 230 this.doInstall(args); 231 warn = false; 232 } 233 if (warn && isVerbose()) { 234 String warning = Iteration.uistrs().get("fixture.supPh.install.once",LABEL); 235 log(warning, Project.MSG_WARN); 236 } 237 } 238 239 240 241 246 public void doUninstall(Map args) 247 { 248 if (!args.isEmpty()) { 249 Iterator itr= args.keySet().iterator(); 250 while (itr.hasNext()) { 251 String prefix = (String )itr.next(); 252 if (!FixtureExaminer.isBuiltinValueURIScheme(prefix)) { 253 Iteration.removeValueURIHandler(prefix); 254 } else { 255 String warning = Iteration.uistrs().get("valueuri.resrved.handlr",prefix); 256 log(warning, Project.MSG_WARN); 257 } 258 } 259 } 260 } 261 262 263 264 270 public void doDisable(Map ignoredArgs) 271 { 272 Project p = getProject(); 273 Map ht = p.getReferences(); 274 boolean warn=false; 275 276 synchronized(ht) { Object o = ht.get(REFID); 278 if (o instanceof Interpreter) { 279 Interpreter interpreter = (Interpreter)o; 280 ht.remove(REFID); 281 282 PropertyHelper ph = PropertyHelper.getPropertyHelper(p); 283 synchronized(ph) { 284 PropertyHelper prevPh=null; 285 do { 286 if (ph.getNext()==interpreter) { break; } 287 prevPh = ph; 288 ph = ph.getNext(); 289 } while(ph!=null); 290 if (ph!=null) { 291 ph.setNext(interpreter.getNext()); 292 if (prevPh!=null) { 293 prevPh.setNext(ph); 294 } 295 } else { 296 warn = true; 297 } 298 interpreter.setNext(null); 299 } 300 interpreter = null; 301 } 302 o = null; 303 } 304 305 if (warn && isVerbose()) { 306 String warning= Iteration.uistrs().get("fixture.supPh.not.instald",LABEL); 307 log(warning,Project.MSG_WARN); 308 } 309 } 310 311 312 313 328 public void doCheck(Map args) 329 { 330 Project p = getProject(); 331 Map ht = p.getReferences(); 332 String result = "no"; 333 334 synchronized(ht) { Object o = ht.get(REFID); 336 if (o instanceof Interpreter) { 337 result = "yes"; 338 } 339 else if (o!=null) { 340 result = "unknown"; 341 } 342 } 343 344 boolean saved=false; 345 346 Object o = args.get("updateproperty"); 347 if (o!=null) { 348 String property = o.toString(); 349 FixtureExaminer.checkIfProperty(p,m_rqlink,property,true); 350 p.setNewProperty(property,result); 351 saved = true; 352 } 353 o = args.get("updatevariable"); 354 if (o!=null) { 355 ExportedProperties.set(o.toString(),result); 356 saved = true; 357 } 358 if (!saved) { 359 int level = NoiseLevel.getDefault(p).getNativeIndex(); 360 String msg = Iteration.uistrs().get("valueuri.instaled",result); 361 log(msg, level); 362 } 363 } 364 365 366 367 377 private static class Interpreter extends PropertyHelper 378 { 379 private final Project m_project; 380 381 Interpreter(Project project) { 382 m_project = project; 383 } 384 385 388 public Object getPropertyHook(String ns, String name, 389 boolean userOnly) { 390 Object o; 391 if ((ns==null || ns.length()==0) && !userOnly) { 392 o= FixtureExaminer.findValue(m_project,name,null); 393 } else { 394 o= null; 395 } 396 if (o==null && getNext()!=null) { 397 o = getNext().getPropertyHook(ns,name,userOnly); 398 } 399 return o; 400 } 401 402 405 public boolean setPropertyHook(String ns, String name, Object value, 406 boolean inherited, boolean userOnly, 407 boolean isNew) { 408 if (getNext()!=null) { 409 return getNext().setPropertyHook(ns,name,value,inherited, 410 userOnly,isNew); 411 } 412 return false; 413 } 414 } 415 416 417 private boolean isVerbose() 418 { 419 return !FeedbackLevel.isQuietish(getFeedbackLevel(),true,true); 421 } 422 } 423 424 | Popular Tags |