exceptionfactory commented on code in PR #6307:
URL: https://github.com/apache/nifi/pull/6307#discussion_r965988126


##########
nifi-nar-bundles/nifi-smb-bundle/nifi-smb-smbj-client/src/main/java/org/apache/nifi/services/smb/SmbjClientProviderService.java:
##########
@@ -116,16 +121,61 @@ public class SmbjClientProviderService extends 
AbstractControllerService impleme
 
     @Override
     public SmbClientService getClient() throws IOException {
-        final SmbjClientService client = new SmbjClientService(smbClient, 
authenticationContext, getServiceLocation());
+        Connection connection = smbClient.connect(hostname, port);
 
         try {
-            client.connectToShare(hostname, port, shareName);
+            return connectToShare(connection);
         } catch (IOException e) {
-            client.forceFullyCloseConnection();
-            client.connectToShare(hostname, port, shareName);
+            getLogger().debug("Closing stale connection and trying to create a 
new one for share " + getServiceLocation());
+
+            closeConnection(connection);
+
+            connection = smbClient.connect(hostname, port);
+            return connectToShare(connection);
+        }
+    }
+
+    private SmbjClientService connectToShare(Connection connection) throws 
IOException {
+        final Session session;
+        final Share share;
+
+        try {
+            session = connection.authenticate(authenticationContext);
+        } catch (Exception e) {
+            throw new IOException("Could not create session for share " + 
getServiceLocation(), e);
+        }
+
+        try {
+            share = session.connectShare(shareName);
+        } catch (Exception e) {
+            closeSession(session);
+            throw new IOException("Could not connect to share " + 
getServiceLocation(), e);
         }
 
-        return client;
+        if (!(share instanceof DiskShare)) {
+            closeSession(session);
+            throw new IllegalArgumentException("DiskShare not found. Share " + 
share.getClass().getSimpleName() + " found on " + getServiceLocation());
+        }
+
+        return new SmbjClientService(session, (DiskShare) share, 
getServiceLocation());
+    }
+
+    private void closeConnection(Connection connection) {
+        try {
+            if (connection != null) {
+                connection.close(true);
+            }
+        } catch (IOException ignore) {

Review Comment:
   I agree with adding some logging, or perhaps using a closeQuietly utility 
method.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to