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
This commit is contained in:
mdb
2008-05-18 11:11:28 +00:00
parent 6ba1f81cbd
commit c51b652abd
3 changed files with 91 additions and 43 deletions
@@ -34,6 +34,7 @@ public abstract class RepositoryListenerUnit<T> extends RepositoryUnit
*/
public RepositoryListenerUnit (ResultListener<T> listener)
{
super(String.valueOf(listener));
_listener = listener;
}
@@ -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;
}
@@ -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;
}