Search in sources :

Example 6 with ProgressListener

use of org.netxms.client.ProgressListener 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 7 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class AgentFileManager method downloadDir.

/**
 * Recursively download directory from agent to local pc
 *
 * @param sf
 * @param localFileName
 * @throws IOException
 * @throws NXCException
 */
private void downloadDir(final AgentFile sf, String localFileName, ZipOutputStream zos, final IProgressMonitor monitor, ConsoleJobCallingServerJob job) throws NXCException, IOException {
    List<AgentFile> files = sf.getChildren();
    if (files == null) {
        files = session.listAgentFiles(sf, sf.getFullName(), sf.getNodeId());
        sf.setChildren(files);
    }
    for (AgentFile f : files) {
        if (job.isCanceled())
            break;
        if (f.isDirectory()) {
            downloadDir(f, localFileName + "/" + f.getName(), zos, monitor, job);
        } else {
            monitor.subTask(String.format("Compressing file %s", f.getFullName()));
            final AgentFileData file = session.downloadFileFromAgent(objectId, f.getFullName(), 0, false, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, job);
            if (file != null && file.getFile() != null) {
                FileInputStream fis = new FileInputStream(file.getFile());
                ZipEntry zipEntry = new ZipEntry(localFileName + "/" + f.getName());
                zos.putNextEntry(zipEntry);
                byte[] bytes = new byte[1024];
                int length;
                while ((length = fis.read(bytes)) >= 0) {
                    zos.write(bytes, 0, length);
                }
                zos.closeEntry();
                fis.close();
            }
        }
    }
}
Also used : ProgressListener(org.netxms.client.ProgressListener) AgentFile(org.netxms.client.server.AgentFile) ZipEntry(java.util.zip.ZipEntry) AgentFileData(org.netxms.client.AgentFileData) FileInputStream(java.io.FileInputStream)

Example 8 with ProgressListener

use of org.netxms.client.ProgressListener 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 9 with ProgressListener

use of org.netxms.client.ProgressListener in project netxms by netxms.

the class ServerFileManager method createFile.

/**
 * Create new file
 */
private void createFile() {
    final StartClientToServerFileUploadDialog dlg = new StartClientToServerFileUploadDialog(getSite().getShell());
    if (dlg.open() == Window.OK) {
        final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
        new ConsoleJob(Messages.get().UploadFileToServer_JobTitle, null, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(final IProgressMonitor monitor) throws Exception {
                List<File> fileList = dlg.getLocalFiles();
                for (int i = 0; i < fileList.size(); i++) {
                    final File localFile = fileList.get(i);
                    String remoteFile = fileList.get(i).getName();
                    if (fileList.size() == 1)
                        remoteFile = dlg.getRemoteFileName();
                    session.uploadFileToServer(localFile, remoteFile, new ProgressListener() {

                        private long prevWorkDone = 0;

                        @Override
                        public void setTotalWorkAmount(long workTotal) {
                            monitor.beginTask(Messages.get(getDisplay()).UploadFileToServer_TaskNamePrefix + localFile.getAbsolutePath(), (int) workTotal);
                        }

                        @Override
                        public void markProgress(long workDone) {
                            monitor.worked((int) (workDone - prevWorkDone));
                            prevWorkDone = workDone;
                        }
                    });
                    monitor.done();
                }
            }

            @Override
            protected String getErrorMessage() {
                return Messages.get().UploadFileToServer_JobError;
            }
        }.start();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NXCSession(org.netxms.client.NXCSession) ProgressListener(org.netxms.client.ProgressListener) List(java.util.List) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) ServerFile(org.netxms.client.server.ServerFile) File(java.io.File) StartClientToServerFileUploadDialog(org.netxms.ui.eclipse.filemanager.dialogs.StartClientToServerFileUploadDialog) PartInitException(org.eclipse.ui.PartInitException)

Example 10 with ProgressListener

use of org.netxms.client.ProgressListener 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

ProgressListener (org.netxms.client.ProgressListener)11 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)9 File (java.io.File)6 AgentFileData (org.netxms.client.AgentFileData)6 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)5 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 PartInitException (org.eclipse.ui.PartInitException)4 NXCException (org.netxms.client.NXCException)4 NXCSession (org.netxms.client.NXCSession)4 ConsoleJobCallingServerJob (org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob)4 AgentFile (org.netxms.client.server.AgentFile)3 SWTException (org.eclipse.swt.SWTException)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 LibraryImage (org.netxms.client.LibraryImage)2 StartClientToServerFileUploadDialog (org.netxms.ui.eclipse.filemanager.dialogs.StartClientToServerFileUploadDialog)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1