diff --git a/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java b/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java new file mode 100644 index 00000000..64118137 --- /dev/null +++ b/src/java/com/samskivert/jdbc/RepositoryListenerUnit.java @@ -0,0 +1,66 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2006 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 com.samskivert.io.PersistenceException; +import com.samskivert.util.ResultListener; + +/** + * Extends the {@link RepositoryUnit} and integrates with a {@link + * ResultListener}. + */ +public abstract class RepositoryListenerUnit extends RepositoryUnit +{ + /** + * Creates a repository listener unit that will report its results to the + * supplied result listener. + */ + public RepositoryListenerUnit (ResultListener listener) + { + _listener = listener; + } + + /** + * Called to perform our persistent action and generate our result. + */ + public abstract T invokePersistResult () + throws PersistenceException; + + @Override // from RepositoryUnit + public void invokePersist () + throws PersistenceException + { + _result = invokePersistResult(); + } + + @Override // from RepositoryUnit + public void handleSuccess () + { + _listener.requestCompleted(_result); + } + + @Override // from RepositoryUnit + public void handleFailure (PersistenceException pe) + { + _listener.requestFailed(pe); + } + + protected ResultListener _listener; + protected T _result; +} diff --git a/src/java/com/samskivert/jdbc/RepositoryUnit.java b/src/java/com/samskivert/jdbc/RepositoryUnit.java new file mode 100644 index 00000000..7ca3edd5 --- /dev/null +++ b/src/java/com/samskivert/jdbc/RepositoryUnit.java @@ -0,0 +1,69 @@ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2006 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 com.samskivert.io.PersistenceException; +import com.samskivert.util.Invoker; + +/** + * Extends the {@link Invoker#Unit} and specializes it for doing database + * repository manipulation. + */ +public abstract class RepositoryUnit extends Invoker.Unit +{ + // from abstract Invoker.Unit + public boolean invoke () + { + try { + invokePersist(); + } catch (PersistenceException pe) { + _error = pe; + } + return true; + } + + @Override // from Invoker.Unit + public void handleResult () + { + if (_error != null) { + handleFailure(_error); + } else { + handleSuccess(); + } + } + + /** + * Called to perform our persistent actions. + */ + public abstract void invokePersist () + throws PersistenceException; + + /** + * 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. + */ + public abstract void handleFailure (PersistenceException pe); + + protected PersistenceException _error; +}