1 package fr.jayasoft.ivy.ant; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.apache.tools.ant.Project; 9 import org.apache.tools.ant.taskdefs.CallTarget; 10 import org.apache.tools.ant.taskdefs.Property; 11 12 import fr.jayasoft.ivy.IvyContext; 13 import fr.jayasoft.ivy.event.AbstractTrigger; 14 import fr.jayasoft.ivy.event.IvyEvent; 15 import fr.jayasoft.ivy.event.Trigger; 16 import fr.jayasoft.ivy.util.IvyPatternHelper; 17 import fr.jayasoft.ivy.util.Message; 18 import fr.jayasoft.ivy.util.MessageImpl; 19 20 36 public class AntCallTrigger extends AbstractTrigger implements Trigger { 37 private boolean _onlyonce = true; 38 private String _target = null; 39 private Collection _calls = new ArrayList (); 40 private String _prefix; 41 42 public void progress(IvyEvent event) { 43 Project project = (Project)IvyContext.getContext().get(IvyTask.ANT_PROJECT_CONTEXT_KEY); 44 if (project == null) { 45 Message.info("ant call trigger can only be used from an ant build. Ignoring."); 46 return; 47 } 48 if (_onlyonce && isTriggered(event)) { 49 Message.verbose("call already triggered for this event, skipping: "+event); 50 } else { 51 CallTarget call = new CallTarget(); 52 53 call.setProject(project); 54 call.setTaskName("antcall"); 55 56 Map attributes = event.getAttributes(); 57 String target = IvyPatternHelper.substituteTokens(getTarget(), attributes); 58 call.setTarget(target); 59 60 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 61 String key = (String ) iter.next(); 62 String value = (String ) attributes.get(key); 63 Property p = call.createParam(); 64 p.setName(_prefix == null?key:_prefix+key); 65 p.setValue(value == null?"":value); 66 } 67 68 Message.verbose("triggering ant call: target="+target+" for "+event); 69 MessageImpl impl = IvyContext.getContext().getMessageImpl(); 70 try { 71 IvyContext.getContext().setMessageImpl(null); 72 call.execute(); 73 markTriggered(event); 74 } finally { 75 IvyContext.getContext().setMessageImpl(impl); 76 } 77 78 Message.debug("triggered ant call finished: target="+target+" for "+event); 79 } 80 } 81 82 83 84 private void markTriggered(IvyEvent event) { 85 _calls.add(event); 86 } 87 88 private boolean isTriggered(IvyEvent event) { 89 return _calls.contains(event); 90 } 91 92 93 94 public String getTarget() { 95 return _target; 96 } 97 98 public void setTarget(String target) { 99 _target = target; 100 } 101 102 public boolean isOnlyonce() { 103 return _onlyonce; 104 } 105 106 public void setOnlyonce(boolean onlyonce) { 107 _onlyonce = onlyonce; 108 } 109 110 public String getPrefix() { 111 return _prefix; 112 } 113 114 public void setPrefix(String prefix) { 115 _prefix = prefix; 116 if (!prefix.endsWith(".")) { 117 _prefix += "."; 118 } 119 } 120 } 121 | Popular Tags |