001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.ivy.core.IvyContext;
026: import org.apache.ivy.core.IvyPatternHelper;
027: import org.apache.ivy.core.event.IvyEvent;
028: import org.apache.ivy.plugins.trigger.AbstractTrigger;
029: import org.apache.ivy.plugins.trigger.Trigger;
030: import org.apache.ivy.util.Message;
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.taskdefs.CallTarget;
033: import org.apache.tools.ant.taskdefs.Property;
034:
035: /**
036: * Triggers an call to an ant target on an event occurence.
037: * <p>
038: * This trigger only works when ivy is called from an ant build file, otherwise the trigger only log
039: * a failure.
040: * <p>
041: * Example of use in an ivysettings file:
042: *
043: * <pre>
044: * <ant-call-trigger event="post-download-artifact" filter="type=zip"
045: * target="unzip"/>
046: * </pre>
047: * Triggers a call to the target "unzip" for any downloaded artifact of type zip
048: *
049: * @see AntBuildTrigger
050: * @since 1.4
051: */
052: public class AntCallTrigger extends AbstractTrigger implements Trigger {
053: private boolean onlyonce = true;
054:
055: private String target = null;
056:
057: private Collection calls = new ArrayList();
058:
059: private String prefix;
060:
061: public void progress(IvyEvent event) {
062: Project project = (Project) IvyContext
063: .peekInContextStack(IvyTask.ANT_PROJECT_CONTEXT_KEY);
064: if (project == null) {
065: Message
066: .info("ant call trigger can only be used from an ant build. Ignoring.");
067: return;
068: }
069: if (onlyonce && isTriggered(event)) {
070: Message
071: .verbose("call already triggered for this event, skipping: "
072: + event);
073: } else {
074: CallTarget call = new CallTarget();
075:
076: call.setProject(project);
077: call.setTaskName("antcall");
078:
079: Map attributes = event.getAttributes();
080: String target = IvyPatternHelper.substituteTokens(
081: getTarget(), attributes);
082: call.setTarget(target);
083:
084: for (Iterator iter = attributes.keySet().iterator(); iter
085: .hasNext();) {
086: String key = (String) iter.next();
087: String value = (String) attributes.get(key);
088: Property p = call.createParam();
089: p.setName(prefix == null ? key : prefix + key);
090: p.setValue(value == null ? "" : value);
091: }
092:
093: Message.verbose("triggering ant call: target=" + target
094: + " for " + event);
095: call.execute();
096: markTriggered(event);
097:
098: Message.debug("triggered ant call finished: target="
099: + target + " for " + event);
100: }
101: }
102:
103: private void markTriggered(IvyEvent event) {
104: calls.add(event);
105: }
106:
107: private boolean isTriggered(IvyEvent event) {
108: return calls.contains(event);
109: }
110:
111: public String getTarget() {
112: return target;
113: }
114:
115: public void setTarget(String target) {
116: this .target = target;
117: }
118:
119: public boolean isOnlyonce() {
120: return onlyonce;
121: }
122:
123: public void setOnlyonce(boolean onlyonce) {
124: this .onlyonce = onlyonce;
125: }
126:
127: public String getPrefix() {
128: return prefix;
129: }
130:
131: public void setPrefix(String prefix) {
132: this .prefix = prefix;
133: if (!prefix.endsWith(".")) {
134: this .prefix += ".";
135: }
136: }
137: }
|