org.alfresco.repo.content
Class AbstractContentWriter

java.lang.Object
  extended by org.alfresco.repo.content.AbstractContentAccessor
      extended by org.alfresco.repo.content.AbstractContentWriter
All Implemented Interfaces:
ContentAccessor, ContentWriter
Direct Known Subclasses:
FileContentWriter

public abstract class AbstractContentWriter
extends AbstractContentAccessor
implements ContentWriter

Implements all the convenience methods of the interface. The only methods that need to be implemented, i.e. provide low-level content access are:


Nested Class Summary
 
Nested classes/interfaces inherited from class org.alfresco.repo.content.AbstractContentAccessor
AbstractContentAccessor.CallbackFileChannel, AbstractContentAccessor.ChannelCloseCallbackAdvise
 
Constructor Summary
protected AbstractContentWriter(java.lang.String contentUrl, ContentReader existingContentReader)
           
 
Method Summary
 void addListener(ContentStreamListener listener)
          Adds the listener after checking that the output stream isn't already in use.
protected abstract  ContentReader createReader()
          A factory method for subclasses to implement that will ensure the proper implementation of the ContentWriter.getReader() method.
 java.io.OutputStream getContentOutputStream()
          Get a stream to write to the underlying channel.
protected abstract  java.nio.channels.WritableByteChannel getDirectWritableChannel()
          Provides low-level access to write content to the repository.
protected  ContentReader getExistingContentReader()
           
 java.nio.channels.FileChannel getFileChannel(boolean truncate)
          Provides read-write, random-access to the underlying content.
 ContentReader getReader()
          Performs checks and copies required reader attributes
 java.nio.channels.WritableByteChannel getWritableChannel()
          Provides low-level access to write to repository content.
 boolean isChannelOpen()
          Gets the open/close state of the underlying IO Channel.
 boolean isClosed()
          An automatically created listener sets the flag
 void putContent(ContentReader reader)
          Copies content from the reader.
 void putContent(java.io.File file)
          Puts content to the repository direct from file
 void putContent(java.io.InputStream is)
          Puts content to the repository
 void putContent(java.lang.String content)
          Makes use of the encoding, if available, to convert the string to bytes.
 
Methods inherited from class org.alfresco.repo.content.AbstractContentAccessor
channelOpened, finalize, getCallbackFileChannel, getContentData, getContentUrl, getEncoding, getLocale, getMimetype, setContentUrl, setEncoding, setLocale, setMimetype, setRetryingTransactionHelper, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.alfresco.service.cmr.repository.ContentAccessor
getContentData, getContentUrl, getEncoding, getLocale, getMimetype, getSize, setEncoding, setLocale, setMimetype, setRetryingTransactionHelper
 

Constructor Detail

AbstractContentWriter

protected AbstractContentWriter(java.lang.String contentUrl,
                                ContentReader existingContentReader)
Parameters:
contentUrl - the content URL
existingContentReader - a reader of a previous version of this content
Method Detail

getExistingContentReader

protected ContentReader getExistingContentReader()
Returns:
Returns a reader onto the previous version of this content

addListener

public void addListener(ContentStreamListener listener)
Adds the listener after checking that the output stream isn't already in use.

Specified by:
addListener in interface ContentAccessor
Parameters:
listener - a listener that will be called for output stream event notification
See Also:
ContentAccessor.setRetryingTransactionHelper(RetryingTransactionHelper)

createReader

protected abstract ContentReader createReader()
                                       throws ContentIOException
A factory method for subclasses to implement that will ensure the proper implementation of the ContentWriter.getReader() method.

Only the instance need be constructed. The required mimetype, encoding, etc will be copied across by this class.

Returns:
Returns a reader onto the location referenced by this instance. The instance must always be a new instance and never null.
Throws:
ContentIOException

getReader

public final ContentReader getReader()
                              throws ContentIOException
Performs checks and copies required reader attributes

Specified by:
getReader in interface ContentWriter
Returns:
Returns a reader onto the underlying content that this writer will or has written to
Throws:
ContentIOException

isClosed

public final boolean isClosed()
An automatically created listener sets the flag

Specified by:
isClosed in interface ContentWriter
Returns:
Return true if the content output stream has been used and closed otherwise false.

isChannelOpen

public boolean isChannelOpen()
Description copied from interface: ContentAccessor
Gets the open/close state of the underlying IO Channel.

Specified by:
isChannelOpen in interface ContentAccessor
Returns:
Returns true if the underlying IO Channel is open

getDirectWritableChannel

protected abstract java.nio.channels.WritableByteChannel getDirectWritableChannel()
                                                                           throws ContentIOException
Provides low-level access to write content to the repository.

This is the only of the content writing methods that needs to be implemented by derived classes. All other content access methods make use of this in their underlying implementations.

Returns:
Returns a channel with which to write content
Throws:
ContentIOException - if the channel could not be opened

getWritableChannel

public final java.nio.channels.WritableByteChannel getWritableChannel()
                                                               throws ContentIOException
Description copied from interface: ContentWriter
Provides low-level access to write to repository content.

The channel returned to the client should remain open (subject to timeouts) until closed by the client. All lock detection, read-only access and other concurrency issues are dealt with during this operation. It remains possible that implementations will throw exceptions when the channel is closed.

The stream will notify any listeners according to the listener interface.

Specified by:
getWritableChannel in interface ContentWriter
Returns:
Returns a channel with which to write content
Throws:
ContentIOException
See Also:
AbstractContentWriter.getDirectWritableChannel(), #getCallbackWritableChannel()

getFileChannel

public java.nio.channels.FileChannel getFileChannel(boolean truncate)
                                             throws ContentIOException
Provides read-write, random-access to the underlying content. In general, this method should be considered more expensive than the sequential-access method, ContentWriter.getWritableChannel().

Underlying implementations use the truncate parameter to determine the most effective means of providing access to the content.

Specified by:
getFileChannel in interface ContentWriter
Parameters:
truncate - true to start with zero length content
Returns:
Returns a random-access channel onto the content
Throws:
ContentIOException
See Also:
ContentWriter.getWritableChannel(), RandomAccessFile.getChannel()

getContentOutputStream

public java.io.OutputStream getContentOutputStream()
                                            throws ContentIOException
Description copied from interface: ContentWriter
Get a stream to write to the underlying channel.

Specified by:
getContentOutputStream in interface ContentWriter
Returns:
Returns an output stream onto the underlying channel
Throws:
ContentIOException
See Also:
Channels.newOutputStream(java.nio.channels.WritableByteChannel)

putContent

public void putContent(ContentReader reader)
                throws ContentIOException
Description copied from interface: ContentWriter
Copies content from the reader.

All resources will be closed automatically.

Specified by:
putContent in interface ContentWriter
Parameters:
reader - the reader acting as the source of the content
Throws:
ContentIOException
See Also:
ContentReader.getContentInputStream(), AbstractContentWriter.putContent(InputStream)

putContent

public final void putContent(java.io.InputStream is)
                      throws ContentIOException
Description copied from interface: ContentWriter
Puts content to the repository

All resources will be closed automatically.

Specified by:
putContent in interface ContentWriter
Parameters:
is - the input stream from which the content will be read
Throws:
ContentIOException
See Also:
ContentWriter.getWritableChannel()

putContent

public final void putContent(java.io.File file)
                      throws ContentIOException
Description copied from interface: ContentWriter
Puts content to the repository direct from file

All resources will be closed automatically.

Specified by:
putContent in interface ContentWriter
Parameters:
file - the file to load the content from
Throws:
ContentIOException
See Also:
ContentWriter.getWritableChannel()

putContent

public final void putContent(java.lang.String content)
                      throws ContentIOException
Makes use of the encoding, if available, to convert the string to bytes.

Specified by:
putContent in interface ContentWriter
Parameters:
content - a string representation of the content
Throws:
ContentIOException
See Also:
ContentAccessor.getEncoding()


Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.