Wednesday Jan 28, 2009

xVM public API

There are two ways of accessing the public API, the simplest is by writing a Java client using JMX, or alternatively for non-Java client programs using Web Services for Management (WS-MAN, JSR-262).

 



This example demonstrates the usage of the direct access to the read-only copy of the domain model. This example can be run against both Sun xVM Server or Sun xVM Ops Center.


This example performs the following functions:



  • Configures the connection

  • Performs security settings

  • Opens the connection (locally or remotely)

  • Queries the domain model for all the OperatingSystems objects and displays the value of their Hosts name

  • Closes the connection


 



ServerClient.java

/\*\*
\* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
\* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
\*/
import com.sun.hss.type.os.OperatingSystem;
import com.sun.hss.type.server.Server;
import com.sun.hss.type.virtserver.VirtServerContainer;
import com.sun.hss.type.virtserver.VirtServerOperatingSystem;
import com.sun.hss.type.xvmserver.XVMApplianceDetails;
import com.sun.hss.type.xvmserver.XVMServer;
import java.util.HashMap;
import java.util.Map;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;
import javax.net.ssl.TrustManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.ObjectName;
import com.sun.xvm.services.guest.GuestServiceMXBean;
import com.sun.xvm.services.xvmserver.XVMServerServiceMXBean;
import com.sun.xvm.services.guest.GuestDetails;

public class ServerClient {

static private JMXConnector connector = null;
static private MBeanServerConnection mbsc = null;
static private String hostname = null;

/\*\*
\* Simple SocketFactory that uses a trust manager that ALWAYS
\* accept the certificate provided by the server we try to connect to.
\*
\* This is unsafe and should NOT be used for production code.
\*/
private static SSLSocketFactory getSocketFactory() {
X509TrustManager tm = new AnyServerX509TrustManager();
TrustManager[] tms = {tm};

try {
SSLContext sslc = SSLContext.getInstance("TLSv1");
sslc.init(null, tms, null);

SSLSocketFactory factory = sslc.getSocketFactory();

return factory;
} catch (Exception ex) {

return null;
}
}

/\*\*
\* Small trustmanager that ALWAYS accepts the certificate provided
\* by the server we try to connect to.
\*
\* This is unsafe and should NOT be used for production code.
\*/
public static class AnyServerX509TrustManager implements X509TrustManager {
// Documented in X509TrustManager

public X509Certificate[] getAcceptedIssuers() {
// since client authentication is not supported by this
// trust manager, there's no certicate authority trusted
// for authenticating peers
return new X509Certificate[0];
}

// Documented in X509TrustManager
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
// this trust manager is dedicated to server authentication
throw new CertificateException("not supported");
}

// Documented in X509TrustManager
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
// any certificate sent by the server is automatically accepted
return;
}
}

/\*\*
\* Create a WSMAN connection using the given credentials
\*
\* @param host
\* @param userName
\* @param userPass
\* @return MBeanServerConnection
\*/
private static void setupConnection(String host, String user, String pass)
throws Exception {
try {
int port = 443;

String urlPath = "/wsman/ea/jmxws";

Map env = new HashMap();

// credentials for basic authentication with the server
String[] creds = new String[2];
creds[0] = user;
creds[1] = pass;

env.put(JMXConnector.CREDENTIALS, creds);

// provide a specific socket factory to avoid the need to setup
// a truststore
env.put("com.sun.xml.ws.transport.https.client.SSLSocketFactory",
getSocketFactory());

// Create JMX Agent URL over https
JMXServiceURL url = new JMXServiceURL("ws-secure", host, port, urlPath);

// System.out.println("WSMAN client opening a connection with url " + url.toString());

// Connect the JMXConnector
connector = JMXConnectorFactory.connect(url, env);

// Get the MBeanServerConnection
mbsc = connector.getMBeanServerConnection();

} catch (Exception ex) {
System.out.println("Got an exception while trying to open a WSMAN connection : " + ex.toString());
throw ex;
}
}

public static void main(String[] args) {
if ((args.length == 0) || (args.length > 3)) {
System.err.println("Usage: user password [target]");
System.exit(1);
}

String userName = args[0];
String userPass = args[1];

hostname = "localhost";

if (args.length == 3) {
hostname = args[2];
}

try {
// Open the WSMAN connection and get the MBeanServerConnection
setupConnection(hostname, userName, userPass);

// get details on these xVM servers
serverService();

} catch (Exception ex) {

System.out.println("WSMAN client error : " + ex.toString());
ex.printStackTrace();
System.exit(1);

} finally {
// close connection if necessary
if (connector != null) {
try {
connector.close();
} catch (Exception dc) {
}

}
}
}

private static void serverService() throws Exception {

try {
XVMServerServiceMXBean xssmxb = ServerClientServices.getXVMServerService(mbsc, false);

// get the list of xVM servers
ObjectName[] servers = xssmxb.getXVMApplianceDetailsObjectNames(null, null);

if ((servers == null) || (servers.length == 0)) {
System.out.println("No xVM server detected on " + hostname);
return;
}

GuestServiceMXBean guestmxb = ServerClientServices.getGuestService(mbsc, false);

// get details on these xVM servers
for (int i = 0; i < servers.length; i++) {

XVMApplianceDetails details = xssmxb.getXVMApplianceDetails(servers[i]);

if (details != null) {
OperatingSystem os = details.getOperatingsystem();

Server svr = details.getServer();

VirtServerContainer vsc = details.getVirtservercontainer();

XVMServer xsvr = details.getXvmserver();

if (xsvr != null) {
System.out.println("xVM Server name = " + xsvr.getApplianceName());
}

}

// get guests on this xVM server
ObjectName[] guests = guestmxb.getGuestObjectNames(servers[i], null, null);

Java.util.Map<java.lang.String, Java.util.Set<java.lang.string>> map = null;
GuestDetails guestDetails[] = guestmxb.getGuestDetails(guests, map);

String s = guests[0].getCanonicalName();

if ((guests == null) || (guests.length == 0)) {
System.out.println("No guest on this xVM server");
}
if (guestDetails != null) {
for (int k = 0; k < guestDetails.length; k++) {
VirtServerOperatingSystem virt = guestDetails[k].getVirtServerOperatingSystem();
System.out.println("guest hostname is " + k + ": " + virt.getHostname());

}
}
}
} catch (Exception ex) {
System.err.println("Got Exception while testing the xVM server service : " + ex.toString());
throw ex;
}

}
}


ServerClientServices.java

public class ServerClientServices
{
   /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
    \* getCacheManagerService
    \*
    \* @param mbsc
    \* @return CacheManagerMXBean
    \* @throws java.lang.Exception
    \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
   public static CacheManagerMXBean getCacheManagerService(MBeanServerConnection mbsc, boolean verbose)
          throws Exception
   {
       CacheManagerMXBean cmmxb = null;

       try {
           // build the objectname to access the service
           ObjectName cmson = new ObjectName(CacheManagerMXBean.DOMAIN +
                                             ":type=" + CacheManagerMXBean.TYPE);

           // verify that the service is currently deployed
           if (!mbsc.isRegistered(cmson)) {
               System.out.println("Cache Manager service is not registered : aborting");
               throw new Exception("MXBean for Cache Manager service not registered");
           }

           // create a proxy to access the service
           cmmxb = JMX.newMXBeanProxy(mbsc,
                                      cmson,
                                      CacheManagerMXBean.class,
                                      false);

           if (verbose) {
               System.out.println("Proxy for Cache Manager service : OK");
           }
           return cmmxb;

       } catch (Exception ex) {
           System.err.println("Got Exception while creating the Cache Manager service proxy : " + ex.toString());
           ex.printStackTrace();
           throw ex;
       }
   }

About

This blog covers cloud computing, big data and virtualization technologies

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today