Example usage for java.util.concurrent Callable call

List of usage examples for java.util.concurrent Callable call

Introduction

In this page you can find the example usage for java.util.concurrent Callable call.

Prototype

V call() throws Exception;

Source Link

Document

Computes a result, or throws an exception if unable to do so.

Usage

From source file:org.alfresco.filesys.repo.ContentDiskDriver.java

/**
 * Delete the specified file.//from  w  w  w .ja  v  a  2 s  .  c om
 * 
 * @param sess Server session
 * @param tree Tree connection
 * @param name NetworkFile
 * @exception java.io.IOException The exception description.
 */
public void deleteFile(final SrvSession sess, final TreeConnection tree, final String name) throws IOException {
    // Get the device context

    if (logger.isDebugEnabled()) {
        logger.debug("Delete file - " + name);
    }

    final ContentContext ctx = (ContentContext) tree.getContext();

    try {
        // Check if pseudo files are enabled

        if (hasPseudoFileInterface(ctx)) {
            // Check if the file name is a pseudo file name

            if (getPseudoFileInterface(ctx).isPseudoFile(sess, tree, name)) {

                // Make sure the parent folder has a file state, and the path exists

                String[] paths = FileName.splitPath(name);
                FileState fstate = ctx.getStateCache().findFileState(paths[0]);

                if (fstate != null) {

                    // Check if the path is to a pseudo file

                    PseudoFile pfile = getPseudoFileInterface(ctx).getPseudoFile(sess, tree, name);
                    if (pfile != null) {
                        // Delete the pseudo file

                        getPseudoFileInterface(ctx).deletePseudoFile(sess, tree, name);
                        return;
                    }
                }
            }
        }

        // Check if there is a quota manager enabled, if so then we need to save the current file size

        final QuotaManager quotaMgr = ctx.getQuotaManager();

        // Perform repository updates in a retryable write transaction
        Callable<Void> postTxn = doInWriteTransaction(sess, new CallableIO<Callable<Void>>() {
            public Callable<Void> call() throws IOException {
                // Get the node and delete it
                final NodeRef nodeRef = getNodeForPath(tree, name);

                Callable<Void> result = null;
                if (fileFolderService.exists(nodeRef)) {
                    // Get the size of the file being deleted

                    final FileInfo fInfo = quotaMgr == null ? null : getFileInformation(sess, tree, name);

                    // Check if the node is versionable

                    final boolean isVersionable = nodeService.hasAspect(nodeRef,
                            ContentModel.ASPECT_VERSIONABLE);

                    if (logger.isDebugEnabled()) {
                        logger.debug("deleted file" + name);
                    }
                    fileFolderService.delete(nodeRef);

                    // Return the operations to perform when the transaction succeeds

                    result = new Callable<Void>() {

                        public Void call() throws Exception {
                            // Remove the file state

                            if (ctx.hasStateCache()) {
                                // Check if the node is versionable, cache the node details for a short while

                                if (isVersionable == true) {

                                    // Make sure the file state is cached for a short while, a new file may be
                                    // renamed to the same name
                                    // in which case we can connect the file to the previous version history

                                    FileState delState = ctx.getStateCache().findFileState(name, true);

                                    if (logger.isDebugEnabled()) {
                                        logger.debug("set delete on close" + name);
                                    }
                                    delState.setExpiryTime(
                                            System.currentTimeMillis() + FileState.DeleteTimeout);
                                    delState.setFileStatus(DeleteOnClose);
                                    delState.setFilesystemObject(nodeRef);
                                } else {

                                    // Remove the file state

                                    ctx.getStateCache().removeFileState(name);
                                }

                                // Update, or create, a parent folder file state

                                String[] paths = FileName.splitPath(name);
                                if (paths[0] != null && paths[0].length() > 1) {
                                    // Get the file state for the parent folder

                                    FileState parentState = getStateForPath(tree, paths[0]);
                                    if (parentState == null && ctx.hasStateCache())
                                        parentState = ctx.getStateCache().findFileState(paths[0], true);

                                    // Update the modification timestamp

                                    parentState.updateModifyDateTime();
                                }
                            }

                            // Release the space back to the users quota

                            if (quotaMgr != null)
                                quotaMgr.releaseSpace(sess, tree, fInfo.getFileId(), name, fInfo.getSize());

                            return null;
                        }
                    };
                }

                // Debug

                if (logger.isDebugEnabled()
                        && (ctx.hasDebug(AlfrescoContext.DBG_FILE) || ctx.hasDebug(AlfrescoContext.DBG_RENAME)))
                    logger.debug("Deleted file: " + name + ", node=" + nodeRef);

                return result;
            }
        });

        // Perform state updates after the transaction succeeds
        postTxn.call();
    } catch (NodeLockedException ex) {
        // Debug

        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Delete file - access denied (locked)");

        // Convert to a filesystem access denied status

        throw new AccessDeniedException("Delete " + name);
    } catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
        // Debug

        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Delete file - access denied");

        // Convert to a filesystem access denied status

        throw new AccessDeniedException("Delete " + name);
    } catch (IOException ex) {
        // Allow I/O Exceptions to pass through
        throw ex;
    } catch (Exception ex) {
        // Debug

        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Delete file error", ex);

        // Convert to a general I/O exception

        IOException ioe = new IOException("Delete file " + name);
        ioe.initCause(ex);
        throw ioe;
    }
}