#5533 writing and reading cdmserver configuration from and to JSON file
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / CdmServerInfo.java
index a5914aae31e3ac37902d9e0a6454aded30345843..28e6446b29d963d1fec667167159dc73810d607e 100644 (file)
@@ -16,7 +16,6 @@ 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;
@@ -38,9 +37,9 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.type.TypeReference;
 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;
@@ -104,14 +103,13 @@ public class CdmServerInfo {
     private boolean ignoreCdmLibVersion = false;
 
 
-    public CdmServerInfo(String name, String server, int port, String prefix, boolean ignoreCdmLibVersion) {
-        this.name = name;
-        this.server = server;
-        this.port = port;
-        this.prefix = prefix;
-        this.ignoreCdmLibVersion = ignoreCdmLibVersion;
+    public CdmServerInfo(CdmServerInfoConfig parameterObject) {
+        this.name = parameterObject.getName();
+        this.server = parameterObject.getServer();
+        this.port = parameterObject.getPort();
+        this.prefix = parameterObject.getPrefix();
+        this.ignoreCdmLibVersion = parameterObject.isIgnoreCdmLibVersion();
         instances = new ArrayList<CdmInstanceInfo>();
-
     }
 
 
@@ -388,7 +386,15 @@ public class CdmServerInfo {
 
 
     public static List<CdmServerInfo> getCdmServers() {
-        return loadFromJSONConfigFile(new File(CdmUtils.perUserCdmFolder, CDM_REMOTE_SERVERS_CONFIG_FILE));
+        List<CdmServerInfoConfig> configList = loadFromConfigFile(new File(CdmUtils.perUserCdmFolder, CDM_REMOTE_SERVERS_CONFIG_FILE));
+        List<CdmServerInfo> serverInfoList = new ArrayList<CdmServerInfo>(configList.size());
+        for(CdmServerInfoConfig config : configList) {
+            serverInfoList.add(new CdmServerInfo(config));
+        }
+        // The local host managed server must not be in the config file
+        CdmServerInfoConfig localHostManagedConfig = new CdmServerInfoConfig(NAME_LOCALHOST_MGD, SERVER_LOCALHOST, NULL_PORT, CDMSERVER_PREFIX, false);
+        serverInfoList.add(new CdmServerInfo(localHostManagedConfig));
+        return serverInfoList;
     }
 
 
@@ -399,25 +405,25 @@ public class CdmServerInfo {
      * @throws JsonMappingException
      * @throws JsonParseException
      */
-    private static List<CdmServerInfo>  loadFromJSONConfigFile(File file) {
+    private static List<CdmServerInfoConfig>  loadFromConfigFile(File file) {
+
+        List<CdmServerInfoConfig> serverList = null;
 
-        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()]));
+                serverList = createDefaultServerConfigList();
+                mapper.writerWithDefaultPrettyPrinter().writeValue(new FileOutputStream(file), serverList);
+
             } 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);
+                serverList = mapper.readValue(new FileInputStream(file), new TypeReference<List<CdmServerInfoConfig>>(){});
             } catch (IOException e) {
                throw new RuntimeException(e);
             }
@@ -432,16 +438,15 @@ public class CdmServerInfo {
     /**
      *
      */
-    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));
+    private static List<CdmServerInfoConfig> createDefaultServerConfigList() {
+
+        List<CdmServerInfoConfig> serverInfoList = new ArrayList<CdmServerInfoConfig>();
+       // serverInfoList.add(new CdmServerInfoConfig(NAME_PRODUCTION, SERVER_PRODUCTION, 80, ""));
+       //serverInfoList.add(new CdmServerInfoConfig(NAME_INTEGRATION, SERVER_INTEGRATION, 80, CDMSERVER_PREFIX));
+        serverInfoList.add(new CdmServerInfoConfig(NAME_DEMO_1, SERVER_DEMO_1, 80, CDMSERVER_PREFIX, false));
+       // serverInfoList.add(new CdmServerInfoConfig(NAME_DEMO_2, SERVER_DEMO_2, 80, CDMSERVER_PREFIX));
+        serverInfoList.add(new CdmServerInfoConfig(NAME_TEST, SERVER_TEST, 80, CDMSERVER_PREFIX, false));
+        serverInfoList.add(new CdmServerInfoConfig(NAME_LOCALHOST, SERVER_LOCALHOST, 8080, CDMSERVER_PREFIX, true));
         return serverInfoList;
     }
 
@@ -475,7 +480,7 @@ public class CdmServerInfo {
         CdmInstanceInfo devInstance = null;
         if(value != null && !value.isEmpty()) {
             int devPort = Integer.valueOf(value);
-            CdmServerInfo devCii = new CdmServerInfo(NAME_LOCALHOST_DEV, SERVER_LOCALHOST_DEV, devPort, "", false);
+            CdmServerInfo devCii = new CdmServerInfo(new CdmServerInfoConfig(NAME_LOCALHOST_DEV, SERVER_LOCALHOST_DEV, devPort, "", false));
             try {
                 devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
                 available = devCii.pingInstance(devInstance, devPort);