Search in sources :

Example 1 with TransactionListenerAdapter

use of org.alfresco.repo.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class ContentNetworkFile method closeFile.

/**
 * Close the file
 *
 * @exception IOException
 */
public void closeFile() throws IOException {
    // Check if this is a directory
    if (logger.isDebugEnabled()) {
        logger.debug("closeFile");
    }
    if (isDirectory()) {
        // Nothing to do
        if (logger.isDebugEnabled()) {
            logger.debug("file is a directory - nothing to do");
        }
        setClosed(true);
        return;
    } else if (!hasContent()) {
        // File was not read/written so content was not opened
        if (logger.isDebugEnabled()) {
            logger.debug("no content to write - nothing to do");
        }
        setClosed(true);
        return;
    }
    // update of channel and content member variables need to be serialized
    synchronized (this) {
        if (modified) {
            if (logger.isDebugEnabled()) {
                logger.debug("content has been modified");
            }
            NodeRef contentNodeRef = getNodeRef();
            ContentWriter writer = (ContentWriter) content;
            // We may be in a retry block, in which case this section will already have executed and channel will be null
            if (channel != null) {
                // Close the channel
                channel.close();
                channel = null;
            }
            // Do we need the mimetype guessing for us when we're done?
            if (content.getMimetype() == null || content.getMimetype().equals(MimetypeMap.MIMETYPE_BINARY)) {
                String filename = (String) nodeService.getProperty(contentNodeRef, ContentModel.PROP_NAME);
                writer.guessMimetype(filename);
            }
            // We always want the encoding guessing
            writer.guessEncoding();
            // Retrieve the content data and stop the content URL from being 'eagerly deleted', in case we need to
            // retry the transaction
            final ContentData contentData = content.getContentData();
            // Update node properties
            nodeService.removeAspect(contentNodeRef, ContentModel.ASPECT_NO_CONTENT);
            try {
                nodeService.setProperty(contentNodeRef, ContentModel.PROP_CONTENT, contentData);
            } catch (ContentQuotaException qe) {
                content = null;
                setClosed(true);
                throw new DiskFullException(qe.getMessage());
            }
            // Tidy up after ourselves after a successful commit. Otherwise leave things to allow a retry.
            AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

                @Override
                public void afterCommit() {
                    synchronized (this) {
                        if (channel == null) {
                            content = null;
                            setClosed(true);
                        }
                    }
                }
            });
        } else if (channel != null) {
            // Close it - it was not modified
            if (logger.isDebugEnabled()) {
                logger.debug("content not modified - simply close the channel");
            }
            channel.close();
            channel = null;
            content = null;
            setClosed(true);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) TransactionListenerAdapter(org.alfresco.repo.transaction.TransactionListenerAdapter) ContentData(org.alfresco.service.cmr.repository.ContentData) DiskFullException(org.alfresco.jlan.server.filesys.DiskFullException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException)

Aggregations

DiskFullException (org.alfresco.jlan.server.filesys.DiskFullException)1 TransactionListenerAdapter (org.alfresco.repo.transaction.TransactionListenerAdapter)1 ContentData (org.alfresco.service.cmr.repository.ContentData)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 ContentQuotaException (org.alfresco.service.cmr.usage.ContentQuotaException)1