#5533 reading/writing remote server config from/to json
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Fri, 5 Feb 2016 16:34:50 +0000 (17:34 +0100)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Thu, 11 Feb 2016 10:27:01 +0000 (11:27 +0100)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfo.java

index b5a0f8af844a92c4feed53ce2d1daad6eeae795c..a5914aae31e3ac37902d9e0a6454aded30345843 100644 (file)
@@ -9,9 +9,14 @@
  */
 package eu.etaxonomy.taxeditor.remoting.source;
 
  */
 package eu.etaxonomy.taxeditor.remoting.source;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.Socket;
 import java.util.ArrayList;
 import java.io.IOException;
 import java.net.Socket;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
@@ -32,7 +37,13 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.config.CdmSourceException;
 import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
 import eu.etaxonomy.cdm.database.ICdmDataSource;
 import eu.etaxonomy.cdm.config.CdmSourceException;
 import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
 import eu.etaxonomy.cdm.database.ICdmDataSource;
@@ -77,14 +88,14 @@ public class CdmServerInfo {
     public final static int NULL_PORT = -1;
     public final static String NULL_PORT_STRING = "N/A";
 
     public final static int NULL_PORT = -1;
     public final static String NULL_PORT_STRING = "N/A";
 
+    private static final String CDM_REMOTE_SERVERS_CONFIG_FILE = "cdm_remote_servers.json";
+
 
     private final String name;
     private final String server;
     private int port;
     private final List<CdmInstanceInfo> instances;
 
 
     private final String name;
     private final String server;
     private int port;
     private final List<CdmInstanceInfo> instances;
 
-    private static List<CdmServerInfo> cdmServerInfoList;
-
     private String cdmlibServicesVersion = "";
     private String cdmlibServicesLastModified = "";
 
     private String cdmlibServicesVersion = "";
     private String cdmlibServicesLastModified = "";
 
@@ -377,17 +388,61 @@ public class CdmServerInfo {
 
 
     public static List<CdmServerInfo> getCdmServers() {
 
 
     public static List<CdmServerInfo> getCdmServers() {
-        if(cdmServerInfoList == null) {
-           cdmServerInfoList = new ArrayList<CdmServerInfo>();
-           // cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80, ""));
-           //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80, CDMSERVER_PREFIX));
-           cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_1, SERVER_DEMO_1, 80, CDMSERVER_PREFIX, false));
-           // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80, CDMSERVER_PREFIX));
-           cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80, CDMSERVER_PREFIX, false));
-           cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080, CDMSERVER_PREFIX, true));
-           cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST, NULL_PORT, CDMSERVER_PREFIX, false));
+        return loadFromJSONConfigFile(new File(CdmUtils.perUserCdmFolder, CDM_REMOTE_SERVERS_CONFIG_FILE));
+    }
+
+
+    /**
+     * @param file
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws JsonMappingException
+     * @throws JsonParseException
+     */
+    private static List<CdmServerInfo>  loadFromJSONConfigFile(File file) {
+
+        List<CdmServerInfo> serverList = null;
+        ObjectMapper mapper = new ObjectMapper();
+
+        if(!file.exists()) {
+            logger.info("The remote server config file '" + file.getAbsolutePath() +
+                    "' does not yet exist, it will be created based on the defaults.");
+             try {
+                serverList = createDefaultServerList();
+                mapper.writerWithDefaultPrettyPrinter().writeValue(new FileOutputStream(file), serverList.toArray(new CdmServerInfo[serverList.size()]));
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            try {
+                CdmServerInfo[] serverInfo = mapper.readValue(new FileInputStream(file),
+                        TypeFactory.defaultInstance().constructArrayType(CdmServerInfo.class));
+                serverList = Arrays.asList(serverInfo);
+            } catch (IOException e) {
+               throw new RuntimeException(e);
+            }
         }
         }
-        return cdmServerInfoList;
+
+        return serverList;
+
+
+    }
+
+
+    /**
+     *
+     */
+    private static List<CdmServerInfo> createDefaultServerList() {
+
+        List<CdmServerInfo> serverInfoList = new ArrayList<CdmServerInfo>();
+       // serverInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80, ""));
+       //serverInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80, CDMSERVER_PREFIX));
+        serverInfoList.add(new CdmServerInfo(NAME_DEMO_1, SERVER_DEMO_1, 80, CDMSERVER_PREFIX, false));
+       // serverInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80, CDMSERVER_PREFIX));
+        serverInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80, CDMSERVER_PREFIX, false));
+        serverInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080, CDMSERVER_PREFIX, true));
+        serverInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST, NULL_PORT, CDMSERVER_PREFIX, false));
+        return serverInfoList;
     }
 
     public String getName() {
     }
 
     public String getName() {