Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.20.0
commons-logging = { module = "commons-logging:commons-logging", version = "1.3.5" }
commons-math3 = { module = "org.apache.commons:commons-math3", version = "3.0" }
commons-net = { module = "commons-net:commons-net", version = "3.9.0" }
commons-pool2 = { module = "org.apache.commons:commons-pool2", version = "2.3" }
commons-pool2 = { module = "org.apache.commons:commons-pool2", version = "2.13.1" }
commons-text = { module = "org.apache.commons:commons-text", version = "1.15.0" }
commons-vfs2 = { module = "org.apache.commons:commons-vfs2", version = "2.10.0" }
derby = { module = "org.apache.derby:derby", version.ref = "derby" }
Expand Down
15 changes: 5 additions & 10 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1481,11 +1481,6 @@
<sha256 value="53d0011352e2e5106f379dafccdac7754ec6f0f78243c0305e4969f5acddf8fb" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.commons" name="commons-parent" version="34">
<artifact name="commons-parent-34.pom">
<sha256 value="3a2e69d06d641d1f3b293126dc9e2e4ea6563bf8c36c87e0ab6fa4292d04b79c" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.commons" name="commons-parent" version="39">
<artifact name="commons-parent-39.pom">
<sha256 value="87cd27e1a02a5c3eb6d85059ce98696bb1b44c2b8b650f0567c86df60fa61da7" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -1579,12 +1574,12 @@
<sha256 value="4a961c2f7718508716618ad31fb41cb7a5cb749428ad0a19b8ff4d0d7f0b0934" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.commons" name="commons-pool2" version="2.3">
<artifact name="commons-pool2-2.3.jar">
<sha256 value="3e794e75be78f63cc31b6676697e45f07085903a1039169459132513d85c62ae" origin="Generated by Gradle"/>
<component group="org.apache.commons" name="commons-pool2" version="2.13.1">
<artifact name="commons-pool2-2.13.1.jar">
<sha256 value="f77a5060d6936a9144023584232f9de3f2248f1abfe156e9d31795f18770674f" origin="Generated by Gradle"/>
</artifact>
<artifact name="commons-pool2-2.3.pom">
<sha256 value="256b8598d469b7b20f92fd70406774d14b913b25affa02cd9488ddf6d1d3d55b" origin="Generated by Gradle"/>
<artifact name="commons-pool2-2.13.1.pom">
<sha256 value="89de9c86af44f8163f6a826eb71b80c93e90479de8b8d9229bc243e5a48e5e03" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.apache.commons" name="commons-text" version="1.15.0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/*
* Copyright (c) Mirth Corporation. All rights reserved.
*
* http://www.mirthcorp.com
*
* The software in this package is published under the terms of the MPL license a copy of which has
* been included with this distribution in the LICENSE.txt file.
*/
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Mirth Corporation
// SPDX-FileCopyrightText: 2026 Tony Germano

package com.mirth.connect.connectors.file;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -48,6 +44,7 @@ public class FileConnector {
private boolean validateConnection;
private int maxTotalConnections = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL;
private boolean keepConnectionOpen = false;
private boolean closeConnectionsAfterUse = false;
private String maxIdleTime;

public FileConnector(String channelId, ConnectorProperties connectorProperties, Connector connector) {
Expand All @@ -69,6 +66,7 @@ public FileConnector(String channelId, ConnectorProperties connectorProperties,
this.validateConnection = fileDispatcherProperties.isValidateConnection();
this.keepConnectionOpen = fileDispatcherProperties.isKeepConnectionOpen();
this.maxIdleTime = fileDispatcherProperties.getMaxIdleTime();
this.closeConnectionsAfterUse = !keepConnectionOpen;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid keeping new state? I don't see the benefit of adding this.

}

if (connector instanceof DestinationConnector) {
Expand Down Expand Up @@ -242,11 +240,23 @@ protected void disconnect() {
*/
protected void destroyConnection(FileSystemConnection connection, FileSystemConnectionOptions fileSystemOptions) throws Exception {
if (connection != null) {
ObjectPool<FileSystemConnection> pool = getConnectionPool(fileSystemOptions);
pool.invalidateObject(connection);

synchronized (connections) {
connections.remove(connection);
try {
ObjectPool<FileSystemConnection> pool = getConnectionPool(fileSystemOptions);
if (closeConnectionsAfterUse) {
/*
* The pool keeps no idle connections in this mode, so returnObject destroys the
* connection and creates a replacement only for a thread waiting to borrow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow what problem described. Consider editing for concise clarity.

Without understanding: We should not have to special-case our way around a pool. Can this be abstracted away from us having to care about pooling in our app logic?

* invalidateObject would create none here, since addObject respects maxIdle, and
* a waiting thread could block forever.
*/
pool.returnObject(connection);
} else {
pool.invalidateObject(connection);
}
} finally {
synchronized (connections) {
connections.remove(connection);
}
}
}
}
Expand All @@ -267,28 +277,34 @@ private synchronized ObjectPool<FileSystemConnection> getConnectionPool(FileSyst
ObjectPool<FileSystemConnection> pool = pools.get(key);

if (pool == null) {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
GenericObjectPoolConfig<FileSystemConnection> config = new GenericObjectPoolConfig<FileSystemConnection>();
config.setMaxTotal(maxTotalConnections);
config.setMinIdle(0); // The makes the evictor thread run, if active, no matter how few objects are in the pool
config.setTimeBetweenEvictionRunsMillis(1000); // How often the evictor thread checks for objects to evict
config.setTimeBetweenEvictionRuns(Duration.ofSeconds(1)); // How often the evictor thread checks for objects to evict

if (keepConnectionOpen) {
int connectionMaxIdleTime = NumberUtils.toInt(maxIdleTime);

if (connectionMaxIdleTime == 0) {
// Disable the eviction thread. https://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/BaseGenericObjectPool.html#setTimeBetweenEvictionRunsMillis-long-
config.setTimeBetweenEvictionRunsMillis(0);
// Disable the eviction thread. A non-positive interval means no evictor runs.
config.setTimeBetweenEvictionRuns(Duration.ZERO);
} else {
config.setMinEvictableIdleTimeMillis(connectionMaxIdleTime);
config.setMinEvictableIdleDuration(Duration.ofMillis(connectionMaxIdleTime));
}
} else {
// Always evict objects on the next check by the evictor thread
config.setMinEvictableIdleTimeMillis(1);
config.setMinEvictableIdleDuration(Duration.ofMillis(1));
}

if (closeConnectionsAfterUse) {
// Never keep idle connections, so that destroyConnection can return them to be destroyed
config.setMaxIdle(0);
}

if (isValidateConnection()) {
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
// Returned connections are always destroyed when they are not kept open
config.setTestOnReturn(!closeConnectionsAfterUse);
}
pool = new GenericObjectPool<FileSystemConnection>(fileSystemConnectionFactory, config);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Tony Germano

package com.mirth.connect.connectors.file;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mirth.connect.connectors.file.filesystems.FileSystemConnection;
import com.mirth.connect.connectors.file.filesystems.FileSystemConnectionFactory;

/**
* Checks how FileConnector uses its connection pool when a File Writer does not keep connections
* open, so that a commons-pool2 upgrade cannot change it unnoticed.
*/
public class FileConnectorConnectionPoolTest {

private CountingFactory factory;
private FileConnector fileConnector;
private FileSystemConnectionOptions options;

@Before
public void setUp() {
options = new FileSystemConnectionOptions(false, "user", "password", null);
factory = new CountingFactory(options);

FileDispatcherProperties properties = new FileDispatcherProperties();
properties.setKeepConnectionOpen(false);

fileConnector = new FileConnector("channelId", properties, null) {
@Override
protected FileSystemConnectionFactory getFileSystemConnectionFactory(FileSystemConnectionOptions fileSystemOptions) {
return factory;
}
};
}

@After
public void tearDown() throws Exception {
fileConnector.doStop();
}

@Test
public void destroyConnectionDoesNotOpenAnotherConnection() throws Exception {
FileSystemConnection connection = fileConnector.getConnection(options);
fileConnector.destroyConnection(connection, options);

assertEquals(1, factory.created.get());
verify(connection).destroy();
}

@Test
public void destroyConnectionForgetsConnectionWhenFactoryFails() throws Exception {
FileSystemConnection connection = fileConnector.getConnection(options);
factory.fail.set(true);

// Creating a connection after destroying one would fail, as when the server is down
try {
fileConnector.destroyConnection(connection, options);
} catch (Exception e) {
// Only the state of the connector matters here
}

// A forgotten connection is not disconnected again
fileConnector.disconnect();
verify(connection, never()).disconnect();
}

@Test
public void getConnectionReplacesDisconnectedConnection() throws Exception {
factory.disconnectNext.set(true);

FileSystemConnection connection = fileConnector.getConnection(options);

assertEquals(2, factory.created.get());
assertSame(factory.last, connection);
verify(factory.first).destroy();

// Only the replacement is tracked as an open connection
fileConnector.disconnect();
verify(factory.first, never()).disconnect();
verify(connection).disconnect();
}

private static class CountingFactory extends FileSystemConnectionFactory {
final AtomicInteger created = new AtomicInteger();
final AtomicBoolean fail = new AtomicBoolean();
final AtomicBoolean disconnectNext = new AtomicBoolean();
volatile FileSystemConnection first;
volatile FileSystemConnection last;

CountingFactory(FileSystemConnectionOptions options) {
super(FileScheme.FTP, options, "localhost", 21, false, false, 0);
}

@Override
public String getPoolKey() {
return "test";
}

@Override
public PooledObject<FileSystemConnection> makeObject() throws Exception {
if (fail.get()) {
throw new IOException("Connection refused");
}
FileSystemConnection connection = mock(FileSystemConnection.class);
when(connection.isConnected()).thenReturn(!disconnectNext.getAndSet(false));
when(connection.isValid()).thenReturn(true);
if (first == null) {
first = connection;
}
last = connection;
created.incrementAndGet();
return new DefaultPooledObject<>(connection);
}
}
}
Loading