2b4b59ee572b0112adfa433e823c6dba0460a095
[public/netxms.git] / src / java / netxms-eclipse / Core / src / org / netxms / ui / eclipse / jobs / ConsoleJobCallingServerJob.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.jobs;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.netxms.client.NXCSession;
26 import org.netxms.client.server.ServerJobIdUpdater;
27 import org.netxms.ui.eclipse.console.Activator;
28 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
29
30 /**
31 * Tailored Job class for NetXMS console. Callers must call start() instead of schedule() for correct execution.
32 */
33 public abstract class ConsoleJobCallingServerJob extends ConsoleJob implements ServerJobIdUpdater
34 {
35 private NXCSession session = null;
36 private int serverJobId = 0;
37 private boolean jobCanceled = false;
38
39 public ConsoleJobCallingServerJob(String name, IWorkbenchPart wbPart, String pluginId, Object jobFamily)
40 {
41 super(name, wbPart, pluginId, jobFamily);
42 session = (NXCSession)ConsoleSharedData.getSession();
43 }
44
45 /*
46 * (non-Javadoc)
47 *
48 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
49 */
50 @Override
51 protected IStatus run(IProgressMonitor monitor)
52 {
53 IStatus status;
54 try
55 {
56 runInternal(monitor);
57 status = Status.OK_STATUS;
58 }
59 catch(Exception e)
60 {
61 if(jobCanceled)
62 {
63 status = Status.CANCEL_STATUS;
64 }
65 else
66 {
67 Activator.logError("Exception in ConsoleJob", e); //$NON-NLS-1$
68 jobFailureHandler();
69 status = createFailureStatus(e);
70 }
71 }
72 finally
73 {
74 jobFinalize();
75 }
76 return status;
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.core.runtime.jobs.Job#canceling()
81 */
82 @Override
83 protected void canceling()
84 {
85 jobCanceled = true;
86 try
87 {
88 session.cancelServerJob(serverJobId);
89 }
90 catch(Exception e)
91 {
92 Activator.logError("Failed to cancel job", e); //$NON-NLS-1$
93 }
94 super.canceling();
95 }
96
97 public void setJobIdCallback(int id)
98 {
99 serverJobId = id;
100 }
101 }