Permalink
Browse files

Type-safe Prevayler

  • Loading branch information...
1 parent 5d07d49 commit 844f2c42aa4f439596a3d3874043b3dff2db0146 @TheNavigateur TheNavigateur committed Oct 28, 2011
Showing with 236 additions and 214 deletions.
  1. +13 −0 CHANGES.txt
  2. +5 −4 core/src/main/java/org/prevayler/SureTransactionWithQuery.java
  3. +2 −2 core/src/main/java/org/prevayler/Transaction.java
  4. +3 −3 core/src/main/java/org/prevayler/TransactionWithQuery.java
  5. +1 −1 core/src/main/java/org/prevayler/foundation/serialization/JavaSerializer.java
  6. +2 −1 core/src/main/java/org/prevayler/foundation/serialization/Serializer.java
  7. +2 −1 core/src/main/java/org/prevayler/implementation/Capsule.java
  8. +9 −8 core/src/main/java/org/prevayler/implementation/PrevalentSystemGuard.java
  9. +13 −12 core/src/main/java/org/prevayler/implementation/PrevaylerImpl.java
  10. +5 −4 core/src/main/java/org/prevayler/implementation/TransactionCapsule.java
  11. +7 −6 core/src/main/java/org/prevayler/implementation/TransactionWithQueryCapsule.java
  12. +8 −6 core/src/main/java/org/prevayler/implementation/publishing/censorship/StrictTransactionCensor.java
  13. +13 −12 core/src/main/java/org/prevayler/implementation/snapshot/GenericSnapshotManager.java
  14. +4 −3 core/src/main/java/org/prevayler/implementation/snapshot/NullSnapshotManager.java
  15. +1 −1 demos/demo1/src/main/java/org/prevayler/demos/demo1/Main.java
  16. +3 −3 demos/demo1/src/main/java/org/prevayler/demos/demo1/NumberStorageTransaction.java
  17. +3 −3 demos/demo1/src/main/java/org/prevayler/demos/demo1/PrimeCalculator.java
  18. +1 −1 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/Main.java
  19. +4 −5 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/commands/AddProject.java
  20. +3 −4 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/commands/AddTask.java
  21. +3 −3 demos/scalability/src/main/java/org/prevayler/demos/scalability/prevayler/AllRecordsReplacement.java
  22. +1 −1 demos/scalability/src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerQuerySubject.java
  23. +1 −1 ...lability/src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerTransactionSubject.java
  24. +3 −3 demos/scalability/src/main/java/org/prevayler/demos/scalability/prevayler/TestTransaction.java
  25. +2 −2 demos/tutorial/src/main/java/org/prevayler/baptism/BadTransaction.java
  26. +4 −12 demos/tutorial/src/main/java/org/prevayler/baptism/GoodTransaction.java
  27. +4 −6 demos/tutorial/src/main/java/org/prevayler/tutorial/AddTask.java
  28. +5 −5 demos/tutorial/src/main/java/org/prevayler/tutorial/Main.java
  29. +4 −4 demos/tutorial/src/main/java/org/prevayler/tutorial/RemoveTask.java
  30. +3 −2 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyQuery.java
  31. +3 −2 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyTransaction.java
  32. +3 −2 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyTransactionWithQuery.java
  33. +5 −5 extras/memento/src/main/java/org/prevayler/demos/memento/TestErrorRecoveryWithMementos.java
  34. +3 −3 extras/memento/src/main/java/org/prevayler/demos/memento/commands/AccountCreation.java
  35. +3 −3 extras/memento/src/main/java/org/prevayler/demos/memento/commands/AccountDeletion.java
  36. +3 −3 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Deposit.java
  37. +4 −3 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Transfer.java
  38. +3 −3 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Withdrawal.java
  39. +4 −2 extras/memento/src/main/java/org/prevayler/util/memento/MementoManagerCommand.java
  40. +6 −3 extras/memento/src/main/java/org/prevayler/util/memento/MementoTransaction.java
  41. +22 −22 factory/src/main/java/org/prevayler/PrevaylerFactory.java
  42. +13 −13 spikes/cluster/src/main/org/prevayler/cluster/ClusteredPrevayler.java
  43. +4 −4 spikes/cluster/src/main/org/prevayler/cluster/ClusteredPrevaylerState.java
  44. +4 −4 spikes/cluster/src/main/org/prevayler/cluster/ClusteredTransaction.java
  45. +9 −9 spikes/mirror-tests/src/test/java/org/prevayler/implementation/ReplicationTest.java
  46. +3 −3 tests/src/main/java/org/prevayler/implementation/AppendTransaction.java
  47. +5 −5 tests/src/main/java/org/prevayler/implementation/AppendTransactionWithQuery.java
  48. +3 −3 tests/src/main/java/org/prevayler/implementation/Appendix.java
  49. +1 −1 tests/src/main/java/org/prevayler/implementation/NondeterministicErrorTransaction.java
  50. +3 −2 tests/src/main/java/org/prevayler/implementation/NullQuery.java
View
13 CHANGES.txt
@@ -1,3 +1,16 @@
+=================================================================
+Changes from Prevayler 2.5 to this forked version (Naveen Chawla)
+
+---------------------------
+Type-safety, using generics
+
+No more need for casting. Objects and return values are delivered in
+original form. Initial declarations of prevalent system, factory, transactions
+and/or queries use similar syntax as ArrayList declarations e.g.
+ArrayList<MyType>, allowing 100% type-safety as well as compile-time
+checking that the prevalent object type implements Serializable.
+
+
===========================================
Changes from Prevayler 2.4 to Prevayler 2.5
View
9 core/src/main/java/org/prevayler/SureTransactionWithQuery.java
@@ -4,16 +4,17 @@
package org.prevayler;
+import java.io.Serializable;
import java.util.Date;
/** The same as TransactionWithQuery except it does not throw Exception when executed.
* @see TransactionWithQuery
*/
-public interface SureTransactionWithQuery extends TransactionWithQuery {
+public interface SureTransactionWithQuery<P extends Serializable,R> extends Serializable{
- /** The same as TransactionWithQuery.execute(Object, Date) except it does not throw Exception when executed.
- * @see TransactionWithQuery#executeAndQuery(Object, Date)
+ /** The same as TransactionWithQuery.executeAndQuery(P, Date) except it does not throw Exception when executed.
+ * @see TransactionWithQuery#executeAndQuery(P, Date)
*/
- public Object executeAndQuery(Object prevalentSystem, Date executionTime);
+ public R executeAndQuery(P prevalentSystem, Date executionTime);
}
View
4 core/src/main/java/org/prevayler/Transaction.java
@@ -9,12 +9,12 @@
/** An atomic Transaction to be executed on a Prevalent System. Any operation which changes the observable state of a prevalent system must be encapsulated as a Transaction. <br><br> IMPORTANT: Transactions CANNOT reference business objects directly. Instead, they must search the business objects they need given the Prevalent System. See org.prevayler.demos for usage examples. <br><br> Business objects referenced in a transaction will be mere copies of the original business objects when that transaction is recovered from the serialized journal file. This will make the transactions work when they are executed for the first time but have no effect during shutdown recovery. This is known as the prevalence baptism problem because everyone comes across it, despite of this warning.
*/
-public interface Transaction extends Serializable {
+public interface Transaction<P extends Serializable> extends Serializable{
/** This method is called by Prevayler.execute(Transaction) to execute this Transaction on the given Prevalent System. See org.prevayler.demos for usage examples.
* @param prevalentSystem The system on which this Transaction will execute.
* @param executionTime The time at which this Transaction is being executed. Every Transaction executes completely within a single moment in time. Logically, a Prevalent System's time does not pass during the execution of a Transaction.
*/
- public void executeOn(Object prevalentSystem, Date executionTime);
+ public void executeOn(P prevalentSystem, Date executionTime);
}
View
6 core/src/main/java/org/prevayler/TransactionWithQuery.java
@@ -10,13 +10,13 @@
/** A Transaction that also returns a result or throws an Exception after executing. <br><br>A "PersonCreation" Transaction, for example, may return the Person it created. Without this, to retrieve the newly created Person, the caller would have to issue a Query like: "What was the last Person I created?". <br><br>Looking at the Prevayler demos is by far the best way to learn how to use this class.
* @see Transaction
*/
-public interface TransactionWithQuery extends Serializable {
+public interface TransactionWithQuery<P extends Serializable,R> extends Serializable {
/** Performs the necessary modifications on the given prevalentSystem and also returns an Object or throws an Exception.
- * This method is called by Prevayler.execute(TransactionWithQuery) to execute this TransactionWithQuery on the given Prevalent System. See org.prevayler.demos for usage examples.
+ * This method is called by Prevayler.execute(TransactionWithQuery<P,R>) to execute this TransactionWithQuery on the given Prevalent System. See org.prevayler.demos for usage examples.
* @param prevalentSystem The system on which this TransactionWithQuery will execute.
* @param executionTime The time at which this TransactionWithQuery is being executed. Every Transaction executes completely within a single moment in time. Logically, a Prevalent System's time does not pass during the execution of a Transaction.
*/
- public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception;
+ public R executeAndQuery(P prevalentSystem, Date executionTime) throws Exception;
}
View
2 core/src/main/java/org/prevayler/foundation/serialization/JavaSerializer.java
@@ -11,7 +11,7 @@
/**
* Writes and reads objects using Java serialization. This serializer can be used for snapshots, journals or both.
*/
-public class JavaSerializer implements Serializer {
+public class JavaSerializer implements Serializer{
private ClassLoader _loader;
View
3 core/src/main/java/org/prevayler/foundation/serialization/Serializer.java
@@ -2,6 +2,7 @@
import java.io.OutputStream;
import java.io.InputStream;
+import java.io.Serializable;
/**
* A strategy for writing objects to and reading objects from streams. Implementations <b>must</b> be safe for
@@ -12,7 +13,7 @@
* for journals, it must be able to write and read any transactions it will be used with, but does not need to be
* able to write or read any other objects.
*/
-public interface Serializer {
+public interface Serializer{
/**
* Write an object to a stream. An implementation must ensure that the object is written
View
3 core/src/main/java/org/prevayler/implementation/Capsule.java
@@ -1,5 +1,6 @@
package org.prevayler.implementation;
+import org.prevayler.Transaction;
import org.prevayler.foundation.Chunk;
import org.prevayler.foundation.serialization.Serializer;
@@ -61,7 +62,7 @@ public void executeOn(Object prevalentSystem, Date executionTime, Serializer jou
* is responsible for synchronizing on the prevalentSystem.
*/
protected abstract void justExecute(Object transaction, Object prevalentSystem, Date executionTime);
-
+
/**
* Make a clean copy of this capsule that will have its own query result fields.
*/
View
17 core/src/main/java/org/prevayler/implementation/PrevalentSystemGuard.java
@@ -11,23 +11,24 @@
import java.io.File;
import java.io.IOException;
+import java.io.Serializable;
import java.util.Date;
-public class PrevalentSystemGuard implements TransactionSubscriber {
+public class PrevalentSystemGuard<P extends Serializable> implements TransactionSubscriber {
- private Object _prevalentSystem; // All access to field is synchronized on "this", and all access to object is synchronized on itself; "this" is always locked before the object
+ private P _prevalentSystem; // All access to field is synchronized on "this", and all access to object is synchronized on itself; "this" is always locked before the object
private long _systemVersion; // All access is synchronized on "this"
private boolean _ignoreRuntimeExceptions; // All access is synchronized on "this"
private final Serializer _journalSerializer;
- public PrevalentSystemGuard(Object prevalentSystem, long systemVersion, Serializer journalSerializer) {
+ public PrevalentSystemGuard(P prevalentSystem, long systemVersion, Serializer journalSerializer) {
_prevalentSystem = prevalentSystem;
_systemVersion = systemVersion;
_ignoreRuntimeExceptions = false;
_journalSerializer = journalSerializer;
}
- public Object prevalentSystem() {
+ public P prevalentSystem() {
synchronized (this) {
if (_prevalentSystem == null) {
throw new Error("Prevayler is no longer allowing access to the prevalent system due to an Error thrown from an earlier transaction.");
@@ -82,7 +83,7 @@ public void receive(TransactionTimestamp transactionTimestamp) {
}
}
- public Object executeQuery(Query sensitiveQuery, Clock clock) throws Exception {
+ public <R> R executeQuery(Query<P,R> sensitiveQuery, Clock clock) throws Exception {
synchronized (this) {
if (_prevalentSystem == null) {
throw new Error("Prevayler is no longer processing queries due to an Error thrown from an earlier transaction.");
@@ -94,7 +95,7 @@ public Object executeQuery(Query sensitiveQuery, Clock clock) throws Exception {
}
}
- public File takeSnapshot(GenericSnapshotManager snapshotManager) throws Exception {
+ public File takeSnapshot(GenericSnapshotManager<P> snapshotManager) throws Exception {
synchronized (this) {
if (_prevalentSystem == null) {
throw new Error("Prevayler is no longer allowing snapshots due to an Error thrown from an earlier transaction.");
@@ -106,7 +107,7 @@ public File takeSnapshot(GenericSnapshotManager snapshotManager) throws Exceptio
}
}
- public PrevalentSystemGuard deepCopy(long systemVersion, Serializer snapshotSerializer) throws Exception {
+ public PrevalentSystemGuard<P> deepCopy(long systemVersion, Serializer snapshotSerializer) throws Exception {
synchronized (this) {
while (_systemVersion < systemVersion && _prevalentSystem != null) {
Cool.wait(this);
@@ -121,7 +122,7 @@ public PrevalentSystemGuard deepCopy(long systemVersion, Serializer snapshotSeri
}
synchronized (_prevalentSystem) {
- return new PrevalentSystemGuard(DeepCopier.deepCopyParallel(_prevalentSystem, snapshotSerializer), _systemVersion, _journalSerializer);
+ return new PrevalentSystemGuard<P>((P)DeepCopier.deepCopyParallel(_prevalentSystem, snapshotSerializer), _systemVersion, _journalSerializer);
}
}
}
View
25 core/src/main/java/org/prevayler/implementation/PrevaylerImpl.java
@@ -16,13 +16,14 @@
import java.io.File;
import java.io.IOException;
+import java.io.Serializable;
-public class PrevaylerImpl implements Prevayler {
+public class PrevaylerImpl<P extends Serializable> implements Prevayler<P>{
- private final PrevalentSystemGuard _guard;
+ private final PrevalentSystemGuard<P> _guard;
private final Clock _clock;
- private final GenericSnapshotManager _snapshotManager;
+ private final GenericSnapshotManager<P> _snapshotManager;
private final TransactionPublisher _publisher;
@@ -36,7 +37,7 @@
* @param prevaylerMonitor The Monitor that will be used to monitor interesting calls to this PrevaylerImpl.
* @param journalSerializer
*/
- public PrevaylerImpl(GenericSnapshotManager snapshotManager, TransactionPublisher transactionPublisher,
+ public PrevaylerImpl(GenericSnapshotManager<P> snapshotManager, TransactionPublisher transactionPublisher,
Serializer journalSerializer) throws IOException, ClassNotFoundException {
_snapshotManager = snapshotManager;
@@ -50,14 +51,14 @@ public PrevaylerImpl(GenericSnapshotManager snapshotManager, TransactionPublishe
_journalSerializer = journalSerializer;
}
- public Object prevalentSystem() { return _guard.prevalentSystem(); }
+ public P prevalentSystem() { return _guard.prevalentSystem(); }
public Clock clock() { return _clock; }
- public void execute(Transaction transaction) {
- publish(new TransactionCapsule(transaction, _journalSerializer)); //TODO Optimizations: 1) The Censor can use the actual given transaction if it is Immutable instead of deserializing a new one from the byte array. 2) Make the baptism fail-fast feature optional (default is on). If it is off, the given transaction can be used instead of deserializing a new one from the byte array.
+ public void execute(Transaction<P> transaction) {
+ publish(new TransactionCapsule<P>(transaction, _journalSerializer)); //TODO Optimizations: 1) The Censor can use the actual given transaction if it is Immutable instead of deserializing a new one from the byte array. 2) Make the baptism fail-fast feature optional (default is on). If it is off, the given transaction can be used instead of deserializing a new one from the byte array.
}
@@ -66,21 +67,21 @@ private void publish(Capsule capsule) {
}
- public Object execute(Query sensitiveQuery) throws Exception {
+ public <R> R execute(Query<P,R> sensitiveQuery) throws Exception {
return _guard.executeQuery(sensitiveQuery, clock());
}
- public Object execute(TransactionWithQuery transactionWithQuery) throws Exception {
- TransactionWithQueryCapsule capsule = new TransactionWithQueryCapsule(transactionWithQuery, _journalSerializer);
+ public <R> R execute(TransactionWithQuery<P,R> transactionWithQuery) throws Exception {
+ TransactionWithQueryCapsule<P,R> capsule = new TransactionWithQueryCapsule<P,R>(transactionWithQuery, _journalSerializer);
publish(capsule);
return capsule.result();
}
- public Object execute(SureTransactionWithQuery sureTransactionWithQuery) {
+ public <R> R execute(SureTransactionWithQuery<P,R> sureTransactionWithQuery) {
try {
- return execute((TransactionWithQuery)sureTransactionWithQuery);
+ return execute((TransactionWithQuery<P,R>)sureTransactionWithQuery);
} catch (RuntimeException runtime) {
throw runtime;
} catch (Exception checked) {
View
9 core/src/main/java/org/prevayler/implementation/TransactionCapsule.java
@@ -3,13 +3,14 @@
import org.prevayler.Transaction;
import org.prevayler.foundation.serialization.Serializer;
+import java.io.Serializable;
import java.util.Date;
-class TransactionCapsule extends Capsule {
+class TransactionCapsule<P extends Serializable> extends Capsule{
private static final long serialVersionUID = 3283271592697928351L;
- public TransactionCapsule(Transaction transaction, Serializer journalSerializer) {
+ public TransactionCapsule(Transaction<P> transaction, Serializer journalSerializer) {
super(transaction, journalSerializer);
}
@@ -18,12 +19,12 @@ public TransactionCapsule(byte[] serialized) {
}
protected void justExecute(Object transaction, Object prevalentSystem, Date executionTime) {
- ((Transaction) transaction).executeOn(prevalentSystem, executionTime);
+ ((Transaction<P>) transaction).executeOn((P)prevalentSystem, executionTime);
}
public Capsule cleanCopy() {
// TransactionCapsule, unlike TransactionWithQueryCapsule, is completely immutable.
return this;
}
-
+
}
View
13 core/src/main/java/org/prevayler/implementation/TransactionWithQueryCapsule.java
@@ -3,15 +3,16 @@
import org.prevayler.TransactionWithQuery;
import org.prevayler.foundation.serialization.Serializer;
+import java.io.Serializable;
import java.util.Date;
-class TransactionWithQueryCapsule extends Capsule {
+class TransactionWithQueryCapsule<P extends Serializable,R> extends Capsule{
private static final long serialVersionUID = 78811627002206298L;
- private transient Object _queryResult;
+ private transient R _queryResult;
private transient Exception _queryException;
- public TransactionWithQueryCapsule(TransactionWithQuery transactionWithQuery, Serializer journalSerializer) {
+ public TransactionWithQueryCapsule(TransactionWithQuery<P,R> transactionWithQuery, Serializer journalSerializer) {
super(transactionWithQuery, journalSerializer);
}
@@ -21,7 +22,7 @@ public TransactionWithQueryCapsule(byte[] serialized) {
protected void justExecute(Object transaction, Object prevalentSystem, Date executionTime) {
try {
- _queryResult = ((TransactionWithQuery) transaction).executeAndQuery(prevalentSystem, executionTime);
+ _queryResult = ((TransactionWithQuery<P,R>) transaction).executeAndQuery((P)prevalentSystem, executionTime);
} catch (RuntimeException rx) {
_queryException = rx;
throw rx; //This is necessary because of the rollback feature.
@@ -30,13 +31,13 @@ protected void justExecute(Object transaction, Object prevalentSystem, Date exec
}
}
- public Object result() throws Exception {
+ public R result() throws Exception {
if (_queryException != null) throw _queryException;
return _queryResult;
}
public Capsule cleanCopy() {
- return new TransactionWithQueryCapsule(serialized());
+ return new TransactionWithQueryCapsule<P,R>(serialized());
}
}
View
14 ...main/java/org/prevayler/implementation/publishing/censorship/StrictTransactionCensor.java
@@ -5,18 +5,20 @@
package org.prevayler.implementation.publishing.censorship;
+import java.io.Serializable;
+
import org.prevayler.foundation.serialization.Serializer;
import org.prevayler.implementation.PrevalentSystemGuard;
import org.prevayler.implementation.TransactionTimestamp;
import org.prevayler.implementation.snapshot.GenericSnapshotManager;
-public class StrictTransactionCensor implements TransactionCensor {
+public class StrictTransactionCensor<P extends Serializable> implements TransactionCensor {
- private final PrevalentSystemGuard _king;
- private PrevalentSystemGuard _royalFoodTaster;
+ private final PrevalentSystemGuard<P> _king;
+ private PrevalentSystemGuard<P> _royalFoodTaster;
private final Serializer _snapshotSerializer;
- public StrictTransactionCensor(GenericSnapshotManager snapshotManager) {
+ public StrictTransactionCensor(GenericSnapshotManager<P> snapshotManager) {
_king = snapshotManager.recoveredPrevalentSystem();
// The _royalFoodTaster cannot be initialized here, or else the pending transactions will not be applied to it.
_snapshotSerializer = snapshotManager.primarySerializer();
@@ -25,7 +27,7 @@ public StrictTransactionCensor(GenericSnapshotManager snapshotManager) {
public void approve(TransactionTimestamp transactionTimestamp) throws RuntimeException, Error {
try {
TransactionTimestamp timestampCopy = transactionTimestamp.cleanCopy();
- PrevalentSystemGuard royalFoodTaster = royalFoodTaster(transactionTimestamp.systemVersion() - 1);
+ PrevalentSystemGuard<P> royalFoodTaster = royalFoodTaster(transactionTimestamp.systemVersion() - 1);
royalFoodTaster.receive(timestampCopy);
} catch (RuntimeException rx) {
letTheFoodTasterDie();
@@ -40,7 +42,7 @@ private void letTheFoodTasterDie() {
_royalFoodTaster = null; // At this moment there might be transactions that have already been approved by this censor but have not yet been applied to the _king. It is a requirement, therefore, that the _royalFoodTaster must not be initialized now, but only when the next transaction arrives to be approved.
}
- private PrevalentSystemGuard royalFoodTaster(long systemVersion) {
+ private PrevalentSystemGuard<P> royalFoodTaster(long systemVersion) {
if (_royalFoodTaster == null) produceNewFoodTaster(systemVersion);
return _royalFoodTaster;
}
View
25 core/src/main/java/org/prevayler/implementation/snapshot/GenericSnapshotManager.java
@@ -10,18 +10,19 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
-public class GenericSnapshotManager {
+public class GenericSnapshotManager<P extends Serializable>{
private Map _strategies;
private String _primarySuffix;
private PrevaylerDirectory _directory;
- private PrevalentSystemGuard _recoveredPrevalentSystem;
+ private PrevalentSystemGuard<P> _recoveredPrevalentSystem;
- public GenericSnapshotManager(Map snapshotSerializers, String primarySnapshotSuffix, Object newPrevalentSystem, PrevaylerDirectory directory, Serializer journalSerializer)
+ public GenericSnapshotManager(Map snapshotSerializers, String primarySnapshotSuffix, P newPrevalentSystem, PrevaylerDirectory directory, Serializer journalSerializer)
throws Exception {
for (Iterator iterator = snapshotSerializers.keySet().iterator(); iterator.hasNext();) {
String suffix = (String) iterator.next();
@@ -40,29 +41,29 @@ public GenericSnapshotManager(Map snapshotSerializers, String primarySnapshotSuf
File latestSnapshot = _directory.latestSnapshot();
long recoveredVersion = latestSnapshot == null ? 0 : PrevaylerDirectory.snapshotVersion(latestSnapshot);
- Object recoveredPrevalentSystem = latestSnapshot == null
+ P recoveredPrevalentSystem = latestSnapshot == null
? newPrevalentSystem
: readSnapshot(latestSnapshot);
- _recoveredPrevalentSystem = new PrevalentSystemGuard(recoveredPrevalentSystem, recoveredVersion, journalSerializer);
+ _recoveredPrevalentSystem = new PrevalentSystemGuard<P>(recoveredPrevalentSystem, recoveredVersion, journalSerializer);
}
- GenericSnapshotManager(Object newPrevalentSystem) {
+ GenericSnapshotManager(P newPrevalentSystem) {
_strategies = Collections.singletonMap("snapshot", new JavaSerializer());
_primarySuffix = "snapshot";
_directory = null;
- _recoveredPrevalentSystem = new PrevalentSystemGuard(newPrevalentSystem, 0, new JavaSerializer());
+ _recoveredPrevalentSystem = new PrevalentSystemGuard<P>(newPrevalentSystem, 0, new JavaSerializer());
}
public Serializer primarySerializer() {
return (Serializer) _strategies.get(_primarySuffix);
}
- public PrevalentSystemGuard recoveredPrevalentSystem() {
+ public PrevalentSystemGuard<P> recoveredPrevalentSystem() {
return _recoveredPrevalentSystem;
}
- public File writeSnapshot(Object prevalentSystem, long version) throws Exception {
+ public File writeSnapshot(P prevalentSystem, long version) throws Exception {
File tempFile = _directory.createTempFile("snapshot" + version + "temp", "generatingSnapshot");
writeSnapshot(prevalentSystem, tempFile);
@@ -75,7 +76,7 @@ public File writeSnapshot(Object prevalentSystem, long version) throws Exception
return permanent;
}
- private void writeSnapshot(Object prevalentSystem, File snapshotFile) throws Exception {
+ private void writeSnapshot(P prevalentSystem, File snapshotFile) throws Exception {
OutputStream out = new FileOutputStream(snapshotFile);
try {
primarySerializer().writeObject(out, prevalentSystem);
@@ -89,15 +90,15 @@ private File snapshotFile(long version) {
return _directory.snapshotFile(version, _primarySuffix);
}
- private Object readSnapshot(File snapshotFile) throws Exception {
+ private P readSnapshot(File snapshotFile) throws Exception {
String suffix = snapshotFile.getName().substring(snapshotFile.getName().indexOf('.') + 1);
if (!_strategies.containsKey(suffix)) throw new IOException(
snapshotFile.toString() + " cannot be read; only " + _strategies.keySet().toString() + " supported");
Serializer serializer = (Serializer) _strategies.get(suffix);
FileInputStream in = new FileInputStream(snapshotFile);
try {
- return serializer.readObject(in);
+ return (P)(serializer.readObject(in));
} finally {
in.close();
}
View
7 core/src/main/java/org/prevayler/implementation/snapshot/NullSnapshotManager.java
@@ -7,17 +7,18 @@
import java.io.File;
import java.io.IOException;
+import java.io.Serializable;
-public class NullSnapshotManager extends GenericSnapshotManager {
+public class NullSnapshotManager<P extends Serializable> extends GenericSnapshotManager<P>{
private final String _snapshotAttemptErrorMessage;
- public NullSnapshotManager(Object newPrevalentSystem, String snapshotAttemptErrorMessage) {
+ public NullSnapshotManager(P newPrevalentSystem, String snapshotAttemptErrorMessage) {
super(newPrevalentSystem);
_snapshotAttemptErrorMessage = snapshotAttemptErrorMessage;
}
- public File writeSnapshot(Object prevalentSystem, long version) throws IOException {
+ public File writeSnapshot(P prevalentSystem, long version) throws IOException {
throw new IOException(_snapshotAttemptErrorMessage);
}
View
2 demos/demo1/src/main/java/org/prevayler/demos/demo1/Main.java
@@ -10,7 +10,7 @@
public static void main(String[] args) throws Exception {
printMessage();
- Prevayler prevayler = PrevaylerFactory.createPrevayler(new NumberKeeper(), "demo1");
+ Prevayler<NumberKeeper> prevayler = PrevaylerFactory.createPrevayler(new NumberKeeper(), "demo1");
new PrimeCalculator(prevayler).start();
}
View
6 demos/demo1/src/main/java/org/prevayler/demos/demo1/NumberStorageTransaction.java
@@ -8,7 +8,7 @@
/**
* To change the state of the business objects, the client code must use a Transaction like this one.
*/
-class NumberStorageTransaction implements Transaction {
+class NumberStorageTransaction implements Transaction<NumberKeeper> {
private static final long serialVersionUID = -2023934810496653301L;
private int _numberToKeep;
@@ -18,7 +18,7 @@ private NumberStorageTransaction() {} //Necessary for Skaringa XML serialization
_numberToKeep = numberToKeep;
}
- public void executeOn(Object prevalentSystem, Date ignored) {
- ((NumberKeeper)prevalentSystem).keep(_numberToKeep);
+ public void executeOn(NumberKeeper prevalentSystem, Date ignored) {
+ prevalentSystem.keep(_numberToKeep);
}
}
View
6 demos/demo1/src/main/java/org/prevayler/demos/demo1/PrimeCalculator.java
@@ -8,13 +8,13 @@
*/
class PrimeCalculator {
- private final Prevayler _prevayler;
+ private final Prevayler<NumberKeeper> _prevayler;
private final NumberKeeper _numberKeeper;
- PrimeCalculator(Prevayler prevayler) {
+ PrimeCalculator(Prevayler<NumberKeeper> prevayler) {
_prevayler = prevayler;
- _numberKeeper = (NumberKeeper)prevayler.prevalentSystem();
+ _numberKeeper = prevayler.prevalentSystem();
}
View
2 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/Main.java
@@ -54,7 +54,7 @@ public static void main(String[] args) {
try {
- Prevayler prevayler =
+ Prevayler<ProjectManagementSystem> prevayler =
PrevaylerFactory.createPrevayler(new ProjectManagementSystem(), "demoJXPath");
if (args.length < 1) {
View
9 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/commands/AddProject.java
@@ -19,14 +19,13 @@
*
* @author Carlos Villela
*/
-public class AddProject implements Transaction {
+public class AddProject implements Transaction<ProjectManagementSystem> {
private Project project;
- public void executeOn(Object system, Date ignored) {
- if(project != null) {
- ProjectManagementSystem pms = (ProjectManagementSystem) system;
- pms.getProjects().add(project);
+ public void executeOn(ProjectManagementSystem system, Date ignored) {
+ if(project != null) {
+ system.getProjects().add(project);
} else {
throw new RuntimeException("No project to add -- please call setProject()");
}
View
7 demos/jxpath/src/main/java/org/prevayler/demos/jxpath/commands/AddTask.java
@@ -20,16 +20,15 @@
/**
* @author Carlos Villela
*/
-public class AddTask implements Transaction {
+public class AddTask implements Transaction<ProjectManagementSystem> {
private Task task;
private int projectId = Integer.MIN_VALUE;
- public void executeOn(Object system, Date ignored) {
+ public void executeOn(ProjectManagementSystem system, Date ignored) {
if(task != null && projectId != Integer.MIN_VALUE) {
- ProjectManagementSystem pms = (ProjectManagementSystem) system;
- List projects = pms.getProjects();
+ List projects = system.getProjects();
Iterator i = projects.iterator();
while (i.hasNext()) {
Project p = (Project) i.next();
View
6 ...bility/src/main/java/org/prevayler/demos/scalability/prevayler/AllRecordsReplacement.java
@@ -5,14 +5,14 @@
import org.prevayler.Transaction;
import org.prevayler.demos.scalability.RecordIterator;
-class AllRecordsReplacement implements Transaction {
+class AllRecordsReplacement implements Transaction<ScalabilitySystem> {
private static final long serialVersionUID = 6283032417365727408L;
private final int _records;
AllRecordsReplacement(int records) { _records = records; }
- public void executeOn(Object system, Date ignored) {
- ((ScalabilitySystem)system).replaceAllRecords(new RecordIterator(_records));
+ public void executeOn(ScalabilitySystem system, Date ignored) {
+ system.replaceAllRecords(new RecordIterator(_records));
}
}
View
2 ...bility/src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerQuerySubject.java
@@ -11,7 +11,7 @@
public PrevaylerQuerySubject() throws Exception {
if (new File(PREVALENCE_BASE).exists()) PrevalenceTest.delete(PREVALENCE_BASE);
- PrevaylerFactory factory = new PrevaylerFactory();
+ PrevaylerFactory<QuerySystem> factory = new PrevaylerFactory<QuerySystem>();
factory.configurePrevalentSystem(new QuerySystem());
factory.configurePrevalenceDirectory(PREVALENCE_BASE);
factory.configureTransactionFiltering(false);
View
2 .../src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerTransactionSubject.java
@@ -39,7 +39,7 @@ public boolean isConsistent() throws Exception {
}
private void initializePrevayler() throws Exception {
- PrevaylerFactory factory = new PrevaylerFactory();
+ PrevaylerFactory<TransactionSystem> factory = new PrevaylerFactory<TransactionSystem>();
factory.configurePrevalentSystem(new TransactionSystem());
factory.configurePrevalenceDirectory(_journalDirectory);
factory.configureJournalSerializer("journal", (Serializer) Class.forName(_journalSerializer).newInstance());
View
6 .../scalability/src/main/java/org/prevayler/demos/scalability/prevayler/TestTransaction.java
@@ -5,7 +5,7 @@
import org.prevayler.Transaction;
import org.prevayler.demos.scalability.Record;
-class TestTransaction implements Transaction {
+class TestTransaction implements Transaction<TransactionSystem> {
private static final long serialVersionUID = -2634307328586761351L;
private final Record recordToInsert;
@@ -18,7 +18,7 @@
this.idToDelete = idToDelete;
}
- public void executeOn(Object system, Date ignored) {
- ((TransactionSystem)system).performTransaction(recordToInsert, recordToUpdate, idToDelete);
+ public void executeOn(TransactionSystem system, Date ignored) {
+ system.performTransaction(recordToInsert, recordToUpdate, idToDelete);
}
}
View
4 demos/tutorial/src/main/java/org/prevayler/baptism/BadTransaction.java
@@ -12,7 +12,7 @@
* @since Apr 6, 2004
*/
// START SNIPPET: bad
-public class BadTransaction implements Transaction {
+public class BadTransaction implements Transaction<MyPrevalentSystem> {
private Calendar calendar;
@@ -23,7 +23,7 @@ public BadTransaction(Calendar calendar, long millis) {
this.millis = millis;
}
- public void executeOn(Object prevalentSystem, Date executionTime) {
+ public void executeOn(MyPrevalentSystem prevalentSystem, Date executionTime) {
calendar.setTimeInMillis(this.millis);
}
}
View
16 demos/tutorial/src/main/java/org/prevayler/baptism/GoodTransaction.java
@@ -12,7 +12,7 @@
* @since Apr 6, 2004
*/
// START SNIPPET: good
-public class GoodTransaction implements Transaction {
+public class GoodTransaction implements Transaction<MyPrevalentSystem> {
private long millis;
private String id;
@@ -22,18 +22,10 @@ public GoodTransaction(String id, long millis) {
this.millis = millis;
}
- public void executeOn(Object prevalentSystem, Date executionTime) {
- MyPrevalentSystem system = (MyPrevalentSystem) prevalentSystem;
- Calendar c = system.lookupCalendar(this.id);
+ public void executeOn(MyPrevalentSystem prevalentSystem, Date executionTime) {
+ Calendar c = prevalentSystem.lookupCalendar(this.id);
c.setTimeInMillis(this.millis);
}
}
-//END SNIPPET: good
-
-// this is here just to get the example to compile.
-class MyPrevalentSystem {
- public Calendar lookupCalendar(String id) {
- return null;
- }
-}
+//END SNIPPET: good
View
10 demos/tutorial/src/main/java/org/prevayler/tutorial/AddTask.java
@@ -11,19 +11,17 @@
import org.prevayler.TransactionWithQuery;
// START SNIPPET: addtask
-public class AddTask implements TransactionWithQuery {
+public class AddTask implements TransactionWithQuery<TaskList,Task> {
private final Task task;
public AddTask(String description, int priority) {
this.task = new Task(description, priority);
}
- public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception {
-
- TaskList system = (TaskList) prevalentSystem;
- system.addTask(task);
-
+ public Task executeAndQuery(TaskList prevalentSystem, Date executionTime) throws Exception {
+
+ prevalentSystem.addTask(task);
return task;
}
}
View
10 demos/tutorial/src/main/java/org/prevayler/tutorial/Main.java
@@ -22,16 +22,16 @@
public static void main(String[] args) throws Exception {
// START SNIPPET: creating
// Create a new prevayler. /tasklist-base is the tx-journal directory.
- Prevayler prevayler = PrevaylerFactory.createPrevayler(new TaskList(), "/tasklist-base");
- TaskList list = (TaskList) prevayler.prevalentSystem();
+ Prevayler<TaskList> prevayler = PrevaylerFactory.createPrevayler(new TaskList(), "/tasklist-base");
+ TaskList list = prevayler.prevalentSystem();
// END SNIPPET: creating
System.out.println("Tasks: " + list.getTasks().size() + ", adding ");
// START SNIPPET: adding
- Task dishes = (Task) prevayler.execute(new AddTask("do the dishes", Task.MAX_PRIORITY));
- Task dog = (Task) prevayler.execute(new AddTask("walk the dog", Task.MED_PRIORITY));
- Task laundry = (Task) prevayler.execute(new AddTask("do the laundry", Task.MIN_PRIORITY));
+ Task dishes = prevayler.execute(new AddTask("do the dishes", Task.MAX_PRIORITY));
+ Task dog = prevayler.execute(new AddTask("walk the dog", Task.MED_PRIORITY));
+ Task laundry = prevayler.execute(new AddTask("do the laundry", Task.MIN_PRIORITY));
// END SNIPPET: adding
// START SNIPPET: iterating
View
8 demos/tutorial/src/main/java/org/prevayler/tutorial/RemoveTask.java
@@ -11,17 +11,17 @@
import org.prevayler.Transaction;
// START SNIPPET: removetask
-public class RemoveTask implements Transaction {
+public class RemoveTask implements Transaction<TaskList> {
private Task task;
public RemoveTask(Task task) {
this.task = task;
}
- public void executeOn(Object prevalentSystem, Date executionTime) {
- TaskList system = (TaskList) prevalentSystem;
- system.removeTask(task);
+ public void executeOn(TaskList prevalentSystem, Date executionTime) {
+
+ prevalentSystem.removeTask(task);
}
}
// END SNIPPET: removetask
View
5 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyQuery.java
@@ -32,6 +32,7 @@
package org.prevayler.contrib.facade;
+import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
@@ -47,7 +48,7 @@
*/
public class ProxyQuery
extends AbstractProxy
- implements Query
+ implements Query<Serializable, Object>
{
private static final long serialVersionUID = 7773538966349888966L;
@@ -59,7 +60,7 @@ public ProxyQuery(Method p_method, Object[] p_args, TransactionHint p_hint)
super(p_method, p_args, p_hint);
}
- public Object query(Object p_prevalentSystem, Date p_timestamp)
+ public Object query(Serializable p_prevalentSystem, Date p_timestamp)
throws Exception
{
return execute(p_prevalentSystem, p_timestamp);
View
5 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyTransaction.java
@@ -32,6 +32,7 @@
package org.prevayler.contrib.facade;
+import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
@@ -47,7 +48,7 @@
*/
public class ProxyTransaction
extends AbstractProxy
- implements Transaction
+ implements Transaction<Serializable>
{
private static final long serialVersionUID = -3720257876251185011L;
@@ -59,7 +60,7 @@ public ProxyTransaction(Method p_method, Object[] p_args, TransactionHint p_hint
super(p_method, p_args, p_hint);
}
- public void executeOn(Object p_prevalentSystem, Date p_timestamp)
+ public void executeOn(Serializable p_prevalentSystem, Date p_timestamp)
{
try
{
View
5 extras/facade/src/main/java/org/prevayler/contrib/facade/ProxyTransactionWithQuery.java
@@ -32,6 +32,7 @@
package org.prevayler.contrib.facade;
+import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
@@ -47,7 +48,7 @@
*/
public class ProxyTransactionWithQuery
extends AbstractProxy
- implements TransactionWithQuery
+ implements TransactionWithQuery<Serializable, Object>
{
private static final long serialVersionUID = -5012126657533926785L;
@@ -59,7 +60,7 @@ public ProxyTransactionWithQuery(Method p_method, Object[] p_args, TransactionHi
super(p_method, p_args, p_hint);
}
- public Object executeAndQuery(Object p_prevalentSystem, Date p_timestamp)
+ public Object executeAndQuery(Serializable p_prevalentSystem, Date p_timestamp)
throws Exception
{
return execute(p_prevalentSystem, p_timestamp);
View
10 extras/memento/src/main/java/org/prevayler/demos/memento/TestErrorRecoveryWithMementos.java
@@ -30,13 +30,13 @@ public static void main(String[] args) {
}
}
- private static Prevayler prevayler;
+ private static Prevayler<Bank> prevayler;
private static Bank bank;
static {
try {
prevayler = PrevaylerFactory.createPrevayler(new Bank(), "demoMemento");
- bank = (Bank)prevayler.prevalentSystem();
+ bank = prevayler.prevalentSystem();
} catch (Exception e) {
System.out.println("FAILED TO CREATE PREVAYLER!");
}
@@ -47,11 +47,11 @@ public static void run() throws Exception {
System.out.println("*** Creating account 1");
MementoTransaction command = new AccountCreation("Owner 1");
- account1 = (Account)execute(command);
+ account1 = execute(command);
System.out.println("*** Creating account 2");
command = new AccountCreation("Owner 2");
- account2 = (Account)execute(command);
+ account2 = execute(command);
System.out.println("*** Depositing 500 into account 1");
command = new Deposit(account1, 500);
@@ -72,7 +72,7 @@ public static void run() throws Exception {
prevayler.takeSnapshot();
}
- private static Object execute(MementoTransaction command) {
+ private static Account execute(MementoTransaction command) {
try {
return new MementoManagerCommand(command).executeAndQuery(prevayler.prevalentSystem(), prevayler.clock().time());
} catch (Exception exception) {
View
6 extras/memento/src/main/java/org/prevayler/demos/memento/commands/AccountCreation.java
@@ -18,8 +18,8 @@ public AccountCreation(String holder) {
this.holder = holder;
}
- protected void findObjects(Object prevalentSystem) {
- bank = (Bank)prevalentSystem;
+ protected void findObjects(Bank prevalentSystem) {
+ bank = prevalentSystem;
}
protected void checkPrecondition() {
@@ -29,7 +29,7 @@ protected void createMementos(MementoCollector collector) {
bank.createMemento(collector);
}
- protected Serializable execute(MementoCollector collector) throws Account.InvalidHolder {
+ protected Account execute(MementoCollector collector) throws Account.InvalidHolder {
return bank.createAccount(holder);
}
}
View
6 extras/memento/src/main/java/org/prevayler/demos/memento/commands/AccountDeletion.java
@@ -18,8 +18,8 @@ public AccountDeletion(Account account) {
accountNumber = account.number();
}
- protected void findObjects(Object prevalentSystem) {
- bank = (Bank)prevalentSystem;
+ protected void findObjects(Bank prevalentSystem) {
+ bank = prevalentSystem;
}
protected void checkPrecondition() {
@@ -29,7 +29,7 @@ protected void createMementos(MementoCollector collector) {
bank.createMemento(collector);
}
- protected Serializable execute(MementoCollector collector) throws Bank.AccountNotFound {
+ protected Account execute(MementoCollector collector) throws Bank.AccountNotFound {
bank.deleteAccount(accountNumber);
return null;
}
View
6 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Deposit.java
@@ -28,13 +28,13 @@ public Deposit(long accountNumber, long amount) {
this.amount = amount;
}
- protected void findObjects(Object prevalentSystem) throws Exception
+ protected void findObjects(Bank prevalentSystem) throws Exception
{
if (JOptionPane.showConfirmDialog(null, "Fail at Deposit::findObjects?", "Prevayler with rollback", JOptionPane.YES_NO_OPTION) == 0) {
throw new RuntimeException();
}
- account = ((Bank)prevalentSystem).findAccount(accountNumber);
+ account = prevalentSystem.findAccount(accountNumber);
}
protected void checkPrecondition()
@@ -54,7 +54,7 @@ protected void createMementos(MementoCollector collector)
account.createMemento(collector);
}
- protected Serializable execute(MementoCollector collector) throws Exception
+ protected Account execute(MementoCollector collector) throws Exception
{
if (JOptionPane.showConfirmDialog(null, "Fail at Deposit::execute?", "Prevayler with rollback", JOptionPane.YES_NO_OPTION) == 0) {
throw new RuntimeException();
View
7 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Transfer.java
@@ -3,6 +3,7 @@
import java.io.Serializable;
import org.prevayler.util.memento.MementoCollector;
import org.prevayler.util.memento.MementoTransaction;
+import org.prevayler.demos.memento.Account;
import org.prevayler.demos.memento.Bank;
public class Transfer extends MementoTransaction {
@@ -23,8 +24,8 @@ public Transfer(long sourceNumber, long destinationNumber, long amount) {
this.amount = amount;
}
- protected void findObjects(Object prevalentSystem) throws Exception {
- bank = (Bank)prevalentSystem;
+ protected void findObjects(Bank prevalentSystem) throws Exception {
+ bank = prevalentSystem;
}
protected void checkPrecondition() {
@@ -33,7 +34,7 @@ protected void checkPrecondition() {
protected void createMementos(MementoCollector collector) {
}
- protected Serializable execute(MementoCollector collector) throws Exception {
+ protected Account execute(MementoCollector collector) throws Exception {
MementoTransaction command = new Withdrawal(sourceNumber, amount);
command.execute(collector, bank);
View
6 extras/memento/src/main/java/org/prevayler/demos/memento/commands/Withdrawal.java
@@ -24,13 +24,13 @@ public Withdrawal(long accountNumber, long amount) {
this.amount = amount;
}
- protected void findObjects(Object prevalentSystem) throws Exception
+ protected void findObjects(Bank prevalentSystem) throws Exception
{
if (JOptionPane.showConfirmDialog(null, "Fail at Withdrawal::findObjects?", "Prevayler with rollback", JOptionPane.YES_NO_OPTION) == 0) {
throw new RuntimeException();
}
- account = ((Bank)prevalentSystem).findAccount(accountNumber);
+ account = prevalentSystem.findAccount(accountNumber);
}
protected void checkPrecondition() throws Exception
@@ -53,7 +53,7 @@ protected void createMementos(MementoCollector collector)
account.createMemento(collector);
}
- protected Serializable execute(MementoCollector collector) throws Exception
+ protected Account execute(MementoCollector collector) throws Exception
{
if (JOptionPane.showConfirmDialog(null, "Fail at Withdrawal::execute?", "Prevayler with rollback", JOptionPane.YES_NO_OPTION) == 0) {
throw new RuntimeException();
View
6 extras/memento/src/main/java/org/prevayler/util/memento/MementoManagerCommand.java
@@ -10,14 +10,16 @@
import java.util.Map;
import org.prevayler.TransactionWithQuery;
+import org.prevayler.demos.memento.Account;
+import org.prevayler.demos.memento.Bank;
/**
* This transaction manages the mementos and restores them in case of a failure.
*
* @author Johan Stuyts
* @version 2.0
*/
-public class MementoManagerCommand implements TransactionWithQuery, MementoCollector {
+public class MementoManagerCommand implements TransactionWithQuery<Bank,Account>, MementoCollector {
/**
* Create a memento manager transaction.
@@ -38,7 +40,7 @@ public MementoManagerCommand(MementoTransaction transaction) {
* @param system The prevalent system on which to execute the transaction.
* @return The object returned by the execution of this transaction. Most commands simply return null.
*/
- public Object executeAndQuery(Object prevalentSystem, Date timestamp) throws Exception {
+ public Account executeAndQuery(Bank prevalentSystem, Date timestamp) throws Exception {
mementos = new HashMap();
try {
return transaction.execute(this, prevalentSystem);
View
9 extras/memento/src/main/java/org/prevayler/util/memento/MementoTransaction.java
@@ -6,6 +6,9 @@
import java.io.Serializable;
+import org.prevayler.demos.memento.Account;
+import org.prevayler.demos.memento.Bank;
+
/**
* An atomic transaction to be executed on a PrevalentSystem. Any operation which changes the
* observable state of a PrevalentSystem must be encapsulated as a Command.
@@ -38,7 +41,7 @@
* @param system The system on which to execute the command.
* @return The object returned by the execution of this command. Most commands simply return null.
*/
- public Serializable execute(MementoCollector collector, Object prevalentSystem) throws Exception {
+ public Account execute(MementoCollector collector, Bank prevalentSystem) throws Exception {
findObjects(prevalentSystem);
checkPrecondition();
@@ -53,7 +56,7 @@ public Serializable execute(MementoCollector collector, Object prevalentSystem)
*
* @param system The prevalent system in which to find the objects.
*/
- protected abstract void findObjects(Object prevalentSystem) throws Exception;
+ protected abstract void findObjects(Bank prevalentSystem) throws Exception;
/**
* Check the precondition.
@@ -73,5 +76,5 @@ public Serializable execute(MementoCollector collector, Object prevalentSystem)
* @param collector The memento collector which can be used to execute subcommands.
* @return The object returned by the execution of this command. Most commands simply return null.
*/
- protected abstract Serializable execute(MementoCollector collector) throws Exception;
+ protected abstract Account execute(MementoCollector collector) throws Exception;
}
View
44 factory/src/main/java/org/prevayler/PrevaylerFactory.java
@@ -39,16 +39,16 @@
* <br>Also by default, the Prevayler instances created by this class will filter out all Transactions that would throw a RuntimeException or Error if executed on the Prevalent System. This requires enough RAM to hold another copy of the prevalent system.
* @see Prevayler
*/
-public class PrevaylerFactory {
+public class PrevaylerFactory<P extends Serializable>{
- private Object _prevalentSystem;
+ private P _prevalentSystem;
private Clock _clock;
private boolean _transactionFiltering = true;
private boolean _transientMode;
private String _prevalenceDirectory;
- private NullSnapshotManager _nullSnapshotManager;
+ private NullSnapshotManager<P> _nullSnapshotManager;
private long _journalSizeThreshold;
private long _journalAgeThreshold;
@@ -70,7 +70,7 @@
/** Creates a Prevayler that will use a directory called "PrevalenceBase" under the current directory to read and write its .snapshot and .journal files.
* @param newPrevalentSystem The newly started, "empty" prevalent system that will be used as a starting point for every system startup, until the first snapshot is taken.
*/
- public static Prevayler createPrevayler(Serializable newPrevalentSystem) throws Exception {
+ public static <P extends Serializable> Prevayler<P> createPrevayler(P newPrevalentSystem) throws Exception {
return createPrevayler(newPrevalentSystem, "PrevalenceBase");
}
@@ -79,8 +79,8 @@ public static Prevayler createPrevayler(Serializable newPrevalentSystem) throws
* @param newPrevalentSystem The newly started, "empty" prevalent system that will be used as a starting point for every system startup, until the first snapshot is taken.
* @param prevalenceBase The directory where the .snapshot files and .journal files will be read and written.
*/
- public static Prevayler createPrevayler(Serializable newPrevalentSystem, String prevalenceBase) throws Exception {
- PrevaylerFactory factory = new PrevaylerFactory();
+ public static <P extends Serializable> Prevayler<P> createPrevayler(P newPrevalentSystem, String prevalenceBase) throws Exception {
+ PrevaylerFactory<P> factory = new PrevaylerFactory<P>();
factory.configurePrevalentSystem(newPrevalentSystem);
factory.configurePrevalenceDirectory(prevalenceBase);
return factory.create();
@@ -93,10 +93,10 @@ public static Prevayler createPrevayler(Serializable newPrevalentSystem, String
* @param newPrevalentSystem The newly started, "empty" prevalent system.
* @see #createCheckpointPrevayler(Serializable newPrevalentSystem, String snapshotDirectory)
*/
- public static Prevayler createTransientPrevayler(Serializable newPrevalentSystem) {
- PrevaylerFactory factory = new PrevaylerFactory();
+ public static <P extends Serializable> Prevayler<P> createTransientPrevayler(P newPrevalentSystem) {
+ PrevaylerFactory<P> factory = new PrevaylerFactory<P>();
factory.configurePrevalentSystem(newPrevalentSystem);
- factory.configureNullSnapshotManager(new NullSnapshotManager(newPrevalentSystem, "Transient Prevaylers are unable to take snapshots."));
+ factory.configureNullSnapshotManager(new NullSnapshotManager<P>(newPrevalentSystem, "Transient Prevaylers are unable to take snapshots."));
factory.configureTransientMode(true);
try {
return factory.create();
@@ -109,16 +109,16 @@ public static Prevayler createTransientPrevayler(Serializable newPrevalentSystem
/** @deprecated Use createCheckpointPrevayler() instead of this method. Deprecated since Prevayler2.00.001.
*/
- public static Prevayler createTransientPrevayler(Serializable newPrevalentSystem, String snapshotDirectory) {
+ public static <P extends Serializable> Prevayler<P> createTransientPrevayler(P newPrevalentSystem, String snapshotDirectory) {
return createCheckpointPrevayler(newPrevalentSystem, snapshotDirectory);
}
/** Creates a Prevayler that will execute Transactions WITHOUT writing them to disk. Snapshots will work as "checkpoints" for the system, therefore. This is useful for stand-alone applications that have a "Save" button, for example.
* @param newPrevalentSystem The newly started, "empty" prevalent system that will be used as a starting point for every system startup, until the first snapshot is taken.
* @param snapshotDirectory The directory where the .snapshot files will be read and written.
*/
- public static Prevayler createCheckpointPrevayler(Serializable newPrevalentSystem, String snapshotDirectory) {
- PrevaylerFactory factory = new PrevaylerFactory();
+ public static <P extends Serializable> Prevayler<P> createCheckpointPrevayler(P newPrevalentSystem, String snapshotDirectory) {
+ PrevaylerFactory<P> factory = new PrevaylerFactory<P>();
factory.configurePrevalentSystem(newPrevalentSystem);
factory.configurePrevalenceDirectory(snapshotDirectory);
factory.configureTransientMode(true);
@@ -171,7 +171,7 @@ public void configurePrevalenceDirectory(String prevalenceDirectory) {
* @param newPrevalentSystem If the default Serializer is used, this prevalentSystem must be Serializable. If another Serializer is used, this prevalentSystem must be compatible with it.
* @see #configureSnapshotSerializer(String,Serializer)
*/
- public void configurePrevalentSystem(Object newPrevalentSystem) {
+ public void configurePrevalentSystem(P newPrevalentSystem) {
_prevalentSystem = newPrevalentSystem;
}
@@ -191,7 +191,7 @@ public void configureReplicationServer(int port) {
}
- private void configureNullSnapshotManager(NullSnapshotManager snapshotManager) {
+ private void configureNullSnapshotManager(NullSnapshotManager<P> snapshotManager) {
_nullSnapshotManager = snapshotManager;
}
@@ -288,11 +288,11 @@ public void configureSnapshotSerializer(String suffix, Serializer serializer) {
* @throws IOException If there is trouble creating the Prevalence Base directory or reading a .journal or .snapshot file.
* @throws ClassNotFoundException If a class of a serialized Object is not found when reading a .journal or .snapshot file.
*/
- public Prevayler create() throws Exception {
- GenericSnapshotManager snapshotManager = snapshotManager();
+ public Prevayler<P> create() throws Exception {
+ GenericSnapshotManager<P> snapshotManager = snapshotManager();
TransactionPublisher publisher = publisher(snapshotManager);
if (_serverPort != -1) new ServerListener(publisher, new OldNetworkImpl(), _serverPort);
- return new PrevaylerImpl(snapshotManager, publisher, journalSerializer());
+ return new PrevaylerImpl<P>(snapshotManager, publisher, journalSerializer());
}
@@ -301,13 +301,13 @@ private String prevalenceDirectory() {
}
- private Object prevalentSystem() {
+ private P prevalentSystem() {
if (_prevalentSystem == null) throw new IllegalStateException("The prevalent system must be configured.");
return _prevalentSystem;
}
- private TransactionPublisher publisher(GenericSnapshotManager snapshotManager) throws IOException {
+ private TransactionPublisher publisher(GenericSnapshotManager<P> snapshotManager) throws IOException {
if (_remoteServerIpAddress != null) return new ClientPublisher(new OldNetworkImpl(), _remoteServerIpAddress, _remoteServerPort);
return new CentralPublisher(clock(), censor(snapshotManager), journal());
}
@@ -339,17 +339,17 @@ private String journalSuffix() {
return _journalSuffix != null ? _journalSuffix : "journal";
}
- private GenericSnapshotManager snapshotManager() throws Exception {
+ private GenericSnapshotManager<P> snapshotManager() throws Exception {
if (_nullSnapshotManager != null)
return _nullSnapshotManager;
PrevaylerDirectory directory = new PrevaylerDirectory(prevalenceDirectory());
if (!_snapshotSerializers.isEmpty())
- return new GenericSnapshotManager(_snapshotSerializers, _primarySnapshotSuffix, prevalentSystem(), directory, journalSerializer());
+ return new GenericSnapshotManager<P>(_snapshotSerializers, _primarySnapshotSuffix, prevalentSystem(), directory, journalSerializer());
String snapshotSuffix = "snapshot";
JavaSerializer snapshotSerializer = new JavaSerializer();
- return new GenericSnapshotManager(Collections.singletonMap(snapshotSuffix, snapshotSerializer), snapshotSuffix, prevalentSystem(), directory, journalSerializer());
+ return new GenericSnapshotManager<P>(Collections.singletonMap(snapshotSuffix, snapshotSerializer), snapshotSuffix, prevalentSystem(), directory, journalSerializer());
}
View
26 spikes/cluster/src/main/org/prevayler/cluster/ClusteredPrevayler.java
@@ -5,14 +5,14 @@
import org.prevayler.*;
-public class ClusteredPrevayler implements Prevayler, ClusterListener {
- private PrevaylerFactory factory;
+public class ClusteredPrevayler<P extends Serializable> implements Prevayler<P>, ClusterListener {
+ private PrevaylerFactory<P> factory;
private String oldPrevalenceBase;
private String newPrevalenceBase;
private Node node;
- private Prevayler prevayler;
+ private Prevayler<P> prevayler;
- public ClusteredPrevayler(PrevaylerFactory factory, String oldPrevalenceBase, String newPrevalenceBase) throws InterruptedException {
+ public ClusteredPrevayler(PrevaylerFactory<P> factory, String oldPrevalenceBase, String newPrevalenceBase) throws InterruptedException {
System.out.println("EXPERIMENTAL CODE! Spike for implementing a clustered homogenous (i.e. without a master) Prevayler using JavaGroups");
System.out.println("For example, the clock is *not* properly synchronized across the cluster");
System.out.println("This code will be thrown away when the real implementation is created");
@@ -28,12 +28,12 @@ public Object receive(Object message) {
return transaction.executeOn(this);
}
- public Object getState() {
+ public P getState() {
System.out.println("Distributing system " + prevalentSystem());
return prevalentSystem();
}
- public void setState(Object prevalentSystem) {
+ public void setState(P prevalentSystem) {
if (prevalentSystem != null) {
backupOldLocalTransactionLogs();
factory.configurePrevalentSystem(prevalentSystem);
@@ -50,29 +50,29 @@ public void setState(Object prevalentSystem) {
}
}
- public Object prevalentSystem() {
+ public P prevalentSystem() {
return prevayler.prevalentSystem();
}
public Clock clock() {
return prevayler.clock();
}
- public void execute(Transaction transaction) {
- ClusteredTransaction clusterTransaction = new ClusteredTransaction(transaction, clock().time());
+ public void execute(Transaction<P> transaction) {
+ ClusteredTransaction<P> clusterTransaction = new ClusteredTransaction<P>(transaction, clock().time());
System.out.println("ClusterTransaction created = " + clusterTransaction);
node.broadcast(clusterTransaction);
}
- public Object execute(Query sensitiveQuery) throws Exception {
+ public <R> R execute(Query<P,R> sensitiveQuery) throws Exception {
return prevayler.execute(sensitiveQuery);
}
- public Object execute(TransactionWithQuery transactionWithQuery) throws Exception {
+ public <R> R execute(TransactionWithQuery<P,R> transactionWithQuery) throws Exception {
throw new UnsupportedOperationException("Not implemented");
}
- public Object execute(SureTransactionWithQuery sureTransactionWithQuery) {
+ public <R> R execute(SureTransactionWithQuery<P,R> sureTransactionWithQuery) {
throw new UnsupportedOperationException("Not implemented");
}
@@ -98,7 +98,7 @@ private void backupOldLocalTransactionLogs() {
new File(newPrevalenceBase).renameTo(oldPrevalenceBaseFile);
}
- public void executeBroadcastedTransaction(Transaction transaction) {
+ public void executeBroadcastedTransaction(Transaction<P> transaction) {
prevayler.execute(transaction);
}
}
View
8 spikes/cluster/src/main/org/prevayler/cluster/ClusteredPrevaylerState.java
@@ -3,16 +3,16 @@
import java.util.Date;
import java.io.Serializable;
-class ClusteredPrevaylerState implements Serializable {
- private Object system;
+class ClusteredPrevaylerState<P extends Serializable> implements Serializable {
+ private P system;
private Date time;
- public ClusteredPrevaylerState(Object system, Date time) {
+ public ClusteredPrevaylerState(P system, Date time) {
this.system = system;
this.time = time;
}
- public Object getSystem() {
+ public P getSystem() {
return system;
}
View
8 spikes/cluster/src/main/org/prevayler/cluster/ClusteredTransaction.java
@@ -6,16 +6,16 @@
import org.prevayler.Transaction;
import org.prevayler.cluster.ClusteredPrevayler;
-class ClusteredTransaction implements Serializable {
- private Transaction transaction;
+class ClusteredTransaction<P extends Serializable> implements Serializable {
+ private Transaction<P> transaction;
private Date executionTime;
- public ClusteredTransaction(Transaction transaction, Date executionTime) {
+ public ClusteredTransaction(Transaction<P> transaction, Date executionTime) {
this.transaction = transaction;
this.executionTime = executionTime;
}
- public Object executeOn(ClusteredPrevayler prevayler) {
+ public Object executeOn(ClusteredPrevayler<P> prevayler) {
// TODO what about executionTime!
System.out.println("Executing " + transaction);
try {
View
18 spikes/mirror-tests/src/test/java/org/prevayler/implementation/ReplicationTest.java
@@ -13,8 +13,8 @@
public class ReplicationTest extends FileIOTest {
- private Prevayler _server;
- private Prevayler _client;
+ private Prevayler<AppendingSystem> _server;
+ private Prevayler<AppendingSystem> _client;
public void testServerFirst() throws Exception {
serverCrashRecover(0);
@@ -85,28 +85,28 @@ private void clientAppend(String appendix, String expectedResult) {
append(_client, appendix, expectedResult);
}
- private void append(Prevayler prevayler, String appendix, String expectedResult) {
+ private void append(Prevayler<AppendingSystem> prevayler, String appendix, String expectedResult) {
prevayler.execute(new Appendix(appendix));
Cool.sleep(10);
assertEquals(expectedResult, serverValue());
assertEquals(expectedResult, clientValue());
}
private void serverCrashRecover(int portOffset) throws Exception {
- PrevaylerFactory factory = factory("server");
+ PrevaylerFactory<AppendingSystem> factory = factory("server");
factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
factory.configureTransientMode(true);
_server = factory.create();
}
private void clientCrashRecover(int portOffset) throws Exception {
- PrevaylerFactory factory = factory("client");
+ PrevaylerFactory<AppendingSystem> factory = factory("client");
factory.configureReplicationClient("localhost", PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
_client = factory.create();
}
- private PrevaylerFactory factory(String directory) {
- PrevaylerFactory factory = new PrevaylerFactory();
+ private PrevaylerFactory<AppendingSystem> factory(String directory) {
+ PrevaylerFactory<AppendingSystem> factory = new PrevaylerFactory<AppendingSystem>();
factory.configurePrevalentSystem(new AppendingSystem());
factory.configurePrevalenceDirectory(_testDirectory + File.separator + directory);
return factory;
@@ -120,12 +120,12 @@ protected void tearDown() throws Exception {
}
private String serverValue() {
- return ((AppendingSystem)_server.prevalentSystem()).value();
+ return _server.prevalentSystem().value();
}
private String clientValue() {
Cool.sleep(100); //The client is notified asynchronously.
- return ((AppendingSystem)_client.prevalentSystem()).value();
+ return _client.prevalentSystem().value();
}
}
View
6 tests/src/main/java/org/prevayler/implementation/AppendTransaction.java
@@ -4,7 +4,7 @@
import java.util.Date;
-public class AppendTransaction implements Transaction {
+public class AppendTransaction implements Transaction<StringBuffer> {
private static final long serialVersionUID = -3830205386199825379L;
public String toAdd;
@@ -17,8 +17,8 @@ public AppendTransaction(String toAdd) {
this.toAdd = toAdd;
}
- public void executeOn(Object prevalentSystem, Date executionTime) {
- ((StringBuffer)prevalentSystem).append(toAdd);
+ public void executeOn(StringBuffer prevalentSystem, Date executionTime) {
+ prevalentSystem.append(toAdd);
}
}
View
10 tests/src/main/java/org/prevayler/implementation/AppendTransactionWithQuery.java
@@ -4,7 +4,7 @@
import java.util.Date;
-public class AppendTransactionWithQuery implements TransactionWithQuery {
+public class AppendTransactionWithQuery implements TransactionWithQuery<StringBuffer,String> {
private static final long serialVersionUID = 7725358482908916942L;
public String toAdd;
@@ -17,10 +17,10 @@ public AppendTransactionWithQuery(String toAdd) {
this.toAdd = toAdd;
}
- public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception {
- StringBuffer system = (StringBuffer) prevalentSystem;
- system.append(toAdd);
- return system.toString();
+ public String executeAndQuery(StringBuffer prevalentSystem, Date executionTime) throws Exception {
+
+ prevalentSystem.append(toAdd);
+ return prevalentSystem.toString();
}
}
View
6 tests/src/main/java/org/prevayler/implementation/Appendix.java
@@ -9,13 +9,13 @@
import org.prevayler.Transaction;
-class Appendix implements Transaction {
+class Appendix implements Transaction<AppendingSystem> {
private static final long serialVersionUID = 7925676108189989759L;
private final String appendix;
- public void executeOn(Object prevalentSystem, Date ignored) {
- ((AppendingSystem)prevalentSystem).append(appendix);
+ public void executeOn(AppendingSystem prevalentSystem, Date ignored) {
+ prevalentSystem.append(appendix);
}
Appendix(String appendix) {
View
2 tests/src/main/java/org/prevayler/implementation/NondeterministicErrorTransaction.java
@@ -22,7 +22,7 @@ public NondeterministicErrorTransaction(String toAdd) {
super(toAdd);
}
- public void executeOn(Object prevalentSystem, Date executionTime) {
+ public void executeOn(AppendingSystem prevalentSystem, Date executionTime) {
triggerBomb();
super.executeOn(prevalentSystem, executionTime);
}
View
5 tests/src/main/java/org/prevayler/implementation/NullQuery.java
@@ -1,12 +1,13 @@
package org.prevayler.implementation;
+import java.io.Serializable;
import java.util.Date;
import org.prevayler.Query;
-public class NullQuery implements Query {
+public class NullQuery implements Query<Serializable,Object> {
- public Object query(Object prevalentSystem, Date executionTime) throws Exception {
+ public Object query(Serializable prevalentSystem, Date executionTime) throws Exception {
return null;
}

0 comments on commit 844f2c4

Please sign in to comment.