Search in sources :

Example 1 with ConsoleJobCallingServerJob

use of org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob in project netxms by netxms.

the class ObjectToolExecutor method executeFileDownload.

/**
 * @param node
 * @param tool
 * @param inputValues
 * @param inputValues
 */
private static void executeFileDownload(final ObjectContext node, final ObjectTool tool, final Map<String, String> inputValues) {
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    // $NON-NLS-1$
    String[] parameters = tool.getData().split("\u007F");
    final String fileName = parameters[0];
    final int maxFileSize = Integer.parseInt(parameters[1]);
    // $NON-NLS-1$
    final boolean follow = parameters[2].equals("true") ? true : false;
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().ObjectToolsDynamicMenu_DownloadFromAgent, null, Activator.PLUGIN_ID, null) {

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().ObjectToolsDynamicMenu_DownloadError, fileName, node.object.getObjectName());
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(node.object.getObjectId(), fileName, maxFileSize, follow, inputValues, node.getAlarmId(), new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Download file " + fileName, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String secondaryId = Long.toString(node.object.getObjectId()) + "&" + URLEncoder.encode(fileName, "UTF-8");
                        AgentFileViewer.createView(secondaryId, node.object.getObjectId(), file, follow);
                    } catch (Exception e) {
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                        MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
                    }
                }
            });
        }
    };
    job.start();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) NXCSession(org.netxms.client.NXCSession) AgentFileData(org.netxms.client.AgentFileData) PartInitException(org.eclipse.ui.PartInitException) ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener)

Example 2 with ConsoleJobCallingServerJob

use of org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob in project netxms by netxms.

the class AgentFileManager method tailFile.

/**
 * Starts file tail
 */
private void tailFile(final boolean followChanges, final int offset) {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.isEmpty())
        return;
    final Object[] objects = selection.toArray();
    if (((AgentFile) objects[0]).isDirectory())
        return;
    final AgentFile sf = ((AgentFile) objects[0]);
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().AgentFileManager_DownloadJobTitle, null, Activator.PLUGIN_ID, null) {

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().AgentFileManager_DownloadJobError, sf.getFullName(), objectId);
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(objectId, sf.getFullName(), offset, followChanges, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Download file " + sf.getFullName(), (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String secondaryId = Long.toString(objectId) + "&" + URLEncoder.encode(sf.getName(), "UTF-8");
                        AgentFileViewer.createView(secondaryId, objectId, file, followChanges);
                    } catch (Exception e) {
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                        MessageDialogHelper.openError(window.getShell(), Messages.get().AgentFileManager_Error, String.format(Messages.get().AgentFileManager_OpenViewError, e.getLocalizedMessage()));
                        Activator.logError("Exception in AgentFileManager.tailFile", e);
                    }
                }
            });
        }
    };
    job.start();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) AgentFile(org.netxms.client.server.AgentFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AgentFileData(org.netxms.client.AgentFileData) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener)

Example 3 with ConsoleJobCallingServerJob

use of org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob in project netxms by netxms.

the class AgentFileManager method startDownload.

/**
 * Download file from agent
 */
private void startDownload() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() != 1)
        return;
    final AgentFile sf = (AgentFile) selection.getFirstElement();
    if (!sf.isDirectory()) {
        downloadFile(sf.getFullName());
    } else {
        ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob("Download from agent", null, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(final IProgressMonitor monitor) throws Exception {
                // create zip from download folder wile download
                long dirSize = -1;
                try {
                    dirSize = session.getAgentFileInfo(sf).getSize();
                } catch (Exception e) {
                }
                monitor.beginTask(String.format("Downloading directory %s", sf.getName()), (int) dirSize);
                final File zipFile = File.createTempFile("download_", ".zip");
                FileOutputStream fos = new FileOutputStream(zipFile);
                ZipOutputStream zos = new ZipOutputStream(fos);
                downloadDir(sf, sf.getName(), zos, monitor, this);
                zos.close();
                fos.close();
                if (!isCanceled()) {
                    DownloadServiceHandler.addDownload(zipFile.getName(), sf.getName() + ".zip", zipFile, "application/octet-stream");
                    runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            DownloadServiceHandler.startDownload(zipFile.getName());
                        }
                    });
                }
                monitor.done();
            }

            @Override
            protected String getErrorMessage() {
                return Messages.get().AgentFileManager_DirectoryReadError;
            }
        };
        job.start();
    }
}
Also used : ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AgentFile(org.netxms.client.server.AgentFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AgentFile(org.netxms.client.server.AgentFile) File(java.io.File) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException)

Example 4 with ConsoleJobCallingServerJob

use of org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob in project netxms by netxms.

the class AgentFileManager method downloadFile.

/**
 * Downloads file
 * @throws NXCException
 * @throws IOException
 */
private void downloadFile(final String remoteName) {
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().SelectServerFileDialog_JobTitle, null, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(objectId, remoteName, 0, false, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Downloading file " + remoteName, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            if (file != null && file.getFile() != null) {
                DownloadServiceHandler.addDownload(file.getFile().getName(), remoteName, file.getFile(), "application/octet-stream");
                runInUIThread(new Runnable() {

                    @Override
                    public void run() {
                        DownloadServiceHandler.startDownload(file.getFile().getName());
                    }
                });
            }
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().AgentFileManager_DirectoryReadError;
        }
    };
    job.setUser(false);
    job.start();
}
Also used : ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener) AgentFileData(org.netxms.client.AgentFileData)

Example 5 with ConsoleJobCallingServerJob

use of org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob in project netxms by netxms.

the class DynamicFileViewer method restartTracking.

/**
 * Restart tracking after failure
 */
private void restartTracking() {
    if (monitoringJob != null) {
        monitoringJob.cancel();
        monitoringJob = null;
    }
    if (restartJob != null)
        restartJob.cancel();
    text.append(// $NON-NLS-1$
    "\n\n" + // $NON-NLS-1$
    "----------------------------------------------------------------------\n" + Messages.get().FileViewer_NotifyFollowConnectionLost + // $NON-NLS-1$
    "\n----------------------------------------------------------------------\n");
    showMessage(ERROR, Messages.get().FileViewer_NotifyFollowConnectionLost);
    restartJob = new ConsoleJobCallingServerJob(Messages.get().DynamicFileViewer_RestartFileTracking, null, Activator.PLUGIN_ID, null) {

        private boolean running = true;

        @Override
        protected void canceling() {
            running = false;
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            // Try to reconnect in every 20 seconds
            while (running) {
                try {
                    final AgentFileData file = session.downloadFileFromAgent(nodeId, remoteFileName, 1024, true, new ProgressListener() {

                        @Override
                        public void setTotalWorkAmount(long workTotal) {
                            monitor.beginTask("Track file " + remoteFileName, (int) workTotal);
                        }

                        @Override
                        public void markProgress(long workDone) {
                            monitor.worked((int) workDone);
                        }
                    }, this);
                    // When successfully connected - display notification to client.
                    runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            if (text.isDisposed()) {
                                running = false;
                                return;
                            }
                            hideMessage();
                            text.append(// $NON-NLS-1$
                            "-------------------------------------------------------------------------------\n" + Messages.get().FileViewer_NotifyFollowConnectionEnabed + // $NON-NLS-1$
                            "\n-------------------------------------------------------------------------------\n\n");
                            append(loadFile(file.getFile()));
                            startTracking(nodeId, fileId, remoteFileName);
                        }
                    });
                    break;
                } catch (Exception e) {
                }
                Thread.sleep(20000);
            }
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().DynamicFileViewer_CannotRestartFileTracking;
        }
    };
    restartJob.setUser(false);
    restartJob.setSystem(true);
    restartJob.start();
}
Also used : ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener) AgentFileData(org.netxms.client.AgentFileData)

Aggregations

IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 ConsoleJobCallingServerJob (org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob)5 AgentFileData (org.netxms.client.AgentFileData)4 ProgressListener (org.netxms.client.ProgressListener)4 PartInitException (org.eclipse.ui.PartInitException)3 IOException (java.io.IOException)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 NXCException (org.netxms.client.NXCException)2 AgentFile (org.netxms.client.server.AgentFile)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 NXCSession (org.netxms.client.NXCSession)1