package com.redislabs.cytoscape.redisgraph.internal.configuration; import org.apache.log4j.Logger; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.time.LocalDateTime; import java.util.Properties; /** * This clas stores data that is used by the plugin. * The settings are stored in a property file the temporary directory (java.io.tmpdir). */ public class AppConfiguration { private static final Logger LOG = Logger.getLogger(AppConfiguration.class); private static final String TEMPLATEDIR = "templatedir"; private static final String REDISGRAPH_URL = "redisgraph.url"; private Properties properties = new Properties(); public String getTemplateDirectory() { return properties.getProperty(TEMPLATEDIR); } public String getRedisGraphURL() { return properties.getProperty(REDISGRAPH_URL); } public void setTemplateDirectory(String templateDir) { properties.setProperty(TEMPLATEDIR, templateDir); save(); } public void load() { Path configurationPath = getConfigurationFile(); if (configurationPath.toFile().exists()) { try { properties.load(Files.newInputStream(configurationPath, StandardOpenOption.READ)); } catch (IOException e) { LOG.warn("Error reading configuration"); } } else { setDefaultProperties(); } } private void setDefaultProperties() { properties.setProperty(REDISGRAPH_URL, "redis://localhost:6379"); properties.setProperty(TEMPLATEDIR, ""); } public void save() { Path configurationFile = getConfigurationFile(); try { properties.store(Files.newOutputStream(configurationFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING), "saved app config: " + LocalDateTime.now()); } catch (IOException e) { LOG.warn("Error writing config file"); } } private Path getConfigurationFile() { String tmpDir = System.getProperty("java.io.tmpdir"); return Paths.get(tmpDir, "redislabs-redisgraph.properties"); } public void setConnectionParameters(String hostname) { properties.setProperty(REDISGRAPH_URL, hostname); } }