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.core.settings;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.ivy.core.IvyPatternHelper;
024: import org.apache.ivy.util.Message;
025:
026: public class IvyVariableContainerImpl implements IvyVariableContainer {
027:
028: private Map variables;
029: private String envPrefix;
030:
031: public IvyVariableContainerImpl() {
032: this .variables = new HashMap();
033: }
034:
035: public IvyVariableContainerImpl(Map variables) {
036: this .variables = variables;
037: }
038:
039: /*
040: * (non-Javadoc)
041: *
042: * @see org.apache.ivy.core.settings.IvyVariableContainer#setVariable(java.lang.String,
043: * java.lang.String, boolean)
044: */
045: public void setVariable(String varName, String value,
046: boolean overwrite) {
047: if (overwrite || !variables.containsKey(varName)) {
048: Message.debug("setting '" + varName + "' to '" + value
049: + "'");
050: variables.put(varName, substitute(value));
051: } else {
052: Message.debug("'" + varName + "' already set: discarding '"
053: + value + "'");
054: }
055: }
056:
057: public void setEnvironmentPrefix(String prefix) {
058: if ((prefix != null) && !prefix.endsWith(".")) {
059: this .envPrefix = prefix + ".";
060: } else {
061: this .envPrefix = prefix;
062: }
063: }
064:
065: private String substitute(String value) {
066: return IvyPatternHelper.substituteVariables(value, this );
067: }
068:
069: protected Map getVariables() {
070: return variables;
071: }
072:
073: protected String getEnvironmentPrefix() {
074: return envPrefix;
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.apache.ivy.core.settings.IvyVariableContainer#getVariable(java.lang.String)
081: */
082: public String getVariable(String name) {
083: String val = null;
084: if ((envPrefix != null) && name.startsWith(envPrefix)) {
085: val = System.getenv(name.substring(envPrefix.length()));
086: } else {
087: val = (String) variables.get(name);
088: }
089:
090: return val;
091: }
092:
093: public Object clone() {
094: IvyVariableContainerImpl clone;
095: try {
096: clone = (IvyVariableContainerImpl) super .clone();
097: } catch (CloneNotSupportedException e) {
098: throw new RuntimeException("unable to clone a "
099: + this .getClass());
100: }
101: clone.variables = new HashMap(this.variables);
102: return clone;
103: }
104: }
|