Saturday, January 31, 2015

How to Connect XenServer and Operating VM Using Java

At the time of final year project in MCA we have done a challenging project on citrix xenserver. Initially we did research and learn about virtualization, xenserver and xen java API concepts later we decided to do a web based project and named as xenserver management on cloud computing. We developed a web console to interact with xenserve and performing various operations on it. In our project we did different modules like establishing connection with  xenserver, getting the xenserver version, vm creation, VM destroy  and virtual machine life cycle. Life cycle is nothing but VM start, stop and reboot.

It is difficult to explain all modules so I will cover most useful functionality how to connect xenserver using java and VM life cycle using java. Don’t be confuse VM is called as virtual machine and it a guest operating system. In below video demo we can watch complete practical part like how to create web project, adding xen API jars in project, configuring apache tomcat server etc.

Screenshots

xenserver project connection page

vm life cycle image

Code to connect xenserver using java

import com.xensource.xenapi.Connection;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Faruk Shaik
*/
public class connection extends HttpServlet {
public static String IP;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String ip=request.getParameter("IP");
IP="http://"+ip;
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
Connection conn = new Connection(IP,uname,pass);
if(conn!=null)
{
HttpSession hs=request.getSession();
hs.setAttribute("ip",IP);
hs.setAttribute("uname",uname);
hs.setAttribute("pass",pass);
javax.swing.JOptionPane.showMessageDialog(null,"connection established");
response.sendRedirect("task.jsp");
}

}
catch(Exception e)
{
javax.swing.JOptionPane.showMessageDialog(null,"connection not established");
response.sendRedirect("connection.jsp");
}
finally {
out.close();
}

}
public String s()
{
return IP;
}
}

Code to Start, stop and reboot the VM using java

import com.xensource.xenapi.APIVersion;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.*;
import com.xensource.xenapi.VM;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author Faruk Shaik
*/
public class vmlife extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name1=null;
try {

HttpSession hs=request.getSession();
String ip=(String)hs.getAttribute("ip");
String uname=(String)hs.getAttribute("uname");
String pass=(String)hs.getAttribute("pass");
Connection conn = new Connection(ip,uname,pass);
String name=request.getParameter("count");
if(name.equalsIgnoreCase("start"))
{

String vmname=request.getParameter("vmname");
Map vms=VM.getAllRecords(conn);
for(VM.Record record: vms.values())
{
if(!record.isATemplate&&!record.isControlDomain )
{

if(vmname.equals(record.nameLabel.toString()))
{
VM.getByUuid(conn,record.uuid).start(conn, false, false);
name1="VM"+vmname+" started successfully";

}
}
}
}
else if(name.equalsIgnoreCase("shut"))
{
String vmname=request.getParameter("vmname");
Map vms=VM.getAllRecords(conn);
for(VM.Record record: vms.values())
{
if(!record.isATemplate&&!record.isControlDomain )
{

if(vmname.equals(record.nameLabel.toString()))
{
VM.getByUuid(conn,record.uuid).hardShutdown(conn);
name1="VM"+vmname+" shutdown successfully";
}
}
}

}
else if(name.equalsIgnoreCase("reboot"))
{
String vmname=request.getParameter("vmname");
Map vms=VM.getAllRecords(conn);
for(VM.Record record: vms.values())
{
if(!record.isATemplate&&!record.isControlDomain )
{

if(vmname.equals(record.nameLabel.toString()))
{
VM.getByUuid(conn,record.uuid).hardReboot(conn);
name1="VM" +vmname+"rebooted successfully";
}
}
}

}

}
catch(Exception e){
System.out.println(e);

javax.swing.JOptionPane.showMessageDialog(null,"xen excepton is"+e);
}finally {
out.close();
}
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.