Skip to content

Commit

Permalink
[refactor] Call through sync instead of manual adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Nikonov authored and adamretter committed Oct 7, 2016
1 parent 40ba0d5 commit a8a6025
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 46 deletions.
15 changes: 6 additions & 9 deletions extensions/expath/src/org/expath/exist/SendRequestFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,12 @@ private EXistResult sendOnceWithAuth(final URI uri, final HttpRequest request, f
}

private void registerConnectionWithContext(final HttpConnection conn) {
context.registerCleanupTask(new XQueryContext.CleanupTask() {
@Override
public void cleanup(final XQueryContext context) {
try {
conn.disconnect();
} catch(final HttpClientException hce) {
logger.error(hce.getMessage(), hce);
}
context.registerCleanupTask(context -> {
try {
conn.disconnect();
} catch(final HttpClientException hce) {
logger.error(hce.getMessage(), hce);
}
});
}
}
}
56 changes: 20 additions & 36 deletions src/org/exist/xquery/XQueryContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import antlr.collections.AST;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer;

/**
* The current XQuery execution context. Contains the static as well as the dynamic
Expand Down Expand Up @@ -3499,36 +3500,23 @@ public boolean requireDebugMode()

@Override
public void registerBinaryValueInstance(final BinaryValue binaryValue) {
if(binaryValueInstances == null) {
binaryValueInstances = new ArrayList<>();
}

if(cleanupTasks.isEmpty() || !cleanupTasks.stream().filter(ct -> ct instanceof BinaryValueCleanupTask).findFirst().isPresent()) {
cleanupTasks.add(new BinaryValueCleanupTask());
}

binaryValueInstances.add(binaryValue);
}

/**
* Cleanup Task which is responsible for relasing the streams
* of any {@link BinaryValue} which have been used during
* query execution
*/
private static class BinaryValueCleanupTask implements CleanupTask {
@Override
public void cleanup(final XQueryContext context) {
if (context.binaryValueInstances != null) {
for (final BinaryValue bv : context.binaryValueInstances) {
try {
bv.close();
} catch (final IOException ioe) {
LOG.error("Unable to close binary value: " + ioe.getMessage(), ioe);
if(this.binaryValueInstances == null) {
this.binaryValueInstances = new ArrayList<>();
registerCleanupTask(context -> {
if (context.binaryValueInstances != null) {
for (final BinaryValue bv : context.binaryValueInstances) {
try {
bv.close();
} catch (final IOException ioe) {
LOG.error("Unable to close binary value: " + ioe.getMessage(), ioe);
}
}
context.binaryValueInstances.clear();
}
context.binaryValueInstances.clear();
}
});
}

this.binaryValueInstances.add(binaryValue);
}

@Override
Expand Down Expand Up @@ -3609,21 +3597,17 @@ public void debug() {

}

private final List<CleanupTask> cleanupTasks = new ArrayList<>();
private List<Consumer<XQueryContext>> cleanupTasks = new CopyOnWriteArrayList<>();

public void registerCleanupTask(final CleanupTask cleanupTask) {
public void registerCleanupTask(final Consumer<XQueryContext> cleanupTask) {
cleanupTasks.add(cleanupTask);
}

public interface CleanupTask {
void cleanup(final XQueryContext context);
}


@Override
public void runCleanupTasks() {
for(final CleanupTask cleanupTask : cleanupTasks) {
for(final Consumer<XQueryContext> cleanupTask : cleanupTasks) {
try {
cleanupTask.cleanup(this);
cleanupTask.accept(this);
} catch(final Throwable t) {
LOG.error("Cleaning up XQueryContext: Ignoring: " + t.getMessage(), t);
}
Expand Down
3 changes: 2 additions & 1 deletion test/src/org/exist/xquery/XQueryContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Consumer;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -172,7 +173,7 @@ private int countBinaryValueInstances(final XQueryContext context) throws NoSuch
private int countCleanupTasks(final XQueryContext context) throws NoSuchFieldException, IllegalAccessException {
final Field fldCleanupTasks = context.getClass().getDeclaredField("cleanupTasks");
fldCleanupTasks.setAccessible(true);
final List<XQueryContext.CleanupTask> cleanupTasks = (List<XQueryContext.CleanupTask>)fldCleanupTasks.get(context);
final List<Consumer<XQueryContext>> cleanupTasks = (List<Consumer<XQueryContext>>)fldCleanupTasks.get(context);
return cleanupTasks.size();
}
}

0 comments on commit a8a6025

Please sign in to comment.