Really Easy Way to Clone a Session: Yet Another Contribution from NetApp

Recently there were questions in the vSphere Java API forum on how to clone a new session from an existing one. Although vSphere Java API wraps around the basic Web Services API cloneSession() method, simply calling the method doesn’t get what you want.

Why? The signature of cloneSession() is as follows. It returns UserSession, which is embedded inside ServiceInstance managed object in vSphere Java API. While using the API, you always starts from the ServiceInstance.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

public UserSession cloneSession(String cloneTicket) throws InvalidLogin, RuntimeFault, RemoteException;

In vSphere Java API, the ServiceInstance and UserSession have one to one mapping. Without a new ServiceInstance around the new UserSession, the UserSession is not much helpful.

Luckily, we got yet another contribution from Eric Forgette who works at NetApp. The contributed code includes a new overloaded method cloneSession() method to return a ServiceInstance object.

As I described before, we’ve been trying very hard to keep the vSphere Java API as close to the basic Web Services as possible. In this case, we decided to break the rule a little bit for good reasons, mainly for usability.

   * Copyright 2009 NetApp, contribution by Eric Forgette
   * Modified by Steve Jin (sjin at
   * This constructor builds a new ServiceInstance based on a ServiceInstance.
   * The new ServiceInstance is effectively a clone of the first.  This clone will
   * NOT become invalid when the first is logged out.
   * @author Eric Forgette (forgette at
   * @throws RemoteException
   * @throws RuntimeFault
   * @throws InvalidLogin
   * @throws MalformedURLException
      public ServiceInstance cloneSession(boolean ignoreCert) throws InvalidLogin, RuntimeFault, RemoteException, MalformedURLException
        ServiceInstance oldsi = getServerConnection().getServiceInstance();
        ServerConnection oldsc = oldsi.getServerConnection();
        String ticket = oldsi.getSessionManager().acquireCloneTicket();
        VimPortType vimService = new VimPortType(oldsc.getUrl().toString(), ignoreCert);
        ServerConnection newsc = new ServerConnection(oldsc.getUrl(), vimService, null);
        ServiceInstance newsi = new ServiceInstance(newsc);
       UserSession userSession = newsi.getSessionManager().cloneSession(ticket); 
       return newsi;

 When you use the new API, you can just call the following to have your sessions cloned:

ServiceInstance newInstance = Si.getSessionManager().cloneSession(true);

The fix is only available in the code repository. You can download the following two files (, until the next release.

Thanks Eric and NetApp for the contributions! For the VMware community, please don’t hesitate to contact me (sjin at if you want to contribute any code that are useful to others in the community.

This entry was posted in vSphere API and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Eric Forgette
    Posted April 30, 2010 at 10:45 am | Permalink

    Hi Steve, I’m happy to help.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.