Exploring a sample Java application
This section describes how to create, compile, and run a simple Java™ application.
To create the Java sample application, complete the following steps:
- Copy the following Java code
into a file called FindXmlExample.java.
The source code is also available in the $COLLATION_HOME/sdk/examples/java directory.
package com.collation.proxy.api.examples.java; // package com.collation.proxy.api.examples.java; import com.collation.proxy.api.client.ApiConnection; import com.collation.proxy.api.client.ApiException; import com.collation.proxy.api.client.ApiSession; import com.collation.proxy.api.client.CMDBApi; import com.collation.proxy.api.client.DataResultSet; import com.ibm.cdb.api.ApiFactory; /** * Simple CMDB API findXML() example: * <p> get connection and log into api server * <p> find all machines which have more than 1 CPU * <p> find all Oracle instances */ public class FindXmlExample { public static void main(String[] args) { CMDBApi api = null; ApiSession sess = null; try { /* * Establish connection to api server * <p> ApiConnection.getConnection(host, port, trustoreLocation, useSSL) */ ApiConnection conn = ApiFactory.getInstance().getApiConnection("localhost", -1, null, false); /* * Get a session: * <p> ApiSession.getSession(connection, username, password, version) */ sess = ApiFactory.getInstance().getSession(conn, "smartoperator", "foobar", ApiSession.DEFAULT_VERSION); /* * Get an CMDBApi instance */ api = sess.createCMDBApi(); System.out.println("all machines which have more than 1 CPU:"); String query = "select * from ComputerSystem where numCPUs>1"; /* * Find all of the ComputerSystem have more than 1 CPU. * The method: findXml(query, depth, indent, mssGuid, permissions) * is deprecated, as the result set may be too large to fit into * memory. Instead, using cursors is encouraged: */ DataResultSet data = api.executeQuery(query, null, null); while (data.next()) { System.out.println(data.getXML(4)); } data.close(); System.out.println("\nall Oracle instances:"); query = "select * from OracleInstance"; data = api.executeQuery(query, null, null); while (data.next()) { System.out.println(data.getXML(4)); } data.close(); } catch (ApiException ae) { System.err.println("api exception:" + ae); ae.printStackTrace(); } catch (Exception ex) { System.err.println("exception:" + ex); ex.printStackTrace(); } finally { try { if (api != null) { api.close(); } if (sess != null) { sess.close(); } } catch (Exception ex) { System.err.println("exception:" + ex); ex.printStackTrace(); } } } }
- By default, the sample program connects to a TADDM server
on the local host. If you wish to connect to a remote server, change
the following line:
For example, to connect to a server named taddmhost.ibm.com using the default ports:ApiConnection conn = ApiFactory.getInstance().getApiConnection("localhost", -1, null, false);
By default, the sample program creates a session with a user ID ofApiConnection conn = ApiFactory.getInstance(). getApiConnection("taddmhost.ibm.com",-1, null, false);
smartoperator
and a password offoobar
. Change this to match a user ID and password that is defined to your TADDM server. For example:sess = ApiFactory.getInstance(). getSession(conn, "administrator", "collation",ApiSession.DEFAULT_VERSION);
- To compile the sample program, along with the other sample Java programs, complete the following
steps:
- On UNIX systems:
- Change to the $COLLATION_HOME/sdk/examples/java directory.
- Make a build.sh executable:
chmod +x build.sh
- Run the build command:
./build.sh
- On Windows:
- Change to the %COLLATION_HOME%\sdk\examples\java directory.
- Run the build command:
build.bat
If the SDK is installed separately from the TADDM server, make sure that javac is already in the path and available.
- On UNIX systems:
- Run the Java application
using a command similar to the following example command:
% java -Dcom.collation.home=$COLLATION_HOME/sdk FindXmlExample
Alternatively, run the following command:
run.sh FindXmlExample
These commands run the sample program and retrieve the XML data from the TADDM server.