Demonstrates how to use CreateCollectorForEvents and EventFilterSpec in the API to get filtered events

QueryEvent.java

Copyright © 2008 VMware, Inc. All rights reserved.


// Copyright 2008 VMware, Inc.  All rights reserved.

//#######################################################################################
// DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS 
// OF ANY KIND, WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY 
// DISCLAIMS ANY IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY 
// QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. 
//#######################################################################################


//#######################################################################################
//
// QueryEvent.java
//	Example to retrieve the information for all the events
//	Parameters:
//		   <url> 					url of ESX/VC
//		   <username> 				user name
//		   <password> 				password

// This script works with VMware VirtualCenter 2.0 or later.

// This script works with VMware ESX Server 3.0 or later.
//#######################################################################################

package com.vmware.samples.general;

import com.vmware.vim.*;
import java.net.URL;
import javax.xml.rpc.Stub;
import java.lang.reflect.Method;
import java.util.*;
import java.lang.reflect.*;

public class QueryEvent {	
   private ManagedObjectReference _svcRef;
   private VimServiceLocator _locator;
   private VimPortType _service; 
   private ServiceContent _sic;              
   //Method to connect to create ServiceInstance MOR
   private void createServiceRef() throws Exception {
      _svcRef = new ManagedObjectReference();
      _svcRef.setType("ServiceInstance");
      _svcRef.set_value("ServiceInstance");
    
   }     
   //Method to connect to VC/ESX
   private void connectAndLogin(String[] args) throws Exception {
      System.setProperty("org.apache.axis.components.net.SecureSocketFactory",
                            "org.apache.axis.components.net.SunFakeTrustSocketFactory");
      String url = args[0];
      String username = args[1];
      String password = args[2];      
      createServiceRef();
      _locator = new VimServiceLocator();
      _locator.setMaintainSession(true);
      _service = _locator.getVimPort(new URL(url));
      _sic = _service.retrieveServiceContent(_svcRef);
      if (_sic.getSessionManager() != null) {
         _service.login(_sic.getSessionManager(), username, password, null);
      }
      System.out.println("info---"+_sic.getAbout().getFullName());
   }
   //Logout   
   private void disconnect() throws Exception {
      if (_service != null) {
         _service.logout(_sic.getSessionManager());
         _service = null;
         _sic = null;
      }
   }
   // Main Method
   public static void main(String[] args) throws Exception {       
      System.setProperty("axis.socketSecureFactory", 
                         "org.apache.axis.components.net.SunFakeTrustSocketFactory");
      QueryEvent obj = new QueryEvent();
      obj.connectAndLogin(args);            
      obj.initialize();  
      System.out.println("Disconnecting");    
      obj.disconnect();
   }     
   // Displays all the Events with Full Formatted message
   private void initialize() {      
      try {
         ManagedObjectReference _propCol = _sic.getPropertyCollector();
         ManagedObjectReference _eventManager = _sic.getEventManager();
         EventFilterSpec eventFilter = new EventFilterSpec();

         Event[] events = _service.queryEvents(_eventManager, eventFilter);
         System.out.println("Events In the latestPage are : ");
         for (int i = 0; i < events.length; i++) {
           Event anEvent = events[i];
           System.out.println("Event: " + anEvent.getClass().getName() + "  FullFormattedMessage: " + anEvent.getFullFormattedMessage());
         }
      } 
      catch (Exception e) {
         System.out.println("Caught Exception : " +
                            " Name : " + e.getClass().getName() +
                            " Message : " + e.getMessage() +
                            " Trace : ");
         e.printStackTrace();
      }
   }
}


The sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.

You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.

VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.