Example usage for java.util.concurrent CancellationException CancellationException

List of usage examples for java.util.concurrent CancellationException CancellationException

Introduction

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

Prototype

public CancellationException(String message) 

Source Link

Document

Constructs a CancellationException with the specified detail message.

Usage

From source file:com.amazonaws.services.s3.transfer.internal.MultipartUploadCallable.java

public UploadResult call() throws Exception {
    final String bucketName = putObjectRequest.getBucketName();
    final String key = putObjectRequest.getKey();

    fireProgressEvent(ProgressEvent.STARTED_EVENT_CODE);

    String uploadId = initiateMultipartUpload(putObjectRequest);

    long optimalPartSize = TransferManagerUtils.calculateOptimalPartSize(putObjectRequest, configuration);
    log.debug("Calculated optimal part size: " + optimalPartSize);

    try {/*from   w  w w .  ja v  a 2s. c om*/
        final List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequestFactory requestFactory = new UploadPartRequestFactory(putObjectRequest, uploadId,
                optimalPartSize);

        if (TransferManagerUtils.isUploadParallelizable(putObjectRequest)) {
            List<Future<PartETag>> futures = new ArrayList<Future<PartETag>>();
            while (requestFactory.hasMoreRequests()) {
                if (threadPool.isShutdown())
                    throw new CancellationException("TransferManager has been shutdown");
                UploadPartRequest request = requestFactory.getNextUploadPartRequest();
                futures.add(threadPool.submit(new UploadPartCallable(s3, request)));
            }
            this.collectPartETags(futures, partETags);
        } else {
            while (requestFactory.hasMoreRequests()) {
                if (threadPool.isShutdown())
                    throw new CancellationException("TransferManager has been shutdown");
                partETags.add(s3.uploadPart(requestFactory.getNextUploadPartRequest()).getPartETag());
            }
        }

        CompleteMultipartUploadResult completeMultipartUploadResult = s3.completeMultipartUpload(
                new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags));
        fireProgressEvent(ProgressEvent.COMPLETED_EVENT_CODE);

        UploadResult uploadResult = new UploadResult();
        uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
        uploadResult.setKey(completeMultipartUploadResult.getKey());
        uploadResult.setETag(completeMultipartUploadResult.getETag());
        uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
        return uploadResult;
    } catch (Exception e) {
        fireProgressEvent(ProgressEvent.FAILED_EVENT_CODE);

        try {
            s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, key, uploadId));
        } catch (Exception e2) {
            log.info("Unable to abort multipart upload, you may need to manually remove uploaded parts: "
                    + e2.getMessage(), e2);
        }
        throw e;
    } finally {
        if (putObjectRequest.getInputStream() != null) {
            try {
                putObjectRequest.getInputStream().close();
            } catch (Exception e) {
                log.warn("Unable to cleanly close input stream: " + e.getMessage(), e);
            }
        }
    }
}

From source file:com.qwazr.server.InFileSessionPersistenceManager.java

private void writeSessionAttribute(final ObjectOutputStream draftOut, final ObjectOutputStream sessionOut,
        final String attribute, final Object object) {
    if (attribute == null || object == null || !(object instanceof Serializable))
        return;//from  www  .  j a  v  a 2 s.co  m
    // First we try to write it to the draftOutputStream
    try {
        draftOut.writeObject(object);
    } catch (IOException e) {
        LOGGER.warning(() -> "Cannot write attribute session object (draft test) " + attribute + " - "
                + object.getClass() + " - " + e.getMessage());
        return;
    }
    try {
        sessionOut.writeUTF(attribute); // Attribute name stored as string
        sessionOut.writeObject(object);
    } catch (IOException e) {
        // The attribute cannot be written, we abort
        throw new CancellationException("Cannot write session attribute " + attribute
                + ": persistence aborted - " + object.getClass() + " - " + e.getMessage());
    }
}

From source file:de.ncoder.studipsync.studip.jsoup.JsoupStudipAdapter.java

@Override
public boolean doLogin() throws CancellationException, StudipException {
    try {//from w w  w .j  a  v a  2 s.co m
        navigate(PAGE_LOGIN);
        if (hasCookies()) {
            restoreCookies();
            navigate(PAGE_BASE);
        } else {
            log.info("Requesting login data.");
            LoginData login = ui.requestLoginData();
            if (login != null) {
                doLogin(login);
                login.clean();
            } else {
                throw new CancellationException("Login cancelled by user");
            }
        }

        if (isLoggedIn()) {
            saveCookies();
            return true;
        } else {
            deleteCookies();
            return false;
        }
    } catch (IOException e) {
        StudipException ex = new StudipException(e);
        ex.put("studip.cookiesPath", cookiesPath);
        ex.put("studip.url", document == null ? "none" : document.baseUri());
        throw ex;
    }
}

From source file:cn.ctyun.amazonaws.services.s3.transfer.internal.UploadCallable.java

/**
 * Uploads all parts in the request in serial in this thread, then completes
 * the upload and returns the result.//ww  w . j  a  va 2  s.  c  o m
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {

    final List<PartETag> partETags = new ArrayList<PartETag>();

    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(s3.uploadPart(uploadPartRequest).getPartETag());
    }

    CompleteMultipartUploadResult completeMultipartUploadResult = s3
            .completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
                    putObjectRequest.getKey(), multipartUploadId, partETags));

    fireProgressEvent(ProgressEvent.COMPLETED_EVENT_CODE);

    UploadResult uploadResult = new UploadResult();
    uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    uploadResult.setKey(completeMultipartUploadResult.getKey());
    uploadResult.setETag(completeMultipartUploadResult.getETag());
    uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    return uploadResult;
}

From source file:org.apache.hadoop.hbase.master.snapshot.CloneSnapshotHandler.java

@Override
public void cancel(String why) {
    if (this.stopped)
        return;//w ww  .  j  a v a  2 s. com
    this.stopped = true;
    String msg = "Stopping clone snapshot=" + snapshot + " because: " + why;
    LOG.info(msg);
    status.abort(msg);
    this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
}

From source file:com.sina.cloudstorage.services.scs.transfer.internal.UploadCallable.java

/**
 * Uploads all parts in the request in serial in this thread, then completes
 * the upload and returns the result./*from  w ww.j a v  a  2s.com*/
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {

    final List<PartETag> partETags = new ArrayList<PartETag>();

    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(s3.uploadPart(uploadPartRequest).getPartETag());
    }

    //        CompleteMultipartUploadResult completeMultipartUploadResult = s3
    //                .completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
    //                        putObjectRequest.getKey(), multipartUploadId, partETags));
    //        UploadResult uploadResult = new UploadResult();
    //        uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    //        uploadResult.setKey(completeMultipartUploadResult.getKey());
    //        uploadResult.setETag(completeMultipartUploadResult.getETag());
    //        uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    //        return uploadResult;

    s3.completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
            putObjectRequest.getKey(), multipartUploadId, partETags));

    UploadResult uploadResult = new UploadResult();
    //        uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    //        uploadResult.setKey(completeMultipartUploadResult.getKey());
    //        uploadResult.setETag(completeMultipartUploadResult.getETag());
    //        uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    return uploadResult;
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploadapp.GiftCloudDialogs.java

public String showInputDialogToSelectProject(final java.util.List<String> projectMap, final Component component,
        final Optional<String> lastProject) throws IOException {
    final String lastProjectName = lastProject.isPresent() ? lastProject.get() : "";

    if (projectMap.size() < 1) {
        return null;
    }//from w ww  .  ja v  a  2s. c  o m

    String[] projectStringArray = projectMap.toArray(new String[0]);

    final String defaultSelection = projectMap.contains(lastProjectName) ? lastProjectName : null;

    final Object returnValue = JOptionPane.showInputDialog(component,
            "Please select a project to which data will be uploaded.", applicationName,
            JOptionPane.QUESTION_MESSAGE, null, projectStringArray, defaultSelection);

    if (returnValue == null) {
        throw new CancellationException("User cancelled project selection during upload");
    }

    if (!(returnValue instanceof String)) {
        throw new RuntimeException("Bad return type");
    }
    return (String) returnValue;
}

From source file:com.amazonaws.mobileconnectors.s3.transfermanager.internal.CopyCallable.java

/**
 * Submits a callable for each part to be copied to our thread pool and
 * records its corresponding Future.// w w  w  .ja v  a  2s  .  c  o m
 */
private void copyPartsInParallel(CopyPartRequestFactory requestFactory) {
    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        CopyPartRequest request = requestFactory.getNextCopyPartRequest();
        futures.add(threadPool.submit(new CopyPartCallable(s3, request)));
    }
}

From source file:cn.ctyun.amazonaws.services.s3.transfer.internal.UploadCallable.java

/**
 * Submits a callable for each part to upload to our thread pool and records its corresponding Future.
 *//*from w w  w . ja  va 2s .  c  o  m*/
private void uploadPartsInParallel(UploadPartRequestFactory requestFactory) {
    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest request = requestFactory.getNextUploadPartRequest();
        futures.add(threadPool.submit(new UploadPartCallable(s3, request)));
    }
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploadapp.GiftCloudDialogs.java

public String showTextInputDialog(final Component component, final String message,
        final Optional<String> defaultName) throws IOException {
    final String initialName = defaultName.isPresent() ? defaultName.get() : "";

    String returnString = "";

    while (returnString.length() < 1) {
        final Object returnValue = JOptionPane.showInputDialog(component, message, applicationName,
                JOptionPane.PLAIN_MESSAGE, icon, null, initialName);
        if (returnValue == null) {
            throw new CancellationException("User cancelled template saving");
        }/*from   ww w  . j av a  2 s.  c  o  m*/
        if (!(returnValue instanceof String)) {
            throw new RuntimeException("Bad return type");
        }
        returnString = (String) returnValue;
    }
    return returnString;
}