Virtualization Pro

Jan 10 2008   7:17PM GMT

VI SDK examples, logging out in style with Java



Posted by: Akutz
Tags:
Andrew Kutz
VI3
Virtualization

Over the past few weeks many users have begun to e-mail me asking for not just help with the VI SDK, but code examples as well. I am more than happy to help, but I want to be able to help as many people as I can. To that end I have decided to post all of my code examples online for everyone’s benefit. You can access these examples at http://www.lostcreations.com/code/wiki/vmware. Examples will generally be available in C#, Java, and VIPerl. If you have an idea for an example, or a language you would like to see it in, please let me know!

The first example that I have posted is how to log into and out of the VI SDK Web service. There are examples in C#, VI Perl, and Java. With Java though there is a catch. See, you cannot actually successfully logout of the VI Web service with Java — it will throw an exception. This is a known bug according to some of VMware’s own code examples — they say that the logout function is not yet implemented.

However, this is not acceptable for anyone writing, oh, I don’t know, an OS-agnostic version of the VirtualInfrastructure client in Java. There has to be a way to force the session to log off. Well there is, but before I tell you what it is I will tell you the methods I first attempted at that failed:

Method 1

The first thing that I tried was to use the TerminateSession method and terminate my own session. Unfortunately terminating your own session is prohibited (I *could* on the other hand terminate someone else’s).

Method 2

The second hair-brained scheme I came up with was using the CreateScheduledTask method to invoke a script on the remote server that would terminate my session given my session ID. Unfortunately I was simply not able to get this to work. I tried it in C#, Java, and VI Perl, but I could not get a task created with action type RunScriptAction. Please let me know if you can get this to work.

Method 3

The method that finally worked involves TerminateSession in VI Perl. Using a VI Perl script I wrote called killsession.pl I terminate my own session by invoking said Perl script from within the Java example. It isn’t pretty, but it works. And until VMware fixes their stuff, it will have to do.

Hope this helps!

12  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Kolcun
    I had read a post on the vmware community forums where you mention that the logout issue exists with the v2.0 SDK. Have you found a similar issue with the v2.5 SDK? thanks!
    0 pointsBadges:
    report
  • Akutz
    I am not aware if this issue exists or not. I will let you know when I find out!
    270 pointsBadges:
    report
  • Kolcun
    After playing around with your example code, it seems that logout no longer throws an exception. I just disabled the perl call, and put the o_vim_svc.logout() call back in. No exception! Watching the hostd.log file on the esx server confirms that the user is being logged out.
    0 pointsBadges:
    report
  • Akutz
    Against 2.5? Great! This is one more reason to upgrade to 2.5.
    270 pointsBadges:
    report
  • Kolcun
    Interesting - I just ran my modified class on the older version of the SDK, and it seems to work in that case too. No exception is thrown. I find this odd, because I am experiencing hung connections, but can't exactly figure out why. So far it seems that if I have a bunch of machines running code that connects to one esx machine, it seems to get the "Disconnected" message in the VI3 client, requiring a restart of the mgmt-vmware service on the esx box. Your post had brought me some hope! I had hoped that I was experiencing this logout issue, but now it seems that this isn't the case. Which specific versions of axis/vi SDK/java did you have logout problems with?
    0 pointsBadges:
    report
  • Akutz
    VMware themselves acknowledge that the Java logout function is broken in 2.0. It is in their own examples. I am running the version of Axis that comes with the VI SDK 2.0 and am using a 1.5 JRE.
    270 pointsBadges:
    report
  • Kolcun
    SDK version 2.0.0 or 2.0.1? when I look at VimBindingStub.java in the \vi-sdk-2.0.1-32042\SDK\samples_2_0\Axis\java\com\vmware\vim directory, it seems that the logout function in implemented.
    0 pointsBadges:
    report
  • Akutz
    I believe 2.0.1. I know it is defined in the java file, but check out page 192 of the Programming Guide (PDF) public void disconnect()throws Exception { if (_service != null) { // does not work at this time //_service.logout(_svcRef); _service = null; _sic = null; }
    270 pointsBadges:
    report
  • Kolcun
    I've think i've got some insight into whats going on here. this post applies to both, the v2.5 and v2.0.1 SDK
    0 pointsBadges:
    report
  • Kolcun
    I've think i've got some insight into whats going on here. this post applies to both, the v2.5 and v2.0.1 SDK The programming guide, pg 192 shows log in as _locator = new VimServiceLocator(); _locator.setMaintainSession(true); _service = _locator.getVimPort(new URL(urlStr)); _sic = _service.retrieveServiceContent(_svcRef); _propCol = _sic.getPropertyCollector(); _rootFolder = _sic.getRootFolder(); if (_sic.getSessionManager() != null) { _service.login(_sic.getSessionManager(), username, password, null); } _sic.getSessionManager() is a ManagedObjectReference (MOR) we login using that. The guide shows the logout method as ... //does not work at this time //_service.logout(_svcRef); ... When uncommented, this does cause an exception. Why though are we passing the _scvRef MOR to logout? We logged in with the _sic.getSessionManager() MOR. I tried using _sic.getSessionManager() in place of _svcRef in the logout method, and it works fine. This seems to make sense to me, we login and logout with the same MOR. Any thoughts? thanks
    0 pointsBadges:
    report
  • Akutz
    Good catch! In fact I was using the session manager in my own example code: // Logout not implemented // // WARNING - The logout method throws a runtime exception with // Java and the VI SDK right now. VMware is aware of this // bug. I do not know if they are working on it. What follows // is an example of how to logout with a method that I created. // This method relies on the VI Perl Toolkit being installed // on your client. // // o_vim_svc.logout( o_vim_svc_content.getSessionManager() ); But then I guess I never actually ran it with the getSessionManager instead of the service reference and just assumed it would throw the same exception per VMware's documentation. I just tested this and it works fine. Great catch!!! FYI - The VMware documentation does say to use the session manager moref (http://pubs.vmware.com/vi301/sdk/ReferenceGuide/vim.SessionManager.html#logout) with logout. In this case, VMware's code example in their PDF was wrong (not the first time actually). Thanks again!
    270 pointsBadges:
    report
  • Kolcun
    see http://communities.vmware.com/thread/124254?tstart=0 on the subject too.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: