All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.tno.bim.nmd.config.NmdConfigImpl Maven / Gradle / Ivy

The newest version!
package nl.tno.bim.nmd.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class NmdConfigImpl implements NmdConfig {

	private String token;
	private int id;
	private String nmd2path;
	private String rootPath;
	private static final String rootNode = "nmd";
	
	private static Logger logger = Logger.getLogger(NmdConfig.class);
	
	public NmdConfigImpl() throws FileNotFoundException {
		Path rootPath = null;
		if (System.getProperty("os.name").contains("Windows")) {
			rootPath = Paths.get("C:\\tmp\\nmd\\");
		} else {
			rootPath = Paths.get("/etc/nmd/");
		}
		if (Files.notExists(rootPath.resolve("config.xml"))) {
			throw new FileNotFoundException("Could not find config file at path :" + rootPath.toAbsolutePath().toString());
		}
		
		this.rootPath = rootPath.toString();
		
		Map nmdPropertyMap = NmdConfigImpl.loadResources(rootPath, rootNode);
		
		this.setToken(nmdPropertyMap.get("token"));
		this.setClientId(Integer.parseInt(nmdPropertyMap.get("id")));
		this.setNmd2DbPath(nmdPropertyMap.get("nmd2Path"));
	}
		
	/**
	 * load resources from a path containing a config.xml file
	 * @param rootPath Path where config.xml should be present
	 * @param rootNode rootnode to start reading the xml from
	 * @return Map with key value config parameters.
	 */
	private static Map loadResources(Path rootPath, String rootNode) {
		Map res = new HashMap();
				
		Path configPath = rootPath.resolve("config.xml");
		if (Files.exists(configPath)) {
			try {
				
				DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
				try (InputStream newInputStream = Files.newInputStream(configPath)) {
					Document doc = documentBuilder.parse(newInputStream);
					Consumer addNodedata = (node) -> {
						res.put(node.getNodeName(), node.getTextContent());
					};
					
					for(int i = 0; i < doc.getChildNodes().getLength(); i++) {
						Node node = doc.getChildNodes().item(i);
						performMethodForRecursiveNode(node, addNodedata);
					}
				}					
			} catch (Exception e) {
				logger.error(e.getMessage());
			} 
		} else {
			logger.error("config.xml not found at location: " + rootPath.toString());
		}
		
		return res;
	}
	
	private static void performMethodForRecursiveNode(Node node, Consumer method) {
		method.accept(node);
		for(int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node child = node.getChildNodes().item(i);
			performMethodForRecursiveNode(child, method);
		}
	}

	@Override
	public String getToken() {
		return this.token;
	}
	
	@Override
	public Integer getClientId() {
		return this.id;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setClientId(int id) {
		this.id = id;
	}

	/**
	 * Only for testing purposes. This path stores where the NMD2.X database file is stored
	 * @return file path string to database
	 */
	@Override
	public String getNmd2DbPath() {
		return this.rootPath + File.separator + this.nmd2path;
	}
	
	public void setNmd2DbPath(String path) {
		this.nmd2path = path;
	}	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy