diff --git a/src/main/log4j2.xml b/src/main/log4j2.xml index 469fd8f1..caba9da5 100644 --- a/src/main/log4j2.xml +++ b/src/main/log4j2.xml @@ -1,8 +1,8 @@ - + - - ${sys:user.home}/.studioforkdb${sys:log4j.studio.envSuffix:-}/log + + ${studiobase:log} diff --git a/src/main/studio/core/Studio.java b/src/main/studio/core/Studio.java index 05d8a757..2fb131ef 100755 --- a/src/main/studio/core/Studio.java +++ b/src/main/studio/core/Studio.java @@ -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); diff --git a/src/main/studio/kdb/Config.java b/src/main/studio/kdb/Config.java index 1ea719e8..9c8f9365 100755 --- a/src/main/studio/kdb/Config.java +++ b/src/main/studio/kdb/Config.java @@ -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.*; @@ -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; @@ -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"; @@ -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 servers; private Collection serverNames; @@ -136,43 +134,46 @@ public static FontStyle getStyle(int fontStyle) { private TableConnExtractor tableConnExtractor; - private final static Map 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() { @@ -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) { diff --git a/src/main/studio/ui/HelpDialog.java b/src/main/studio/ui/HelpDialog.java index ec951dbc..492f77fc 100755 --- a/src/main/studio/ui/HelpDialog.java +++ b/src/main/studio/ui/HelpDialog.java @@ -1,8 +1,8 @@ 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; @@ -10,13 +10,11 @@ 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", "Studio for kdb+

Studio for kdb+

" + "

" diff --git a/src/main/studio/ui/StudioPanel.java b/src/main/studio/ui/StudioPanel.java index 7a314807..516802ee 100755 --- a/src/main/studio/ui/StudioPanel.java +++ b/src/main/studio/ui/StudioPanel.java @@ -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.*; @@ -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); diff --git a/src/main/studio/utils/WindowsAppUserMode.java b/src/main/studio/utils/WindowsAppUserMode.java index 6ee6278b..41199ac2 100644 --- a/src/main/studio/utils/WindowsAppUserMode.java +++ b/src/main/studio/utils/WindowsAppUserMode.java @@ -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() { diff --git a/src/main/studio/utils/log4j/EnvConfig.java b/src/main/studio/utils/log4j/EnvConfig.java new file mode 100644 index 00000000..c246b516 --- /dev/null +++ b/src/main/studio/utils/log4j/EnvConfig.java @@ -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); + } +} diff --git a/src/test/studio/kdb/ConfigTest.java b/src/test/studio/kdb/ConfigTest.java index 1b870b11..57181803 100644 --- a/src/test/studio/kdb/ConfigTest.java +++ b/src/test/studio/kdb/ConfigTest.java @@ -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, @@ -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 @@ -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 propsModification) throws IOException {