FileDocCategorySizeDatePackage
MonitoringInfoHelper.javaAPI DocGlassfish v2 API17622Fri May 04 22:34:44 BST 2007com.sun.enterprise.diagnostics.collect

MonitoringInfoHelper

public class MonitoringInfoHelper extends Object
A helper which executes List, Get for Dotted Names Command
author
Jagadish Ramu

Fields Summary
private static final String
LIST_COMMAND
private static final String
LIST_OPERATION
private static final String
LIST_MONITORING_OPERATION
private static final String
GET_COMMAND
private static final String
GET_OPERATION
private static final String
GET_MONITORING_OPERATION
public static final String
STRING_ARRAY
private static final String
MONITOR_OPTION
private static final String
OBJECT_NAME
private static final String
PROPERTY_STRING
public static final String
SECURE
private ArrayList
operands
private String
name
private HashMap
options
private static Logger
logger
private ArrayList
output
private static String
OUTER_ARRAY_DELIM
private static String
INNER_ARRAY_DELIM
Constructors Summary
public MonitoringInfoHelper()


      
        options = new HashMap();
    
Methods Summary
private voidaddToResults(java.lang.String msg)
add the results to output

param
msg

        if (output != null) {
            output.add(msg);

        }
    
public booleancheckPropertyToConvert(java.lang.String param)
This method checks if the element in the dotted name contains "property" or "system-property". If the element is "property" or "system-property" then return true else return false.

param
param - dotted name
return
true if dotted name contains "property" or "system-property" false

        final int index = param.lastIndexOf('.");
        if (index < 0) {
            return false;
        }
        final String elementName = param.substring(index + 1);
        return elementName.matches(PROPERTY_STRING);
    
private javax.management.Attribute[]collectAllAttributes(java.lang.Object[] results)

        // use a HashMap to eliminate duplicates; use name as the key
        final HashMap attrs = new HashMap();

        for (final Object result : results) {
            if (result instanceof Attribute) {
                attrs.put(((Attribute) result).getName(), result);
            } else if (result instanceof Attribute[]) {
                final Attribute[]    list = (Attribute[]) result;

                for (int attrIndex = 0; attrIndex < list.length; ++attrIndex) {
                    final Attribute attr = list[attrIndex];

                    attrs.put(attr.getName(), attr);
                }
            } else {
                assert(result instanceof Exception);
            }
        }

        final Attribute[]    attrsArray = new Attribute[ attrs.size() ];
        attrs.values().toArray(attrsArray);
        Arrays.sort(attrsArray, new AttributeComparator());

        return (attrsArray);
    
public java.lang.StringconvertUnderscoreToHyphen(java.lang.String param)
This method will convert the attribute in the dotted name notation from underscore to hyphen.

param
param - the dotted name to convert
return
the converted string

        int endIndex = param.indexOf('=");
        int begIndex = (endIndex > 0) ? param.lastIndexOf('.", endIndex) :
                param.lastIndexOf('.");
        if (begIndex < 1 || checkPropertyToConvert(
                param.substring(0, begIndex))) {
            return param;
        }
        if (endIndex > 0) {
            return param.substring(0, begIndex) +
                    param.substring(begIndex, endIndex).replace('_", '-") +
                    param.substring(endIndex);
        } else {
            return param.substring(0, begIndex) +
                    param.substring(begIndex).replace('_", '-");
        }
    
private voiddisplayResultFromGetOrSet(java.lang.String[] inputs, java.lang.Object[] returnValues)
figure out the returnValue type and call appropriate print methods

params returnval
throws CommandException if could not print AttributeList

    // top-level array

                              
     
              
                                         
              
        if (returnValues.length == 1) {
            // there was a single string provided as input
            final Object result = returnValues[0];

            if (result instanceof Exception) {
                throw (Exception) result;
            } else if (result.getClass() == Attribute[].class) {
                // this must have been the result of a wildcard input
                final Attribute[]    attrs = (Attribute[]) result;

                if (attrs.length == 0) {
                    throw new AttributeNotFoundException("NoWildcardMatches");
                }

                printMessage(stringify(attrs, OUTER_ARRAY_DELIM));
            } else {
                printMessage(stringify(result, OUTER_ARRAY_DELIM));
            }
        } else {
            // more than one input String; collect all the resulting Attributes
            // into one big non-duplicate sorted list and print them.
            final Attribute[]    attrs = collectAllAttributes(returnValues);

            //addToResults(  stringify( attrs, OUTER_ARRAY_DELIM ) );

            for (Attribute attr : attrs) {
                addToResults(attr.getName() + " = " + attr.getValue());
            }

            // tell about any failures
            for (int i = 0; i < returnValues.length; ++i) {
                if (returnValues[i] instanceof Exception) {
                    final Exception e = (Exception) returnValues[i];

                    final String msg = "ErrorInGetSet " +
                            inputs[i] + e.getLocalizedMessage();

                    printMessage(msg);
                }
            }
        }
    
private voiddisplayResultFromList(java.lang.String[] result)

        if (result.length == 0) {
            //need to convert the operands to String for display

            final String displayOperands = stringify(getDottedNamesParam(true),
                    INNER_ARRAY_DELIM);
            printMessage("EmptyList - " + displayOperands);
        } else {
            for (String value : result) {
                addToResults(value);
            }
        }
    
protected booleangetBooleanOption(java.lang.String optionName)
Finds the option with the give name

return
boolean return boolean type of the option value

        return Boolean.valueOf(getOption(optionName));
    
private java.lang.Object[]getDottedNamesParam(boolean convertUnderscore)
get the dotted notation from the operand and convert it to a Object[]

return
Object[]

        final ArrayList<String> dottedNames = getOperands();
        String [] dottedNamesArray = new String[dottedNames.size()];

        for (int ii = 0; ii < dottedNames.size(); ii++) {
            if (convertUnderscore) {
                dottedNamesArray[ii] = convertUnderscoreToHyphen
                        (dottedNames.get(ii));
            } else {
                dottedNamesArray[ii] = dottedNames.get(ii);
            }
        }
        return new Object[]{dottedNamesArray};


    
private java.lang.StringgetExceptionMessage(java.lang.Exception e)

        String msg = null;

        if (e instanceof RuntimeMBeanException) {
            RuntimeMBeanException rmbe = (RuntimeMBeanException) e;
            msg = rmbe.getTargetException().getLocalizedMessage();
        } else if (e instanceof RuntimeOperationsException) {
            RuntimeOperationsException roe = (RuntimeOperationsException) e;
            msg = roe.getTargetException().getLocalizedMessage();
        } else {
            msg = e.getLocalizedMessage();
        }
        if (msg == null || msg.length() == 0) {
            msg = e.getMessage();
        }

        if (msg == null || msg.length() == 0) {
            msg = e.getClass().getName();
        }

        return (msg);
    
public java.lang.StringgetName()
get the command name (list / get)

return
String

        return name;
    
public java.util.ArrayListgetOperands()
Operands passed to List/ Get command

return
Vector representing the operands

        return operands;
    
private java.lang.StringgetOperation()
get the operation to invoke depending on the command name and option if command name is "set" then the operation is dottedNameSet if command name is "get" then the operation is dottedNameGet if the command name is "get" with --monitor option to true, then the operation is dottedNameMonitoringGet all others return null.

return
name of th operation


        if (getName().equals(GET_COMMAND)) {
            if (getBooleanOption(MONITOR_OPTION)) {
                return GET_MONITORING_OPERATION;
            } else {
                return GET_OPERATION;
            }
        } else if (getName().equals(LIST_COMMAND)) {
            if (getBooleanOption(MONITOR_OPTION)) {
                return LIST_MONITORING_OPERATION;
            } else {
                return LIST_OPERATION;
            }
        }
        return null;

    
public java.lang.StringgetOption(java.lang.String optionName)
Finds the option with the give name

return
Option return option if found else return null

        if (!optionNameExist(optionName)) {
            return null;
        }
        return (String) options.get(optionName);
    
private booleanoptionNameExist(java.lang.String optionName)
returns true if the option name exist in the options list

true if option name exist

        return options.containsKey(optionName);
    
private voidprintMessage(java.lang.String msg)
Log the messages

param
msg

        logger.log(Level.INFO, msg, "INFO");
    
public voidrunCommand(java.util.ArrayList result)
execute the command (list or get)

param
result - list inwhich results are stored
throws
com.sun.enterprise.diagnostics.DiagnosticException

        output = result;
        //use http connector

        final Object[] params = getDottedNamesParam
                (getName().equals(LIST_COMMAND) ?
                false : true);
        final String[] types = new String[]{STRING_ARRAY};
        final String operationName = getOperation();


        try {

            MBeanServer mbs = AppServerMBeanServerFactory.
                    getMBeanServerInstance();
            // we always invoke with an array, and so the
            //  result is always an Object []
            final Object[] returnValues = (Object[]) mbs.
                    invoke(new ObjectName(OBJECT_NAME),
                    operationName, params, types);
            if (operationName.indexOf("List") >= 0) {
                // a list operation, just print the String []
                displayResultFromList((String []) returnValues);
            }

            else {
                final String[]    userArgs = (String []) params[0];

                displayResultFromGetOrSet(userArgs, returnValues);
            }
        }

        catch (InitException ie) {
            logger.log(Level.WARNING, "Initialization" +
                    " exception occurred while getting" +
                    " MBean Server Instance", ie);
            throw new DiagnosticException(ie.getMessage());
        }

        catch (Exception e) {
            final String msg = getExceptionMessage(e);
            if (msg != null) {
                logger.log(Level.WARNING, e.getMessage(), e);
            }
            throw new DiagnosticException(e.getMessage());
        }
    
public voidsetName(java.lang.String name)
set the command name ( list / get )

param
name

        this.name = name;
    
public voidsetOperands(java.util.ArrayList operands)
Operands passed to List/ Get command

param
operands

        this.operands = operands;
    
public voidsetOption(java.lang.String optionName, java.lang.String optionValue)
Sets the option value for the give name

param
optionName name of the option
param
optionValue value of the option

        options.put(optionName, optionValue);
    
public voidsetOptions(java.util.HashMap options)
Sets the list of options for this Command

param
options List of options for this command

        this.options = options;
    
private java.lang.Stringstringify(java.lang.Object o, java.lang.String delim)

        String result = null;

        if (o == null) {
            result = "";
        } else if (o instanceof Attribute) {
            final Attribute attr = (Attribute) o;

            result = attr.getName() + " = " + stringify(attr.getValue(),
                    INNER_ARRAY_DELIM);
        } else if (ClassUtil.objectIsPrimitiveArray(o)) {
            final Object [] objectList = ArrayConversion.toAppropriateType(o);

            result = stringifyArray(objectList, delim);
        } else if (ClassUtil.objectIsArray(o)) {
            result = stringifyArray((Object []) o, delim);
        } else if (o instanceof Exception) {
            final Exception e = (Exception) o;

            result = getExceptionMessage(e);
        } else {
            result = o.toString();
        }


        return (result);
    
private java.lang.StringstringifyArray(java.lang.Object[] a, java.lang.String delim)

    // when an array is inside another

            
        final StringBuffer buf = new StringBuffer();

        for (int i = 0; i < a.length; ++i) {
            buf.append(stringify(a[i], INNER_ARRAY_DELIM));
            if (i != a.length - 1) {
                buf.append(delim);
            }
        }
        return (buf.toString());