#5533 writing and reading cdmserver configuration from and to JSON file
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Thu, 11 Feb 2016 10:26:50 +0000 (11:26 +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
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfoConfig.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/ui/dialogs/CdmServerInfoTest.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);
diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfoConfig.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfoConfig.java
new file mode 100644 (file)
index 0000000..be17270
--- /dev/null
@@ -0,0 +1,122 @@
+// $Id$
+/**
+* Copyright (C) 2016 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.remoting.source;
+
+/**
+ * @author  a.kohlbecker
+ * @date  Feb 10, 2016
+ */
+public class CdmServerInfoConfig {
+    /**
+     *
+     */
+    private String name;
+    /**
+     *
+     */
+    private String server;
+    /**
+     *
+     */
+    private int port;
+    /**
+     *
+     */
+    private String prefix;
+    /**
+     *
+     */
+    private boolean ignoreCdmLibVersion;
+
+    /**
+     *
+     */
+    public CdmServerInfoConfig(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 CdmServerInfoConfig() {
+
+    }
+
+    /**
+     * @return the name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @param name the name to set
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * @return the server
+     */
+    public String getServer() {
+        return server;
+    }
+
+    /**
+     * @param server the server to set
+     */
+    public void setServer(String server) {
+        this.server = server;
+    }
+
+    /**
+     * @return the port
+     */
+    public int getPort() {
+        return port;
+    }
+
+    /**
+     * @param port the port to set
+     */
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    /**
+     * @return the prefix
+     */
+    public String getPrefix() {
+        return prefix;
+    }
+
+    /**
+     * @param prefix the prefix to set
+     */
+    public void setPrefix(String prefix) {
+        this.prefix = prefix;
+    }
+
+    /**
+     * @return the ignoreCdmLibVersion
+     */
+    public boolean isIgnoreCdmLibVersion() {
+        return ignoreCdmLibVersion;
+    }
+
+    /**
+     * @param ignoreCdmLibVersion the ignoreCdmLibVersion to set
+     */
+    public void setIgnoreCdmLibVersion(boolean ignoreCdmLibVersion) {
+        this.ignoreCdmLibVersion = ignoreCdmLibVersion;
+    }
+}
\ No newline at end of file
index d1eb71224b53c4e9a6fd04adada6e3ddf63e5c58..7da28a2a2264dfdbc21ecb4ba1d750920db25808 100644 (file)
@@ -21,6 +21,7 @@ import eu.etaxonomy.cdm.config.CdmSourceException;
 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
+import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfoConfig;
 
 /**
  * @author cmathew
@@ -42,7 +43,7 @@ public class CdmServerInfoTest extends UnitilsJUnit4 {
     @Ignore // this should be targetting integration or production
     @Test
     public void pingInstancesTest() {
-        CdmServerInfo csii = new CdmServerInfo("edit-test", "test.e-taxonomy.eu", 8080, "cdmserver/", false);
+        CdmServerInfo csii = new CdmServerInfo(new CdmServerInfoConfig("edit-test", "test.e-taxonomy.eu", 8080, "cdmserver/", false));
         try {
             csii.refreshInstances();
             List<CdmInstanceInfo> instances = csii.getInstances();
@@ -56,7 +57,7 @@ public class CdmServerInfoTest extends UnitilsJUnit4 {
 
     @Test
     public void refreshWrongInstancesTest() {
-        CdmServerInfo wrongCsii = new CdmServerInfo("local", "local", 8080, "noserver", false);
+        CdmServerInfo wrongCsii = new CdmServerInfo(new CdmServerInfoConfig("local", "local", 8080, "noserver", false));
         try {
             wrongCsii.refreshInstances();
             List<CdmInstanceInfo> instances = wrongCsii.getInstances();
@@ -69,7 +70,7 @@ public class CdmServerInfoTest extends UnitilsJUnit4 {
     @Ignore // this should be targetting integration or production
     @Test
     public void refreshInstancesTest() throws CDMServerException {
-        CdmServerInfo cdmServerInfo = new CdmServerInfo("edit-test", "test.e-taxonomy.eu", 8080, "cdmserver/", false);
+        CdmServerInfo cdmServerInfo = new CdmServerInfo(new CdmServerInfoConfig("edit-test", "test.e-taxonomy.eu", 8080, "cdmserver/", false));
         cdmServerInfo.refreshInstances();
         List<CdmInstanceInfo> instances = cdmServerInfo.getInstances();
         Assert.assertTrue(instances != null && !instances.isEmpty());