|
| 1 | +package configuration; |
| 2 | + |
| 3 | +import commons.CommonRails; |
| 4 | +import org.w3c.dom.Document; |
| 5 | +import org.w3c.dom.Element; |
| 6 | +import org.w3c.dom.NodeList; |
| 7 | + |
| 8 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 9 | +import java.io.File; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * NweConfig — loads configuration/nwe-config.xml at startup. |
| 15 | + * |
| 16 | + * Provides: |
| 17 | + * - isEnabled(serverId) inclusion decision for each <server> block |
| 18 | + * - adminUsername() initial administrator username |
| 19 | + * - adminPassword() initial administrator password |
| 20 | + * |
| 21 | + * Call NweConfig.load() once at the top of Main() before any service is |
| 22 | + * instantiated. ModuleAdmin.PASSWORD is updated from the XML value so the |
| 23 | + * admin may log in to any TCP service with the configured credentials. |
| 24 | + */ |
| 25 | +public class NweConfig |
| 26 | +{ |
| 27 | + private static final String CONFIG_FILE = "configuration/nwe-config.xml"; |
| 28 | + |
| 29 | + private static NweConfig INSTANCE; |
| 30 | + |
| 31 | + private final Map<String, Boolean> ENABLED = new HashMap<>(); |
| 32 | + private final String ADMIN_USERNAME; |
| 33 | + private final String ADMIN_PASSWORD; |
| 34 | + |
| 35 | + private NweConfig(final Map<String, Boolean> ENABLED, |
| 36 | + final String ADMIN_USERNAME, |
| 37 | + final String ADMIN_PASSWORD) |
| 38 | + { |
| 39 | + this.ENABLED.putAll(ENABLED); |
| 40 | + this.ADMIN_USERNAME = ADMIN_USERNAME; |
| 41 | + this.ADMIN_PASSWORD = ADMIN_PASSWORD; |
| 42 | + } |
| 43 | + |
| 44 | + // ── Public API ──────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | + /** Returns true if the <server id="..."> block has <enabled>true</enabled>. |
| 47 | + * Defaults to true when the tag is absent (safe default — no service is silently dropped). */ |
| 48 | + public static boolean isEnabled(final String SERVER_ID) |
| 49 | + { |
| 50 | + return get().ENABLED.getOrDefault(SERVER_ID, true); |
| 51 | + } |
| 52 | + |
| 53 | + public static String adminUsername() { return get().ADMIN_USERNAME; } |
| 54 | + public static String adminPassword() { return get().ADMIN_PASSWORD; } |
| 55 | + |
| 56 | + /** Load (or reload) configuration from disk. Called once from Main(). */ |
| 57 | + public static synchronized NweConfig load() |
| 58 | + { |
| 59 | + File file = new File(CONFIG_FILE); |
| 60 | + |
| 61 | + if (!file.exists()) |
| 62 | + { |
| 63 | + CommonRails.printSystemComponent( |
| 64 | + NweConfig.class, NweConfig.class.hashCode(), |
| 65 | + ". NweConfig — " + CONFIG_FILE + " not found; all servers enabled, default admin .", |
| 66 | + CommonRails.COLOR_YELLOW); |
| 67 | + INSTANCE = defaults(); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + try |
| 72 | + { |
| 73 | + Document doc = DocumentBuilderFactory.newInstance() |
| 74 | + .newDocumentBuilder().parse(file); |
| 75 | + doc.getDocumentElement().normalize(); |
| 76 | + |
| 77 | + Map<String, Boolean> enabled = new HashMap<>(); |
| 78 | + |
| 79 | + NodeList servers = doc.getElementsByTagName("server"); |
| 80 | + for (int i = 0; i < servers.getLength(); i++) |
| 81 | + { |
| 82 | + Element el = (Element) servers.item(i); |
| 83 | + String id = el.getAttribute("id"); |
| 84 | + String val = text(el, "enabled", "true"); |
| 85 | + if (!id.isEmpty()) enabled.put(id, Boolean.parseBoolean(val)); |
| 86 | + } |
| 87 | + |
| 88 | + Element root = doc.getDocumentElement(); |
| 89 | + NodeList adminNodes = root.getElementsByTagName("admin"); |
| 90 | + String adminUser = "mearvk"; |
| 91 | + String adminPass = "n21admin"; |
| 92 | + if (adminNodes.getLength() > 0) |
| 93 | + { |
| 94 | + Element adminEl = (Element) adminNodes.item(0); |
| 95 | + adminUser = text(adminEl, "username", adminUser); |
| 96 | + adminPass = text(adminEl, "password", adminPass); |
| 97 | + } |
| 98 | + |
| 99 | + INSTANCE = new NweConfig(enabled, adminUser, adminPass); |
| 100 | + |
| 101 | + CommonRails.printSystemComponent( |
| 102 | + NweConfig.class, NweConfig.class.hashCode(), |
| 103 | + ". NweConfig loaded — " + enabled.size() + " server entries, admin='" + adminUser + "' .", |
| 104 | + CommonRails.COLOR_LIME_GREEN); |
| 105 | + } |
| 106 | + catch (Exception e) |
| 107 | + { |
| 108 | + CommonRails.printSystemComponent( |
| 109 | + NweConfig.class, NweConfig.class.hashCode(), |
| 110 | + ". NweConfig parse error: " + e.getMessage() + " — using defaults .", |
| 111 | + CommonRails.COLOR_STANDARD_RED); |
| 112 | + INSTANCE = defaults(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Propagate admin password into ModuleAdmin so TCP services pick it up |
| 117 | + admin.ModuleAdmin.setPassword(INSTANCE.ADMIN_PASSWORD); |
| 118 | + |
| 119 | + return INSTANCE; |
| 120 | + } |
| 121 | + |
| 122 | + // ── Internals ───────────────────────────────────────────────────────────── |
| 123 | + |
| 124 | + private static NweConfig get() |
| 125 | + { |
| 126 | + if (INSTANCE == null) load(); |
| 127 | + return INSTANCE; |
| 128 | + } |
| 129 | + |
| 130 | + private static NweConfig defaults() |
| 131 | + { |
| 132 | + return new NweConfig(new HashMap<>(), "mearvk", "n21admin"); |
| 133 | + } |
| 134 | + |
| 135 | + private static String text(final Element EL, final String TAG, final String DEF) |
| 136 | + { |
| 137 | + NodeList nl = EL.getElementsByTagName(TAG); |
| 138 | + if (nl.getLength() == 0) return DEF; |
| 139 | + String v = nl.item(0).getTextContent().trim(); |
| 140 | + return v.isEmpty() ? DEF : v; |
| 141 | + } |
| 142 | +} |
0 commit comments