d1c873c429a86b0ba44bf951d46a7f3f7975b79d
[public/netxms.git] / webui / webapp / ObjectTools / src / org / netxms / ui / eclipse / objecttools / api / ObjectToolExecutor.java
1 /**
2 *
3 */
4 package org.netxms.ui.eclipse.objecttools.api;
5
6 import java.net.URLEncoder;
7 import java.util.Set;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.rap.rwt.RWT;
10 import org.eclipse.rap.rwt.client.service.UrlLauncher;
11 import org.eclipse.ui.IWorkbenchPage;
12 import org.eclipse.ui.IWorkbenchWindow;
13 import org.eclipse.ui.PartInitException;
14 import org.eclipse.ui.PlatformUI;
15 import org.netxms.client.AgentFile;
16 import org.netxms.client.NXCSession;
17 import org.netxms.client.objecttools.ObjectTool;
18 import org.netxms.ui.eclipse.console.resources.StatusDisplayInfo;
19 import org.netxms.ui.eclipse.jobs.ConsoleJob;
20 import org.netxms.ui.eclipse.objecttools.Activator;
21 import org.netxms.ui.eclipse.objecttools.Messages;
22 import org.netxms.ui.eclipse.objecttools.views.AgentActionResults;
23 import org.netxms.ui.eclipse.objecttools.views.FileViewer;
24 import org.netxms.ui.eclipse.objecttools.views.TableToolResults;
25 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
26 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
27
28 /**
29 * Executor for object tool
30 */
31 public final class ObjectToolExecutor
32 {
33 /**
34 * Private constructor to forbid instantiation
35 */
36 private ObjectToolExecutor()
37 {
38 }
39
40 /**
41 * Check if tool is allowed for execution on each node from set
42 *
43 * @param tool
44 * @param nodes
45 * @return
46 */
47 public static boolean isToolAllowed(ObjectTool tool, Set<NodeInfo> nodes)
48 {
49 if (tool.getType() != ObjectTool.TYPE_INTERNAL)
50 return true;
51
52 ObjectToolHandler handler = ObjectToolsCache.findHandler(tool.getData());
53 if (handler != null)
54 {
55 for(NodeInfo n : nodes)
56 if (!handler.canExecuteOnNode(n.object, tool))
57 return false;
58 return true;
59 }
60 else
61 {
62 return false;
63 }
64 }
65
66 /**
67 * Check if given tool is applicable for all nodes in set
68 *
69 * @param tool
70 * @param nodes
71 * @return
72 */
73 public static boolean isToolApplicable(ObjectTool tool, Set<NodeInfo> nodes)
74 {
75 for(NodeInfo n : nodes)
76 if (!tool.isApplicableForNode(n.object))
77 return false;
78 return true;
79 }
80
81 /**
82 * Execute object tool on node set
83 * @param tool Object tool
84 */
85 public static void execute(final Set<NodeInfo> nodes, final ObjectTool tool)
86 {
87 if ((tool.getFlags() & ObjectTool.ASK_CONFIRMATION) != 0)
88 {
89 String message = tool.getConfirmationText();
90 if (nodes.size() == 1)
91 {
92 NodeInfo node = nodes.iterator().next();
93 message = message.replace("%OBJECT_IP_ADDR%", node.object.getPrimaryIP().getHostAddress()); //$NON-NLS-1$
94 message = message.replace("%OBJECT_NAME%", node.object.getObjectName()); //$NON-NLS-1$
95 message = message.replace("%OBJECT_ID%", Long.toString(node.object.getObjectId())); //$NON-NLS-1$
96 message = message.replace("%USERNAME%", ConsoleSharedData.getSession().getUserName()); //$NON-NLS-1$
97 }
98 else
99 {
100 message = message.replace("%OBJECT_IP_ADDR%", Messages.get().ObjectToolsDynamicMenu_MultipleNodes); //$NON-NLS-1$
101 message = message.replace("%OBJECT_NAME%", Messages.get().ObjectToolsDynamicMenu_MultipleNodes); //$NON-NLS-1$
102 message = message.replace("%OBJECT_ID%", Messages.get().ObjectToolsDynamicMenu_MultipleNodes); //$NON-NLS-1$
103 message = message.replace("%USERNAME%", ConsoleSharedData.getSession().getUserName()); //$NON-NLS-1$
104 }
105 if (!MessageDialogHelper.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
106 Messages.get().ObjectToolsDynamicMenu_ConfirmExec, message))
107 return;
108 }
109
110 for(NodeInfo n : nodes)
111 executeOnNode(n, tool);
112 }
113
114 /**
115 * Execute object tool on single node
116 *
117 * @param node
118 * @param tool
119 */
120 private static void executeOnNode(final NodeInfo node, final ObjectTool tool)
121 {
122 switch(tool.getType())
123 {
124 case ObjectTool.TYPE_INTERNAL:
125 executeInternalTool(node, tool);
126 break;
127 case ObjectTool.TYPE_SERVER_COMMAND:
128 executeServerCommand(node, tool);
129 break;
130 case ObjectTool.TYPE_ACTION:
131 executeAgentAction(node, tool);
132 break;
133 case ObjectTool.TYPE_TABLE_AGENT:
134 case ObjectTool.TYPE_TABLE_SNMP:
135 executeTableTool(node, tool);
136 break;
137 case ObjectTool.TYPE_URL:
138 openURL(node, tool);
139 break;
140 case ObjectTool.TYPE_FILE_DOWNLOAD:
141 executeFileDownload(node, tool);
142 break;
143 }
144 }
145
146 /**
147 * Execute table tool
148 *
149 * @param node
150 * @param tool
151 */
152 private static void executeTableTool(final NodeInfo node, final ObjectTool tool)
153 {
154 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
155 try
156 {
157 final IWorkbenchPage page = window.getActivePage();
158 final TableToolResults view = (TableToolResults)page.showView(TableToolResults.ID,
159 Long.toString(tool.getId()) + "&" + Long.toString(node.object.getObjectId()), IWorkbenchPage.VIEW_ACTIVATE); //$NON-NLS-1$
160 view.refreshTable();
161 }
162 catch(PartInitException e)
163 {
164 MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
165 }
166 }
167
168 /**
169 * @param node
170 * @param tool
171 */
172 private static void executeAgentAction(final NodeInfo node, final ObjectTool tool)
173 {
174 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
175 final String action = substituteMacros(tool.getData(), node);
176
177 if ((tool.getFlags() & ObjectTool.GENERATES_OUTPUT) == 0)
178 {
179 new ConsoleJob(String.format(Messages.get().ObjectToolsDynamicMenu_ExecuteOnNode, node.object.getObjectName()), null, Activator.PLUGIN_ID, null) {
180 @Override
181 protected String getErrorMessage()
182 {
183 return String.format(Messages.get().ObjectToolsDynamicMenu_CannotExecuteOnNode, node.object.getObjectName());
184 }
185
186 @Override
187 protected void runInternal(IProgressMonitor monitor) throws Exception
188 {
189 session.executeAction(node.object.getObjectId(), action);
190 runInUIThread(new Runnable() {
191 @Override
192 public void run()
193 {
194 MessageDialogHelper.openInformation(null, Messages.get().ObjectToolsDynamicMenu_ToolExecution, String.format(Messages.get().ObjectToolsDynamicMenu_ExecSuccess, action, node.object.getObjectName()));
195 }
196 });
197 }
198 }.start();
199 }
200 else
201 {
202 final String secondaryId = Long.toString(node.object.getObjectId()) + "&" + Long.toString(tool.getId()); //$NON-NLS-1$
203 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
204 try
205 {
206 AgentActionResults view = (AgentActionResults)window.getActivePage().showView(AgentActionResults.ID, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
207 view.executeAction(action);
208 }
209 catch(Exception e)
210 {
211 MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
212 }
213 }
214 }
215
216 /**
217 * Execute server command
218 *
219 * @param node
220 * @param tool
221 */
222 private static void executeServerCommand(final NodeInfo node, final ObjectTool tool)
223 {
224 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
225 new ConsoleJob(Messages.get().ObjectToolsDynamicMenu_ExecuteServerCmd, null, Activator.PLUGIN_ID, null) {
226 @Override
227 protected void runInternal(IProgressMonitor monitor) throws Exception
228 {
229 session.executeServerCommand(node.object.getObjectId(), tool.getData());
230 runInUIThread(new Runnable() {
231 @Override
232 public void run()
233 {
234 MessageDialogHelper.openInformation(null, Messages.get().ObjectToolsDynamicMenu_Information, Messages.get().ObjectToolsDynamicMenu_ServerCommandExecuted);
235 }
236 });
237 }
238
239 @Override
240 protected String getErrorMessage()
241 {
242 return Messages.get().ObjectToolsDynamicMenu_ServerCmdExecError;
243 }
244 }.start();
245 }
246
247 /**
248 * @param node
249 * @param tool
250 */
251 private static void executeFileDownload(final NodeInfo node, final ObjectTool tool)
252 {
253 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
254 String[] parameters = tool.getData().split("\u007F"); //$NON-NLS-1$
255
256 final String fileName = parameters[0];
257 final int maxFileSize = Integer.parseInt(parameters[1]);
258 final boolean follow = parameters[2].equals("true") ? true : false; //$NON-NLS-1$
259
260 ConsoleJob job = new ConsoleJob(Messages.get().ObjectToolsDynamicMenu_DownloadFromAgent, null, Activator.PLUGIN_ID, null) {
261 @Override
262 protected String getErrorMessage()
263 {
264 return String.format(Messages.get().ObjectToolsDynamicMenu_DownloadError, fileName, node.object.getObjectName());
265 }
266
267 @Override
268 protected void runInternal(IProgressMonitor monitor) throws Exception
269 {
270 final AgentFile file = session.downloadFileFromAgent(node.object.getObjectId(), fileName, maxFileSize, follow);
271 runInUIThread(new Runnable() {
272 @Override
273 public void run()
274 {
275 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
276 try
277 {
278 String secondaryId = Long.toString(node.object.getObjectId()) + "&" + URLEncoder.encode(fileName, "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
279 FileViewer view = (FileViewer)window.getActivePage().showView(FileViewer.ID, secondaryId,
280 IWorkbenchPage.VIEW_ACTIVATE);
281 view.showFile(file.getFile(), follow, file.getId(), maxFileSize);
282 }
283 catch(Exception e)
284 {
285 MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
286 }
287 }
288 });
289 }
290 };
291 job.start();
292 }
293
294 /**
295 * @param node
296 * @param tool
297 */
298 private static void executeInternalTool(final NodeInfo node, final ObjectTool tool)
299 {
300 ObjectToolHandler handler = ObjectToolsCache.findHandler(tool.getData());
301 if (handler != null)
302 {
303 handler.execute(node.object, tool);
304 }
305 else
306 {
307 MessageDialogHelper.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.get().ObjectToolsDynamicMenu_Error, Messages.get().ObjectToolsDynamicMenu_HandlerNotDefined);
308 }
309 }
310
311 /**
312 * @param node
313 * @param tool
314 */
315 private static void openURL(final NodeInfo node, final ObjectTool tool)
316 {
317 final String url = substituteMacros(tool.getData(), node);
318 final UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
319 launcher.openURL(url);
320 }
321
322 /**
323 * Substitute macros in string
324 *
325 * @param s
326 * @param node
327 * @return
328 */
329 private static String substituteMacros(String s, NodeInfo node)
330 {
331 StringBuilder sb = new StringBuilder();
332
333 char[] src = s.toCharArray();
334 for(int i = 0; i < s.length(); i++)
335 {
336 if (src[i] == '%')
337 {
338 StringBuilder p = new StringBuilder();
339 for(i++; src[i] != '%' && i < s.length(); i++)
340 p.append(src[i]);
341 if (p.length() == 0) // %%
342 {
343 sb.append('%');
344 }
345 else
346 {
347 String name = p.toString();
348 if (name.equals("OBJECT_IP_ADDR")) //$NON-NLS-1$
349 {
350 sb.append(node.object.getPrimaryIP().getHostAddress());
351 }
352 else if (name.equals("OBJECT_NAME")) //$NON-NLS-1$
353 {
354 sb.append(node.object.getObjectName());
355 }
356 else if (name.equals("OBJECT_ID")) //$NON-NLS-1$
357 {
358 sb.append(node.object.getObjectId());
359 }
360 else if (name.equals("ALARM_ID")) //$NON-NLS-1$
361 {
362 if (node.alarm != null)
363 sb.append(node.alarm.getId());
364 }
365 else if (name.equals("ALARM_MESSAGE")) //$NON-NLS-1$
366 {
367 if (node.alarm != null)
368 sb.append(node.alarm.getMessage());
369 }
370 else if (name.equals("ALARM_SEVERITY")) //$NON-NLS-1$
371 {
372 if (node.alarm != null)
373 sb.append(node.alarm.getCurrentSeverity());
374 }
375 else if (name.equals("ALARM_SEVERITY_TEXT")) //$NON-NLS-1$
376 {
377 if (node.alarm != null)
378 sb.append(StatusDisplayInfo.getStatusText(node.alarm.getCurrentSeverity()));
379 }
380 else if (name.equals("ALARM_STATE")) //$NON-NLS-1$
381 {
382 if (node.alarm != null)
383 sb.append(node.alarm.getState());
384 }
385 else if (name.equals("USERNAME")) //$NON-NLS-1$
386 {
387 sb.append(ConsoleSharedData.getSession().getUserName());
388 }
389 else
390 {
391 String custAttr = node.object.getCustomAttributes().get(name);
392 if (custAttr != null)
393 sb.append(custAttr);
394 }
395 }
396 }
397 else
398 {
399 sb.append(src[i]);
400 }
401 }
402
403 return sb.toString();
404 }
405 }