Sun WBEM SDK Developer's Guide

Chapter 7 Using Sun WBEM SDK Examples

This chapter describes the sample programs provided in the Sun WBEM SDK and includes the following topics:

About Example Programs

The Sun WBEM SDK provides example Java programs, which are installed in /usr/demo/wbem. You can use the source code as a basis for developing your own programs. Three types of example programs are provided:

Using the Applet

The GetPackageInfoAp is a Java Applet that you can use to list the Solaris software packages that are installed on a system running Solaris WBEM Services. You can select a package and display the detailed information about that package. You can use this applet to connect to a CIM Object Manager running on your local system or a remote system.

To run the applet you need one of the following:

For detailed information on running the applet, see /usr/demo/wbem/README.

Using Client Examples

The client examples use the client APIs to create, delete, and list classes, instances, and namespaces. The five types of client programs are:

Client Example Files

The following table describes the example client program files and lists the commands and arguments to run each example.

Table 7–1 Client Example Files

Example File Name 

Description 

Command to Run 

CreateNameSpace

Connects to the CIM Object Manager as the specified user and creates a namespace on the specified host. You must type the root user name and password. 

java CreateNameSpace host parentNS childNS username password

DeleteNameSpace

Deletes the specified namespace on the specified host. You must type the root user name and password. 

java DeleteNameSpace host parentNS childNS username password

ClientEnum

Enumerates classes and instances in the specified class in the default namespace root\cimv2 on the specified host.

java ClientEnum host className

CreateLog

Creates a log record on the specified host. You must type the root user name and password. 

java CreateLog host username password

ReadLog

Reads a log record on the specified host. You must type the root user name and password. 

java ReadLog host username password

DeleteClass

Deletes the specified class in the default namespace root\cimv2 on the specified host. You must type the root user name and password.

java DeleteClass host className username password

DeleteInstances

Deletes instances of the specified class in the default namespace root\cimv2 on the specified host. You must type the root user name and password.

java DeleteInstances host className username password

CreateQualifierType

Creates the specified qualifier type in the specified namespace on the specified host. You must type the root user name and password. 

java CreateQualifierType host parentNS username password qualifierTypeName

SystemInfo

Displays Solaris processor and system information for the specified host in a window. 

java SystemInfo host

Running the Client Examples

To run a client example program, type the command:


% java program_name

Most of the example programs take required arguments that have default values. For example, the CreateNameSpace example program takes five arguments:

Use the following syntax to specify default values for command line arguments.

Argument 

Default Value 

Syntax 

Host name

local host

.

Parent namespace

root\cimv2

" "

Child namespace

Null

" "

User name

GUEST

" "

Password

GUEST

" "

The following example runs the CreateNameSpace example, which connects to the default root\cimv2 namespace on the local host as user root with password secret.


% java CreateNameSpace . "" root secret

Using the Provider Examples

The example provider is a Java program that returns system properties and prints the string, “Hello World.” The provider calls native C methods to execute the code and return the values to the provider.

Provider Example Files

The following table describes the files that make up the example Provider program.

Table 7–2 Provider Example Files

File 

Purpose 

NativeProvider

Top level provider program that fulfills requests from the CIM Object Manager and routes them to the Native_Example provider. The NativeProvider program implements the instanceProvider and methodProvider APIs, and declares methods that enumerate instances and get an instance of the Native_Example class. It also declares a method that invokes a method to print the string "Hello World."

Native_Example.mof

Creates a class that registers the NativeProvider provider with the CIM Object Manager. The Native_Example.mof file identifies NativeProvider as the provider to service requests for dynamic data in the Native_Example class. This MOF file also declares the properties and methods to be implemented by the NativeProvider.

Native_Example.java

The NativeProvider program calls this provider to implement methods that enumerate instances and get an instance of the Native_Example class. The Native_Example provider uses the APIs to enumerate objects and create instances of objects. The Native_Example class declares native methods, which call C functions in the native.c file to get system-specific values, such as host name, serial number, release, machine, architecture, and manufacturer.

native.c

C program that implements calls from the Native_Example Java provider in native C code.

Native_Example.h

Machine-generated header file for Native_Example class. Defines the correspondence between the Java native method names and the native C functions that execute those methods.

libnative.so

Binary native C code compiled from the native.c file.

Writing a Native Provider

For detailed information on writing and integrating Java programs with native methods, visit the Java Web page at http://www.javasoft.com/docs/books/tutorial/native1.1/TOC.html.

Setting Up the Provider Example

The example provider program, NativeProvider, enumerates instances and gets properties for instances of the Native_Example class. You can use the CIM WorkShop to view this class and its instances.

How to Set Up the Provider Example
  1. Specify the location of shared library files in one of the following ways:

    • Set the LD_LIBRARY_PATH environment variable to the location of the shared library files. For example, using the C shell:


      Note –

      If you set the LD_LIBRARY_PATH environment variable in a shell, you must stop and restart the CIM Object Manager in the same shell for this new value to be recognized.



      % setenv LD_LIBRARY_PATH /wbem/provider/
      

      For example, using the Bourne shell:


      % LD_LIBRARY_PATH = /wbem/provider/
      

    • Copy the shared library files to the directory specified by the LD_LIBRARY_PATH environment variable. Installation sets this environment variable to /usr/sadm/lib/wbem. For example:


    % cp libnative.so /usr/sadm/lib/wbem 
    % cp native.c /usr/sadm/lib/wbem 
    % cp Native_Example.h /usr/sadm/lib/wbem
    

  2. Move the provider class files to /usr/sadm/lib/wbem.

    Move the provider class files to the same path as the package in which they are defined. For example, if the provider is packaged as com.sun.providers.myprovider.*, move the provider class files to /usr/sadm/lib/wbem/com/sun/wbem/myprovider/.class.

  3. Set the Solaris Provider CLASSPATH variable to the directory that contains the provider class files as described in To Set the Provider CLASSPATH.

  4. Stop the CIM Object Manager by typing the following command:


    # /etc/init.d/init.wbem -stop
    

  5. Restart the CIM Object Manager by typing the following command:


    # /etc/init.d/init.wbem -start
    

  6. Compile the Native_Example.mof file. For example:


    % mofcomp Native_Example.mof
    

    Compiling this MOF file loads the Native_Example class in the CIM Object Manager and identifies NativeProvider as its provider.

  7. Run CIM WorkShop and view the Native_Example class. For example:


    % /usr/sadm/bin/cimworkshop &
    

  8. In the Toolbar, click the Find Class icon.

  9. In the Input dialog box, type Native_Example and click OK.