Java WebServices API getNetworkAdapter

Discussions related to using VirtualBox on Windows hosts.
Post Reply
Regression
Posts: 9
Joined: 27. Jun 2012, 16:58

Java WebServices API getNetworkAdapter

Post by Regression »

Hi

I'm using the Java webservices api to build out a set of commands for Java Virtualbox. One of the things that I am stuck on is disabling a Network Adapter on a particular Guest VM. Basically virtually unplugging the network cable.
When I used VboxManage.exe, I could call the command VBoxManage.exe controlvm sGuestVMName setlinkstate[adapter ID] off

Now that I'm using the Java WebServices API this type of command is not so easy to define... At least for me...
Here's my code...

Code: Select all


public void setLinkState(VirtualBoxManager oJavaVirtualBoxMgr, IVirtualBox oVirtualBoxInstance, String sVMName,String linkState, int iAdapterID){
        ISession oSession = null;
        IConsole oConsole = null;
        IMachine oMachine = null;
        INetworkAdapter oNetwork = null;

        
        try{
        	oMachine = oVirtualBoxInstance.findMachine(sVMName);
        	oSession = oJavaVirtualBoxMgr.getSessionObject();
        	oMachine.lockMachine(oSession,  LockType.Shared);
           	oNetwork = oMachine.getNetworkAdapter();[b] <---- Here is where I'm stuck... Trying to define the iAdapterID which is an int to the long that is required for Slot???? Not sure[/b]
        	if (linkState.equals("On")){
        		oNetwork.setCableConnected(true);[b] <---- Also I'm not sure if this works the way I hope it does.. Meaning will it work like "setlinkstate" in VboxManage.exe[/b]
        	
        	}else{
        		oNetwork.setCableConnected(false);
        	}
        	oSession.unlockMachine();
        }catch (VBoxException e)
        {
            e.printStackTrace();
        }
    }
I've scoured the web and I can't find any easy examples of how to do this...

Any insight on this would help me immensely..

Thanks

Bill
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Java WebServices API getNetworkAdapter

Post by noteirak »

Code: Select all

oNetwork = oMachine.getNetworkAdapter();[b] <---- Here is where I'm stuck... Trying to define the iAdapterID which is an int to the long that is required for Slot???? Not sure
This is a general Java API inconsistency error, that I have reported on bug tracker as ticket #11316 - it is a general inconsistency for the generated glue java code.

Code: Select all

if (linkState.equals("On")){
              oNetwork.setCableConnected(true);[b] <---- Also I'm not sure if this works the way I hope it does.. Meaning will it work like "setlinkstate" in VboxManage.exe
use .getCableConnected() to get the link status. Link state is only a vision of Vboxmanage to modify runtime settings (instead of modifyvm that needs powered off VM), but is not a method of the API.
.setCableConnected() does the same as setlinkstate, yes
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Regression
Posts: 9
Joined: 27. Jun 2012, 16:58

Re: Java WebServices API getNetworkAdapter

Post by Regression »

Thank you for the reply.
When I execute the code now it throws an exception (see below) on setCableConnected(false)
I've tried different iAdapterID's values of 0 and 1 since this might be 0 based. The VM only has 1 adapter for the test.
I've tried this code with the VM shut down and running but get the same exception...

What does that exception mean?

How might I resolve it?

Thanks

Bill

Code: Select all


When I call this > setLinkState(oJavaVirtualBoxMgr, oVirtualBoxInstance, "TESTENV10-HOST","Off", 0)

public void setLinkState(VirtualBoxManager oJavaVirtualBoxMgr, IVirtualBox oVirtualBoxInstance, String sVMName,String linkState, int iAdapterID){
        ISession oSession = null;
        IConsole oConsole = null;
        IMachine oMachine = null;
        INetworkAdapter oNetwork = null;
        NetworkAdapterType oSlot = null;
        
        try{
        	oMachine = oVirtualBoxInstance.findMachine(sVMName);
        	oSession = oJavaVirtualBoxMgr.getSessionObject();
        	oMachine.lockMachine(oSession,  LockType.Shared);
        	oNetwork = oMachine.getNetworkAdapter(Long.valueOf(iAdapterID)); <---- Convert the int to a long
        	oSlot = oNetwork.getAdapterType(); <---------- This basically does nothing. I just wanted to see what info was in there
        	if (linkState.equals("On")){
        		if (oNetwork.getCableConnected() == false){ 
        			oNetwork.setCableConnected(true);
        		}
          	}else{
        		if (oNetwork.getCableConnected() == true){ <----- This returns true which calls the next line of code.
        			oNetwork.setCableConnected(false); <----- This is where I get the exception
        		}
        	}
        	oSession.unlockMachine();
        }catch (VBoxException e)
        {
            e.printStackTrace();
        }
    }

The exception states:

org.virtualbox_4_2.VBoxException: VirtualBox error: The machine is not mutable (state is Running) (0x80BB0002)
at org.virtualbox_4_2.INetworkAdapter.setCableConnected(INetworkAdapter.java:407)
at utils.javaVirtualBox.JavaVirtualboxServices.setLinkState(JavaVirtualboxServices.java:108)
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Java WebServices API getNetworkAdapter

Post by noteirak »

Code: Select all

oMachine = oVirtualBoxInstance.findMachine(sVMName);
oSession = oJavaVirtualBoxMgr.getSessionObject();
oMachine.lockMachine(oSession,  LockType.Shared);
oNetwork = oMachine.getNetworkAdapter(Long.valueOf(iAdapterID));
Your mistake is that you are still using the non-mutable object, even tho you lock and get a session.

This is the correct code :

Code: Select all

oMachine = oVirtualBoxInstance.findMachine(sVMName);
oSession = oJavaVirtualBoxMgr.getSessionObject();
oMachine.lockMachine(oSession,  LockType.Shared);
IMachine oMachineMutable = oSession.getMachine();
oNetwork = oMachineMutable.getNetworkAdapter(Long.valueOf(iAdapterID));
More info in section 3.2 of the SKDRef.pdf file

And don't forget to release the remote on the mutable object as well.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Regression
Posts: 9
Joined: 27. Jun 2012, 16:58

Re: Java WebServices API getNetworkAdapter

Post by Regression »

Let me give that a try!

Thanks

Bill
Post Reply