You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2017/04/15 05:23:03 UTC

svn commit: r1791449 - in /manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf: authorities/authorities/nuxeo/NuxeoAuthorityConnector.java crawler/connectors/nuxeo/NuxeoRepositoryConnector.java

Author: kwright
Date: Sat Apr 15 05:23:03 2017
New Revision: 1791449

URL: http://svn.apache.org/viewvc?rev=1791449&view=rev
Log:
Fix connection management

Modified:
    manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/nuxeo/NuxeoAuthorityConnector.java
    manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoRepositoryConnector.java

Modified: manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/nuxeo/NuxeoAuthorityConnector.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/nuxeo/NuxeoAuthorityConnector.java?rev=1791449&r1=1791448&r2=1791449&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/nuxeo/NuxeoAuthorityConnector.java (original)
+++ manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/authorities/authorities/nuxeo/NuxeoAuthorityConnector.java Sat Apr 15 05:23:03 2017
@@ -78,6 +78,9 @@ public class NuxeoAuthorityConnector ext
 
   protected NuxeoClient nuxeoClient = null;
 
+  protected long lastSessionFetch = -1L;
+  protected static final long timeToRelease = 300000L;
+
   // Constructor
   public NuxeoAuthorityConnector() {
     super();
@@ -114,11 +117,18 @@ public class NuxeoAuthorityConnector ext
     username = params.getParameter(NuxeoConfiguration.Server.USERNAME);
     password = params.getObfuscatedParameter(NuxeoConfiguration.Server.PASSWORD);
 
-    try {
-      initNuxeoClient();
-    } catch (ManifoldCFException manifoldCFException) {
-      manifoldCFException.printStackTrace();
-    }
+  }
+
+  @Override
+  public void disconnect() throws ManifoldCFException {
+    shutdownNuxeoClient();
+    protocol = null;
+    host = null;
+    port = null;
+    path = null;
+    username = null;
+    password = null;
+    super.disconnect();
   }
 
   /**
@@ -126,29 +136,15 @@ public class NuxeoAuthorityConnector ext
    */
   @Override
   public String check() throws ManifoldCFException {
+    shutdownNuxeoClient();
+    initNuxeoClient();
     try {
-      if (!isConnected()) {
-        initNuxeoClient();
-      }
-
-      Boolean result = null;
-      try {
-        getGroupsByUser("Administrator");
-        result = true;
-      } catch (Exception ex) {
-        result = false;
-      }
-
-      if (result)
-        return super.check();
-      else
-        throw new ManifoldCFException("Nuxeo instance could not be reached");
-
-    } catch (ManifoldCFException manifoldCFException) {
-      return "Connection failed: " + manifoldCFException.getMessage();
-    } catch (Exception e) {
-      return "Connection failed: " + e.getMessage();
+      nuxeoClient.repository().getDocumentRoot();
+    } catch (Exception ex) {
+      return "Connection failed: "+ex.getMessage();
     }
+
+    return super.check();
   }
 
   /**
@@ -174,6 +170,30 @@ public class NuxeoAuthorityConnector ext
     }
 
   }
+  
+  /**
+   * Shut down Nuxeo client
+   */
+  private void shutdownNuxeoClient() {
+    if (nuxeoClient != null) {
+      nuxeoClient.shutdown();
+      nuxeoClient = null;
+      lastSessionFetch = -1L;
+    }
+  }
+
+  @Override
+  public void poll() throws ManifoldCFException {
+    if (lastSessionFetch == -1L) {
+      return;
+    }
+
+    long currentTime = System.currentTimeMillis();
+
+    if (currentTime > lastSessionFetch + timeToRelease) {
+      shutdownNuxeoClient();
+    }
+  }
 
   /**
    * Formatter URL
@@ -328,6 +348,7 @@ public class NuxeoAuthorityConnector ext
   @Override
   public AuthorizationResponse getAuthorizationResponse(String username) {
     try {
+      initNuxeoClient();
       List<String> authorities = getGroupsByUser(username);
       if (authorities == null || authorities.isEmpty()) {
         return RESPONSE_USERNOTFOUND;

Modified: manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoRepositoryConnector.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoRepositoryConnector.java?rev=1791449&r1=1791448&r2=1791449&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoRepositoryConnector.java (original)
+++ manifoldcf/branches/CONNECTORS-1290-2/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoRepositoryConnector.java Sat Apr 15 05:23:03 2017
@@ -280,39 +280,32 @@ public class NuxeoRepositoryConnector ex
     username = params.getParameter(NuxeoConfiguration.Server.USERNAME);
     password = params.getObfuscatedParameter(NuxeoConfiguration.Server.PASSWORD);
 
-    try {
-      initNuxeoClient();
-    } catch (ManifoldCFException manifoldCFException) {
-      logger.debug("Not possible to initialize Nuxeo client. Reason: {}", manifoldCFException.getMessage());
-      manifoldCFException.printStackTrace();
-    }
   }
 
+  @Override
+  public void disconnect() throws ManifoldCFException {
+    shutdownNuxeoClient();
+    protocol = null;
+    host = null;
+    port = null;
+    path = null;
+    username = null;
+    password = null;
+    super.disconnect();
+  }
+  
   // Check the connection
   @Override
   public String check() throws ManifoldCFException {
+    shutdownNuxeoClient();
+    initNuxeoClient();
     try {
-      if (!isConnected()) {
-        initNuxeoClient();
-      }
-
-      Boolean result = true;
-      try {
-        nuxeoClient.repository().getDocumentRoot();
-      } catch (Exception ex) {
-        result = false;
-      }
-
-      if (result)
-        return super.check();
-      else
-        throw new ManifoldCFException("Nuxeo instance could not be reached");
-
-    } catch (ManifoldCFException manifoldCFException) {
-      return "Connection failed: " + manifoldCFException.getMessage();
-    } catch (Exception e) {
-      return "Connection failed: " + e.getMessage();
+      nuxeoClient.repository().getDocumentRoot();
+    } catch (Exception ex) {
+      return "Connection failed: "+ex.getMessage();
     }
+
+    return super.check();
   }
 
   /**
@@ -342,6 +335,17 @@ public class NuxeoRepositoryConnector ex
   }
 
   /**
+   * Shut down Nuxeo client
+   */
+  private void shutdownNuxeoClient() {
+    if (nuxeoClient != null) {
+      nuxeoClient.shutdown();
+      nuxeoClient = null;
+      lastSessionFetch = -1L;
+    }
+  }
+  
+  /**
    * Formatter URL
    * 
    * @throws ManifoldCFException
@@ -381,9 +385,7 @@ public class NuxeoRepositoryConnector ex
     long currentTime = System.currentTimeMillis();
 
     if (currentTime > lastSessionFetch + timeToRelease) {
-      nuxeoClient.shutdown();
-      nuxeoClient = null;
-      lastSessionFetch = -1;
+      shutdownNuxeoClient();
     }
   }
 
@@ -392,9 +394,7 @@ public class NuxeoRepositoryConnector ex
   public String addSeedDocuments(ISeedingActivity activities, Specification spec, String lastSeedVersion,
       long seedTime, int jobMode) throws ManifoldCFException, ServiceInterruption {
 
-    if (!isConnected())
-      initNuxeoClient();
-
+    initNuxeoClient();
     try {
 
       int lastStart = 0;
@@ -493,6 +493,8 @@ public class NuxeoRepositoryConnector ex
       IProcessActivity activities, int jobMode, boolean usesDefaultAuthority)
       throws ManifoldCFException, ServiceInterruption {
 
+    initNuxeoClient();
+        
     for (int i = 0; i < documentsIdentifieres.length; i++) {
 
       String documentId = documentsIdentifieres[i];
@@ -504,10 +506,6 @@ public class NuxeoRepositoryConnector ex
 
       try {
 
-        if (!isConnected()) {
-          initNuxeoClient();
-        }
-
         pResult = processDocument(documentId, spec, version, activities, doLog,
             Maps.<String, String> newHashMap());
       } catch (Exception exception) {