Search in sources :

Example 31 with AzureString

use of com.microsoft.azure.toolkit.lib.common.bundle.AzureString in project azure-tools-for-java by Microsoft.

the class SpringCloudStreamingLogAction method startLogStreaming.

public static void startLogStreaming(@Nonnull SpringCloudApp app, @Nullable Project project) {
    final IAzureMessager messager = AzureMessager.getMessager();
    final AzureString title = AzureOperationBundle.title("springcloud|log_stream.open", app.name());
    AzureTaskManager.getInstance().runInBackground(new AzureTask<>(project, title, false, () -> {
        try {
            final SpringCloudDeployment deployment = app.activeDeployment();
            if (deployment == null || !deployment.exists()) {
                messager.warning(NO_ACTIVE_DEPLOYMENT, FAILED_TO_START_LOG_STREAMING);
                return;
            }
            final List<SpringCloudDeploymentInstanceEntity> instances = deployment.entity().getInstances();
            if (CollectionUtils.isEmpty(instances)) {
                messager.warning(NO_AVAILABLE_INSTANCES, FAILED_TO_START_LOG_STREAMING);
            } else {
                showLogStreamingDialog(instances, app, project);
            }
        } catch (final Exception e) {
            final String errorMessage = StringUtils.isEmpty(e.getMessage()) ? FAILED_TO_LIST_INSTANCES : String.format(FAILED_TO_LIST_INSTANCES_WITH_MESSAGE, e.getMessage());
            messager.error(errorMessage, FAILED_TO_START_LOG_STREAMING);
        }
    }));
}
Also used : IAzureMessager(com.microsoft.azure.toolkit.lib.common.messager.IAzureMessager) SpringCloudDeployment(com.microsoft.azure.toolkit.lib.springcloud.SpringCloudDeployment) List(java.util.List) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 32 with AzureString

use of com.microsoft.azure.toolkit.lib.common.bundle.AzureString in project azure-tools-for-java by Microsoft.

the class SpringCloudStreamingLogConsoleView method shutdown.

public void shutdown() {
    synchronized (this) {
        if (getStatus() != ConsoleViewStatus.ACTIVE && getStatus() != ConsoleViewStatus.STARTING) {
            return;
        }
        setStatus(ConsoleViewStatus.STOPPING);
    }
    final AzureString title = AzureOperationBundle.title("springcloud|log_stream.close", resourceName);
    AzureTaskManager.getInstance().runInBackground(new AzureTask<>(getProject(), title, false, () -> {
        try {
            if (logInputStream != null) {
                logInputStream.close();
            }
            if (executorService != null) {
                ThreadPoolUtils.stop(executorService, 100, TimeUnit.MICROSECONDS);
            }
        } catch (final IOException e) {
        // swallow io exception when close
        } finally {
            setStatus(ConsoleViewStatus.STOPPED);
        }
    }));
}
Also used : IOException(java.io.IOException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 33 with AzureString

use of com.microsoft.azure.toolkit.lib.common.bundle.AzureString in project azure-tools-for-java by Microsoft.

the class SignInCommandHandler method loginNonDeviceCodeSingle.

private static Single<AuthMethodDetails> loginNonDeviceCodeSingle(AuthConfiguration auth) {
    final AzureString title = AzureOperationBundle.title("account.sign_in");
    final AzureTask<AuthMethodDetails> task = new AzureTask<>(null, title, true, () -> doLogin(AzureTaskContext.current().getTask().getMonitor(), auth));
    return AzureTaskManager.getInstance().runInBackgroundAsObservable(task).toSingle();
}
Also used : AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString)

Example 34 with AzureString

use of com.microsoft.azure.toolkit.lib.common.bundle.AzureString in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method openAppServiceFile.

@AzureOperation(name = "appservice|file.open", params = { "target.getName()" }, type = AzureOperation.Type.SERVICE)
@SneakyThrows
public void openAppServiceFile(AppServiceFile target, Object context) {
    final IAppService appService = target.getApp();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance((Project) context);
    final VirtualFile virtualFile = getOrCreateVirtualFile(target, fileEditorManager);
    final OutputStream output = virtualFile.getOutputStream(null);
    final AzureString title = AzureOperationBundle.title("appservice|file.open", virtualFile.getName());
    final AzureTask<Void> task = new AzureTask<>(null, title, false, () -> {
        final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setIndeterminate(true);
        indicator.setText2("Checking file existence");
        final AppServiceFile file = appService.getFileByPath(target.getPath());
        if (file == null) {
            final String failureFileDeleted = String.format("Target file (%s) has been deleted", target.getName());
            UIUtil.invokeLaterIfNeeded(() -> Messages.showWarningDialog(failureFileDeleted, "Open File"));
            return;
        }
        indicator.setText2("Loading file content");
        final String failure = String.format("Can not open file (%s). Try downloading it first and open it manually.", virtualFile.getName());
        appService.getFileContent(file.getPath()).doOnComplete(() -> AzureTaskManager.getInstance().runLater(() -> {
            final Consumer<String> contentSaver = content -> saveFileToAzure(target, content, fileEditorManager.getProject());
            if (!openFileInEditor(contentSaver, virtualFile, fileEditorManager)) {
                Messages.showWarningDialog(failure, "Open File");
            }
        }, AzureTask.Modality.NONE)).doAfterTerminate(() -> IOUtils.closeQuietly(output, null)).subscribe(bytes -> {
            try {
                if (bytes != null) {
                    output.write(bytes.array(), 0, bytes.limit());
                }
            } catch (final IOException e) {
                final String error = "failed to load data into editor";
                final String action = "try later or downloading it first";
                throw new AzureToolkitRuntimeException(error, e, action);
            }
        }, IDEHelperImpl::onRxException);
    });
    AzureTaskManager.getInstance().runInModal(task);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IAppService(com.microsoft.azure.toolkit.lib.appservice.service.IAppService) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Consumer(com.intellij.util.Consumer) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AppServiceFile(com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) SneakyThrows(lombok.SneakyThrows)

Example 35 with AzureString

use of com.microsoft.azure.toolkit.lib.common.bundle.AzureString 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) {
        final AzureString title = AzureOperationBundle.title("blob.download", targetFile, blobContainer.getName());
        AzureTaskManager.getInstance().runInBackground(new AzureTask(project, title, false, () -> {
            final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
            try {
                progressIndicator.setIndeterminate(false);
                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();
                        progressIndicator.setFraction(progress);
                        progressIndicator.setText2(String.format("%s%% downloaded", (int) (progress * 100)));
                    }
                };
                try {
                    Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                StorageClientSDKManager.getManager().downloadBlobFileContent(connectionString, fileSelection, bufferedOutputStream);
                                if (open && targetFile.exists()) {
                                    Desktop.getDesktop().open(targetFile);
                                }
                            } catch (AzureCmdException e) {
                                Throwable connectionFault = e.getCause().getCause();
                                progressIndicator.setText("Error downloading Blob");
                                progressIndicator.setText2((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : connectionFault.getMessage());
                            } catch (IOException ex) {
                                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));
                                    if (p.waitFor() != 0) {
                                        throw new Exception(errResponse);
                                    }
                                } catch (Exception e) {
                                    progressIndicator.setText("Error openning file");
                                    progressIndicator.setText2(ex.getMessage());
                                }
                            }
                        }
                    });
                    while (!future.isDone()) {
                        progressIndicator.checkCanceled();
                        if (progressIndicator.isCanceled()) {
                            future.cancel(true);
                        }
                    }
                } finally {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                PluginUtil.displayErrorDialogAndLog(message("errTtl"), "An error occurred while attempting to download Blob.", e);
            }
        }));
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) SocketTimeoutException(java.net.SocketTimeoutException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) SocketTimeoutException(java.net.SocketTimeoutException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Future(java.util.concurrent.Future) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask)

Aggregations

AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)46 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)36 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)21 Project (com.intellij.openapi.project.Project)7 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)6 List (java.util.List)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 AzureOperationBundle (com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle)5 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)5 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)5 EventUtil (com.microsoft.azuretools.telemetrywrapper.EventUtil)4 BlobFile (com.microsoft.tooling.msservices.model.storage.BlobFile)4 IOException (java.io.IOException)4 MessageType (com.intellij.openapi.ui.MessageType)3 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)3 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)3 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)3 File (java.io.File)3 javax.swing (javax.swing)3 FileChooserDescriptorFactory (com.intellij.openapi.fileChooser.FileChooserDescriptorFactory)2