#5533 reading/writing remote server config from/to json
[taxeditor.git] / 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;
 
+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.util.Arrays;
 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 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.common.CdmUtils;
 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";
 
+    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 static List<CdmServerInfo> cdmServerInfoList;
-
     private String cdmlibServicesVersion = "";
     private String cdmlibServicesLastModified = "";
 
@@ -377,17 +388,61 @@ public class CdmServerInfo {
 
 
     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() {