Author: ozeigermann
Date: Thu Jul 12 04:14:42 2007
New Revision: 555581

URL: http://svn.apache.org/viewvc?view=rev&rev=555581
Log:
More cleanup and addition of input stream multicaster

Added:
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/
      - copied from r555573, 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/transaction/file/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/ComboInputStreamMulticaster.java
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/InputStreamMulticaster.java
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/
      - copied from r555573, 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/transaction/locking/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java
Removed:
    jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/conf/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/transaction/
    jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java1.4/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/FileResourceManagerTest.java
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/locking/
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/memory/
Modified:
    jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/GenericLockManager.java
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/LockManager.java
    
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/NativeLockManager.java

Modified: jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml?view=diff&rev=555581&r1=555580&r2=555581
==============================================================================
--- jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml 
(original)
+++ jakarta/commons/proper/transaction/branches/TRANSACTION_2/project.xml Thu 
Jul 12 04:14:42 2007
@@ -148,7 +148,7 @@
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
-      <version>3.8.1</version>
+      <version>junit-4.3.1</version>
       <properties>
         <scope>test</scope>
       </properties>
@@ -196,12 +196,7 @@
     <aspectSourceDirectory/>
     <unitTest>
       <includes>
-        
<include>org/apache/commons/transaction/file/FileResourceManagerTest.java</include>
-        
<include>org/apache/commons/transaction/memory/MapWrapperTest.java</include>
-        
<include>org/apache/commons/transaction/memory/OptimisticMapWrapperTest.java</include>
-        
<include>org/apache/commons/transaction/memory/PessimisticMapWrapperTest.java</include>
-        
<include>org/apache/commons/transaction/locking/GenericLockTest.java</include>
-        
<include>org/apache/commons/transaction/locking/LockTestRepeatableReads.java</include>
+        
<include>org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java</include>
       </includes>
     </unitTest>
     <resources>

Added: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/ComboInputStreamMulticaster.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/ComboInputStreamMulticaster.java?view=auto&rev=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/ComboInputStreamMulticaster.java
 (added)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/ComboInputStreamMulticaster.java
 Thu Jul 12 04:14:42 2007
@@ -0,0 +1,148 @@
+package org.apache.commons.transaction.file;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class ComboInputStreamMulticaster implements InputStreamMulticaster {
+
+    private int memoryBufferSize = 8192;
+
+    private Log log = LogFactory.getLog(getClass());
+
+    protected List<InputStream> spawned;
+
+    protected byte buf[];
+
+    protected File bufferFile;
+
+    protected boolean isOpen = false;
+
+    public synchronized void close() {
+        if (!isOpen) {
+            throw new IllegalStateException("You can not close: Stream 
multicaster is not open!");
+        }
+        isOpen = false;
+    }
+
+    public synchronized void open(InputStream backingInputStream) throws 
IOException {
+        if (isOpen) {
+            throw new IllegalStateException(
+                    "You can not open a new stream: Stream multicaster is 
already open!");
+        }
+        if (backingInputStream == null) {
+            throw new IllegalStateException("You can not open a null stream!");
+        }
+
+        buf = new byte[memoryBufferSize];
+        spawned = new ArrayList<InputStream>();
+
+        try {
+            int len = backingInputStream.read(buf);
+            // which means the memory buffer hasn't been large enough
+            if (len == buf.length) {
+                // if so, we buffer in a file
+                this.bufferFile = File.createTempFile("muticast", null);
+                OutputStream os = null;
+                try {
+                    os = new BufferedOutputStream(new 
FileOutputStream(bufferFile));
+                    os.write(buf);
+                    int read;
+                    while ((read = backingInputStream.read(buf)) != -1) {
+                        os.write(buf, 0, read);
+                    }
+                } finally {
+                    buf = null;
+                    if (os != null)
+                        os.close();
+                }
+            }
+        } finally {
+            backingInputStream.close();
+        }
+        isOpen = true;
+    }
+
+    public synchronized InputStream spawn() throws IOException {
+        if (!isOpen) {
+            throw new IllegalStateException(
+                    "You can not spwan new streams: Stream multicaster has 
already been closed!");
+        }
+        InputStream sis = null;
+        if (buf != null) {
+            sis = new ByteArrayInputStream(buf) {
+                @Override
+                public void close() throws IOException {
+                    closeSpawned(this);
+                    super.close();
+                }
+            };
+        } else {
+            try {
+                sis = new BufferedInputStream(new FileInputStream(bufferFile)) 
{
+                    @Override
+                    public void close() throws IOException {
+                        closeSpawned(this);
+                        super.close();
+                    }
+                };
+            } catch (FileNotFoundException e) {
+                // fatal as this really should have been created
+                log.fatal("Internal error: Buffer file has not been created", 
e);
+            }
+        }
+        spawned.add(sis);
+        return sis;
+    }
+
+    public int getMemoryBufferSize() {
+        return memoryBufferSize;
+    }
+
+    public void setMemoryBufferSize(int memoryBufferSize) {
+        this.memoryBufferSize = memoryBufferSize;
+    }
+
+    public synchronized void forceShutdown() {
+        isOpen = false;
+        if (spawned != null) {
+            for (InputStream is : spawned) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    log.warn("Could not close spawned input stream on forced 
shutdown", e);
+                }
+            }
+        }
+        cleanUp();
+    }
+
+    protected void closeSpawned(InputStream is) {
+        spawned.remove(this);
+        if (!isOpen && spawned.isEmpty()) {
+            cleanUp();
+        }
+    }
+
+    protected void cleanUp() {
+        if (bufferFile != null) {
+            bufferFile.delete();
+            bufferFile = null;
+        }
+        buf = null;
+        spawned = null;
+    }
+
+}

Added: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/InputStreamMulticaster.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/InputStreamMulticaster.java?view=auto&rev=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/InputStreamMulticaster.java
 (added)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/InputStreamMulticaster.java
 Thu Jul 12 04:14:42 2007
@@ -0,0 +1,9 @@
+package org.apache.commons.transaction.file;
+import java.io.IOException;
+import java.io.InputStream;
+
+public interface InputStreamMulticaster {
+    void open(InputStream backingInputStream) throws IOException;
+    InputStream spawn() throws IOException;
+    void close() throws IOException;
+}

Modified: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/GenericLockManager.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/GenericLockManager.java?view=diff&rev=555581&r1=555573&r2=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/GenericLockManager.java
 (original)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/GenericLockManager.java
 Thu Jul 12 04:14:42 2007
@@ -1,4 +1,4 @@
-package org.apache.transaction.locking;
+package org.apache.commons.transaction.locking;
 
 import java.util.concurrent.ConcurrentHashMap;
 

Modified: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/LockManager.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/LockManager.java?view=diff&rev=555581&r1=555573&r2=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/LockManager.java
 (original)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/LockManager.java
 Thu Jul 12 04:14:42 2007
@@ -1,4 +1,4 @@
-package org.apache.transaction.locking;
+package org.apache.commons.transaction.locking;
 
 public interface LockManager<K, L> {
     public L getLock(K key);

Modified: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/NativeLockManager.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/NativeLockManager.java?view=diff&rev=555581&r1=555573&r2=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/NativeLockManager.java
 (original)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/locking/NativeLockManager.java
 Thu Jul 12 04:14:42 2007
@@ -1,4 +1,4 @@
-package org.apache.transaction.locking;
+package org.apache.commons.transaction.locking;
 
 
 public class NativeLockManager extends GenericLockManager<Object, Object> 
implements LockManager<Object, Object> {

Added: 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java?view=auto&rev=555581
==============================================================================
--- 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java
 (added)
+++ 
jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/test/org/apache/commons/transaction/file/ComboInputStreamMulticasterTest.java
 Thu Jul 12 04:14:42 2007
@@ -0,0 +1,122 @@
+package org.apache.commons.transaction.file;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.commons.transaction.file.ComboInputStreamMulticaster;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ComboInputStreamMulticasterTest {
+    ComboInputStreamMulticaster ism1;
+
+    ComboInputStreamMulticaster ism2;
+
+    ComboInputStreamMulticaster ism3;
+
+    public static junit.framework.Test suite() {
+        return new JUnit4TestAdapter(ComboInputStreamMulticasterTest.class);
+    }
+
+    static InputStream fakeStream(int length) {
+        return new ByteArrayInputStream(init(new byte[length])) {
+            boolean isClosed = false;
+
+            @Override
+            public void close() throws IOException {
+                if (isClosed)
+                    throw new IOException("Already closed!");
+                isClosed = true;
+            }
+        };
+    }
+
+    static InputStream fakeStream() {
+        return fakeStream(1);
+    }
+
+    static byte[] init(byte[] bytes) {
+        for (int i = 0; i < bytes.length; i++) {
+            bytes[i] = (byte) i;
+        }
+        return bytes;
+    }
+
+    @Before
+    public void createMulticasters() {
+        ism1 = new ComboInputStreamMulticaster();
+        ism2 = new ComboInputStreamMulticaster();
+        ism3 = new ComboInputStreamMulticaster();
+    }
+
+    @After
+    public void destroyMulticasters() {
+        ism1.forceShutdown();
+        ism1 = null;
+        ism2.forceShutdown();
+        ism2 = null;
+        ism3.forceShutdown();
+        ism3 = null;
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void closeWithoutOpen() throws IOException {
+        ism1.close();
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void openWhileOpen() throws IOException {
+        ism1.open(fakeStream());
+        ism1.open(fakeStream());
+    }
+
+    @Test(expected = IOException.class)
+    public void backinStreamMemClosed() throws IOException {
+        InputStream backingStream = fakeStream(ism1.getMemoryBufferSize() - 1);
+        ism1.open(backingStream);
+        backingStream.close();
+    }
+
+    @Test(expected = IOException.class)
+    public void backinStreamFileClosed() throws IOException {
+        InputStream backingStream = fakeStream(ism1.getMemoryBufferSize() + 1);
+        ism1.open(backingStream);
+        backingStream.close();
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void spwanWithoutOpen() throws IOException {
+        ism1.spawn();
+    }
+
+    @Test
+    public void bufferMemory() throws IOException {
+        ism1.open(fakeStream(1000));
+
+        InputStream is11 = ism1.spawn();
+        InputStream is12 = ism1.spawn();
+        InputStream is13 = ism1.spawn();
+
+        is11.close();
+        ism1.close();
+
+    }
+
+    @Test
+    public void bufferFile() throws IOException {
+        ism1.open(fakeStream(1000));
+
+        InputStream is11 = ism1.spawn();
+        InputStream is12 = ism1.spawn();
+        InputStream is13 = ism1.spawn();
+
+        is11.close();
+        ism1.close();
+
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to