Example usage for javax.management ObjectName toString

List of usage examples for javax.management ObjectName toString

Introduction

In this page you can find the example usage for javax.management ObjectName toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the object name.

Usage

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Create a new AjpConnector/*from ww  w  .ja va 2  s  .  com*/
 *
 * @param parent MBean Name of the associated parent component
 * @param address The IP address on which to bind
 * @param port TCP port number to listen on
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createAjpConnector(String parent, String address, int port) throws Exception {

    Object retobj = null;

    try {

        // Create a new CoyoteConnector instance for AJP
        // use reflection to avoid j-t-c compile-time circular dependencies
        Class cls = Class.forName("org.apache.coyote.tomcat4.CoyoteConnector");
        Constructor ct = cls.getConstructor(null);
        retobj = ct.newInstance(null);
        Class partypes1[] = new Class[1];
        // Set address
        String str = new String();
        partypes1[0] = str.getClass();
        Method meth1 = cls.getMethod("setAddress", partypes1);
        Object arglist1[] = new Object[1];
        arglist1[0] = address;
        meth1.invoke(retobj, arglist1);
        // Set port number
        Class partypes2[] = new Class[1];
        partypes2[0] = Integer.TYPE;
        Method meth2 = cls.getMethod("setPort", partypes2);
        Object arglist2[] = new Object[1];
        arglist2[0] = new Integer(port);
        meth2.invoke(retobj, arglist2);
        // set protocolHandlerClassName for AJP
        Class partypes3[] = new Class[1];
        partypes3[0] = str.getClass();
        Method meth3 = cls.getMethod("setProtocolHandlerClassName", partypes3);
        Object arglist3[] = new Object[1];
        arglist3[0] = new String("org.apache.jk.server.JkCoyoteHandler");
        meth3.invoke(retobj, arglist3);

    } catch (Exception e) {
        throw new MBeanException(e);
    }

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Server server = ServerFactory.getServer();
    Service service = server.findService(pname.getKeyProperty("name"));
    service.addConnector((Connector) retobj);

    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("CoyoteConnector");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), (Connector) retobj);
    return (oname.toString());

}

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Create a new HttpsConnector/* w  w w.ja  v  a  2 s .  com*/
 *
 * @param parent MBean Name of the associated parent component
 * @param address The IP address on which to bind
 * @param port TCP port number to listen on
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createHttpsConnector(String parent, String address, int port) throws Exception {

    Object retobj = null;

    try {

        // Create a new CoyoteConnector instance
        // use reflection to avoid j-t-c compile-time circular dependencies
        Class cls = Class.forName("org.apache.coyote.tomcat4.CoyoteConnector");
        Constructor ct = cls.getConstructor(null);
        retobj = ct.newInstance(null);
        Class partypes1[] = new Class[1];
        // Set address
        String str = new String();
        partypes1[0] = str.getClass();
        Method meth1 = cls.getMethod("setAddress", partypes1);
        Object arglist1[] = new Object[1];
        arglist1[0] = address;
        meth1.invoke(retobj, arglist1);
        // Set port number
        Class partypes2[] = new Class[1];
        partypes2[0] = Integer.TYPE;
        Method meth2 = cls.getMethod("setPort", partypes2);
        Object arglist2[] = new Object[1];
        arglist2[0] = new Integer(port);
        meth2.invoke(retobj, arglist2);
        // Set scheme
        Class partypes3[] = new Class[1];
        partypes3[0] = str.getClass();
        Method meth3 = cls.getMethod("setScheme", partypes3);
        Object arglist3[] = new Object[1];
        arglist3[0] = new String("https");
        meth3.invoke(retobj, arglist3);
        // Set secure
        Class partypes4[] = new Class[1];
        partypes4[0] = Boolean.TYPE;
        Method meth4 = cls.getMethod("setSecure", partypes4);
        Object arglist4[] = new Object[1];
        arglist4[0] = new Boolean(true);
        meth4.invoke(retobj, arglist4);
        // Set factory 
        Class serverSocketFactoryCls = Class.forName("org.apache.catalina.net.ServerSocketFactory");
        Class coyoteServerSocketFactoryCls = Class
                .forName("org.apache.coyote.tomcat4.CoyoteServerSocketFactory");
        Constructor factoryConst = coyoteServerSocketFactoryCls.getConstructor(null);
        Object factoryObj = factoryConst.newInstance(null);
        Class partypes5[] = new Class[1];
        partypes5[0] = serverSocketFactoryCls;
        Method meth5 = cls.getMethod("setFactory", partypes5);
        Object arglist5[] = new Object[1];
        arglist5[0] = factoryObj;
        meth5.invoke(retobj, arglist5);
    } catch (Exception e) {
        throw new MBeanException(e);
    }

    try {
        // Add the new instance to its parent component
        ObjectName pname = new ObjectName(parent);
        Server server = ServerFactory.getServer();
        Service service = server.findService(pname.getKeyProperty("name"));
        service.addConnector((Connector) retobj);
    } catch (Exception e) {
        // FIXME
        // disply error message 
        // the user needs to use keytool to configure SSL first
        // addConnector will fail otherwise
        return null;
    }

    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("CoyoteConnector");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), (Connector) retobj);
    return (oname.toString());

}

From source file:org.midonet.midolman.tools.MmStat.java

public void dumpMBeans(String jmxDomain, String filter, int count, int interval) {

    ObjectName on = null;
    try {/*w ww.java  2s.  co  m*/
        on = new ObjectName(jmxDomain + ":*");
    } catch (MalformedObjectNameException e) {
        System.err.println("Aborted: malformed domain " + jmxDomain);
        e.printStackTrace();
        System.exit(1);
    }
    java.util.Set<ObjectInstance> beans = null;
    try {
        beans = mbsc.queryMBeans(on, null);
    } catch (IOException e) {
        System.err.println("Aborted: querying Mbeans failed " + e);
        e.printStackTrace();
    }

    // Remember MBean instances to dump
    List<ObjectInstance> objectInstances = new ArrayList<>();
    for (ObjectInstance oi : beans) {
        if (filter != null) {
            String objectName = oi.getObjectName().toString();
            if (!objectName.contains(filter)) {
                continue;
            }
        }
        objectInstances.add(oi);
    }

    int attempt = 1;
    while (true) {
        // Header
        System.out.println("============================");
        System.out.println(String.format("Reading at %s (%s/%s)", new Date().toString(), attempt, count));
        System.out.println("============================");

        try {
            for (ObjectInstance oi : objectInstances) {
                on = oi.getObjectName();
                System.out.println("MBean: " + on.toString());
                MBeanInfo info = mbsc.getMBeanInfo(on);

                for (MBeanAttributeInfo attr : info.getAttributes()) {
                    System.out.print("\t" + attr.getName() + ": ");
                    System.out.println(mbsc.getAttribute(on, attr.getName()));
                }
            }
            if (attempt == count)
                System.exit(0);
            attempt++;
            //* Sleep "interval" seconds" */
            Thread.sleep(interval * 1000);
        } catch (IOException e) {
            System.err.println("Aborted: got IOException: " + e);
            e.printStackTrace();
            System.exit(1);
        } catch (JMException e) {
            System.err.println("Aborted: got JMException: " + e);
            e.printStackTrace();
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.catalina.core.StandardDefaultContext.java

/**
 * Return the MBean Names of all the defined resource links for this
 * application//from   w ww.  j  av  a  2s.com
 */
public String[] getResourceLinks() {

    ContextResourceLink[] links = getNamingResources().findResourceLinks();
    ArrayList results = new ArrayList();
    for (int i = 0; i < links.length; i++) {
        try {
            ObjectName oname = MBeanUtils.createObjectName(getDomain(), links[i]);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("Cannot create object name for resource " + links[i]);
        }
    }
    return ((String[]) results.toArray(new String[results.size()]));

}

From source file:org.apache.catalina.core.StandardDefaultContext.java

/**
 * Return the MBean Names of the set of defined environment entries for
 * this web application//from  w  ww .  ja v a 2  s  .  com
 */
public String[] getEnvironments() {
    ContextEnvironment[] envs = getNamingResources().findEnvironments();
    ArrayList results = new ArrayList();
    for (int i = 0; i < envs.length; i++) {
        try {
            ObjectName oname = MBeanUtils.createObjectName(this.getDomain(), envs[i]);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("Cannot create object name for environment " + envs[i]);
        }
    }
    return ((String[]) results.toArray(new String[results.size()]));

}

From source file:org.apache.catalina.core.StandardDefaultContext.java

/**
 * Add a resource reference for this web application.
 *
 * @param resourceName New resource reference name
 *//* w  ww  .j  a v  a2s .  c o  m*/
public String addResource(String resourceName, String type) throws MalformedObjectNameException {

    NamingResources nresources = getNamingResources();
    if (nresources == null) {
        return null;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource != null) {
        throw new IllegalArgumentException("Invalid resource name - already exists'" + resourceName + "'");
    }
    resource = new ContextResource();
    resource.setName(resourceName);
    resource.setType(type);
    nresources.addResource(resource);

    // Return the corresponding MBean name
    ManagedBean managed = Registry.getRegistry().findManagedBean("ContextResource");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);

    return (oname.toString());
}

From source file:org.apache.catalina.core.StandardDefaultContext.java

/**
 * Return the MBean Names of all the defined resource references for this
 * application.//from  ww  w . ja v  a  2 s.  c  o  m
 * XXX This changed - due to conflict
 */
public String[] getResourceNames() {

    ContextResource[] resources = getNamingResources().findResources();
    ArrayList results = new ArrayList();
    for (int i = 0; i < resources.length; i++) {
        try {
            ObjectName oname = MBeanUtils.createObjectName(getDomain(), resources[i]);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("Cannot create object name for resource " + resources[i]);
        }
    }
    return ((String[]) results.toArray(new String[results.size()]));

}

From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java

/**
 * Decomposes a composite data type so it can be traced
 * @param objectName The ObjectName of the MBean the composite data came from
 * @param cd The composite data instance
 * @return A map of values keyed by synthesized ObjectNames that represent the structure down to the numeric composite data items
 *///from  w  w  w .  j a va  2  s.  c o m
protected Map<ObjectName, Number> fromOpenType(final ObjectName objectName, final CompositeData cd) {
    if (objectName == null)
        throw new IllegalArgumentException("The passed ObjectName was null");
    if (cd == null)
        throw new IllegalArgumentException("The passed CompositeData was null");
    final Map<ObjectName, Number> map = new HashMap<ObjectName, Number>();
    final CompositeType ct = cd.getCompositeType();
    for (final String key : ct.keySet()) {
        final Object value = cd.get(key);
        if (value == null || !(value instanceof Number))
            continue;
        StringBuilder b = new StringBuilder(objectName.toString());
        b.append(",ctype=").append(simpleName(ct.getTypeName()));
        b.append(",metric=").append(key);
        ObjectName on = JMXHelper.objectName(clean(b));
        map.put(on, (Number) value);

    }
    return map;
}

From source file:org.jboss.console.plugins.monitor.ManageSnapshotServlet.java

protected void doit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String action = req.getParameter("action");
    if (action == null) {
        error("unknown action: ", req, resp);
        return;//  w w  w  .  j a v  a  2 s.c o m
    }
    action = action.trim();
    MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
    ObjectName monitorObjectName;
    String attribute = null;
    try {
        monitorObjectName = new ObjectName(req.getParameter("monitorObjectName"));
        attribute = (String) mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute");
    } catch (Exception ex) {
        error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp);
        return;
    }
    if (action.equals("Start Snapshot")) {
        Object[] nullArgs = {};
        String[] nullSig = {};
        try {
            mbeanServer.invoke(monitorObjectName, "startSnapshot", nullArgs, nullSig);
        } catch (Exception ex) {
            error("Problem invoking startSnapshot: " + ex.toString(), req, resp);
            return;
        }
        req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
        return;
    } else if (action.equals("Stop Snapshot")) {
        Object[] nullArgs = {};
        String[] nullSig = {};
        try {
            mbeanServer.invoke(monitorObjectName, "endSnapshot", nullArgs, nullSig);
        } catch (Exception ex) {
            error("Problem invoking endSnapshot: " + ex.toString(), req, resp);
            return;
        }
        req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
        return;
    } else if (action.equals("Clear Dataset")) {
        Object[] nullArgs = {};
        String[] nullSig = {};
        try {
            mbeanServer.invoke(monitorObjectName, "clearData", nullArgs, nullSig);
        } catch (Exception ex) {
            error("Problem invoking clearData: " + ex.toString(), req, resp);
            return;
        }
        req.setAttribute("error", "Dataset Cleared!");
        req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
        return;
    } else if (action.equals("Remove Snapshot")) {
        try {
            log.debug("removing snapshot: " + monitorObjectName.toString());
            mbeanServer.unregisterMBean(monitorObjectName);
            req.getRequestDispatcher("/ServerInfo.jsp").forward(req, resp);
        } catch (Exception ex) {
            error("Failed to Remove Monitor: " + ex.toString(), req, resp);
        }
        return;
    } else if (action.equals("Show Dataset")) {
        ArrayList data = null;
        long start, end = 0;
        try {
            data = (ArrayList) mbeanServer.getAttribute(monitorObjectName, "Data");
            start = ((Long) mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
            end = ((Long) mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
        } catch (Exception ex) {
            error("Problem invoking getData: " + ex.toString(), req, resp);
            return;
        }
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.println("<html>");
        writer.println("<body>");
        writer.println("<b>Start Time:</b> " + start + "ms<br>");
        writer.println("<b>End Time:</b> " + end + "ms<br>");
        writer.println("<b>Total Time:</b> " + (end - start) + "ms<br>");
        writer.println("<br><table border=\"0\">");
        for (int i = 0; i < data.size(); i++) {
            writer.println("<tr><td>" + data.get(i) + "</td></tr");
        }
        writer.println("</table></body></html>");
        return;
    } else if (action.equals("Graph Dataset")) {
        ArrayList data = null;
        long start, end = 0;
        try {
            data = (ArrayList) mbeanServer.getAttribute(monitorObjectName, "Data");
            start = ((Long) mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
            end = ((Long) mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
        } catch (Exception ex) {
            error("Problem invoking getData: " + ex.toString(), req, resp);
            return;
        }
        XYSeries set = new XYSeries(attribute, false, false);
        for (int i = 0; i < data.size(); i++) {
            set.add(new Integer(i), (Number) data.get(i));
        }
        DefaultTableXYDataset dataset = new DefaultTableXYDataset(false);
        dataset.addSeries(set);
        JFreeChart chart = ChartFactory.createXYLineChart("JMX Attribute: " + attribute, "count", attribute,
                dataset, PlotOrientation.VERTICAL, true, true, false);
        resp.setContentType("image/png");
        OutputStream out = resp.getOutputStream();
        ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
        out.close();
        return;
    }
    error("Unknown Action", req, resp);
    return;
}