Skip to content

Commit

Permalink
Ensure AssetCursors are closed when written
Browse files Browse the repository at this point in the history
Make AssetCursor implement Closable, and then read the AssetCursor
within a try-with-resources so that it gets closed.
  • Loading branch information
Azquelt committed Feb 13, 2017
1 parent eee6c5a commit c331e2c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
@Override
public void writeTo(AssetCursor cursor, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
MAPPER.writeValue(entityStream, cursor);
try (AssetCursor cursorToBeClosed = cursor) {
MAPPER.writeValue(entityStream, cursorToBeClosed);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package com.ibm.ws.lars.rest.model;

import java.io.Closeable;
import java.util.Iterator;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand All @@ -32,7 +33,7 @@
* returned.
*/
@JsonSerialize(as = Iterator.class)
public interface AssetCursor extends Iterator<Asset> {
public interface AssetCursor extends Iterator<Asset>, Closeable {

/**
* Get the total number of assets which will be returned from this cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public void addOperation(AssetOperation op) {
operations.add(op);
}

@Override
public void close() {
cursor.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package com.ibm.ws.lars.rest;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -61,4 +62,10 @@ public void addOperation(AssetOperation filter) {

}

/** {@inheritDoc} */
@Override
public void close() throws IOException {
// Do nothing
}

}

0 comments on commit c331e2c

Please sign in to comment.