Java subagent plugins can be specified as relative path to libdir (issue #NX-1260)
authorVictor Kirhenshtein <victor@netxms.org>
Wed, 28 Jun 2017 14:48:17 +0000 (17:48 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Wed, 28 Jun 2017 14:48:17 +0000 (17:48 +0300)
ChangeLog
src/agent/subagents/java/.cproject
src/agent/subagents/java/SubAgent.cpp
src/agent/subagents/java/java/Makefile.am
src/agent/subagents/java/java/src/main/java/org/netxms/agent/DirectoryType.java [new file with mode: 0644]
src/agent/subagents/java/java/src/main/java/org/netxms/agent/SubAgent.java

index b6e74f7..fd0b6d6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -20,7 +20,7 @@
        - Fixed bug in folder download in file manager
        - Proper support for ANSI colors in object tools output in web UI
        - Object dragging on map disabled if user don't have write access to map object
-- Fixed issues: NX-1039, NX-1041, NX-1166, NX-1180, NX-1194, NX-1212, NX-1214, NX-1215, NX-1234, NX-1236, NX-1243, NX-1244, NX-1251, NX-1253, NX-1257, NX-1264
+- Fixed issues: NX-1039, NX-1041, NX-1166, NX-1180, NX-1194, NX-1212, NX-1214, NX-1215, NX-1234, NX-1236, NX-1243, NX-1244, NX-1251, NX-1253, NX-1257, NX-1260, NX-1264
 
 
 *
 - Management console
        - New editors for Agent Config Policy and Log Parser Policy. 
        - DCI summary tables with empty menu path not shown in object context menu
-- Fixed issues: NX-44, NX-53, NX-65, NX-272, NX-355, NX-393, NX-520, NX-587, NX-694, NX-697, NX-726, NX-732, NX-742, NX-773, NX-780, NX-787, NX-796, NX-825, NX-832, NX-876, NX-880, NX-934, NX-956, NX-960, NX-989, NX-996, NX-1000, NX-1001, NX-1004, NX-1009, NX-1012, NX-1016, NX-1018, NX-1026, NX-1033, NX-1034, NX-1038, NX-1051
+- Fixed issues: NX-44, NX-53, NX-65, NX-272, NX-355, NX-393, NX-520, NX-587, NX-694, NX-697, NX-726, NX-732, NX-742, NX-773, NX-780, NX-787, NX-796, NX-825, NX-832, NX-876, NX-880, NX-934, NX-956, NX-960, NX-980, NX-989, NX-996, NX-1000, NX-1001, NX-1004, NX-1009, NX-1012, NX-1016, NX-1018, NX-1026, NX-1033, NX-1034, NX-1038, NX-1051
 
 
 *
index b514c7c..bdbc0f2 100644 (file)
@@ -5,12 +5,12 @@
                        <storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.base.2026808676" moduleId="org.eclipse.cdt.core.settings" name="Default">
                                <externalSettings/>
                                <extensions>
+                                       <extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
                                        <extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
                                        <extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
                                        <extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
                                        <extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
                                        <extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
-                                       <extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
                                </extensions>
                        </storageModule>
                        <storageModule moduleId="cdtBuildSystem" version="4.0.0">
@@ -25,6 +25,7 @@
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}/include&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include&quot;"/>
+                                                                       <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include/linux&quot;"/>
                                                                </option>
                                                                <option id="gnu.cpp.compiler.option.preprocessor.def.516155203" name="Defined symbols (-D)" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols">
                                                                        <listOptionValue builtIn="false" value="_THREAD_SAFE"/>
@@ -39,6 +40,7 @@
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}/include&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include&quot;"/>
+                                                                       <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include/linux&quot;"/>
                                                                </option>
                                                                <option id="gnu.c.compiler.option.preprocessor.def.symbols.240236153" name="Defined symbols (-D)" superClass="gnu.c.compiler.option.preprocessor.def.symbols" valueType="definedSymbols">
                                                                        <listOptionValue builtIn="false" value="_THREAD_SAFE"/>
@@ -60,6 +62,7 @@
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${NETXMS_BASE}/include&quot;"/>
                                                                        <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include&quot;"/>
+                                                                       <listOptionValue builtIn="false" value="&quot;${JAVA_HOME}/include/linux&quot;"/>
                                                                </option>
                                                                <inputType id="cdt.managedbuild.tool.gnu.assembler.input.1889915984" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
                                                        </tool>
@@ -83,4 +86,5 @@
                        <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
                </scannerConfigBuildInfo>
        </storageModule>
+       <storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
 </cproject>
index 49ad49c..fac4416 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  ** Java-Bridge NetXMS subagent
  ** Copyright (c) 2013 TEMPEST a.s.
- ** Copyright (c) 2015-2016 Raden Solutions SIA
+ ** Copyright (c) 2015-2017 Raden Solutions SIA
  **
  ** 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
@@ -61,11 +61,23 @@ jmethodID SubAgent::m_tableHandler = NULL;
 bool SubAgent::m_initialized = false;
 
 /**
- * Class:     org_netxms_agent_SubAgent
- * Method:    AgentGetParameterArg
+ * Class:     org.netxms.agent.SubAgent
+ * Method:    getNetXMSDirectoryInternal
+ * Signature: (I)Ljava/lang/String;
+ */
+static jstring JNICALL J_getNetXMSDirectoryInternal(JNIEnv *jenv, jclass jcls, jint type)
+{
+   TCHAR buffer[MAX_PATH];
+   GetNetXMSDirectory(static_cast<nxDirectoryType>(type), buffer);
+   return JavaStringFromCString(jenv, buffer);
+}
+
+/**
+ * Class:     org.netxms.agent.SubAgent
+ * Method:    getParameterArg
  * Signature: (Ljava/lang/String;I)Ljava/lang/String;
  */
-static jstring JNICALL Java_org_netxms_agent_SubAgent_getParameterArg(JNIEnv *jenv, jclass jcls, jstring jparam, jint jindex)
+static jstring JNICALL J_getParameterArg(JNIEnv *jenv, jclass jcls, jstring jparam, jint jindex)
 {
    jstring jresult = NULL;
    if (jparam)
@@ -82,11 +94,11 @@ static jstring JNICALL Java_org_netxms_agent_SubAgent_getParameterArg(JNIEnv *je
 }
 
 /**
- * Class:     org_netxms_agent_SubAgent
+ * Class:     org.netxms.agent.SubAgent
  * Method:    sendTrap
  * Signature: (ILjava/lang/String;[Ljava/lang/String;)V
  */
-static void JNICALL Java_org_netxms_agent_SubAgent_sendTrap(JNIEnv *jenv, jclass jcls, jint event, jstring jname, jobjectArray jargs)
+static void JNICALL J_sendTrap(JNIEnv *jenv, jclass jcls, jint event, jstring jname, jobjectArray jargs)
 {
    if ((jname != NULL) && (jargs != NULL))
    {
@@ -109,11 +121,11 @@ static void JNICALL Java_org_netxms_agent_SubAgent_sendTrap(JNIEnv *jenv, jclass
 }
 
 /**
- * Class:     org_netxms_agent_SubAgent
- * Method:    AgentPushParameterData
+ * Class:     org.netxms.agent.SubAgent
+ * Method:    pushParameterData
  * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
  */
-static jboolean JNICALL Java_org_netxms_agent_SubAgent_pushParameterData(JNIEnv *jenv, jclass jcls, jstring jname, jstring jvalue)
+static jboolean JNICALL J_pushParameterData(JNIEnv *jenv, jclass jcls, jstring jname, jstring jvalue)
 {
    jboolean res = false;
    if ((jname != NULL) && (jvalue != NULL))
@@ -128,11 +140,11 @@ static jboolean JNICALL Java_org_netxms_agent_SubAgent_pushParameterData(JNIEnv
 }
 
 /**
- * Class:     org_netxms_agent_SubAgent
- * Method:    AgentWriteLog
+ * Class:     org.netxms.agent.SubAgent
+ * Method:    writeLog
  * Signature: (ILjava/lang/String;)V
  */
-static void JNICALL Java_org_netxms_agent_SubAgent_writeLog(JNIEnv *jenv, jclass jcls, jint level, jstring jmessage)
+static void JNICALL J_writeLog(JNIEnv *jenv, jclass jcls, jint level, jstring jmessage)
 {
    if (jmessage != NULL)
    {
@@ -143,11 +155,11 @@ static void JNICALL Java_org_netxms_agent_SubAgent_writeLog(JNIEnv *jenv, jclass
 }
 
 /**
- * Class:     org_netxms_agent_SubAgent
- * Method:    AgentWriteDebugLog
+ * Class:     org.netxms.agent.SubAgent
+ * Method:    writeDebugLog
  * Signature: (ILjava/lang/String;)V
  */
-static void JNICALL Java_org_netxms_agent_SubAgent_writeDebugLog(JNIEnv *jenv, jclass jcls, jint level, jstring jmessage)
+static void JNICALL J_writeDebugLog(JNIEnv *jenv, jclass jcls, jint level, jstring jmessage)
 {
    if (jmessage != NULL)
    {
@@ -162,11 +174,12 @@ static void JNICALL Java_org_netxms_agent_SubAgent_writeDebugLog(JNIEnv *jenv, j
  */
 static JNINativeMethod s_jniNativeMethods[] =
 {
-   { (char *)"getParameterArg", (char *)"(Ljava/lang/String;I)Ljava/lang/String;", (void *)Java_org_netxms_agent_SubAgent_getParameterArg },
-   { (char *)"pushParameterData", (char *)"(Ljava/lang/String;Ljava/lang/String;)Z", (void *)Java_org_netxms_agent_SubAgent_pushParameterData },
-   { (char *)"sendTrap", (char *)"(ILjava/lang/String;[Ljava/lang/String;)V", (void *)Java_org_netxms_agent_SubAgent_sendTrap },
-   { (char *)"writeDebugLog", (char *)"(ILjava/lang/String;)V", (void *)Java_org_netxms_agent_SubAgent_writeDebugLog },
-   { (char *)"writeLog", (char *)"(ILjava/lang/String;)V", (void *)Java_org_netxms_agent_SubAgent_writeLog }
+   { (char *)"getNetXMSDirectoryInternal", (char *)"(I)Ljava/lang/String;", (void *)J_getNetXMSDirectoryInternal },
+   { (char *)"getParameterArg", (char *)"(Ljava/lang/String;I)Ljava/lang/String;", (void *)J_getParameterArg },
+   { (char *)"pushParameterData", (char *)"(Ljava/lang/String;Ljava/lang/String;)Z", (void *)J_pushParameterData },
+   { (char *)"sendTrap", (char *)"(ILjava/lang/String;[Ljava/lang/String;)V", (void *)J_sendTrap },
+   { (char *)"writeDebugLog", (char *)"(ILjava/lang/String;)V", (void *)J_writeDebugLog },
+   { (char *)"writeLog", (char *)"(ILjava/lang/String;)V", (void *)J_writeLog }
 };
 
 /**
index 07f09aa..848d363 100644 (file)
@@ -6,4 +6,7 @@ netxms-agent.jar: pom.xml
        mvn install
        mv target/netxms-agent.jar .
 
+clean-local:
+       rm -rf netxms-agent.jar
+
 EXTRA_DIST = Makefile.w32 pom.xml netxms-agent.jar
diff --git a/src/agent/subagents/java/java/src/main/java/org/netxms/agent/DirectoryType.java b/src/agent/subagents/java/java/src/main/java/org/netxms/agent/DirectoryType.java
new file mode 100644 (file)
index 0000000..23aa2ea
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Java-Bridge NetXMS subagent
+ * Copyright (C) 2013 TEMPEST a.s.
+ * Copyright (C) 2014-2017 Raden Solutions
+ *
+ * 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.agent;
+
+/**
+ * NetXMS directory type
+ */
+public enum DirectoryType
+{
+   BIN(0), DATA(1), ETC(2), LIB(3), SHARE(4); 
+   
+   private int value;
+
+   DirectoryType(final int value)
+   {
+      this.value = value;
+   }
+
+   public int getValue()
+   {
+      return value;
+   }
+}
index 26bc5c6..c8ae4b4 100644 (file)
@@ -120,6 +120,14 @@ public class SubAgent
    /*===== native methods exposed by agent (see nms_agent.h) =====*/
    
    /**
+    * Get NetXMS directory (internal bridge to native code)
+    * 
+    * @param type directory type
+    * @return path to directory or empty string on error
+    */
+   protected static native String getNetXMSDirectoryInternal(int type);
+   
+   /**
     * Extract argument from full parameter name.
     * 
     * For example:
@@ -169,6 +177,16 @@ public class SubAgent
       }
    }
    
+   /**
+    * Get NetXMS directory
+    * 
+    * @param type directory type
+    * @return path to directory or empty string on error
+    */
+   public static String getNetXMSDirectory(DirectoryType type)
+   {
+      return getNetXMSDirectoryInternal(type.getValue());
+   }
 
    /**
     * Initialize (to be called from native subagent)
@@ -309,7 +327,25 @@ public class SubAgent
       URLClassLoader classLoader;
       try
       {
-         classLoader = new URLClassLoader(new URL[] { new File(jarFile).toURI().toURL() }, Thread.currentThread().getContextClassLoader() );
+         File file = new File(jarFile);
+         if (!file.isAbsolute())
+         {
+            writeDebugLog(5, "Plugin file path \"" + jarFile + "\" is not absolute");
+            String libDir = getNetXMSDirectory(DirectoryType.LIB);
+            if ((libDir != null) && !libDir.isEmpty())
+            {
+               file = new File(libDir + File.separatorChar + jarFile);
+            }
+         }
+         
+         if (!file.isFile())
+         {
+            writeLog(LogLevel.WARNING, "File \"" + file.getAbsolutePath() + "\" does not exist or is not a regular file");
+            return null;
+         }
+         
+         writeDebugLog(2, "Loading plugin file \"" + file.getAbsolutePath() + "\"");
+         classLoader = new URLClassLoader(new URL[] { file.toURI().toURL() }, Thread.currentThread().getContextClassLoader() );
          URL url = classLoader.findResource(MANIFEST_PATH);
          if (url != null)
          {