diff --git a/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java b/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java index 59ad1ea..c999d78 100644 --- a/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java +++ b/src/org/linkeddatafragments/fragments/ILinkedDataFragment.java @@ -1,5 +1,7 @@ package org.linkeddatafragments.fragments; +import java.io.Closeable; + import com.hp.hpl.jena.rdf.model.StmtIterator; /** @@ -7,7 +9,7 @@ * * @author Olaf Hartig */ -public interface ILinkedDataFragment +public interface ILinkedDataFragment extends Closeable { /** * Returns an iterator over the RDF data of this fragment (possibly only diff --git a/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java b/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java index 9da713c..28faf25 100644 --- a/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java +++ b/src/org/linkeddatafragments/fragments/LinkedDataFragmentBase.java @@ -34,6 +34,14 @@ protected LinkedDataFragmentBase( final String fragmentURL, this.isLastPage = isLastPage; } + /** + * Does nothing. May be overridden by subclasses that hold some objects + * that need to be closed (such as iterators from the underlying data + * source). + */ + @Override + public void close() {} + @Override public boolean isPageOnly() { return true; diff --git a/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java b/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java index 1ef06e2..fc58bea 100644 --- a/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java +++ b/src/org/linkeddatafragments/servlet/LinkedDataFragmentServlet.java @@ -138,6 +138,7 @@ private IDataSource getDataSource(HttpServletRequest request) throws DataSourceN @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { + ILinkedDataFragment fragment = null; try { final IDataSource dataSource = getDataSource( request ); @@ -145,9 +146,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro dataSource.getRequestParser() .parseIntoFragmentRequest( request, config ); - final ILinkedDataFragment fragment = - dataSource.getRequestProcessor() - .createRequestedFragment( ldfRequest ); + fragment = dataSource.getRequestProcessor() + .createRequestedFragment( ldfRequest ); final Model output = ModelFactory.createDefaultModel(); output.setNsPrefixes(config.getPrefixes()); @@ -176,6 +176,17 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro throw new ServletException(ex1); } } + finally { + // close the fragment + if ( fragment != null ) { + try { + fragment.close(); + } + catch ( Exception e ) { + // ignore + } + } + } } }