IBM Tivoli Composite Application Manager for Application Diagnostics, Version 7.1.0.1

J2SE JMX plug-in sample - J2EE Agent

package com.testware.standalone.jmx;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; 
import javax.management.AttributeNotFoundException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL; 
import com.ibm.tivoli.itcam.j2se.jmxe.JMXEnginePlugin;

public class CustomJMXEngine implements JMXEnginePlugin {
  private String hostip = "";
  private int port = 0;
  private String username = "";
  private String password = "";
  MBeanServerConnection mbsc = null;
  /**
   * @param prop All variables about jmx will be set into this properties. 
     Such as host, port, username and password
   */
  public void initialize(Properties prop) throws Exception {
    this.hostip = prop.getProperty(HOST,"127.0.0.1");
    String port_s = prop.getProperty(PORT);
    try {
      this.port = Integer.parseInt(port_s);
    } catch (NumberFormatException e) {
      this.port = 0;
    }
    this.username = prop.getProperty(USERNAME);
    this.password = prop.getProperty(PASSWORD);
    
    if(mbsc == null)
    {
      MBeanUtils.getInstance().createMBeanServer();
      mbsc = this.getMBeanServerConnection();
    }
  }
  private MBeanServerConnection getMBeanServerConnection() throws Exception {
    // Get MBeanServerConnection
    MBeanServerConnection connection;
    try {
      // The address of the connector server       JMXServiceURL url = new JMXServiceURL("rmi", this.hostip, this.port, 
"/jndi/jmx");

      // The credentials are passed via the environment Map
      Map environment = new HashMap();
      String[] credentials = new String[]{this.username, this.password};
      environment.put(JMXConnector.CREDENTIALS, credentials);

      // Connect to the server       JMXConnector cntor = JMXConnectorFactory.connect(url, environment);

      connection = cntor.getMBeanServerConnection();
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
    return connection;
  }
  /**
   * Framwork use this method to get customer's mbean server and register 
some mbeans into it    */
  public MBeanServer getRegistrationMBeanServer() {
    MBeanServer server = MBeanUtils.getInstance().getMBeanServer();
    return server;
  }
  /**
   * Proxy function to invoke method of MBean Object.
   */
  public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
    Object returnValue = null;

    try {
      if (method.getName().equals("getDefaultDomain")) {
        returnValue = mbsc.getDefaultDomain();
      } else if (method.getName().equals("queryNames")) {
        returnValue = mbsc.queryNames((ObjectName) args[0],(QueryExp) args[1]);
      } else if (method.getName().equals("getAttribute")) {
        returnValue = mbsc.getAttribute((ObjectName) args[0],(String) args[1]);
      } else if (method.getName().equals("invoke")) {
        returnValue = mbsc.invoke((ObjectName) args[0],(String) args[1], 
(Object[]) args[2],(String[]) args[3]);
      } else if (method.getName().equals("getMBeanInfo")) {
        returnValue = mbsc.getMBeanInfo((ObjectName) args[0]);
      } else if (method.getName().equals("getAttributes")) {
        returnValue = mbsc.getAttributes((ObjectName) args[0],(String[]) args[1]);
      } else {
         throw new Exception(method.getName()
         + " IS NOT IMPLEMENTED OR IS UNKNOWN");
      }
    }catch (AttributeNotFoundException e) { //ignore all attribute not found 
excpetion.
    }catch (Exception re) {
      re.printStackTrace();
    }
    return returnValue;
  }
  public String buildObjectNameString(String domainName, String type, String name, 
Properties extraProperties) {
    return null;
  }
}


Parent topic:

Agent for J2EE data collector Installation Guide

+

Search Tips   |   Advanced Search