org.jpos.q2.SimpleConfigurationFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpos Show documentation
Show all versions of jpos Show documentation
jPOS is an ISO-8583 based financial transaction
library/framework that can be customized and
extended in order to implement financial interchanges.
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2021 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.jpos.q2;
import org.jdom2.Element;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;
import org.jpos.core.SimpleConfiguration;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class SimpleConfigurationFactory implements ConfigurationFactory {
public Configuration getConfiguration(Element e) throws ConfigurationException {
Properties props = new Properties ();
Iterator iter = e.getChildren ("property").iterator();
while (iter.hasNext()) {
Element property = (Element) iter.next ();
String name = property.getAttributeValue("name");
String value = property.getAttributeValue("value");
String file = property.getAttributeValue("file");
if (file != null) {
file = Environment.get(file);
try {
if (file.endsWith(".yml")) {
readYAML(props, file);
} else {
props.load(new FileInputStream(new File(file)));
}
} catch (Exception ex) {
throw new ConfigurationException(file, ex);
}
}
else if (name != null && value != null) {
Object obj = props.get (name);
if (obj instanceof String[]) {
String[] mobj = (String[]) obj;
String[] m = new String[mobj.length + 1];
System.arraycopy(mobj,0,m,0,mobj.length);
m[mobj.length] = value;
props.put (name, m);
} else if (obj instanceof String) {
String[] m = new String[2];
m[0] = (String) obj;
m[1] = value;
props.put (name, m);
} else
props.put (name, value);
}
}
return new SimpleConfiguration(props);
}
private void readYAML (Properties props, String fileName) throws IOException {
File f = new File(fileName);
if (f.exists() && f.canRead()) {
try (InputStream fis = new FileInputStream(f)) {
Yaml yaml = new Yaml();
Iterable