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.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Map.Entry;
024:
025: import org.apache.ivy.core.settings.IvyVariableContainer;
026: import org.apache.ivy.core.settings.IvyVariableContainerImpl;
027: import org.apache.ivy.util.Message;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.taskdefs.Property;
030:
031: class IvyAntVariableContainer extends IvyVariableContainerImpl
032: implements IvyVariableContainer {
033:
034: private Map overwrittenProperties = new HashMap();
035:
036: private Project project;
037:
038: public IvyAntVariableContainer(Project project) {
039: this .project = project;
040: }
041:
042: public String getVariable(String name) {
043: String r = (String) overwrittenProperties.get(name);
044: if (r == null) {
045: r = project.getProperty(name);
046: }
047: if (r == null) {
048: r = super .getVariable(name);
049: }
050: return r;
051: }
052:
053: public void setVariable(String varName, String value,
054: boolean overwrite) {
055: if (overwrite) {
056: Message.debug("setting '" + varName + "' to '" + value
057: + "'");
058: overwrittenProperties.put(varName, value);
059: } else {
060: super .setVariable(varName, value, overwrite);
061: }
062: }
063:
064: /**
065: * Updates the Ant Project used in this container with variables set in Ivy.
066: *
067: * All variables defined in Ivy will be set in the Ant project under two names:
068: * <ul>
069: * <li>the name of the variable</li>
070: * <li>the name of the variable suffxied with a dot + the given id,
071: * if the given id is not null</li>
072: * </ul>
073: *
074: * @param
075: * id The identifier of the settings in which the variables have been set, which
076: * should be used as property names suffix
077: */
078: public void updateProject(String id) {
079: Map r = new HashMap(super .getVariables());
080: r.putAll(overwrittenProperties);
081: for (Iterator it = r.entrySet().iterator(); it.hasNext();) {
082: Entry entry = (Entry) it.next();
083:
084: setPropertyIfNotSet((String) entry.getKey(), (String) entry
085: .getValue());
086: if (id != null) {
087: setPropertyIfNotSet((String) entry.getKey() + "." + id,
088: (String) entry.getValue());
089: }
090: }
091:
092: if (getEnvironmentPrefix() != null) {
093: Property propTask = new Property();
094: propTask.setProject(project);
095: propTask.setEnvironment(getEnvironmentPrefix());
096: propTask.init();
097: propTask.execute();
098: }
099: }
100:
101: private void setPropertyIfNotSet(String property, String value) {
102: if (project.getProperty(property) == null) {
103: project.setProperty(property, value);
104: }
105: }
106:
107: public Object clone() {
108: IvyAntVariableContainer result = (IvyAntVariableContainer) super
109: .clone();
110: result.overwrittenProperties = (HashMap) ((HashMap) this.overwrittenProperties)
111: .clone();
112: return result;
113: }
114: }
|