| | |
| | | */ |
| | | package de.uhilger.neon.entity; |
| | | |
| | | import com.grack.nanojson.JsonArray; |
| | | import com.grack.nanojson.JsonObject; |
| | | import com.grack.nanojson.JsonParser; |
| | | import com.grack.nanojson.JsonParserException; |
| | | import java.io.File; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileReader; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.logging.Logger; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * Objekte der Klasse NeonDescriptor beschreiben einen Neon-Server |
| | |
| | | public AuthenticatorDescriptor authenticator; |
| | | public List<ServerDescriptor> server; |
| | | public List<WsServerDescriptor> wsserver; |
| | | |
| | | |
| | | /** |
| | | * Den NeonDescriptor aus einer in JSON abgefassten Serverbeschreibungsdatei lesen. |
| | | * |
| | | * vgl. https://uhilger.de/data/pg/neon/anleitung.adoc#srv-desc |
| | | * |
| | | * @param fileName Name und ggf. Pfad zur Serverbeschreibung |
| | | */ |
| | | public void read(String fileName) { |
| | | try { |
| | | File f = new File(fileName); |
| | | JsonObject serverDesc = JsonParser.object().from(new FileReader(f)); |
| | | |
| | | //NeonDescriptor d = new NeonDescriptor(); |
| | | this.instanceName = serverDesc.getString("instanceName"); |
| | | this.contentType = serverDesc.getString("contentType"); |
| | | this.contentId = serverDesc.getString("contentId"); |
| | | this.contentDescription = serverDesc.getString("contentDescription"); |
| | | this.instanceDescription = serverDesc.getString("instanceDescription"); |
| | | this.actorPackages = getArray(serverDesc, "actorPackages"); |
| | | |
| | | JsonObject authObj = serverDesc.getObject("authenticator"); |
| | | if (null != authObj) { |
| | | this.authenticator = new AuthenticatorDescriptor(); |
| | | this.authenticator.className = authObj.getString("className"); |
| | | this.authenticator.attributes = getAttrMap(authObj, "attributes"); |
| | | } |
| | | JsonArray serverArray = serverDesc.getArray("server"); |
| | | this.server = new ArrayList<>(); |
| | | Iterator serverIterator = serverArray.iterator(); |
| | | while (serverIterator.hasNext()) { |
| | | Object iteratorObject = serverIterator.next(); |
| | | if (iteratorObject instanceof JsonObject) { |
| | | JsonObject serverObject = (JsonObject) iteratorObject; |
| | | ServerDescriptor sd = new ServerDescriptor(); |
| | | sd.name = serverObject.getString("name"); |
| | | sd.port = serverObject.getInt("port", 0); |
| | | sd.contexts = new ArrayList<>(); |
| | | JsonArray contextsArray = serverObject.getArray("contexts"); |
| | | Iterator contextIterator = contextsArray.iterator(); |
| | | while (contextIterator.hasNext()) { |
| | | Object contextObj = contextIterator.next(); |
| | | if (contextObj instanceof JsonObject) { |
| | | JsonObject context = (JsonObject) contextObj; |
| | | ContextDescriptor cd = new ContextDescriptor(); |
| | | cd.className = context.getString("className"); |
| | | cd.sharedHandler = context.getBoolean("sharedHandler"); |
| | | cd.contextPath = context.getString("contextPath"); |
| | | if (context.containsKey("filter")) { |
| | | cd.filter = getArray(context, "filter"); |
| | | } |
| | | cd.authenticator = context.getString("authenticator"); |
| | | cd.attributes = getAttrMap(context, "attributes"); |
| | | sd.contexts.add(cd); |
| | | } |
| | | } |
| | | this.server.add(sd); |
| | | } |
| | | } |
| | | //return d; |
| | | } catch (JsonParserException | FileNotFoundException ex) { |
| | | Logger.getLogger(NeonDescriptor.class.getName()).severe(ex.getMessage()); |
| | | //return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Die Eintraege eines einfachen JSON-Array in eine String-Liste uebertragen |
| | | * @param src das Objekt, aus dem die Inhalte des JSON-Arrays gelesen werden |
| | | * @param name der Name des zu lesenden JSON-Arrays |
| | | * @return der Inhalt des JSON-Arrays als String-Liste |
| | | */ |
| | | private List<String> getArray(JsonObject src, String name) { |
| | | return src.getArray(name) |
| | | .stream() |
| | | .map((o) -> Objects.toString(o, null)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * Eine Liste von Attributen (Schluessel/Wert-Paare) aus einem JSON-Objekt |
| | | * in eine Map uebertragen. |
| | | * |
| | | * @param src das JSON-Objekt, aus dem die Attribute gelesen werden sollen |
| | | * @param name der Name des JSON-Elements, das die Attribute enthaelt |
| | | * @return eine Map mit den gelesenen Attributen |
| | | */ |
| | | private HashMap<String, String> getAttrMap(JsonObject src, String name) { |
| | | HashMap<String, String> map = new HashMap<>(); |
| | | JsonObject attrObj = src.getObject(name); |
| | | Iterator attributeIterator = attrObj.keySet().iterator(); |
| | | while (attributeIterator.hasNext()) { |
| | | Object keyObj = attributeIterator.next(); |
| | | if (keyObj instanceof String) { |
| | | String key = (String) keyObj; |
| | | map.put(key, attrObj.get(key).toString()); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | } |