WBEMfor Solaris on Sun Developer's Guide

Writing Data to a Log File

Enabling an application to write data to a log file involves the following main tasks:

How to Create an Instance of Solaris_LogRecord to Write Data
  1. Import all necessary java.rmi classes.


    Example 13-1 Importing Classes

    import java.rmi.*; 
    import com.sun.wbem.client.CIMClient; 
    import com.sun.wbem.cim.CIMInstance; 
    import com.sun.wbem.cim.CIMValue; 
    import com.sun.wbem.cim.CIMProperty; 
    import com.sun.wbem.cim.CIMNameSpace; 
    import com.sun.wbem.cim.CIMObjectPath; 
    import com.sun.wbem.cim.CIMClass; 
    import com.sun.wbem.cim.CIMException; 
    import com.sun.wbem.solarisprovider.*; 
    import java.util.*; 
    import java.util.Enumeration; 

  2. Declare the public class CreateLog and the following values:

    • CIMClient value

    • CIMObjectPath value

    • CIMNameSpace value


    Example 13-2 Declaring the CreateLog Class and Values

    public class CreateLog {
        public static void main(String args[]) throws CIMException {
    	
    	if ( args.length != 3) {
    	    System.out.println("Usage: CreateLog host username password"); 
    	    System.exit(1);
    	}
    
    	CIMClient cc = null;
    	CIMObjectPath cop = null;
    	try {
    	    CIMNameSpace cns = new CIMNameSpace(args[0]);
    	    cc = new CIMClient(cns, args[1], args[2]);

  3. Specify the vector of properties to be returned. Set values for the properties of the qualifiers.


    Example 13-3 Specifying the Vector of Properties and their Values

    Vector keys = new Vector();  		
    CIMProperty logsvcKey = new CIMProperty("RecordID"); 		
    logsvcKey.setValue(new CIMValue(new Integer(0))); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("RecordHashCode"); 		
    logsvcKey.setValue(new CIMValue(new Integer(0))); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("Filename"); 		
    logsvcKey.setValue(new CIMValue("some_file")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("category"); 		
    logsvcKey.setValue(new CIMValue(new Integer(2))); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("severity"); 		
    logsvcKey.setValue(new CIMValue(new Integer(2))); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("AppName"); 		
    logsvcKey.setValue(new CIMValue("SomeApp")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("UserName"); 		
    logsvcKey.setValue(new CIMValue("molly")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("ClientMachineName"); 		
    logsvcKey.setValue(new CIMValue("dragonfly")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("ServerMachineName"); 		
    logsvcKey.setValue(new CIMValue("spider")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("SummaryMessage"); 		
    logsvcKey.setValue(new CIMValue("brief_description")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("DetailedMessage"); 		
    logsvcKey.setValue(new CIMValue("detailed_description")); 		
    keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("data"); 		
    logsvcKey.setValue(new CIMValue("0xfe 0x45 0xae 0xda")); 
    	keys.addElement(logsvcKey); 		
    logsvcKey = new CIMProperty("SyslogFlag"); 		
    logsvcKey.setValue(new CIMValue(new Boolean(true))); 		
    keys.addElement(logsvcKey); 

  4. Declare the new instance of the CIMObject Path for the log record.


    Example 13-4 Declaring the New Instance of CIMObjectPath

    CIMObjectPath logreccop = new CIMObjectPath("Solaris_LogRecord", keys);

  5. Declare the new instance of Solaris_LogRecord. Set vector of properties to write to a file.


    Example 13-5 Setting the Instance and Properties

    CIMInstance ci = new CIMInstance();
    		ci.setClassName("Solaris_LogRecord");
    		ci.setProperties(keys);
    		//System.out.println(ci.toString());
    		cc.setInstance(logreccop,ci);
    	}
    	catch (Exception e) {
    	    System.out.println("Exception: "+e);
    		e.printStackTrace();
    	}

  6. Close the session after data has been written to the log file.


    Example 13-6 Closing the Session

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