Skip to content

Commit 872133c

Browse files
committed
[bugfix] Prevent memory leaks from the XML:DB API (and by extension XML-RPC API) by ensuring that resources used in queries and retrieving documents are correctly cleaned up.
See xmldb-org/xmldb-api#251
1 parent 5de1e3a commit 872133c

226 files changed

Lines changed: 17046 additions & 11654 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
set -e
2929

30+
# Uncomment below to assist in debugging
31+
#set -x
32+
3033
TARGET="useage"
3134
DEBUG=false
3235
OFFLINE=false
@@ -173,7 +176,7 @@ fi
173176

174177
if [ "${TARGET}" == "format-poms" ]; then
175178
SAXON="${HOME}/.m2/repository/net/sf/saxon/Saxon-HE/9.9.1-8/Saxon-HE-9.9.1-8.jar"
176-
POMS="$(find . -name pom.xml)"
179+
POMS="$(find $SCRIPT_DIR -name pom.xml)"
177180
for pom in $POMS; do
178181

179182
echo -n "Formatting ${pom} ... "

elemental-parent/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@
491491
</dependency>
492492
</dependencies>
493493
<configuration>
494-
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory" />
494+
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
495495
<forkCount>${surefire.forkCount}</forkCount>
496496
<reuseForks>${surefire.reuseForks}</reuseForks>
497497
<argLine>@{jacocoArgLine} -XX:+IgnoreUnrecognizedVMOptions --illegal-access=debug --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED -Dfile.encoding=${project.build.sourceEncoding}</argLine>
@@ -814,4 +814,4 @@
814814
</profile>
815815
</profiles>
816816

817-
</project>
817+
</project>

exist-ant/pom.xml

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
<dependency>
107107
<groupId>com.google.code.findbugs</groupId>
108108
<artifactId>jsr305</artifactId>
109-
<scope>test</scope>
110109
</dependency>
111110

112111
<dependency>
@@ -184,7 +183,34 @@
184183
<include>pom.xml</include>
185184
<include>src/test/resources-filtered/conf.xml</include>
186185
<include>src/test/resources/log4j2.xml</include>
186+
<include>src/main/java/org/exist/ant/AbstractXMLDBTask.java</include>
187+
<include>src/main/java/org/exist/ant/AddGroupTask.java</include>
188+
<include>src/main/java/org/exist/ant/AddUserTask.java</include>
189+
<include>src/main/java/org/exist/ant/ChmodTask.java</include>
190+
<include>src/main/java/org/exist/ant/ChownTask.java</include>
191+
<include>src/test/java/org/exist/ant/FileTaskTest.java</include>
192+
<include>src/main/java/org/exist/ant/ListGroupsTask.java</include>
193+
<include>src/main/java/org/exist/ant/ListUsersTask.java</include>
194+
<include>src/main/java/org/exist/ant/LockResourceTask.java</include>
195+
<include>src/main/java/org/exist/ant/RemoveGroupTask.java</include>
196+
<include>src/main/java/org/exist/ant/RemoveUserTask.java</include>
197+
<include>src/main/java/org/exist/ant/RestoreTask.java</include>
198+
<include>src/test/java/org/exist/ant/ServerTaskTest.java</include>
199+
<include>src/main/java/org/exist/ant/UserPasswordTask.java</include>
200+
<include>src/main/java/org/exist/ant/UserTask.java</include>
201+
<include>src/main/java/org/exist/ant/XMLDBCopyTask.java</include>
202+
<include>src/main/java/org/exist/ant/XMLDBCreateTask.java</include>
203+
<include>src/main/java/org/exist/ant/XMLDBExistTask.java</include>
204+
<include>src/main/java/org/exist/ant/XMLDBExtractTask.java</include>
205+
<include>src/main/java/org/exist/ant/XMLDBListTask.java</include>
206+
<include>src/main/java/org/exist/ant/XMLDBMoveTask.java</include>
207+
<include>src/main/java/org/exist/ant/XMLDBRemoveTask.java</include>
208+
<include>src/main/java/org/exist/ant/XMLDBShutdownTask.java</include>
187209
<include>src/main/java/org/exist/ant/XMLDBStoreTask.java</include>
210+
<include>src/test/java/org/exist/ant/XmldbTaskTest.java</include>
211+
<include>src/main/java/org/exist/ant/XMLDBXPathTask.java</include>
212+
<include>src/main/java/org/exist/ant/XMLDBXQueryTask.java</include>
213+
<include>src/main/java/org/exist/ant/XMLDBXUpdateTask.java</include>
188214
</includes>
189215
</licenseSet>
190216

@@ -202,7 +228,34 @@
202228
<exclude>xquery-license-style.xml</exclude>
203229
<exclude>src/test/resources-filtered/conf.xml</exclude>
204230
<exclude>src/test/resources/log4j2.xml</exclude>
231+
<exclude>src/main/java/org/exist/ant/AbstractXMLDBTask.java</exclude>
232+
<exclude>src/main/java/org/exist/ant/AddGroupTask.java</exclude>
233+
<exclude>src/main/java/org/exist/ant/AddUserTask.java</exclude>
234+
<exclude>src/main/java/org/exist/ant/ChmodTask.java</exclude>
235+
<exclude>src/main/java/org/exist/ant/ChownTask.java</exclude>
236+
<exclude>src/test/java/org/exist/ant/FileTaskTest.java</exclude>
237+
<exclude>src/main/java/org/exist/ant/ListGroupsTask.java</exclude>
238+
<exclude>src/main/java/org/exist/ant/ListUsersTask.java</exclude>
239+
<exclude>src/main/java/org/exist/ant/LockResourceTask.java</exclude>
240+
<exclude>src/main/java/org/exist/ant/RemoveGroupTask.java</exclude>
241+
<exclude>src/main/java/org/exist/ant/RemoveUserTask.java</exclude>
242+
<exclude>src/main/java/org/exist/ant/RestoreTask.java</exclude>
243+
<exclude>src/test/java/org/exist/ant/ServerTaskTest.java</exclude>
244+
<exclude>src/main/java/org/exist/ant/UserPasswordTask.java</exclude>
245+
<exclude>src/main/java/org/exist/ant/UserTask.java</exclude>
246+
<exclude>src/main/java/org/exist/ant/XMLDBCopyTask.java</exclude>
247+
<exclude>src/main/java/org/exist/ant/XMLDBCreateTask.java</exclude>
248+
<exclude>src/main/java/org/exist/ant/XMLDBExistTask.java</exclude>
249+
<exclude>src/main/java/org/exist/ant/XMLDBExtractTask.java</exclude>
250+
<exclude>src/main/java/org/exist/ant/XMLDBListTask.java</exclude>
251+
<exclude>src/main/java/org/exist/ant/XMLDBMoveTask.java</exclude>
252+
<exclude>src/main/java/org/exist/ant/XMLDBRemoveTask.java</exclude>
253+
<exclude>src/main/java/org/exist/ant/XMLDBShutdownTask.java</exclude>
205254
<exclude>src/main/java/org/exist/ant/XMLDBStoreTask.java</exclude>
255+
<exclude>src/test/java/org/exist/ant/XmldbTaskTest.java</exclude>
256+
<exclude>src/main/java/org/exist/ant/XMLDBXPathTask.java</exclude>
257+
<exclude>src/main/java/org/exist/ant/XMLDBXQueryTask.java</exclude>
258+
<exclude>src/main/java/org/exist/ant/XMLDBXUpdateTask.java</exclude>
206259
</excludes>
207260
</licenseSet>
208261
</licenseSets>

exist-ant/src/main/java/org/exist/ant/AbstractXMLDBTask.java

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
* admin@evolvedbinary.com
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -37,6 +61,7 @@
3761
import org.xmldb.api.base.XMLDBException;
3862
import org.xmldb.api.modules.CollectionManagementService;
3963

64+
import javax.annotation.Nullable;
4065
import java.util.StringTokenizer;
4166

4267

@@ -188,79 +213,67 @@ protected void registerDatabase() throws BuildException
188213
}
189214

190215

191-
protected final Collection mkcol(final Collection rootCollection, final String baseURI, String path, final String relPath ) throws XMLDBException
192-
{
193-
CollectionManagementService mgtService;
194-
Collection current = rootCollection;
195-
Collection collection;
196-
String token;
197-
198-
///TODO : use dedicated function in XmldbURI
199-
final StringTokenizer tokenizer = new StringTokenizer( relPath, "/" );
200-
201-
while( tokenizer.hasMoreTokens() ) {
202-
203-
token = tokenizer.nextToken();
216+
protected final Collection mkcol(Collection collection, final String baseURI, String path, final String relPath) throws XMLDBException {
217+
final StringTokenizer tokenizer = new StringTokenizer(relPath, "/");
204218

205-
if( path != null ) {
219+
while (tokenizer.hasMoreTokens()) {
220+
final String token = tokenizer.nextToken();
221+
if (path != null) {
206222
path = path + "/" + token;
207223
} else {
208224
path = "/" + token;
209225
}
210226

211-
log( "Get collection " + baseURI + path, Project.MSG_DEBUG );
212-
collection = DatabaseManager.getCollection( baseURI + path, user, password );
213-
214-
if( collection == null ) {
215-
log( "Create collection management service for collection " + current.getName(), Project.MSG_DEBUG );
216-
mgtService = (CollectionManagementService)current.getService( "CollectionManagementService", "1.0" );
217-
log( "Create child collection " + token, Project.MSG_DEBUG );
218-
current = mgtService.createCollection( token );
219-
log( "Created collection " + current.getName() + '.', Project.MSG_DEBUG );
227+
Collection child = DatabaseManager.getCollection(baseURI + path, user, password);
228+
if (child == null) {
229+
final CollectionManagementService mgtService = (CollectionManagementService) collection.getService("CollectionManagementService", "1.0");
230+
log("Create child collection " + token, Project.MSG_DEBUG);
231+
child = mgtService.createCollection(token);
232+
log("Created collection " + child.getName() + '.', Project.MSG_DEBUG);
233+
}
220234

221-
} else {
222-
current = collection;
235+
try {
236+
// close the parent collection
237+
collection.close();
238+
} catch (final XMLDBException e) {
239+
// no-op
223240
}
241+
242+
collection = child;
224243
}
225-
return( current );
244+
245+
return collection;
226246
}
227247

228248

229-
protected final void setPermissions(final Resource res ) throws BuildException
230-
{
231-
Collection base = null;
232-
UserManagementService service = null;
233-
234-
if( uri == null ) {
235-
throw( new BuildException( "you have to specify an XMLDB collection URI" ) );
249+
protected final void setPermissions(final Resource res) throws BuildException {
250+
if (uri == null) {
251+
throw new BuildException( "you have to specify an XMLDB collection URI");
236252
}
237253

238-
try {
239-
log( "Get base collection: " + uri, Project.MSG_DEBUG );
240-
base = DatabaseManager.getCollection( uri, user, password );
254+
log("Get base collection: " + uri, Project.MSG_DEBUG);
255+
try (final Collection base = DatabaseManager.getCollection(uri, user, password)) {
241256

242-
if( base == null ) {
257+
if (base == null ) {
243258
final String msg = "Collection " + uri + " could not be found.";
244259

245-
if( failonerror ) {
246-
throw( new BuildException( msg ) );
260+
if (failonerror) {
261+
throw new BuildException(msg);
247262
} else {
248-
log( msg, Project.MSG_ERR );
263+
log(msg, Project.MSG_ERR);
249264
}
250265
} else {
251-
service = (UserManagementService)base.getService( "UserManagementService", "1.0" );
252-
253-
setPermissions( res, service );
266+
final UserManagementService service = (UserManagementService) base.getService( "UserManagementService", "1.0" );
267+
setPermissions(res, service);
254268
}
255269

256-
}
257-
catch( final XMLDBException e ) {
270+
} catch (final XMLDBException e) {
258271
final String msg = "XMLDB exception caught: " + e.getMessage();
259272

260-
if( failonerror ) {
261-
throw( new BuildException( msg, e ) );
273+
if (failonerror) {
274+
throw new BuildException(msg, e);
262275
} else {
263-
log( msg, e, Project.MSG_ERR );
276+
log(msg, e, Project.MSG_ERR);
264277
}
265278
}
266279
}
@@ -285,7 +298,7 @@ protected final void setPermissions(final Collection col ) throws BuildException
285298
}
286299

287300

288-
protected final void setPermissions(final Resource res, final UserManagementService service ) throws BuildException
301+
protected final void setPermissions(@Nullable final Resource res, final UserManagementService service ) throws BuildException
289302
{
290303
try {
291304
if( permissions != null ) {

exist-ant/src/main/java/org/exist/ant/AddGroupTask.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
* admin@evolvedbinary.com
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -34,12 +58,8 @@ public class AddGroupTask extends UserTask
3458
{
3559
private String name;
3660

37-
/* (non-Javadoc)
38-
* @see org.apache.tools.ant.Task#execute()
39-
*/
40-
public void execute() throws BuildException
41-
{
42-
super.execute();
61+
@Override
62+
public void executeUserTask() throws BuildException {
4363

4464
if( name == null ) {
4565
throw( new BuildException( "Must specify a group name" ) );
@@ -49,7 +69,7 @@ public void execute() throws BuildException
4969
final GroupAider group = new GroupAider( name );
5070

5171
log( "Adding group " + name, Project.MSG_INFO );
52-
service.addGroup( group );
72+
getService().addGroup( group );
5373

5474
}
5575
catch( final XMLDBException e ) {

exist-ant/src/main/java/org/exist/ant/AddUserTask.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
* admin@evolvedbinary.com
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -37,12 +61,8 @@ public class AddUserTask extends UserTask {
3761
private String primaryGroup;
3862
private String secret;
3963

40-
/* (non-Javadoc)
41-
* @see org.apache.tools.ant.Task#execute()
42-
*/
43-
public void execute() throws BuildException
44-
{
45-
super.execute();
64+
@Override
65+
public void executeUserTask() throws BuildException {
4666

4767
if( name == null ) {
4868
throw( new BuildException( "Must specify at leat a user name" ) );
@@ -60,7 +80,7 @@ public void execute() throws BuildException
6080
}
6181

6282
log( "Adding user " + name, Project.MSG_INFO );
63-
service.addAccount( usr );
83+
getService().addAccount( usr );
6484

6585
}
6686
catch( final XMLDBException e ) {

0 commit comments

Comments
 (0)