From e3fff09c76810db982739eee599588a42bf575bb Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 21 Jun 2012 15:31:13 +0200 Subject: [PATCH] added close method to repository service --- .../scm/repository/api/RepositoryService.java | 45 ++++++++++++++++++- .../spi/RepositoryServiceProvider.java | 24 +++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java index 8c824a1f5f..0d72f7c908 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java @@ -43,6 +43,11 @@ import sonia.scm.repository.PreProcessorUtil; import sonia.scm.repository.Repository; import sonia.scm.repository.spi.RepositoryServiceProvider; +//~--- JDK imports ------------------------------------------------------------ + +import java.io.Closeable; +import java.io.IOException; + /** * From the {@link RepositoryService} it is possible to access all commands for * a single {@link Repository}. The {@link RepositoryService} is only access @@ -53,12 +58,17 @@ import sonia.scm.repository.spi.RepositoryServiceProvider; * the command is not supported the method will trow a * {@link CommandNotSupportedException}. It is possible to check if the command * is supported by the {@link RepositoryService} with the - * {@link RepositoryService#isSupported(Command)} method. + * {@link RepositoryService#isSupported(Command)} method.
+ *
+ * + * Warning: You should always close the connection to the repository + * after work is finished. For closing the connection to the repository use the + * {@link #close()} method. * * @author Sebastian Sdorra * @since 1.17 */ -public final class RepositoryService +public final class RepositoryService implements Closeable { /** @@ -88,6 +98,37 @@ public final class RepositoryService this.preProcessorUtil = preProcessorUtil; } + //~--- methods -------------------------------------------------------------- + + /** + * Closes the connection to the repository and releases all locks + * and resources. This method should be called in a finally block e.g.: + * + *

+   * RepositoryService service = null;
+   * try {
+   *   service = factory.create("repositoryId");
+   *   // do something with the service
+   * } finally {
+   *   if ( service != null ){
+   *     service.close();
+   *   }
+   * }
+   * 
+ */ + @Override + public void close() + { + try + { + provider.close(); + } + catch (IOException ex) + { + logger.error("cound not close repository service provider", ex); + } + } + //~--- get methods ---------------------------------------------------------- /** diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java b/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java index 6944c50e29..8fbc820d04 100644 --- a/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java +++ b/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java @@ -40,6 +40,9 @@ import sonia.scm.repository.api.CommandNotSupportedException; //~--- JDK imports ------------------------------------------------------------ +import java.io.Closeable; +import java.io.IOException; + import java.util.Set; /** @@ -47,7 +50,7 @@ import java.util.Set; * @author Sebastian Sdorra * @since 1.17 */ -public abstract class RepositoryServiceProvider +public abstract class RepositoryServiceProvider implements Closeable { /** @@ -58,6 +61,25 @@ public abstract class RepositoryServiceProvider */ public abstract Set getSupportedCommands(); + //~--- methods -------------------------------------------------------------- + + /** + * The default implementation of this method does nothing. If you need to + * free resources, close connections or release locks than you have to + * override this method. + * + * + * @throws IOException + */ + @Override + public void close() throws IOException + { + + // should be implmentented from a service provider + } + + //~--- get methods ---------------------------------------------------------- + /** * Method description *