Hi,

After much scratching of head and grepping through the code I have
created the attached patch. I'm unsure if the using the BeanFactory to
pull out the NodeDao object is the correct method to use but as the
documentation on this is on the wiki is fairly sparse it's what I've
done.

Comments? Would you like this added to bugzilla?

-- 
Alex, homepage: http://www.bennee.com/~alex/
http://www.half-llama.co.uk
From d7f50b17347b4c3eae309d2cf8346661049cf1d1 Mon Sep 17 00:00:00 2001
From: Alex Bennee <a...@pitcairn.cambridgebroadband.com>
Date: Wed, 10 Feb 2010 18:39:11 +0000
Subject: [PATCH] Make OnmsNode visible to the Scriptd beanshell

In my use case for scripting (generating snmp trap events on certain
nodes going up and down) I need to know what those nodes are. This
patch exports the OnmsNode to the beanshell so more refined scripting
can be done.
---
 .../java/org/opennms/netmgt/scriptd/Executor.java  |   33 ++++++++++++++++++-
 .../java/org/opennms/netmgt/scriptd/Scriptd.java   |   10 +++++-
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Executor.java b/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Executor.java
index 07d45e2..2944ebb 100644
--- a/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Executor.java
+++ b/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Executor.java
@@ -53,6 +53,9 @@ import org.opennms.netmgt.config.scriptd.Uei;
 import org.opennms.netmgt.xml.event.Event;
 import org.opennms.netmgt.xml.event.Script;
 
+import org.opennms.netmgt.model.OnmsNode;
+import org.opennms.netmgt.dao.NodeDao;
+
 import org.apache.bsf.BSFException;
 import org.apache.bsf.BSFManager;
 
@@ -106,16 +109,24 @@ final class Executor implements Runnable, PausableFiber {
     private BSFManager m_mgr;
 
     /**
+     * The DAO object for fetching nodes
+     */
+    private NodeDao m_nodeDao;
+
+
+    /**
      * Constructs a new action daemon execution environment. The constructor
-     * takes two arguments that define the source of commands to be executed and
+     * takes three arguments that define the source of commands to be executed and
      * the maximum time that a command may run.
      * 
      * @param execQ
      *            The execution queue
      * @param config
      *            The <em>Scriptd</em> configuration.
+     * @param nodeDao
+     *            The <em>DAO</em> for fetching node information
      */
-    Executor(FifoQueue execQ, ScriptdConfigFactory config) {
+    Executor(FifoQueue execQ, ScriptdConfigFactory config, NodeDao nodeDao) {
         m_execQ = execQ;
         m_config = config;
 
@@ -125,6 +136,8 @@ final class Executor implements Runnable, PausableFiber {
         m_name = "Scriptd-Executor";
         m_mgr = null;
         m_status = START_PENDING;
+
+	m_nodeDao = nodeDao;
     }
 
     /**
@@ -264,8 +277,19 @@ final class Executor implements Runnable, PausableFiber {
 
                 m_mgr.registerBean("event", event);
 
+		// And the events node
+		OnmsNode node = null;
+
+		if (event.hasNodeid()) {
+		    Long nodeLong = event.getNodeid();
+		    Integer nodeInt = new Integer(nodeLong.intValue());
+		    node = m_nodeDao.get(nodeInt);
+		    m_mgr.registerBean("node", node);
+		}
+
                 // execute the scripts attached to the event
 
+                log.debug("Executing attached scripts");
                 if (attachedScripts.length > 0) {
                     for (int i = 0; i < attachedScripts.length; i++) {
                         try {
@@ -281,6 +305,7 @@ final class Executor implements Runnable, PausableFiber {
 
                 // execute the scripts mapped to the UEI
 
+                log.debug("Executing mapped scripts");
                 if (mapScripts != null) {
                     for (int i = 0; i < mapScripts.size(); i++) {
                         try {
@@ -296,6 +321,7 @@ final class Executor implements Runnable, PausableFiber {
 
                 // execute the scripts that are not mapped to any UEI
 
+                log.debug("Executing global scripts");
                 for (int i = 0; i < m_eventScripts.size(); i++) {
                     try {
                         EventScript script = (EventScript) m_eventScripts.get(i);
@@ -307,6 +333,9 @@ final class Executor implements Runnable, PausableFiber {
                     }
                 }
 
+		if (node != null)
+		    m_mgr.unregisterBean("node");
+		
                 m_mgr.unregisterBean("event");
 
                 log.debug("Finished executing scripts for: " + event.getUei());
diff --git a/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Scriptd.java b/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Scriptd.java
index a14c725..67b7d60 100644
--- a/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Scriptd.java
+++ b/opennms-services/src/main/java/org/opennms/netmgt/scriptd/Scriptd.java
@@ -49,6 +49,10 @@ import org.opennms.core.queue.FifoQueueImpl;
 import org.opennms.netmgt.config.ScriptdConfigFactory;
 import org.opennms.netmgt.daemon.AbstractServiceDaemon;
 
+import org.opennms.netmgt.dao.NodeDao;
+import org.opennms.core.utils.BeanUtils;
+import org.springframework.beans.factory.access.BeanFactoryReference;
+
 /**
  * This class implements a script execution service. This service subscribes to
  * all events, and passes received events to the set of configured scripts.
@@ -121,7 +125,11 @@ public final class Scriptd extends AbstractServiceDaemon {
             throw new UndeclaredThrowableException(ex);
         }
 
-        m_execution = new Executor(execQ, aFactory);
+	// get the node DAO
+	BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
+	NodeDao nodeDao = BeanUtils.getBean(bf, "nodeDao", NodeDao.class);
+
+        m_execution = new Executor(execQ, aFactory, nodeDao);
     }
 
     protected void onStart() {
-- 
1.6.6.1

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
Please read the OpenNMS Mailing List FAQ:
http://www.opennms.org/index.php/Mailing_List_FAQ

opennms-devel mailing list

To *unsubscribe* or change your subscription options, see the bottom of this 
page:
https://lists.sourceforge.net/lists/listinfo/opennms-devel

Reply via email to