Non-instantiable class that provides jmx utility methods : Java Management API « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Java Management APIScreenshots 
Non-instantiable class that provides jmx utility methods
Non-instantiable class that provides jmx utility methods
  
// Copyright 2007 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;

/**
 * Non-instantiable class that provides jmx utility methods.
 */
public class Utilities {


  /**
   * Prints info about a bean to a {@link VarOutputSink}.
   
   @param sink The {@link VarOutputSink} to which info will be sent
   @param mbs The {@link MBeanServer} with respect to which the
   *          {@code objectName} is accessed
   @param objectName The {@link ObjectName} that identifies this bean
   */
  public static void printMBeanInfo(VarOutputSink sink, MBeanServer mbs,
      ObjectName objectName) {
    MBeanInfo info = getMBeanInfoSafely(sink, mbs, objectName);
    if (info == null) { return}

    sink.echo("\nCLASSNAME: \t" + info.getClassName());
    sink.echo("\nDESCRIPTION: \t" + info.getDescription());
    sink.echo("\nATTRIBUTES");
    MBeanAttributeInfo[] attrInfo = info.getAttributes();
    sink.printVariable("attrcount", Integer.toString(attrInfo.length));
    if (attrInfo.length > 0) {
      for (int i = 0; i < attrInfo.length; i++) {
        sink.echo(" ** NAME: \t" + attrInfo[i].getName());
        sink.echo("    DESCR: \t" + attrInfo[i].getDescription());
        sink
            .echo("    TYPE: \t" + attrInfo[i].getType() "\tREAD: "
                + attrInfo[i].isReadable() "\tWRITE: "
                + attrInfo[i].isWritable());
      }
    else sink.echo(" ** No attributes **");
    sink.echo("\nCONSTRUCTORS");
    MBeanConstructorInfo[] constrInfo = info.getConstructors();
    for (int i = 0; i < constrInfo.length; i++) {
      sink.echo(" ** NAME: \t" + constrInfo[i].getName());
      sink.echo("    DESCR: \t" + constrInfo[i].getDescription());
      sink.echo("    PARAM: \t" + constrInfo[i].getSignature().length
          " parameter(s)");
    }
    sink.echo("\nOPERATIONS");
    MBeanOperationInfo[] opInfo = info.getOperations();
    if (opInfo.length > 0) {
      for (int i = 0; i < opInfo.length; i++) {
        sink.echo(" ** NAME: \t" + opInfo[i].getName());
        sink.echo("    DESCR: \t" + opInfo[i].getDescription());
        sink.echo("    PARAM: \t" + opInfo[i].getSignature().length
            " parameter(s)");
      }
    else sink.echo(" ** No operations ** ");
    sink.echo("\nNOTIFICATIONS");
    MBeanNotificationInfo[] notifInfo = info.getNotifications();
    if (notifInfo.length > 0) {
      for (int i = 0; i < notifInfo.length; i++) {
        sink.echo(" ** NAME: \t" + notifInfo[i].getName());
        sink.echo("    DESCR: \t" + notifInfo[i].getDescription());
        String notifTypes[] = notifInfo[i].getNotifTypes();
        for (int j = 0; j < notifTypes.length; j++) {
          sink.echo("    TYPE: \t" + notifTypes[j]);
        }
      }
    else sink.echo(" ** No notifications **");
  }

  private static MBeanInfo getMBeanInfoSafely(VarOutputSink sink,
      MBeanServer mbs, ObjectName objectName) {
    MBeanInfo info = null;
    try {
      info = mbs.getMBeanInfo(objectName);
    catch (InstanceNotFoundException e) {
      sink.printVariable("ObjectName""Not found");
    catch (IntrospectionException e) {
      sink.printVariable("ObjectName""IntrospectionException");
    catch (ReflectionException e) {
      sink.printVariable("ObjectName""ReflectionException");
    }
    return info;
  }

  /**
   * Prints bean attributes to a {@link VarOutputSink}.
   
   @param sink The {@link VarOutputSink} to which attributes will be sent
   @param mbs The {@link MBeanServer} with respect to which the
   *          {@code objectName} is accessed
   @param objectName The {@link ObjectName} that identifies this bean
   */
  public static void printMBeanAttributes(VarOutputSink sink, MBeanServer mbs,
      ObjectName objectName) {
    MBeanInfo info = getMBeanInfoSafely(sink, mbs, objectName);
    if (info == null) {
      sink.printVariable(objectName.getCanonicalName()"can't fetch info");
      return;
    }
    MBeanAttributeInfo[] attrInfo = info.getAttributes();
    if (attrInfo.length > 0) {
      for (int i = 0; i < attrInfo.length; i++) {
        String attrName = attrInfo[i].getName();
        Object attrValue = null;
        String attrValueString = null;
        try {
          attrValue = mbs.getAttribute(objectName, attrName);
        catch (AttributeNotFoundException e) {
          attrValueString = "AttributeNotFoundException";
        catch (InstanceNotFoundException e) {
          attrValueString = "InstanceNotFoundException";
        catch (MBeanException e) {
          attrValueString = "MBeanException";
        catch (ReflectionException e) {
          attrValueString = "ReflectionException";
        }
        if (attrValueString == null) {
          attrValueString = attrValue.toString();
        }
        sink.printVariable(attrName, attrValueString);
      }
    }
  }

  /**
   * Helper interface defining output sinks used with
   {@link Utilities#printMBeanInfo(com.google.enterprise.util.jmx.Utils.VarOutputSink, MBeanServer, ObjectName)}
   * and
   {@link Utilities#printMBeanAttributes(com.google.enterprise.util.jmx.Utils.VarOutputSink, MBeanServer, ObjectName)}
   */
  public interface VarOutputSink {
    public void printVariable(String name, String value);
    public void echo(String string);
  }

  /**
   * Static {@link VarOutputSink} that uses {@link System#out}
   */
  public static final VarOutputSink SYSTEM_OUT_SINK = new PrintStreamVarOutputSink(
      System.out);

  public static class LoggerVarOutputSink implements VarOutputSink {
    private final Logger logger;

    public LoggerVarOutputSink(Logger logger) {
      this.logger = logger;
    }

    public void printVariable(String name, String value) {
      logger.info(name + " " + value);
    }

    public void echo(String string) {
      logger.info(string);
    }
  }

  public static class PrintWriterVarOutputSink implements VarOutputSink {
    private final PrintWriter printWriter;

    public PrintWriterVarOutputSink(PrintWriter printWriter) {
      this.printWriter = printWriter;
    }

    public void printVariable(String name, String value) {
      printWriter.print("  <b>");
      printWriter.print(name);
      printWriter.print("</b> ");
      printWriter.print(value);
      printWriter.println("<br>");
    }

    public void echo(String string) {
      printWriter.print(string);
    }
  }

  public static class PrintStreamVarOutputSink implements VarOutputSink {
    private final PrintStream printStream;

    public PrintStreamVarOutputSink(PrintStream printStream) {
      this.printStream = printStream;
    }

    public void printVariable(String name, String value) {
      printStream.print("  <b>");
      printStream.print(name);
      printStream.print("</b> ");
      printStream.print(value);
      printStream.println("<br>");
    }

    public void echo(String string) {
      printStream.print(string);
    }
  }
}

   
    
  
Related examples in the same category
1. Example of using the java.lang.management API to sort threads by CPU usage
2. Demo code which plots the memory usage by all memory pools
3. This VerboseGC class demonstrates the capability to get the garbage collection statistics and memory usage remotely
4. This FullThreadDump class demonstrates the capability to get a full thread dump and also detect deadlock remotely.
5. Performing deadlock detection programmatically within the application using the java.lang.management APIPerforming deadlock detection programmatically within the application using the java.lang.management API
6. MBeanTyperInvoker handles method invocations against the MBeanTyper target object and forwards them to the MBeanServer and ObjectName for invocation.
7. Several utility functions for the JMX implementation
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.