Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
Chapter 3, Writing a Client Program Opening and Closing a Client Connection Opening a Client Connection   Previous   Contents   Next 
   
 

Closing a Client Connection

Use the close method of the CIMClient class to close a client connection and free the server resources used by the session.


Example 3-4 Closing a Client Connection

This example closes a client connection. The instance variable cc represents the client connection.

...
cc.close();
...

Performing Basic Client Operations

This section describes how to use the the javax.wbem.client APIs to request operations and perform common programming tasks.

Creating an Instance

Use the newInstance method to create an instance of an existing class. If the existing class has a key property, the application must set the key property to a unique value. As an option, an instance can define additional qualifiers that are not defined for the class. These qualifiers can be defined for the instance or for a particular property of the instance and do not need to appear in the class declaration.

Applications can use the getQualifiers method to get the set of qualifiers defined for a class.


Example 3-5 Creating an Instance

This example uses the newInstance method to create a Java class representing a CIM instance, for example, a Solaris package, from the Solaris_Package class.

...
{ 
/*Connect to the CIM Object Manager in the root\cimv2 
namespace on the local host. Specify the username and password of an 
account that has write permission to the objects in the 
root\cimv2 namespace. */
 
    UserPrincipal up = new UserPrincipal("root");
    PasswordCredential pc = new PasswordCredential("root_password"); 
    /* Connect to the namespace as root with the root password. */
 
    CIMClient cc = new CIMClient(cns, up, pc);
...
 
// Get the Solaris_Package class
cimclass = cc.getClass(new CIMObjectPath("Solaris_Package"),  
                                          true, true, true, null);
 
/* Create a new instance of the Solaris_Package 
 class populated with the default values for properties. If the provider
 for the class does not specify default values, the values of the 
 properties will be null and must be explicitly set. */
 
 CIMInstance ci = cc.createInstance (new CIMObjectPath("Solaris_Package"), 
 ci);
}
... 


Deleting an Instance

Use the deleteInstance method to delete an instance.


Example 3-6 Deleting Instances

The following example connects the client application to the CIM Object Manager, uses CIMObjectPath to construct an object containing the CIM object path of the object to be deleted, enumerateInstance to get the instance and all instances of its subclasses, and deleteInstance to delete each instance.

 
import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.CIMClient;
import javax.wbem.client.PasswordCredential;
import javax.wbem.client.UserPrincipal;

/** 
 * Returns all instances of the specified class.
 * This example takes five arguments: hostname (args[0]), username
 * (args[1]), password (args[2]) namespace (args[3] and classname (args[4]) 
 * It will delete all instances of the specified classname.  The specified 
 * username must have write permissions to the specified namespace  
 */
public class DeleteInstances {
    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        // if not five arguments, show usage and exit
        if (args.length != 5) {
           System.out.println("Usage: DeleteInstances host username " +
                              "password namespace classname ");
           System.exit(1);
           }
       try {
          // args[0] contains the hostname and args[3] contains the 
          // namespace.  We create a CIMNameSpace (cns) pointing to 
          // the specified namespace on the specified host
          CIMNameSpace cns = new CIMNameSpace(args[0], args[3]);

         // args[1] and args[2] contain the username and password.
         // We create a UserPrincipal (up) using the username and
         // a PasswordCredential using the password.
         UserPrincipal up = new UserPrincipal(args[1]);
         PasswordCredential pc = new PasswordCredential(args[2]);

        // Connect to the CIM Object Manager and pass it the
        // CIMNameSpace, UserPrincipal and PasswordCredential objects
        // we created. 
        cc = new CIMClient(cns, up, pc);

        // Get the class name (args[4]) and create a CIMObjectPath
        CIMObjectPath cop = new CIMObjectPath(args[4]);

        // Get an enumeration of all the instance object paths of the
        // class and all subclasses of the class.  An instance object 
        // path is a reference used by the CIM object manager to 
        // locate the instance
        Enumeration e = cc.enumerateInstanceNames(cop);


        // Iterate through the instance object paths in the enumeration.
        // Construct an object to store the object path of each 
        // enumerated instance, print the instance and then delete it 
        while (e.hasMoreElements()) {
           CIMObjectPath  op = (CIMObjectPath)e.nextElement();
           System.out.println(op);
           cc.deleteInstance(op);
        } // end while 
  } catch (Exception e) {
       // is we have an exception, catch it and print it out.
       System.out.println("Exception: "+e);
  } // end catch

 // close session.
 if (cc != null) {
     cc.close();
       }
    }
}


 
 
 
  Previous   Contents   Next