TrueZIP Kernel 7.0-rc2

de.schlichtherle.truezip.socket
Class ConcurrentOutputShop<E extends Entry>

java.lang.Object
  extended by de.schlichtherle.truezip.entry.DecoratingEntryContainer<E,O>
      extended by de.schlichtherle.truezip.socket.DecoratingOutputShop<E,OutputShop<E>>
          extended by de.schlichtherle.truezip.socket.ConcurrentOutputShop<E>
Type Parameters:
E - The type of the entries.
All Implemented Interfaces:
EntryContainer<E>, OutputService<E>, OutputShop<E>, Closeable, Iterable<E>
Direct Known Subclasses:
FsDefaultArchiveController.Output

@ThreadSafe
public class ConcurrentOutputShop<E extends Entry>
extends DecoratingOutputShop<E,OutputShop<E>>

Decorates an OutputShop to add accounting and multithreading synchronization for all output streams created by the decorated output shop.

Author:
Christian Schlichtherle
See Also:
ConcurrentInputShop

Nested Class Summary
private  class ConcurrentOutputShop.ConcurrentOutputStream
           
private  class ConcurrentOutputShop.SynchronizedConcurrentOutputStream
           
 
Field Summary
private static String CLASS_NAME
           
private  boolean closed
           
private static Logger logger
           
private  Map<Closeable,Thread> threads
          The pool of all open entry streams.
 
Fields inherited from class de.schlichtherle.truezip.entry.DecoratingEntryContainer
delegate
 
Constructor Summary
ConcurrentOutputShop(OutputShop<E> output)
          Constructs a concurrent output shop.
 
Method Summary
private  void assertNotShopClosed()
          Needs to be externally synchronized!
 void close()
          Closes this concurrent output shop.
<E extends Exception>
void
closeAll(ExceptionHandler<IOException,E> handler)
          Closes and disconnects all entry output streams created by this concurrent output shop.
 OutputSocket<? extends E> getOutputSocket(E entry)
          Returns an output socket for write access to the given entry.
private  int threadStreams()
          Returns the number of streams opened by the current thread.
 int waitCloseOthers(long timeout)
          Waits until all entry output streams which have been opened by other threads get closed or a timeout occurs.
 
Methods inherited from class de.schlichtherle.truezip.entry.DecoratingEntryContainer
getEntry, getSize, iterator, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface de.schlichtherle.truezip.entry.EntryContainer
getEntry, getSize, iterator
 

Field Detail

CLASS_NAME

private static final String CLASS_NAME

logger

private static final Logger logger

threads

private final Map<Closeable,Thread> threads
The pool of all open entry streams. This is implemented as a map where the keys are the streams and the value is the current thread. The weak hash map allows the garbage collector to pick up an entry stream if there are no more references to it. This reduces the likeliness of an OutputBusyException in case a sloppy client application has forgot to close a stream before this output shop gets closed.


closed

private volatile boolean closed
Constructor Detail

ConcurrentOutputShop

public ConcurrentOutputShop(OutputShop<E> output)
Constructs a concurrent output shop.

Parameters:
output - the shop to decorate.
Throws:
NullPointerException - if output is null.
Method Detail

waitCloseOthers

public final int waitCloseOthers(long timeout)
Waits until all entry output streams which have been opened by other threads get closed or a timeout occurs. If the current thread is interrupted while waiting, a warn message is logged using java.util.logging and this method returns.

Unless otherwise prevented, another thread could immediately open another stream upon return of this method. So there is actually no guarantee that really all streams are closed upon return of this method - use carefully!

Returns:
The number of all open streams.

threadStreams

private int threadStreams()
Returns the number of streams opened by the current thread.


closeAll

public final <E extends Exception> void closeAll(ExceptionHandler<IOException,E> handler)
                    throws E extends Exception
Closes and disconnects all entry output streams created by this concurrent output shop. Disconnecting means that any subsequent operation on the entry streams will throw an IOException, with the exception of their close() method.

Throws:
E extends Exception

close

public final void close()
                 throws IOException
Closes this concurrent output shop.

Specified by:
close in interface Closeable
Overrides:
close in class DecoratingOutputShop<E extends Entry,OutputShop<E extends Entry>>
Throws:
IllegalStateException - If any open output streams are detected.
IOException
See Also:
closeAll(de.schlichtherle.truezip.util.ExceptionHandler)

assertNotShopClosed

private void assertNotShopClosed()
                          throws IOException
Needs to be externally synchronized!

Throws:
IOException

getOutputSocket

public final OutputSocket<? extends E> getOutputSocket(E entry)
Description copied from interface: OutputService
Returns an output socket for write access to the given entry.

Specified by:
getOutputSocket in interface OutputService<E extends Entry>
Overrides:
getOutputSocket in class DecoratingOutputShop<E extends Entry,OutputShop<E extends Entry>>
Parameters:
entry - the non-null local target.
Returns:
An output socket for writing to the local target.

TrueZIP Kernel 7.0-rc2

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.