New object tool type "server script"
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 19 Sep 2016 06:50:33 +0000 (09:50 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 19 Sep 2016 06:50:33 +0000 (09:50 +0300)
src/java/netxms-eclipse/ObjectTools/icons/script.png [new file with mode: 0644]
src/java/netxms-eclipse/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java [new file with mode: 0644]
webui/webapp/ObjectTools/icons/script.png [new file with mode: 0644]
webui/webapp/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java [new file with mode: 0644]

diff --git a/src/java/netxms-eclipse/ObjectTools/icons/script.png b/src/java/netxms-eclipse/ObjectTools/icons/script.png
new file mode 100644 (file)
index 0000000..0c76bd1
Binary files /dev/null and b/src/java/netxms-eclipse/ObjectTools/icons/script.png differ
diff --git a/src/java/netxms-eclipse/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java b/src/java/netxms-eclipse/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java
new file mode 100644 (file)
index 0000000..e883950
--- /dev/null
@@ -0,0 +1,189 @@
+/**
+ * NetXMS - open source network management system
+ * Copyright (C) 2003-2016 Victor Kirhenshtein
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.netxms.ui.eclipse.objecttools.views;
+
+import java.io.IOException;
+import java.util.Map;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.ui.console.IOConsoleOutputStream;
+import org.netxms.client.NXCSession;
+import org.netxms.client.TextOutputListener;
+import org.netxms.ui.eclipse.console.resources.SharedIcons;
+import org.netxms.ui.eclipse.jobs.ConsoleJob;
+import org.netxms.ui.eclipse.objecttools.Activator;
+import org.netxms.ui.eclipse.objecttools.Messages;
+import org.netxms.ui.eclipse.shared.ConsoleSharedData;
+
+/**
+ * View for server script execution results
+ */
+public class ServerScriptResults extends AbstractCommandResults implements TextOutputListener
+{
+   public static final String ID = "org.netxms.ui.eclipse.objecttools.views.ServerScriptResults"; //$NON-NLS-1$
+
+   private IOConsoleOutputStream out;
+   private String lastScript = null;
+   private Action actionRestart;
+   private Map<String, String> lastInputValues = null;
+   
+   /**
+    * Create actions
+    */
+   protected void createActions()
+   {
+      super.createActions();
+      
+      actionRestart = new Action(Messages.get().LocalCommandResults_Restart, SharedIcons.RESTART) {
+         @Override
+         public void run()
+         {
+            executeScript(lastScript, lastInputValues);
+         }
+      };
+      actionRestart.setEnabled(false);
+   }
+   
+   /**
+    * Fill local pull-down menu
+    * 
+    * @param manager Menu manager for pull-down menu
+    */
+   protected void fillLocalPullDown(IMenuManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillLocalPullDown(manager);
+   }
+
+   /**
+    * Fill local tool bar
+    * 
+    * @param manager Menu manager for local toolbar
+    */
+   protected void fillLocalToolBar(IToolBarManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillLocalToolBar(manager);
+   }
+
+   /**
+    * Fill context menu
+    * 
+    * @param mgr Menu manager
+    */
+   protected void fillContextMenu(final IMenuManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillContextMenu(manager);
+   }
+   
+   /**
+    * @param script
+    * @param inputValues 
+    */
+   public void executeScript(final String script, final Map<String, String> inputValues)
+   {
+      actionRestart.setEnabled(false);
+      final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
+      out = console.newOutputStream();
+      lastScript = script;
+      lastInputValues = inputValues;
+      ConsoleJob job = new ConsoleJob(String.format(Messages.get().ObjectToolsDynamicMenu_ExecuteOnNode, session.getObjectName(nodeId)), null, Activator.PLUGIN_ID, null) {
+         @Override
+         protected String getErrorMessage()
+         {
+            return String.format(Messages.get().ObjectToolsDynamicMenu_CannotExecuteOnNode, session.getObjectName(nodeId));
+         }
+
+         @Override
+         protected void runInternal(IProgressMonitor monitor) throws Exception
+         {
+            try
+            {
+               session.executeLibraryScript(nodeId, script, inputValues, ServerScriptResults.this);
+            }
+            finally
+            {
+               if (out != null)
+               {
+                  out.close();
+                  out = null;
+               }
+            }
+         }
+
+         @Override
+         protected void jobFinalize()
+         {
+            runInUIThread(new Runnable() {
+               @Override
+               public void run()
+               {
+                  actionRestart.setEnabled(true);
+               }
+            });
+         }
+      };
+      job.setUser(false);
+      job.setSystem(true);
+      job.start();
+   }
+
+   /* (non-Javadoc)
+    * @see org.netxms.client.ActionExecutionListener#messageReceived(java.lang.String)
+    */
+   @Override
+   public void messageReceived(String text)
+   {
+      try
+      {
+         if (out != null)
+            out.write(text.replace("\r", "")); //$NON-NLS-1$ //$NON-NLS-2$
+      }
+      catch(IOException e)
+      {
+      }
+   }
+
+   /* (non-Javadoc)
+    * @see org.eclipse.ui.part.WorkbenchPart#dispose()
+    */
+   @Override
+   public void dispose()
+   {
+      if (out != null)
+      {
+         try
+         {
+            out.close();
+         }
+         catch(IOException e)
+         {
+         }
+         out = null;
+      }
+      super.dispose();
+   }
+}
diff --git a/webui/webapp/ObjectTools/icons/script.png b/webui/webapp/ObjectTools/icons/script.png
new file mode 100644 (file)
index 0000000..0c76bd1
Binary files /dev/null and b/webui/webapp/ObjectTools/icons/script.png differ
diff --git a/webui/webapp/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java b/webui/webapp/ObjectTools/src/org/netxms/ui/eclipse/objecttools/views/ServerScriptResults.java
new file mode 100644 (file)
index 0000000..fc0a34e
--- /dev/null
@@ -0,0 +1,189 @@
+/**
+ * NetXMS - open source network management system
+ * Copyright (C) 2003-2016 Victor Kirhenshtein
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package org.netxms.ui.eclipse.objecttools.views;
+
+import java.io.IOException;
+import java.util.Map;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.Separator;
+import org.netxms.client.NXCSession;
+import org.netxms.client.TextOutputListener;
+import org.netxms.ui.eclipse.console.resources.SharedIcons;
+import org.netxms.ui.eclipse.jobs.ConsoleJob;
+import org.netxms.ui.eclipse.objecttools.Activator;
+import org.netxms.ui.eclipse.objecttools.Messages;
+import org.netxms.ui.eclipse.shared.ConsoleSharedData;
+import org.netxms.ui.eclipse.widgets.TextConsole.IOConsoleOutputStream;
+
+/**
+ * View for server script execution results
+ */
+public class ServerScriptResults extends AbstractCommandResults implements TextOutputListener
+{
+   public static final String ID = "org.netxms.ui.eclipse.objecttools.views.ServerScriptResults"; //$NON-NLS-1$
+
+   private IOConsoleOutputStream out;
+   private String lastScript = null;
+   private Action actionRestart;
+   private Map<String, String> lastInputValues = null;
+   
+   /**
+    * Create actions
+    */
+   protected void createActions()
+   {
+      super.createActions();
+      
+      actionRestart = new Action(Messages.get().LocalCommandResults_Restart, SharedIcons.RESTART) {
+         @Override
+         public void run()
+         {
+            executeScript(lastScript, lastInputValues);
+         }
+      };
+      actionRestart.setEnabled(false);
+   }
+   
+   /**
+    * Fill local pull-down menu
+    * 
+    * @param manager Menu manager for pull-down menu
+    */
+   protected void fillLocalPullDown(IMenuManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillLocalPullDown(manager);
+   }
+
+   /**
+    * Fill local tool bar
+    * 
+    * @param manager Menu manager for local toolbar
+    */
+   protected void fillLocalToolBar(IToolBarManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillLocalToolBar(manager);
+   }
+
+   /**
+    * Fill context menu
+    * 
+    * @param mgr Menu manager
+    */
+   protected void fillContextMenu(final IMenuManager manager)
+   {
+      manager.add(actionRestart);
+      manager.add(new Separator());
+      super.fillContextMenu(manager);
+   }
+   
+   /**
+    * @param script
+    * @param inputValues 
+    */
+   public void executeScript(final String script, final Map<String, String> inputValues)
+   {
+      actionRestart.setEnabled(false);
+      final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
+      out = console.newOutputStream();
+      lastScript = script;
+      lastInputValues = inputValues;
+      ConsoleJob job = new ConsoleJob(String.format(Messages.get().ObjectToolsDynamicMenu_ExecuteOnNode, session.getObjectName(nodeId)), null, Activator.PLUGIN_ID, null) {
+         @Override
+         protected String getErrorMessage()
+         {
+            return String.format(Messages.get().ObjectToolsDynamicMenu_CannotExecuteOnNode, session.getObjectName(nodeId));
+         }
+
+         @Override
+         protected void runInternal(IProgressMonitor monitor) throws Exception
+         {
+            try
+            {
+               session.executeLibraryScript(nodeId, script, inputValues, ServerScriptResults.this);
+            }
+            finally
+            {
+               if (out != null)
+               {
+                  out.close();
+                  out = null;
+               }
+            }
+         }
+
+         @Override
+         protected void jobFinalize()
+         {
+            runInUIThread(new Runnable() {
+               @Override
+               public void run()
+               {
+                  actionRestart.setEnabled(true);
+               }
+            });
+         }
+      };
+      job.setUser(false);
+      job.setSystem(true);
+      job.start();
+   }
+
+   /* (non-Javadoc)
+    * @see org.netxms.client.ActionExecutionListener#messageReceived(java.lang.String)
+    */
+   @Override
+   public void messageReceived(String text)
+   {
+      try
+      {
+         if (out != null)
+            out.write(text.replace("\r", "")); //$NON-NLS-1$ //$NON-NLS-2$
+      }
+      catch(IOException e)
+      {
+      }
+   }
+
+   /* (non-Javadoc)
+    * @see org.eclipse.ui.part.WorkbenchPart#dispose()
+    */
+   @Override
+   public void dispose()
+   {
+      if (out != null)
+      {
+         try
+         {
+            out.close();
+         }
+         catch(IOException e)
+         {
+         }
+         out = null;
+      }
+      super.dispose();
+   }
+}