2014-05-09 1 views
0

for 루프에 할당 된 배열 변수의 모든 값을 반환하는 방법을 알고 싶습니다.액세스 배열 값 Java의 측면 범위

아래의 방법에서 출력 배열에 값을 할당합니다. 이제 출력 배열의 모든 값을 반환 인수로 표시하려고합니다. 범위 수준 때문에 마지막 값을 얻고 있습니다.

지금은 범위 문제로 인해 마지막 값 하나를 반환 할 수 있습니다.

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; 
} 

기본적으로,이 방법의 출력의 반환 값이 될 것 J2EE의 웹 서비스와 웹 서비스의 응답에 위의 방법을 변환하려합니다.

나는이 문제를 해결하고자하는 방법에 2 가지 이슈가 있습니다.

1) 콤마 , 세퍼레이터 mbeanAppName, mbeanAppFQDNmbeanAppStatus의 값을 연결하고 배열 변수에 할당 할.

2) 이전 값을 모두 보유해야하는 배열 결과를 반환합니다.

+0

샘플 출력을 표시 할 수 있습니까? –

+0

응답 주셔서 감사합니다 ... 샘플 출력 MBean을 채 웁니다 App # 63details ... getMBeanAppsStatus 출력 mbeanAppName Aramex_TEST_SOStatusMBean mbeanAppFQDN 기본값 : type = Application, name = Aramex_TEST_SOStatusMBean mbeanAppStatus : DISABLED 출력에서 ​​마지막 Mbean은 63 번째입니다. 모든 이전 값은 다음 출력으로 덮어 씁니다. – Tarak

+0

각 반복에서 이전 출력을 현재 출력으로 겹쳐 쓰므로 63 번째 출력 만 볼 수 있습니다. 63 개의 모든 출력을 String 배열 출력에 저장하려고합니까? –

답변

1

각 반복마다 이전 출력을 현재 출력으로 겹쳐 쓰므로 최종 출력 만 표시됩니다.

  1. 당신은 mbeanAppName, mbeanAppFQDNmbeanAppStatus 쉼표 ,로 구분 연결할 수있는 StringBuilder를 사용할 수 있습니다. 이것은 다음과 같은 방법으로 수행 할 수 있습니다

    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. 당신은 각 반복 한 후 출력을 저장하는 List를 사용할 수 있습니다. 배열과 달리 List은 크기를 변경할 수 있습니다. 초기화 할 때 선언 할 필요는 없습니다. 각 반복마다 생성 된 List에 출력을 추가하십시오. 예를 들어 :

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

올바른 방향을 설정해야 수정 된 샘플입니다.

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; 
} 
+0

Jean, 고마워. .. 당신의 솔루션은 효과가있었습니다. 아웃 3 변수. Aramex_TEST_SOStatusMBean 기본 같은 유형 = 응용 프로그램 이름 = Aramex_TEST_SOStatusMBean DISABLED ====== Aramex_TEST_SOStatusMBean1 기본 : 유형 = 응용 프로그램 이름 = Aramex_TEST_SOStatusMBean1 DISABLED 은 위의 값과 각각 같은 반환해야 2 개의 MBean가있는 경우 하나의 변수를 유지해야합니다. 자바에서 가능합니까 ?? – Tarak