From c51b652abd0796a8b40f079ab516c1664cd745ab Mon Sep 17 00:00:00 2001 From: mdb Date: Sun, 18 May 2008 11:11:28 +0000 Subject: [PATCH] Created a unit for when we just want to write something to the database and do nothing with the results. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2305 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../jdbc/RepositoryListenerUnit.java | 1 + .../com/samskivert/jdbc/RepositoryUnit.java | 48 ++--------- .../com/samskivert/jdbc/WriteOnlyUnit.java | 85 +++++++++++++++++++ 3 files changed, 91 insertions(+), 43 deletions(-) create mode 100644 src/java/com/samskivert/jdbc/WriteOnlyUnit.java diff --git a/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java b/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java index 7435c5e8..839d8f0b 100644 --- a/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java +++ b/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java @@ -34,6 +34,7 @@ public abstract class RepositoryListenerUnit extends RepositoryUnit */ public RepositoryListenerUnit (ResultListener listener) { + super(String.valueOf(listener)); _listener = listener; } diff --git a/src/java/com/samskivert/jdbc/RepositoryUnit.java b/src/java/com/samskivert/jdbc/RepositoryUnit.java index 2a9c425c..6a24d508 100644 --- a/src/java/com/samskivert/jdbc/RepositoryUnit.java +++ b/src/java/com/samskivert/jdbc/RepositoryUnit.java @@ -20,23 +20,12 @@ package com.samskivert.jdbc; -import java.util.logging.Level; - -import com.samskivert.Log; -import com.samskivert.io.PersistenceException; -import com.samskivert.util.Invoker; - /** - * Extends the {@link com.samskivert.util.Invoker.Unit} and specializes it for doing database - * repository manipulation. + * Extends {@link WriteOnlyUnit} and allows for processing of results back on the event processing + * thread. */ -public abstract class RepositoryUnit extends Invoker.Unit +public abstract class RepositoryUnit extends WriteOnlyUnit { - /** The default constructor. */ - public RepositoryUnit () - { - } - /** * Create a RepositoryUnit which will report the supplied name in {@link #toString}. */ @@ -45,7 +34,7 @@ public abstract class RepositoryUnit extends Invoker.Unit super(name); } - // from abstract Invoker.Unit + @Override // from WriteOnlyUnit public boolean invoke () { try { @@ -56,7 +45,7 @@ public abstract class RepositoryUnit extends Invoker.Unit return true; } - @Override // from Invoker.Unit + @Override // from WriteOnlyUnit public void handleResult () { if (_error != null) { @@ -66,35 +55,8 @@ public abstract class RepositoryUnit extends Invoker.Unit } } - /** - * Called to perform our persistent actions. - */ - public abstract void invokePersist () - throws Exception; - /** * Called if our persistent actions have succeeded, back on the non-invoker thread. */ public abstract void handleSuccess (); - - /** - * Called if our persistent actions failed, back on the non-invoker thread. Note that this may - * be either an {@link Exception} thrown by {@link #invokePersist} or a {@link - * RuntimeException} thrown by same. The default implementation logs an error message and a - * stack trace. - */ - public void handleFailure (Exception pe) - { - Log.log.log(Level.WARNING, getFailureMessage(), pe); - } - - /** - * Returns the error message to be logged if {@link #invokePersist} throws an exception. - */ - protected String getFailureMessage () - { - return this + " failed."; - } - - protected Exception _error; } diff --git a/src/java/com/samskivert/jdbc/WriteOnlyUnit.java b/src/java/com/samskivert/jdbc/WriteOnlyUnit.java new file mode 100644 index 00000000..a60556fb --- /dev/null +++ b/src/java/com/samskivert/jdbc/WriteOnlyUnit.java @@ -0,0 +1,85 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2008 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc; + +import java.util.logging.Level; + +import com.samskivert.util.Invoker; + +import static com.samskivert.Log.log; + +/** + * Extends the {@link Invoker.Unit} and specializes it for writing to a database repository. + */ +public abstract class WriteOnlyUnit extends Invoker.Unit +{ + /** + * Creates a unit which will report the supplied name in {@link #toString} and in the event of + * failure. + */ + public WriteOnlyUnit (String name) + { + super(name); + } + + // from abstract Invoker.Unit + public boolean invoke () + { + try { + invokePersist(); + return false; + } catch (Exception pe) { + _error = pe; + return true; + } + } + + @Override // from Invoker.Unit + public void handleResult () + { + handleFailure(_error); + } + + /** + * Called to perform our persistent actions. + */ + public abstract void invokePersist () + throws Exception; + + /** + * Called if our persistent actions failed, back on the non-invoker thread. The default + * implementation logs an error message and a stack trace. + */ + public void handleFailure (Exception e) + { + log.log(Level.WARNING, getFailureMessage(), e); + } + + /** + * Returns the error message to be logged if {@link #invokePersist} throws an exception. + */ + protected String getFailureMessage () + { + return this + " failed."; + } + + protected Exception _error; +}