javajava-ws

Access Array value of side scope in Java


I want to know how to return all values of a array variable which are assigned in for loop.

In the method below, I am assigning values to an Output array. Now I want to display all the value in the Output array as return argument. Because of scope level, I am getting the last value.

Right now I am able to return one the last value because of the scope issue.

public  static String[] getMBeanAppsStatus(String host, String port, 
                                          String container, String filter, 
                                          String usr, String pwd) {
    String Output[] = new String[3];
    int mbeanAppsIdx = 0;
    int LVar = mbeanAppsIdx;
    try {
        Object[] connections = 
            connectMethd(host, port, container, filter, usr, pwd);

        MBeanServerConnection serverConn = 
            (MBeanServerConnection)connections[0];
        System.out.println("serverConn from array variable in getMBeanAppsStatus" + 
                           serverConn);
        /* retrieve mbean apps matching given filter */
        ObjectName mbeanDomainName = new ObjectName(filter);
        Set mbeanAppNames = serverConn.queryNames(mbeanDomainName, null);
        /* pattern to extract mbean application names */
        Pattern mbeanAppPat = 
            Pattern.compile("name=", Pattern.CASE_INSENSITIVE);

        /* mbean application name */
        ObjectName mbeanApp = null;

        /* retrieve mbean apps count */
        Iterator i;
        for (i = mbeanAppNames.iterator(); i.hasNext(); ) {
            mbeanAppsIdx++;
            System.out.println("Populating MBean App #" + mbeanAppsIdx + 
                               "details...");
            /* retrieve mbean app name and status */
            mbeanApp = (ObjectName)i.next();
            String mbeanAppFQDN = mbeanApp.toString();
            String mbeanAppName = mbeanAppPat.split(mbeanAppFQDN)[1];
            Boolean mbeanAppRunning = 
                Boolean.valueOf(serverConn.getAttribute(mbeanApp, 
                                                        "Running").toString());
            String mbeanAppStatus = "DISABLED";
            if (mbeanAppRunning.booleanValue())
                mbeanAppStatus = "ENABLED";
            Output[0] = mbeanAppName;
            Output[1] = mbeanAppFQDN;
            Output[2] = mbeanAppStatus;
            System.out.println("getMBeanAppsStatus output " + 
                               "mbeanAppName " + mbeanAppName + 
                               " mbeanAppFQDN " + mbeanAppFQDN + 
                               " mbeanAppStatus : " + mbeanAppStatus);

        }

    } catch (MalformedObjectNameException e) {

        e.getStackTrace();
    } catch (InstanceNotFoundException e) {

        e.getStackTrace();
    } catch (AttributeNotFoundException e) {

        e.getStackTrace();
    } catch (ReflectionException e) {

        e.getStackTrace();
    } catch (MBeanException e) {
        e.getStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe);
    }

    System.out.println("getMBeanAppsStatus Output " + Output);
    return Output;
}

Basically, I am trying to convert the above method to a J2EE webservice and the response from the web service will be a return value of Output from the method.

I have 2 issues here in method which i want to rectify it.

1) Want to concatenate the value of mbeanAppName, mbeanAppFQDN and mbeanAppStatus with comma , separator and assign to array variable.

2) Return the array result which should hold all the previous values also.


Solution

  • You are only seeing the last ouput because at each iteration, you are overwritting the previous output with the current one.

    1. You can use a StringBuilder to concatenate mbeanAppName, mbeanAppFQDN and mbeanAppStatus separated by a comma ,. This can be accomplished in the following way:

         StringBuilder sb = new StringBuilder();
      
         /* Declare and initialise variables somewhere in between*/
      
         sb.append(mbeanAppName);
         sb.append(",");
         sb.append(mbeanAppFQDN);
         sb.append(",");
         sb.append(mbeanAppStatus);
      
         String generatedStringOutputFromStringBuilder = sb.toString();
      
    2. You can use a List to store the output after each iteration. Unlike an array, a List can change size. You don't have to declare its when when you initialise it. Add the output to the created List at each iteration. For example:

         List<String> output = new ArrayList<String>();
      
         /* Declare and initialise variables somewhere in between*/
      
         output.add(generatedStringOutputFromStringBuilder);
      

    Here is the corrected sample that should set you in the right direction.

    public  static List<String> getMBeanAppsStatus(String host, String port, 
                                              String container, String filter, 
                                              String usr, String pwd) {
        // A new List
        List<String> output = new ArrayList<String>();
    
        //A StringBuilder that will be used to build each ouput String
        StringBuilder sb = new StringBuilder();
    
        int mbeanAppsIdx = 0;
        int LVar = mbeanAppsIdx;
        try {
            Object[] connections = connectMethd(host, port, container, filter, usr, pwd);
    
            MBeanServerConnection serverConn = (MBeanServerConnection)connections[0];
    
            System.out.println("serverConn from array variable in getMBeanAppsStatus" + serverConn);
    
            /* retrieve mbean apps matching given filter */
            ObjectName mbeanDomainName = new ObjectName(filter);
            Set mbeanAppNames = serverConn.queryNames(mbeanDomainName, null);
    
            /* pattern to extract mbean application names */
            Pattern mbeanAppPat = Pattern.compile("name=", Pattern.CASE_INSENSITIVE);
    
            /* mbean application name */
            ObjectName mbeanApp = null;
    
            /* retrieve mbean apps count */
            Iterator i;
            for (i = mbeanAppNames.iterator(); i.hasNext(); ) {
    
                mbeanAppsIdx++;
                System.out.println("Populating MBean App #" + mbeanAppsIdx + "details...");
    
                /* retrieve mbean app name and status */
                mbeanApp = (ObjectName)i.next();
    
                String mbeanAppFQDN = mbeanApp.toString();
                String mbeanAppName = mbeanAppPat.split(mbeanAppFQDN)[1];
                Boolean mbeanAppRunning = Boolean.valueOf(serverConn.getAttribute(mbeanApp, "Running").toString());
                String mbeanAppStatus = "DISABLED";
    
                if (mbeanAppRunning.booleanValue())
                    mbeanAppStatus = "ENABLED";
    
                // Construct the output using a string builder as
                //      mbeanAppName,mbeanAppFQDN,mbeanAppStatus
                sb.append(mbeanAppName);
                sb.append(",");
                sb.append(mbeanAppFQDN);
                sb.append(",");
                sb.append(mbeanAppStatus);
    
                // Store the current ouput in the List output
                output.add(sb.toString());
    
                // Empty string builder
                sb.setLength(0)
    
                System.out.println("getMBeanAppsStatus output " + 
                                   "mbeanAppName " + mbeanAppName + 
                                   " mbeanAppFQDN " + mbeanAppFQDN + 
                                   " mbeanAppStatus : " + mbeanAppStatus);
    
            }
    
        } catch (MalformedObjectNameException e) {
    
            e.getStackTrace();
        } catch (InstanceNotFoundException e) {
    
            e.getStackTrace();
        } catch (AttributeNotFoundException e) {
    
            e.getStackTrace();
        } catch (ReflectionException e) {
    
            e.getStackTrace();
        } catch (MBeanException e) {
            e.getStackTrace();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    
        System.out.println("getMBeanAppsStatus Output " + output);
        return output;
    }