Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / CdmServerInfo.java
index 0ec0dd91c4bf7696a5d02b6795a6bf2af8e6ade5..3065a86fb14b26da4ebdb819f3df18dbd453414b 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2015 EDIT
  * European Distributed Institute of Taxonomy
@@ -9,6 +8,10 @@
  */
 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;
@@ -32,12 +35,19 @@ import org.json.JSONArray;
 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 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.model.metadata.CdmMetaData;
 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
+import eu.etaxonomy.taxeditor.remoting.server.CDMServerUtils;
 
 /**
  * @author cmathew
@@ -47,22 +57,13 @@ import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
 public class CdmServerInfo {
     public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
 
-    private final static String CDMSERVER_PREFIX = "";
+    private final static String CDMSERVER_PREFIX = "cdmserver/";
     private final static String NAME_PRODUCTION = "cybertaxonomy.org";
     private final static String SERVER_PRODUCTION = "api.cybertaxonomy.org";
 
-    private final static String NAME_INTEGRATION = "edit-integration";
-    private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
-
     private final static String NAME_DEMO_1 = "edit-WS I";
     private final static String SERVER_DEMO_1 = "160.45.63.230";
 
-    private final static String NAME_DEMO_2 = "edit-WS II";
-    private final static String SERVER_DEMO_2 = "160.45.63.231";
-
-    private final static String NAME_TEST = "edit-test";
-    private final static String SERVER_TEST = "test.e-taxonomy.eu";
-
     public final static String SERVER_LOCALHOST = "localhost";
     private final static String NAME_LOCALHOST = "localhost";
     public final static String NAME_LOCALHOST_MGD = "localhost mgd.";
@@ -70,30 +71,34 @@ public class CdmServerInfo {
     private final static String NAME_LOCALHOST_DEV = "localhost-dev";
     private final static String NAME_INSTANCE_LOCALHOST_DEV = "local-dev";
     private final static String SERVER_LOCALHOST_DEV = "localhost";
-    private final static int PORT_LOCALHOST_DEV = 8080;
     private final static String BASEPATH_LOCALHOST_DEV = "";
 
     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 = "";
 
+    private String prefix = "";
+
+    private boolean ignoreCdmLibVersion = false;
 
-    public CdmServerInfo(String name, String server, int port) {
-        this.name = name;
-        this.server = server;
-        this.port = port;
-        instances = new ArrayList<CdmInstanceInfo>();
 
+    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>();
     }
 
 
@@ -141,7 +146,7 @@ public class CdmServerInfo {
     }
 
     public void updateInfo() throws CDMServerException {
-        String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/info.jsp";
+        String url = "http://" + server + ":" + String.valueOf(port) + "/" + prefix + "info.jsp";
         String responseBody = getResponse(url);
         if(responseBody != null) {
             try {
@@ -156,7 +161,7 @@ public class CdmServerInfo {
 
     public void addInstancesViaHttp() throws CDMServerException {
         updateInfo();
-        String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
+        String url = "http://" + server + ":" + String.valueOf(port) + "/" + prefix + "instances.jsp";
         String responseBody = getResponse(url);
         if(responseBody != null) {
             try {
@@ -221,8 +226,9 @@ public class CdmServerInfo {
 
     public void addInstancesFromDataSourcesConfig() {
         for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
-            logger.info("Adding local instance " + dataSource.getName());
-            addInstance(dataSource.getName(), dataSource.getName());
+            String datasourceNCName = CDMServerUtils.xmlNCNamefrom(dataSource.getName());
+            logger.info("Adding local instance " + dataSource.getName() + " as " + datasourceNCName);
+            addInstance(datasourceNCName, datasourceNCName);
         }
     }
 
@@ -248,8 +254,8 @@ public class CdmServerInfo {
             return CdmRemoteSource.NewInstance(name,
                     server,
                     port,
-                    instance.getBasePath(),
-                    null);
+                    instance.getBasePath()
+                    );
         }
         return null;
     }
@@ -308,8 +314,11 @@ public class CdmServerInfo {
 
         String serverVersion = cdmlibServicesVersion;
         String serverCdmlibLastModified = cdmlibServicesLastModified;
-
-        return compareCdmlibServicesVersion(serverVersion, serverCdmlibLastModified);
+        if(ignoreCdmLibVersion) {
+            return 0;
+        } else {
+            return compareCdmlibServicesVersion(serverVersion, serverCdmlibLastModified);
+        }
     }
 
 
@@ -360,23 +369,71 @@ public class CdmServerInfo {
             return serverLastModified.compareTo(editorLastModified);
         }
 
-        return 0;
+        return result;
     }
 
 
 
     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));
-            cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_1, SERVER_DEMO_1, 80));
-           // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80));
-           cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
-            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
-            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,NULL_PORT));
+        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;
+    }
+
+
+    /**
+     * @param file
+     * @throws IOException
+     * @throws FileNotFoundException
+     * @throws JsonMappingException
+     * @throws JsonParseException
+     */
+    private static List<CdmServerInfoConfig>  loadFromConfigFile(File file) {
+
+        List<CdmServerInfoConfig> 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 = createDefaultServerConfigList();
+                mapper.writerWithDefaultPrettyPrinter().writeValue(new FileOutputStream(file), serverList);
+
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            try {
+                serverList = mapper.readValue(new FileInputStream(file), new TypeReference<List<CdmServerInfoConfig>>(){});
+            } catch (IOException e) {
+               throw new RuntimeException(e);
+            }
         }
-        return cdmServerInfoList;
+
+        return serverList;
+
+
+    }
+
+
+    /**
+     *
+     */
+    private static List<CdmServerInfoConfig> createDefaultServerConfigList() {
+
+        List<CdmServerInfoConfig> serverInfoList = new ArrayList<CdmServerInfoConfig>();
+        serverInfoList.add(new CdmServerInfoConfig(NAME_PRODUCTION, SERVER_PRODUCTION, 80, "", false));
+        serverInfoList.add(new CdmServerInfoConfig(NAME_DEMO_1, SERVER_DEMO_1, 80, CDMSERVER_PREFIX, false));
+        serverInfoList.add(new CdmServerInfoConfig(NAME_LOCALHOST, SERVER_LOCALHOST, 8080, CDMSERVER_PREFIX, true));
+        return serverInfoList;
     }
 
     public String getName() {
@@ -409,7 +466,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);
+            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);