KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > AntCallTrigger


1 package fr.jayasoft.ivy.ant;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
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 /**
21  * Triggers an call to an ant target on an event occurence.
22  *
23  * This trigger only works when ivy is called from an ant build file, otherwise the trigger
24  * only log a failure.
25  *
26  * Example of use in an ivyconf file:
27  * <ant-call-trigger event="post-download-artifact" filter="type=zip"
28  * target="unzip"/>
29  * Triggers a call to the target "unzip" for any downloaded artifact of type zip
30  *
31  * @see AntBuildTrigger
32  * @since 1.4
33  * @author Xavier Hanin
34  *
35  */

36 public class AntCallTrigger extends AbstractTrigger implements Trigger {
37     private boolean _onlyonce = true;
38     private String JavaDoc _target = null;
39     private Collection JavaDoc _calls = new ArrayList JavaDoc();
40     private String JavaDoc _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 JavaDoc attributes = event.getAttributes();
57             String JavaDoc target = IvyPatternHelper.substituteTokens(getTarget(), attributes);
58             call.setTarget(target);
59             
60             for (Iterator JavaDoc iter = attributes.keySet().iterator(); iter.hasNext();) {
61                 String JavaDoc key = (String JavaDoc) iter.next();
62                 String JavaDoc value = (String JavaDoc) 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 JavaDoc getTarget() {
95         return _target;
96     }
97
98     public void setTarget(String JavaDoc 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 JavaDoc getPrefix() {
111         return _prefix;
112     }
113
114     public void setPrefix(String JavaDoc prefix) {
115         _prefix = prefix;
116         if (!prefix.endsWith(".")) {
117             _prefix += ".";
118         }
119     }
120 }
121
Popular Tags