NewJavaObject

The NewJavaObject function is used to call the constructor for a Java class.

Adding Java archive (JAR) files to the shared library directory

Before you can use this policy function, you must make the Java classes available to Netcool/Impact during run time. To make the Java classes available, complete the following steps:
  1. Copy the Java classes to the $IMPACT_HOME/dsalib directory.
  2. Restart the Impact Server to load the JAR files.
You must repeat this procedure for each Impact Server. This is necessary because the Java class files in the $IMPACT_HOME/dsalib directory are not replicated between servers.

Syntax

NewJavaObject( ClassName, Parameters )

Parameters

Table 1. NewJavaObject function parameters

Parameter

Description

ClassName

Name of the Java class you are instantiating a Java object for.

Parameters

An array of parameter values a constructor for this class requires.

Returns

The instantiated object.

Examples

To create a Java String Object "This is a string!" and assign it to variable str, then in an IPL policy, put in the following line:

str = NewJavaObject("String", {"This is a string!"});

This IPL example of code creates a Java object of class java.util.HashTable and then adds, retrieves, and removes data from it:

// Create a new instance of Java Hashtable class.
my_hash = NewJavaObject("java.util.Hashtable", null);
// Add table entry ( "one", "aaaa" } to my_hash.
JavaCall(null, my_hash, "put", { "one", "aaaa" } );
// Add entry ("two", "bbbb") to table my_hash.
JavaCall(null, my_hash, "put", { "two", "bbbb" } );
// Add entry ("three", "cccc") to table.
JavaCall(null, my_hash, "put", { "three", "cccc" });
// Print the table entry value indexed by the key "three"
log("Check hashtable value indexed by key \"three\". Value is " +
JavaCall(null, my_hash, "get", {"three" }));
// Remove the entry indexed by the key "one" from the table
JavaCall(null, my_hash, "remove", {"one"});
log("After remove call, my_hash becomes " + my_hash);

To create a Java String Object "This is a string!" and assign it to variable str, then in a JavaScript policy, put in the following line:

str = NewJavaObject("String", ["This is a string!"]);

This JavaScript example of code creates a Java object of class java.util.HashTable and then adds, retrieves, and removes data from it:

// Create a new instance of Java Hashtable class.
my_hash = NewJavaObject("java.util.Hashtable", null);
// Add table entry ("one", "aaaa") to my_hash.
JavaCall(null, my_hash, "put", ["one", "aaaa"]);
// Add entry ("two", "bbbb") to table my_hash.
JavaCall(null, my_hash, "put", ["two", "bbbb"] );
// Add entry ("three", "cccc") to table.
JavaCall(null, my_hash, "put", ["three", "cccc"]);
// Print the table entry value indexed by the key "three"
Log("Check hashtable value indexed by key \"three\". Value is " +
JavaCall(null, my_hash, "get", ["three"]));
// Remove the entry indexed by the key "one" from the table
JavaCall(null, my_hash, "remove", ["one"]);
Log("After remove call, my_hash becomes " + my_hash); 

The Impact policy does not support file or directory operations. The Java API, however, supports these operations in its java.io.* library. You can access this library and all other functions the Java API provides by using the Java Policy functions. This piece of code, for example, calls java.io.File class, opens a directory, and outputs a list of the files in the directory:

This example applies to IPL.

homedir = NewJavaObject("java.io.File", {"/home/user/"});
file_list = JavaCall(null, homedir, "list", {});
Log("file_list is " + file_list);

This example applies to JavaScript.

homedir = NewJavaObject("java.io.File", ["/home/user/"] );
file_list = JavaCall(null, homedir, "list", []);
Log("file_list is " + file_list);