/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.core.util;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import br.net.woodstock.rockframework.core.RockFrameworkException;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.collection.ImmutableMap;
import br.net.woodstock.rockframework.core.utils.Resources;
public class MultipleProperties implements Serializable {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
private Map<String, String> map;
public MultipleProperties(final String propertiesName) {
super();
Assert.notEmpty(propertiesName, "propertiesName");
try {
Map<String, String> tmpMap = new HashMap<String, String>();
Collection<URL> urls = Resources.getResources(propertiesName);
if (urls != null) {
for (URL url : urls) {
String s = url.toString();
if ((!s.startsWith(Resources.FILE_PREFIX)) && (!s.startsWith(Resources.VFSFILE_PREFIX))) {
InputStream inputStream = Resources.getInputStream(url, propertiesName);
if (inputStream != null) {
Properties p = new Properties();
p.load(inputStream);
for (Object key : p.keySet()) {
tmpMap.put(key.toString(), p.getProperty(key.toString()));
}
}
inputStream.close();
}
}
for (URL url : urls) {
String s = url.toString();
if ((s.startsWith(Resources.FILE_PREFIX)) || (s.startsWith(Resources.VFSFILE_PREFIX))) {
InputStream inputStream = Resources.getInputStream(url, propertiesName);
if (inputStream != null) {
Properties p = new Properties();
p.load(inputStream);
for (Object key : p.keySet()) {
tmpMap.put(key.toString(), p.getProperty(key.toString()));
}
}
inputStream.close();
}
}
}
this.map = ImmutableMap.toImmutable(tmpMap);
} catch (Exception e) {
throw new RockFrameworkException(e);
}
}
public Collection<String> getKeys() {
return this.map.keySet();
}
public Collection<String> getValues() {
return this.map.values();
}
public String getValue(final String key) {
return this.map.get(key);
}
public String getValue(final String key, final String defaultValue) {
if (this.map.containsKey(key)) {
return this.map.get(key);
}
return defaultValue;
}
}