Skip to content

Commit

Permalink
Merge pull request #87 from dzmipt/finosConfigFolderConfiguration
Browse files Browse the repository at this point in the history
config and log folder can be set with KDBSTUDIO_CONFIG_HOME variable
  • Loading branch information
gyorokpeter authored Apr 8, 2022
2 parents 3f8f64e + b26b97b commit f7f9162
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/main/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Configuration status="warn" packages="studio.utils.log4j">
<Properties>
<!-- Reference to the folder with Config properties - see Config class -->
<Property name="baseFolder">${sys:user.home}/.studioforkdb${sys:log4j.studio.envSuffix:-}/log</Property>
<!-- studiobase lookup plugin is defined in studio.utils.log4j.EnvConfig -->
<Property name="baseFolder">${studiobase:log}</Property>
</Properties>

<Appenders>
Expand Down
8 changes: 0 additions & 8 deletions src/main/studio/core/Studio.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public class Studio {
private static boolean macOSSystemMenu = false;

private static void initLogger() {
String env = System.getProperty("env");
if (env != null) {
log.info("Set environment to {}", env);
System.setProperty("log4j.studio.envSuffix", "/" + env);
((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).reconfigure();
Config.setEnvironment(env);
}

PrintStream stdoutStream = IoBuilder.forLogger("stdout").setLevel(Level.INFO).buildPrintStream();
PrintStream stderrStream = IoBuilder.forLogger("stderr").setLevel(Level.ERROR).buildPrintStream();
System.setOut(stdoutStream);
Expand Down
69 changes: 29 additions & 40 deletions src/main/studio/kdb/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import studio.utils.LineEnding;
import studio.utils.QConnection;
import studio.utils.TableConnExtractor;
import studio.utils.log4j.EnvConfig;

import javax.swing.tree.TreeNode;
import java.awt.*;
Expand All @@ -20,7 +21,6 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.tree.TreeNode;
Expand Down Expand Up @@ -113,7 +113,6 @@ public static FontStyle getStyle(int fontStyle) {
private static final String PATH = System.getProperties().getProperty("user.home") + "/.studioforkdb";
private static final String CONFIG_FILENAME = "studio.properties";
private static final String WORKSPACE_FILENAME = "workspace.properties";
private static String environment = null;

private static final String VERSION13 = "1.3";
private static final String VERSION12 = "1.2";
Expand All @@ -122,8 +121,7 @@ public static FontStyle getStyle(int fontStyle) {
private static final String VERSION = VERSION13;


private String env;
private String filename;
private final String filename;
private Properties p = new Properties();
private Map<String, Server> servers;
private Collection<String> serverNames;
Expand All @@ -136,43 +134,46 @@ public static FontStyle getStyle(int fontStyle) {

private TableConnExtractor tableConnExtractor;

private final static Map<String, Config> instances = new ConcurrentHashMap<>();
private final static Config instance = new Config();

public enum ExecAllOption {Execute, Ask, Ignore}

private Config(String env, String filename, Properties properties) {
this.env = env;
private Config(String filename, Properties properties) {
this.filename = filename;
init(filename, properties);
}

private Config(String env, String filename) {
this(env, filename, null);
protected Config(String filename) {
this(filename, null);
}

private static String getConfigFilename(String env, String filename) {
return PATH + (env == null ? "" : "/" + env) + "/" + filename;
private static void copyConfig(String configFileName) throws IOException {
Path src = Paths.get(EnvConfig.getFilepath(null, configFileName));
Path target = Paths.get(EnvConfig.getFilepath(configFileName));
if (Files.exists(src)) {
log.info("Copying from {} to {}", src, target);
Files.copy(src, target);
}
}

private String getWorkspaceFilename() {
return getConfigFilename(env, WORKSPACE_FILENAME);
}
private Config() {
filename = EnvConfig.getFilepath(CONFIG_FILENAME);

public synchronized static String getEnvironment() {
return environment;
String env = EnvConfig.getEnvironment();
if (env != null && ! Files.exists(Paths.get(filename))) {
log.info("Config for environment {} is not found. Copying from default location: {}", env, EnvConfig.getBaseFolder(null));
try {
copyConfig(CONFIG_FILENAME);
copyConfig(WORKSPACE_FILENAME);
} catch (IOException e) {
log.error("Error during copying configs", e);
}
}
init(filename, null);
}

public synchronized static void setEnvironment(String env) {
Config.environment = env;
String configFileName = getConfigFilename(env, CONFIG_FILENAME);

if (! Files.exists(Paths.get(configFileName))) {
log.info("Config for environment {} is not found. Copying from default...", env);
Config defaultConfig = Config.getByEnvironment(null);
Config config = new Config(env, configFileName, defaultConfig.p);
config.save();
config.saveWorkspace(defaultConfig.loadWorkspace());
}
private String getWorkspaceFilename() {
return EnvConfig.getFilepath(WORKSPACE_FILENAME);
}

public Workspace loadWorkspace() {
Expand Down Expand Up @@ -291,19 +292,7 @@ public String getEncoding() {
}

public static Config getInstance() {
return getByEnvironment(environment);
}

public static Config getByEnvironment(String env) {
return getInstance(env, getConfigFilename(env, CONFIG_FILENAME));
}

public static Config getByFilename(String filename) {
return getInstance(environment, filename);
}

private static Config getInstance(String env, String filename) {
return instances.computeIfAbsent(filename, name -> new Config(env, name));
return instance;
}

private void init(String filename, Properties properties) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/studio/ui/HelpDialog.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package studio.ui;

import studio.kdb.Config;
import studio.kdb.Lm;
import studio.utils.BrowserLaunch;
import studio.utils.log4j.EnvConfig;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class HelpDialog extends JDialog {
public HelpDialog(JFrame parent) {
super(parent, "Studio for kdb+");
String env = Config.getEnvironment();
String env = EnvConfig.getEnvironment();
final JEditorPane jep = new JEditorPane("text/html",
"<html><head><title>Studio for kdb+</title></head><body><h1>Studio for kdb+</h1>"
+ "<p>"
Expand Down
3 changes: 2 additions & 1 deletion src/main/studio/ui/StudioPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import studio.ui.rstextarea.FindReplaceAction;
import studio.ui.rstextarea.RSTextAreaFactory;
import studio.utils.*;
import studio.utils.log4j.EnvConfig;

import javax.swing.FocusManager;
import javax.swing.*;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void refreshTitle() {

if (! loading) {
Server server = editor.getServer();
String env = Config.getEnvironment();
String env = EnvConfig.getEnvironment();
String frameTitle = editor.getTitle() + (editor.isModified() ? " (not saved) " : "") + (server != null ? " @" + server.toString() : "") + " Studio for kdb+ " + Lm.version + (env == null ? "" : " [" + env + "]");
if (!frameTitle.equals(frame.getTitle())) {
frame.setTitle(frameTitle);
Expand Down
4 changes: 2 additions & 2 deletions src/main/studio/utils/WindowsAppUserMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import com.sun.jna.WString;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import studio.kdb.Config;
import studio.kdb.Lm;
import studio.ui.Util;
import studio.utils.log4j.EnvConfig;

public class WindowsAppUserMode {
private static final Logger log = LogManager.getLogger();

private final static boolean initialized = init();

private final static String mainID = "kdbStudioAppID" + Config.getEnvironment() + Lm.version;
private final static String mainID = "kdbStudioAppID" + EnvConfig.getEnvironment() + Lm.version;
private final static String chartID = mainID + "Chart";

private static boolean init() {
Expand Down
47 changes: 47 additions & 0 deletions src/main/studio/utils/log4j/EnvConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package studio.utils.log4j;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;

@Plugin(name="studiobase", category = StrLookup.CATEGORY)
public class EnvConfig implements StrLookup {

private final static String environment = System.getProperty("env");
private final static String homeFolder = getValue("KDBSTUDIO_CONFIG_HOME", System.getProperty("user.home") + "/.studioforkdb");

public static String getEnvironment() {
return environment;
}

public static String getBaseFolder(String env) {
return env == null ? homeFolder : homeFolder + "/" + env;
}

public static String getBaseFolder() {
return getBaseFolder(environment);
}

public static String getFilepath(String env, String filename) {
return getBaseFolder(env) + "/" + filename;
}

public static String getFilepath(String filename) {
return getFilepath(environment, filename);
}

private static String getValue(String key, String defaultValue) {
String value = System.getProperty(key, System.getenv(key));
return value == null ? defaultValue : value;
}

@Override
public String lookup(String key) {
return getFilepath(key);
}

@Override
public String lookup(LogEvent event, String key) {
return lookup(key);
}
}
6 changes: 3 additions & 3 deletions src/test/studio/kdb/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ConfigTest {
public void init() throws IOException {
tmpFile = File.createTempFile("studioforkdb", ".tmp");
tmpFile.deleteOnExit();
config = Config.getByFilename(tmpFile.getPath());
config = new Config(tmpFile.getPath());
System.out.println("temp file " + tmpFile.getPath());

server = new Server("testServer", "localhost",1111,
Expand Down Expand Up @@ -77,7 +77,7 @@ public void testDifferentConfigs() throws IOException{
assertEquals(value+1, config.getResultTabsCount());
assertEquals(value, config1.getResultTabsCount());

assertEquals(value+1, Config.getByFilename(tmpFile.getPath()).getResultTabsCount());
assertEquals(value+1, new Config(tmpFile.getPath()).getResultTabsCount());
}

@Test
Expand Down Expand Up @@ -148,7 +148,7 @@ private Config getConfig(Properties properties) throws IOException {
properties.store(out, null);
out.close();

return Config.getByFilename(newFile.getPath());
return new Config(newFile.getPath());
}

private Config copyConfig(Config config, Consumer<Properties> propsModification) throws IOException {
Expand Down

0 comments on commit f7f9162

Please sign in to comment.