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.io.File;
021: import java.io.FileInputStream;
022: import java.io.InputStream;
023: import java.net.URL;
024: import java.util.Iterator;
025: import java.util.Properties;
026:
027: import org.apache.ivy.Ivy;
028: import org.apache.ivy.core.settings.IvySettings;
029: import org.apache.tools.ant.BuildException;
030:
031: /**
032: * This task let user set ivy variables from ant.
033: */
034: public class IvyVar extends IvyTask {
035: private String name;
036:
037: private String value;
038:
039: private File file;
040:
041: private String url;
042:
043: private String prefix;
044:
045: public File getFile() {
046: return file;
047: }
048:
049: public void setFile(File aFile) {
050: file = aFile;
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public void setName(String aName) {
058: name = aName;
059: }
060:
061: public String getPrefix() {
062: return prefix;
063: }
064:
065: public void setPrefix(String aPrefix) {
066: prefix = aPrefix;
067: }
068:
069: public String getUrl() {
070: return url;
071: }
072:
073: public void setUrl(String aUrl) {
074: url = aUrl;
075: }
076:
077: public String getValue() {
078: return value;
079: }
080:
081: public void setValue(String aValue) {
082: value = aValue;
083: }
084:
085: public void doExecute() throws BuildException {
086: Ivy ivy = getIvyInstance();
087: IvySettings settings = ivy.getSettings();
088: if (getName() != null) {
089: settings.setVariable(getVarName(getName()), getValue());
090: } else {
091: Properties props = new Properties();
092: InputStream is = null;
093: try {
094: if (getFile() != null) {
095: is = new FileInputStream(getFile());
096: } else if (getUrl() != null) {
097: is = new URL(getUrl()).openStream();
098: } else {
099: throw new BuildException(
100: "specify either name or file or url to ivy var task");
101: }
102: props.load(is);
103: } catch (Exception ex) {
104: throw new BuildException(
105: "impossible to load variables from file: " + ex,
106: ex);
107: } finally {
108: if (is != null) {
109: try {
110: is.close();
111: } catch (Exception e) {
112: //ignore
113: }
114: }
115: }
116: for (Iterator iter = props.keySet().iterator(); iter
117: .hasNext();) {
118: String name = (String) iter.next();
119: String value = (String) props.get(name);
120: settings.setVariable(getVarName(name), value);
121: }
122: }
123: }
124:
125: private String getVarName(String name) {
126: String prefix = getPrefix();
127: if (prefix != null) {
128: if (prefix.endsWith(".")) {
129: return prefix + name;
130: } else {
131: return prefix + "." + name;
132: }
133: }
134: return name;
135: }
136: }
|