Permalink
Browse files

Added GZIPSerializer and DESSerializer.

  • Loading branch information...
1 parent a4db09f commit fd6715373fa4f07bbe2a8cf3fb24c3b278c1c437 @jsampson committed Mar 14, 2009
Showing with 311 additions and 311 deletions.
  1. +6 −0 CHANGES.txt
  2. +2 −2 core/src/main/java/org/prevayler/Prevayler.java
  3. +6 −14 core/src/main/java/org/prevayler/foundation/DeepCopier.java
  4. +0 −24 core/src/main/java/org/prevayler/foundation/gzip/ContinuableGZIPInputStream.java
  5. +0 −64 core/src/main/java/org/prevayler/foundation/gzip/MultiMemberGZIPInputStream.java
  6. +0 −44 core/src/main/java/org/prevayler/foundation/gzip/MultiMemberGZIPOutputStream.java
  7. +0 −33 core/src/main/java/org/prevayler/foundation/gzip/NonCloseableOutputStream.java
  8. +90 −0 core/src/main/java/org/prevayler/foundation/serialization/DESSerializer.java
  9. +44 −0 core/src/main/java/org/prevayler/foundation/serialization/GZIPSerializer.java
  10. +2 −3 core/src/main/java/org/prevayler/foundation/serialization/Serializer.java
  11. +2 −2 core/src/main/java/org/prevayler/implementation/PrevalentSystemGuard.java
  12. +1 −1 core/src/main/java/org/prevayler/implementation/PrevaylerImpl.java
  13. +4 −4 core/src/main/java/org/prevayler/implementation/snapshot/GenericSnapshotManager.java
  14. +1 −2 demos/scalability/src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerQuerySubject.java
  15. +2 −3 ...lability/src/main/java/org/prevayler/demos/scalability/prevayler/PrevaylerTransactionSubject.java
  16. +1 −2 extras/memento/src/main/java/org/prevayler/demos/memento/TestErrorRecoveryWithMementos.java
  17. +4 −4 factory/src/main/java/org/prevayler/PrevaylerFactory.java
  18. +20 −15 tests/src/main/java/org/prevayler/foundation/FileIOTest.java
  19. +5 −6 tests/src/test/java/org/prevayler/foundation/DeepCopierTest.java
  20. +0 −43 tests/src/test/java/org/prevayler/foundation/gzip/MultiMemberGZIPTest.java
  21. +1 −3 tests/src/test/java/org/prevayler/implementation/CheckpointTest.java
  22. +1 −1 tests/src/test/java/org/prevayler/implementation/JournalFileRollingTest.java
  23. +92 −9 tests/src/test/java/org/prevayler/implementation/JournalSerializerTest.java
  24. +1 −1 tests/src/test/java/org/prevayler/implementation/PersistenceTest.java
  25. +2 −3 tests/src/test/java/org/prevayler/implementation/PrevaylerFactoryTest.java
  26. +4 −4 tests/src/test/java/org/prevayler/implementation/SkipOldTransactionsTest.java
  27. +6 −9 tests/src/test/java/org/prevayler/implementation/SnapshotSerializerTest.java
  28. +1 −1 tests/src/test/java/org/prevayler/implementation/TransientPrevaylerTest.java
  29. +13 −14 tests/src/test/java/org/prevayler/implementation/snapshot/GenericSnapshotManagerTest.java
View
@@ -58,6 +58,12 @@ calling threads would simply block, which had the result of hanging the
application. Now, an exception is thrown (TurnAbortedException) to indicate
that processing has been halted.
+--------------------------------
+GZIPSerializer and DESSerializer
+
+Serializers are now provided that can wrap any other Serializer with
+GZIP compression and DES or 3DES encryption.
+
---------------------------------------
Workaround for a Java 6 ClassLoader bug
@@ -52,9 +52,9 @@
* This will accelerate future system startups. Taking a snapshot once a day is enough for most applications.
* This method synchronizes on the prevalentSystem() in order to take the snapshot. This means that transaction execution will be blocked while the snapshot is taken.
* @return The file to which the snapshot was written. This file should be left where it is, so that Prevayler can read it during startup. You can copy it to another location for backup purposes if desired.
- * @throws IOException if there is trouble writing to the snapshot file.
+ * @throws Exception if there is trouble writing to the snapshot file or serializing the prevalent system.
*/
- public File takeSnapshot() throws IOException;
+ public File takeSnapshot() throws Exception;
/** Closes any files or other system resources opened by this Prevayler.
* @throws IOException if there is trouble closing a file or some other system resource.
@@ -44,7 +44,7 @@ public static Object deepCopy(Object original, Serializer serializer) {
* object in order to respect any synchronization the caller may have around it, and a new thread is used for
* deserializing the copy.
*/
- public static Object deepCopyParallel(Object original, Serializer serializer) throws IOException, ClassNotFoundException {
+ public static Object deepCopyParallel(Object original, Serializer serializer) throws Exception {
PipedOutputStream outputStream = new PipedOutputStream();
PipedInputStream inputStream = new PipedInputStream(outputStream);
@@ -64,9 +64,7 @@ public static Object deepCopyParallel(Object original, Serializer serializer) th
private InputStream _inputStream;
private Serializer _serializer;
private Object _result;
- private IOException _ioException;
- private ClassNotFoundException _classNotFoundException;
- private RuntimeException _runtimeException;
+ private Exception _exception;
private Error _error;
public Receiver(InputStream inputStream, Serializer serializer) {
@@ -78,12 +76,8 @@ public Receiver(InputStream inputStream, Serializer serializer) {
public void run() {
try {
_result = _serializer.readObject(_inputStream);
- } catch (IOException e) {
- _ioException = e;
- } catch (ClassNotFoundException e) {
- _classNotFoundException = e;
- } catch (RuntimeException e) {
- _runtimeException = e;
+ } catch (Exception e) {
+ _exception = e;
} catch (Error e) {
_error = e;
throw e;
@@ -99,7 +93,7 @@ public void run() {
}
}
- public Object getResult() throws ClassNotFoundException, IOException {
+ public Object getResult() throws Exception {
try {
join();
} catch (InterruptedException e) {
@@ -109,9 +103,7 @@ public Object getResult() throws ClassNotFoundException, IOException {
// join() guarantees that all shared memory is synchronized between the two threads
if (_error != null) throw new RuntimeException("Error during deserialization", _error);
- if (_runtimeException != null) throw _runtimeException;
- if (_classNotFoundException != null) throw _classNotFoundException;
- if (_ioException != null) throw _ioException;
+ if (_exception != null) throw _exception;
if (_result == null) throw new RuntimeException("Deep copy failed in an unknown way");
return _result;
@@ -1,24 +0,0 @@
-package org.prevayler.foundation.gzip;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.SequenceInputStream;
-import java.util.zip.GZIPInputStream;
-
-public class ContinuableGZIPInputStream extends GZIPInputStream {
-
- public ContinuableGZIPInputStream(InputStream stream) throws IOException {
- super(stream);
- }
-
- public InputStream remainingInput() {
- int afterTrailer = inf.getRemaining() - 8;
- if (afterTrailer > 0) {
- return new SequenceInputStream(new ByteArrayInputStream(buf, len - afterTrailer, afterTrailer), in);
- } else {
- return in;
- }
- }
-
-}
@@ -1,64 +0,0 @@
-package org.prevayler.foundation.gzip;
-
-import org.prevayler.foundation.gzip.ContinuableGZIPInputStream;
-
-import java.io.EOFException;
-import java.io.IOException;
-import java.io.InputStream;
-
-public class MultiMemberGZIPInputStream extends InputStream {
-
- private ContinuableGZIPInputStream _gzip;
-
- public MultiMemberGZIPInputStream(InputStream stream) throws IOException {
- _gzip = new ContinuableGZIPInputStream(stream);
- }
-
- public int available() throws IOException {
- return _gzip.available();
- }
-
- public int read() throws IOException {
- byte[] buf = new byte[1];
- int n = read(buf);
- return n == -1 ? -1 : buf[0];
- }
-
- public int read(byte b[]) throws IOException {
- return read(b, 0, b.length);
- }
-
- public int read(byte b[], int off, int len) throws IOException {
- int n = _gzip.read(b, off, len);
- if (n == -1) {
- try {
- _gzip = new ContinuableGZIPInputStream(_gzip.remainingInput());
- } catch (EOFException e) {
- return -1;
- }
- return _gzip.read(b, off, len);
- }
- return n;
- }
-
- public void close() throws IOException {
- throw new UnsupportedOperationException();
- }
-
- public void reset() throws IOException {
- throw new UnsupportedOperationException();
- }
-
- public boolean markSupported() {
- return false;
- }
-
- public void mark(int readlimit) {
- throw new UnsupportedOperationException();
- }
-
- public long skip(long n) throws IOException {
- throw new UnsupportedOperationException();
- }
-
-}
@@ -1,44 +0,0 @@
-package org.prevayler.foundation.gzip;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.zip.GZIPOutputStream;
-
-public class MultiMemberGZIPOutputStream extends OutputStream {
-
- private OutputStream _stream;
- private GZIPOutputStream _gzip;
-
- public MultiMemberGZIPOutputStream(OutputStream stream) {
- _stream = new NonCloseableOutputStream(stream);
- _gzip = null;
- }
-
- public void write(int b) throws IOException {
- write(new byte[]{(byte) b});
- }
-
- public void write(byte b[]) throws IOException {
- write(b, 0, b.length);
- }
-
- public void write(byte b[], int off, int len) throws IOException {
- if (_gzip == null) {
- _gzip = new GZIPOutputStream(_stream);
- }
- _gzip.write(b, off, len);
- }
-
- public void flush() throws IOException {
- if (_gzip != null) {
- _gzip.close();
- _gzip = null;
- }
- _stream.flush();
- }
-
- public void close() throws IOException {
- throw new UnsupportedOperationException();
- }
-
-}
@@ -1,33 +0,0 @@
-package org.prevayler.foundation.gzip;
-
-import java.io.IOException;
-import java.io.OutputStream;
-
-public class NonCloseableOutputStream extends OutputStream {
-
- private final OutputStream _stream;
-
- public NonCloseableOutputStream(OutputStream stream) {
- _stream = stream;
- }
-
- public void close() throws IOException {
- }
-
- public void flush() throws IOException {
- _stream.flush();
- }
-
- public void write(int b) throws IOException {
- _stream.write(b);
- }
-
- public void write(byte b[]) throws IOException {
- _stream.write(b);
- }
-
- public void write(byte b[], int off, int len) throws IOException {
- _stream.write(b, off, len);
- }
-
-}
@@ -0,0 +1,90 @@
+// Prevayler, The Free-Software Prevalence Layer
+// Copyright 2001-2006 by Klaus Wuestefeld
+//
+// 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.
+//
+// Prevayler is a trademark of Klaus Wuestefeld.
+// See the LICENSE file for license details.
+
+package org.prevayler.foundation.serialization;
+
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESKeySpec;
+import javax.crypto.spec.DESedeKeySpec;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.GeneralSecurityException;
+import java.security.spec.KeySpec;
+
+public class DESSerializer implements Serializer {
+
+ private ThreadLocal _ciphers = new ThreadLocal() {
+ protected Object initialValue() {
+ try {
+ return Cipher.getInstance(_triple ? "DESede" : "DES");
+ } catch (GeneralSecurityException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ private final Serializer _delegate;
+
+ private final SecretKey _key;
+
+ private final boolean _triple;
+
+ /**
+ * @param key
+ * An 8-byte DES key or a 24-byte 3DES key.
+ */
+ public DESSerializer(Serializer delegate, byte[] key) throws GeneralSecurityException {
+ _delegate = delegate;
+ if (key != null && key.length == 8) {
+ _triple = false;
+ } else if (key != null && key.length == 24) {
+ _triple = true;
+ } else {
+ throw new IllegalArgumentException("Key must be 8 or 24 bytes");
+ }
+ KeySpec keySpec = _triple ?
+ (KeySpec) new DESedeKeySpec(key) :
+ (KeySpec) new DESKeySpec(key);
+ _key = SecretKeyFactory.getInstance(_triple ? "DESede" : "DES").generateSecret(keySpec);
+ }
+
+ public void writeObject(OutputStream stream, Object object) throws Exception {
+ Cipher cipher = getCipher();
+ cipher.init(Cipher.ENCRYPT_MODE, _key);
+ CipherOutputStream encrypt = new CipherOutputStream(stream, cipher);
+ _delegate.writeObject(encrypt, object);
+ encrypt.close();
+ }
+
+ public Object readObject(InputStream stream) throws Exception {
+ Cipher cipher = getCipher();
+ cipher.init(Cipher.DECRYPT_MODE, _key);
+ CipherInputStream decrypt = new CipherInputStream(stream, cipher);
+ return _delegate.readObject(decrypt);
+ }
+
+ private Cipher getCipher() throws GeneralSecurityException {
+ try {
+ return (Cipher) _ciphers.get();
+ } catch (RuntimeException e) {
+ if (e.getCause() instanceof GeneralSecurityException) {
+ throw (GeneralSecurityException) e.getCause();
+ } else {
+ throw e;
+ }
+ }
+ }
+
+}
@@ -0,0 +1,44 @@
+// Prevayler, The Free-Software Prevalence Layer
+// Copyright 2001-2006 by Klaus Wuestefeld
+//
+// 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.
+//
+// Prevayler is a trademark of Klaus Wuestefeld.
+// See the LICENSE file for license details.
+
+package org.prevayler.foundation.serialization;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+public class GZIPSerializer implements Serializer {
+
+ private final Serializer _delegate;
+
+ private final int _bufferSize;
+
+ public GZIPSerializer(Serializer delegate) {
+ this(delegate, 512);
+ }
+
+ public GZIPSerializer(Serializer delegate, int gzipBufferSize) {
+ _delegate = delegate;
+ _bufferSize = gzipBufferSize;
+ }
+
+ public void writeObject(OutputStream stream, Object object) throws Exception {
+ GZIPOutputStream gzip = new GZIPOutputStream(stream, _bufferSize);
+ _delegate.writeObject(gzip, object);
+ gzip.close();
+ }
+
+ public Object readObject(InputStream stream) throws Exception {
+ GZIPInputStream gunzip = new GZIPInputStream(stream, _bufferSize);
+ return _delegate.readObject(gunzip);
+ }
+
+}
Oops, something went wrong.

0 comments on commit fd67153

Please sign in to comment.