Search in sources :

Example 21 with AzureCmdException

use of com.microsoft.azuretools.azurecommons.helpers.AzureCmdException in project azure-tools-for-java by Microsoft.

the class HDInsightUtil method setHDInsightRootModule.

public static void setHDInsightRootModule(@NotNull AzureModule azureModule) {
    HDInsightRootModuleImpl hdInsightRootModule = new HDInsightRootModuleImpl(azureModule);
    // add telemetry for HDInsight Node
    hdInsightRootModule.addClickActionListener(new NodeActionListener() {

        @Override
        protected void actionPerformed(NodeActionEvent e) throws AzureCmdException {
            AppInsightsClient.create(HDInsightBundle.message("HDInsightExplorerHDInsightNodeExpand"), null);
        }
    });
    azureModule.setHdInsightModule(hdInsightRootModule);
}
Also used : HDInsightRootModuleImpl(com.microsoft.azure.hdinsight.serverexplore.HDInsightRootModuleImpl) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) NodeActionListener(com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener) NodeActionEvent(com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent)

Example 22 with AzureCmdException

use of com.microsoft.azuretools.azurecommons.helpers.AzureCmdException in project azure-tools-for-java by Microsoft.

the class CreateBlobContainerForm method okPressed.

@Override
protected void okPressed() {
    final String name = nameTextField.getText();
    if (name.length() < NAME_MIN || name.length() > NAME_MAX || !name.matches(NAME_REGEX)) {
        DefaultLoader.getUIHelper().showError("Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.\n" + "Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.\n" + "All letters in a container name must be lowercase.\n" + "Container names must be from 3 through 63 characters long.", "Azure Explorer");
        return;
    }
    DefaultLoader.getIdeHelper().runInBackground(null, "Creating blob container...", false, true, "Creating blob container...", new Runnable() {

        @Override
        public void run() {
            try {
                for (BlobContainer blobContainer : StorageClientSDKManager.getManager().getBlobContainers(connectionString)) {
                    if (blobContainer.getName().equals(name)) {
                        DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                DefaultLoader.getUIHelper().showError("A blob container with the specified name already exists.", "Azure Explorer");
                            }
                        });
                        return;
                    }
                }
                BlobContainer blobContainer = new BlobContainer(name, "", /*storageAccount.getBlobsUri() + name*/
                "", Calendar.getInstance(), "");
                StorageClientSDKManager.getManager().createBlobContainer(connectionString, blobContainer);
                if (onCreate != null) {
                    DefaultLoader.getIdeHelper().invokeLater(onCreate);
                }
            } catch (AzureCmdException e) {
                DefaultLoader.getUIHelper().showException("Error creating blob container", e, "Error creating blob container", false, true);
            }
        }
    });
    super.okPressed();
}
Also used : AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobContainer(com.microsoft.tooling.msservices.model.storage.BlobContainer)

Example 23 with AzureCmdException

use of com.microsoft.azuretools.azurecommons.helpers.AzureCmdException in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method uploadFile.

private void uploadFile(final String path, final File selectedFile) {
    Job job = new Job("Uploading blob...") {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask("Uploading blob...", IProgressMonitor.UNKNOWN);
            try {
                final BlobDirectory blobDirectory = directoryQueue.peekLast();
                final BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(selectedFile));
                monitor.subTask("0% uploaded");
                try {
                    final CallableSingleArg<Void, Long> callable = new CallableSingleArg<Void, Long>() {

                        @Override
                        public Void call(Long uploadedBytes) throws Exception {
                            double progress = ((double) uploadedBytes) / selectedFile.length();
                            monitor.worked((int) (100 * progress));
                            monitor.subTask(String.format("%s%% uploaded", (int) (progress * 100)));
                            return null;
                        }
                    };
                    try {
                        StorageClientSDKManager.getManager().uploadBlobFileContent(connectionString, blobContainer, path, bufferedInputStream, callable, 1024 * 1024, selectedFile.length());
                    } catch (AzureCmdException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            bufferedInputStream.close();
                        } catch (IOException ignored) {
                        }
                    }
                    if (monitor.isCanceled()) {
                        //                                future.cancel(true);
                        bufferedInputStream.close();
                        for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, blobDirectory)) {
                            if (blobItem instanceof BlobFile && blobItem.getPath().equals(path)) {
                                StorageClientSDKManager.getManager().deleteBlobFile(connectionString, (BlobFile) blobItem);
                            }
                        }
                    }
                    try {
                        directoryQueue.clear();
                        directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                        for (String pathDir : path.split("/")) {
                            for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.getLast())) {
                                if (blobItem instanceof BlobDirectory && blobItem.getName().equals(pathDir)) {
                                    directoryQueue.addLast((BlobDirectory) blobItem);
                                }
                            }
                        }
                    } catch (AzureCmdException e) {
                        DefaultLoader.getUIHelper().showException("Error showing new blob", e, "Error showing new blob", false, true);
                    }
                    DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            fillGrid();
                        }
                    });
                } catch (Exception e) {
                    Throwable connectionFault = e.getCause();
                    Throwable realFault = null;
                    if (connectionFault != null) {
                        realFault = connectionFault.getCause();
                    }
                    monitor.setTaskName("Error uploading Blob");
                    String message = realFault == null ? null : realFault.getMessage();
                    if (connectionFault != null && message == null) {
                        message = "Error type " + connectionFault.getClass().getName();
                    }
                    monitor.subTask((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : message);
                }
            } catch (Exception e) {
                DefaultLoader.getUIHelper().showException("Error uploading Blob", e, "Error uploading Blob", false, true);
                return Status.CANCEL_STATUS;
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}
Also used : BlobDirectory(com.microsoft.tooling.msservices.model.storage.BlobDirectory) CallableSingleArg(com.microsoft.tooling.msservices.helpers.CallableSingleArg) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PartInitException(org.eclipse.ui.PartInitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Job(org.eclipse.core.runtime.jobs.Job)

Example 24 with AzureCmdException

use of com.microsoft.azuretools.azurecommons.helpers.AzureCmdException in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method fillGrid.

public void fillGrid() {
    setUIState(true);
    DefaultLoader.getIdeHelper().runInBackground(null, "Loading blobs...", false, true, "Loading blobs...", new Runnable() {

        @Override
        public void run() {
            try {
                if (directoryQueue.peekLast() == null) {
                    directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                }
                blobItems = StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.peekLast());
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        if (!queryTextField.getText().isEmpty()) {
                            for (int i = blobItems.size() - 1; i >= 0; i--) {
                                BlobItem blobItem = blobItems.get(i);
                                if (blobItem instanceof BlobFile && !blobItem.getName().startsWith(queryTextField.getText())) {
                                    blobItems.remove(i);
                                }
                            }
                        }
                        pathLabel.setText(directoryQueue.peekLast().getPath());
                        tableViewer.setInput(blobItems);
                        tableViewer.refresh();
                        setUIState(false);
                    //
                    //                            blobListTable.clearSelection();
                    }
                });
            } catch (AzureCmdException ex) {
                DefaultLoader.getUIHelper().showException("Error querying blob list.", ex, "Error querying blobs", false, true);
            }
        }
    });
}
Also used : BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile)

Example 25 with AzureCmdException

use of com.microsoft.azuretools.azurecommons.helpers.AzureCmdException in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method downloadSelectedFile.

private void downloadSelectedFile(final File targetFile, final boolean open) {
    final BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        Job job = new Job("Downloading blob...") {

            @Override
            protected IStatus run(final IProgressMonitor monitor) {
                monitor.beginTask("Downloading blob...", IProgressMonitor.UNKNOWN);
                try {
                    if (!targetFile.exists()) {
                        if (!targetFile.createNewFile()) {
                            throw new IOException("File not created");
                        }
                    }
                    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile), 65536) {

                        private long runningCount = 0;

                        @Override
                        public synchronized void write(@NotNull byte[] bytes, int i, int i1) throws IOException {
                            super.write(bytes, i, i1);
                            runningCount += i1;
                            double progress = (double) runningCount / fileSelection.getSize();
                            monitor.worked((int) (100 * progress));
                            monitor.subTask(String.format("%s%% downloaded", (int) (progress * 100)));
                        }
                    };
                    try {
                        //                                public void run() {
                        try {
                            StorageClientSDKManager.getManager().downloadBlobFileContent(connectionString, fileSelection, bufferedOutputStream);
                            if (open && targetFile.exists()) {
                                try {
                                    final Process p;
                                    Runtime runtime = Runtime.getRuntime();
                                    p = runtime.exec(new String[] { "open", "-R", targetFile.getName() }, null, targetFile.getParentFile());
                                    InputStream errorStream = p.getErrorStream();
                                    String errResponse = new String(IOUtils.readFully(errorStream, -1, true));
                                    if (p.waitFor() != 0) {
                                        throw new Exception(errResponse);
                                    }
                                } catch (Exception e) {
                                    monitor.setTaskName("Error opening file");
                                    monitor.subTask(e.getMessage());
                                }
                            //                                            Desktop.getDesktop().open(targetFile);
                            }
                        } catch (AzureCmdException e) {
                            Throwable connectionFault = e.getCause().getCause();
                            monitor.setTaskName("Error downloading Blob");
                            monitor.subTask((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : connectionFault.getMessage());
                            return Status.CANCEL_STATUS;
                        }
                    } finally {
                        bufferedOutputStream.close();
                    }
                } catch (IOException e) {
                    DefaultLoader.getUIHelper().showException("Error downloading Blob", e, "Error downloading Blob", false, true);
                    return Status.CANCEL_STATUS;
                } finally {
                    monitor.done();
                }
                return Status.OK_STATUS;
            }
        };
        job.schedule();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) IOException(java.io.IOException) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull) PartInitException(org.eclipse.ui.PartInitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SocketTimeoutException(java.net.SocketTimeoutException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) FileOutputStream(java.io.FileOutputStream) Job(org.eclipse.core.runtime.jobs.Job) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)55 NotNull (com.microsoft.azuretools.azurecommons.helpers.NotNull)20 BlobFile (com.microsoft.tooling.msservices.model.storage.BlobFile)8 CloudQueue (com.microsoft.azure.storage.queue.CloudQueue)7 CloudQueueClient (com.microsoft.azure.storage.queue.CloudQueueClient)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 Task (com.intellij.openapi.progress.Task)5 Azure (com.microsoft.azure.management.Azure)5 AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)5 NodeActionEvent (com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent)5 NodeActionListener (com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener)5 IOException (java.io.IOException)5 BlobDirectory (com.microsoft.tooling.msservices.model.storage.BlobDirectory)4 BlobItem (com.microsoft.tooling.msservices.model.storage.BlobItem)4 ArrayList (java.util.ArrayList)4 CloudQueueMessage (com.microsoft.azure.storage.queue.CloudQueueMessage)3 SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)3 BlobContainer (com.microsoft.tooling.msservices.model.storage.BlobContainer)3 FileInputStream (java.io.FileInputStream)3 SocketTimeoutException (java.net.SocketTimeoutException)3