diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java | 128 |
1 files changed, 107 insertions, 21 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java b/dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java index 8ff3a584..34dceee3 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/Branding.java @@ -1,6 +1,8 @@ package org.openslx.dozmod; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -19,61 +21,141 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; -import org.openslx.dozmod.Config.ProxyMode; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.openslx.dozmod.Config.ProxyMode; +import org.openslx.dozmod.util.OsHelper; +import org.openslx.util.Util; public final class Branding { private final static Logger LOGGER = LogManager.getLogger(Branding.class); + private static final String VIRTUALIZER_WEBSITE = "virtualizer.website"; + private static final String SERVICE_EMAIL = "service.email"; + private static final String CONFIG_DIRECTORY = "config.directory"; + private static final String SERVICE_NAME = "service.name"; + private static final String SERVICE_FAQ_WEBSITE = "service.faq.website"; + private static final String APPLICATION_NAME = "application.name"; + private static final String MASTERSERVER_IDM = "masterserver.idm"; + private static final String MASTERSERVER_ADDRESS = "masterserver.address"; + private final static String PROPERTIES_FILE = "branding.properties"; private final static Properties PROPERTIES = new Properties(); private final static List<Path> RESOURCES = new ArrayList<Path>(); + public static final String RESOURCE_FS_DIR; + + private final static Map<String, String> DEFAULTS = new HashMap<String, String>() { + private static final long serialVersionUID = -5625624201188857134L; + + { + put(MASTERSERVER_ADDRESS, "bwlp-masterserver.ruf.uni-freiburg.de"); + put(MASTERSERVER_IDM, "bwIDM"); + put(APPLICATION_NAME, "bwLehrpool-Suite"); + put(SERVICE_FAQ_WEBSITE, "https://www.bwLehrpool.de"); + put(SERVICE_NAME, "bwLehrpool"); + put(CONFIG_DIRECTORY, "bwSuite"); + put(SERVICE_EMAIL, "support@bwlehrpool.de"); + put(VIRTUALIZER_WEBSITE, "https://www.bwlehrpool.de/doku.php/allgemein/virtualisierer"); + } + }; + static { - try (InputStream in = App.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE)) { - PROPERTIES.load(in); - } catch (Exception e) { - LOGGER.error("Failed to read '" + PROPERTIES_FILE + "': ", e); + String path = null; + try { + RESOURCES.add(Paths.get(PROPERTIES_FILE)); + RESOURCES.add(Paths.get("img")); + RESOURCES.add(Paths.get("txt")); + // Built-in properties file takes precedence + boolean ok = false; + try (InputStream in = App.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE)) { + PROPERTIES.load(in); + LOGGER.info("Loaded branding from jar"); + ok = true; + } catch (Exception e) { + LOGGER.debug("Failed to read 'jar:" + PROPERTIES_FILE + "': "); + } + if (!ok) { + // Try CWD + try (InputStream in = new FileInputStream(PROPERTIES_FILE)) { + PROPERTIES.load(in); + LOGGER.info("Loaded branding from current working directory"); + path = "./"; + ok = true; + } catch (Exception e) { + LOGGER.debug("Failed to read './" + PROPERTIES_FILE + "': "); + } + } + if (!ok) { + // Try system-wide defaults + if (OsHelper.isWindows()) { + path = System.getenv("ProgramData"); + if (Util.isEmptyString(path)) { + path = "C:\\ProgramData"; + } + } else { + path = "/etc"; + } + path = path + "/bwlp/"; + try (InputStream in = new FileInputStream(path + PROPERTIES_FILE)) { + PROPERTIES.load(in); + LOGGER.info("Loaded system-wide branding"); + ok = true; + } catch (Exception e) { + LOGGER.debug("Failed to read '" + path + PROPERTIES_FILE + "': "); + path = null; + } + } + // Fill in missing fields + PROPERTIES.store(System.out, ""); + for (Entry<String, String> it : DEFAULTS.entrySet()) { + if (!PROPERTIES.containsKey(it.getKey())) { + PROPERTIES.setProperty(it.getKey(), it.getValue()); + } + } + PROPERTIES.store(System.out, ""); + } catch (Throwable t) { + t.printStackTrace(); + System.exit(1); } - RESOURCES.add(Paths.get(PROPERTIES_FILE)); - RESOURCES.add(Paths.get("img")); - RESOURCES.add(Paths.get("txt")); + RESOURCE_FS_DIR = path; } public final static String getMasterServerAddress() { - return PROPERTIES.getProperty("masterserver.address", "bwlp-masterserver.ruf.uni-freiburg.de"); + return PROPERTIES.getProperty(MASTERSERVER_ADDRESS); } public final static String getMasterServerIdm() { - return PROPERTIES.getProperty("masterserver.idm", "bwIDM"); + return PROPERTIES.getProperty(MASTERSERVER_IDM); } public final static String getApplicationName() { - return PROPERTIES.getProperty("application.name", "bwLehrpool-Suite"); + return PROPERTIES.getProperty(APPLICATION_NAME); } public final static String getServiceFAQWebsite() { - return PROPERTIES.getProperty("service.faq.website", "https://www.bwLehrpool.de"); + return PROPERTIES.getProperty(SERVICE_FAQ_WEBSITE); } public final static String getServiceName() { - return PROPERTIES.getProperty("service.name", "bwLehrpool"); + return PROPERTIES.getProperty(SERVICE_NAME); } public final static String getConfigDirectory() { - return PROPERTIES.getProperty("config.directory", "bwSuite"); + return PROPERTIES.getProperty(CONFIG_DIRECTORY); } public final static String getServiceEmail() { - return PROPERTIES.getProperty("service.email", "support@bwlehrpool.de"); + return PROPERTIES.getProperty(SERVICE_EMAIL); } public final static String getVirtualizerWebsite() { - return PROPERTIES.getProperty("virtualizer.website", "https://www.bwlehrpool.de/doku.php/allgemein/virtualisierer"); + return PROPERTIES.getProperty(VIRTUALIZER_WEBSITE); } public final static String getProxyMode() { @@ -128,9 +210,6 @@ public final class Branding { private static void copyBranding(final URI jarUri, final String localDir, final boolean pack) { try (FileSystem fs = FileSystems.newFileSystem(URI.create("jar:" + jarUri), new HashMap<String, String>() { - /** - * Version for serialization. - */ private static final long serialVersionUID = -5625624201188857134L; { @@ -160,8 +239,15 @@ public final class Branding { dst = rootPathLocal.resolve(res); } if (!Files.isReadable(src)) { - LOGGER.error("Failed to find or read '" + src + "'."); - return; + if (!pack && res.endsWith(PROPERTIES_FILE)) { + // Missing properties file is OK, just dump current state + try (FileOutputStream fos = new FileOutputStream(dst.toFile())) { + PROPERTIES.store(fos, ""); + } + } else { + LOGGER.error("Failed to find or read '" + src + "'."); + } + continue; } if (Files.isDirectory(src)) { |