Package com.storedobject.core
Class StreamContentProducer
java.lang.Object
com.storedobject.core.StreamContentProducer
- All Implemented Interfaces:
Executable,ContentProducer,ContentType,RequiresTransactionManager,Closeable,AutoCloseable,Runnable
- Direct Known Subclasses:
AbstractSpreadSheet,ImageProducer,TextContentProducer
The StreamContentProducer class is an abstract implementation of the
ContentProducer interface
with additional support for managing output streams, transaction management, and access control.
This class is designed for producing content in a stream-based manner, where the actual content generation
must be implemented by subclasses.
It provides facilities for handling concurrent execution, stream and writer management, and integration with a transaction management system. Additionally, it incorporates mechanisms for determining and enforcing content access restrictions.
- Author:
- Syam
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for creating a StreamContentProducer instance.Constructs a StreamContentProducer with the specified output stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method may be called when the content generation is aborted due to some error.voidclose()abstract voidGenerate the content here and write to 'out'Retrieves the content as an InputStream.final EntityRetrieves theEntityassociated with the current context.getError()Get the current error (set viaContentProducer.abort(Throwable)).Retrieves the name of the file associated with the content.final ReportFormatRetrieves the report format configuration associated with the current transaction manager or entity context.Retrieves the transaction manager associated with this object.protected Writerstatic booleanisBlocked(ContentProducer contentProducer, String type) final booleanvoidproduce()Initiates the content production process and manages its lifecycle.voidready()Waits for the readiness signal, blocking the current thread until the condition is met.voidSets the transaction manager for this object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.storedobject.core.ContentProducer
execute, extractContent, getFileExtension, getStreamData, getStreamDataProvider, getSystemEntity, saveTo, saveTo, saveTo, saveToMethods inherited from interface com.storedobject.core.ContentType
getContentType, getLink, getMimeType, isAudio, isHTML, isImage, isLink, isMedia, isPDF, isText, isVideoMethods inherited from interface com.storedobject.common.Executable
act, run
-
Field Details
-
out
-
entity
-
-
Constructor Details
-
StreamContentProducer
public StreamContentProducer()Default constructor for creating a StreamContentProducer instance. Initializes the instance without specifying an output stream. The output stream will be set to null. -
StreamContentProducer
Constructs a StreamContentProducer with the specified output stream. This constructor initializes the instance with the provided OutputStream and sets up a data listener to handle readiness notifications.- Parameters:
out- The output stream where the generated content will be written. Cannot be null.
-
-
Method Details
-
produce
public void produce()Initiates the content production process and manages its lifecycle. This method ensures thread-safe execution by allowing only one thread to execute the critical section at a time. It prepares the output stream, retrieves the input content, generates the content, and finally, closes the output resources. In case of any error during the content generation or processing, it handles the exception by logging it and invoking the abort mechanism.The following describes the key stages in the process: - Thread-safe execution is ensured by synchronizing on the current object and checking the executing flag. - If the output stream has not been initialized, it retrieves it from the associated IO instance. - Content generation is initiated via the abstract `generateContent` method, which subclasses must implement to define specific content creation logic. - Resources are properly closed by invoking the `close` method. - On encountering a throwable, the exception is logged via the ApplicationServer, and the abort mechanism is triggered with the error details.
Note: Implementations of the abstract `generateContent` method must ensure that content is written to the 'out' stream. Thread starvation is mitigated by calling `Thread.yield()` if the current thread cannot acquire execution.- Specified by:
producein interfaceContentProducer
-
ready
public void ready()Waits for the readiness signal, blocking the current thread until the condition is met. This method ensures that dependent threads can coordinate their execution based on the readiness state. It uses the `ready` CountDownLatch to manage synchronization. Any interruption of the waiting thread is caught and ignored, ensuring that the method continues operation without propagating the `InterruptedException`.- Specified by:
readyin interfaceContentProducer
-
getFileName
Description copied from interface:ContentProducerRetrieves the name of the file associated with the content.- Specified by:
getFileNamein interfaceContentProducer- Returns:
- a String representing the name of the file, or null if the file name is unavailable.
-
generateContent
-
getContent
Description copied from interface:ContentProducerRetrieves the content as an InputStream.Note: This may not invoke
ContentProducer.produce()and it may just return the content if it is already available. It is implementation-dependent.- Specified by:
getContentin interfaceContentProducer- Returns:
- an InputStream representing the content. This may be null if the content is not yet available.
-
setTransactionManager
Description copied from interface:RequiresTransactionManagerSets the transaction manager for this object. The transaction manager is responsible for managing transaction boundaries.- Specified by:
setTransactionManagerin interfaceRequiresTransactionManager- Parameters:
tm- the transaction manager to be set
-
getTransactionManager
Description copied from interface:RequiresTransactionManagerRetrieves the transaction manager associated with this object.- Specified by:
getTransactionManagerin interfaceContentProducer- Specified by:
getTransactionManagerin interfaceRequiresTransactionManager- Returns:
- the transaction manager responsible for managing transaction boundaries
-
getWriter
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
abort
Description copied from interface:ContentProducerThis method may be called when the content generation is aborted due to some error.Note: This could be invoked multiple times.
- Specified by:
abortin interfaceContentProducer- Parameters:
error- Error.
-
getError
Description copied from interface:ContentProducerGet the current error (set viaContentProducer.abort(Throwable)).- Specified by:
getErrorin interfaceContentProducer- Returns:
- Current error, if any.
-
getReportFormat
Retrieves the report format configuration associated with the current transaction manager or entity context. If a transaction manager is available, the report format is obtained based on it. Otherwise, the report format is derived from the current entity.- Returns:
- The report format associated with the current transaction manager if available, otherwise the report format associated with the current entity.
-
getEntity
Description copied from interface:ContentProducerRetrieves theEntityassociated with the current context. This method internally checks for aSystemEntityusingContentProducer.getSystemEntity(), and if it exists, retrieves the correspondingEntity.- Specified by:
getEntityin interfaceContentProducer- Returns:
- an
Entityobject if aSystemEntityis available and associated with anEntity, or null if no such association exists.
-
isBlocked
-
isBlocked
-