2012-08-28 2 views
6

이 질문은 모든 것을 말해줍니다. , WF-008-DAM-PS 그 주요 부분을 제외입니다 :javax.naming.directory.Attribute에서 값을 추출하는 방법

cn: WF-008-DAM-PS 

코드 조각은 다음과 같습니다 :

private void searchGroup() throws NamingException { 
    NamingEnumeration<SearchResult> searchResults = getLdapDirContext().search(groupDN, "(objectclass=groupOfUniqueNames)", getSearchControls()); 
    String searchGroupCn = getCNForBrand(m_binder.getLocal("brandId"), m_binder.getLocal("brandName")); 
    Log.info(searchGroupCn); 
    while (searchResults.hasMore()) { 
     SearchResult searchResult = searchResults.next(); 
     Attributes attributes = searchResult.getAttributes(); 
     Attribute groupCn = attributes.get("cn"); 
     if(groupCn != null) { 
      Log.info(groupCn.toString());    
     } 
    } 
} 

가 어떻게이 아니라 가치를 얻을 수 있습니다 내가 특성을 인쇄하고 언제입니까? 감사합니다.

답변

4

getValue() 메서드 또는 getValue(int) 메서드를 호출하십시오.

+0

이 두 방법 javax.naming.directory.BasicAttribute 또는 javax.naming.directory.Attribute에 존재는합니다 (Attribute 인스턴스가 여러 개의 값을 가지는 경우)? get (int) 메소드가 있습니다. –

+0

'Attribute'는 인터페이스이고,'BasicAttribute'는'Attribute'를 구현합니다. 그래서,'final Object o = groupCn.getValue()','groupCn'이 단일 값이라고 가정합니다. 다중 값인 경우 정수 인덱스를 'groupCn.getValue (index)'에 대한 매개 변수로 사용하십시오. –

+0

감사하지만 getValue() 메소드는 http://docs.oracle.com/javase/1.4에는 없습니다. 2/docs/api/javax/naming/directory/BasicAttribute.html 또는 http://docs.oracle.com/javase/1.4.2/docs/api/javax/naming/directory/Attribute.html –

6

솔루션은 다음과 같습니다

Attribute groupCn = attributes.get("cn"); 
String value = groupCn.get(); 
1

일반

이의 우리가 있다고 가정 해 봅시다 :

Attributes attributes; 
Attribute a = attributes.get("something"); 
  • if(a.size() == 1)
    • 가 다음 모든 값을 통해 고유 한 값을
  • if(a.size() > 1)

    • 반복 처리를 얻을 수 a.get() 또는 a.get(0)을 사용할 수 있습니다 : 당신이 여기 a.get()를 사용하는 경우

      for (int i = 0 ; i < a.size() ; i++) { 
          Object currentVal = a.get(i); 
          // do something with currentVal 
      } 
      

      , 그것은 반환 내부 구현 (BasicAttribute)이 다음과 같으므로 첫 번째 값만 :

      ,
      public Object get() throws NamingException { 
          if (values.size() == 0) { 
           throw new NoSuchElementException("Attribute " + getID() + " has no value"); 
          } else { 
           return values.elementAt(0); 
          } 
      } 
      

두 방법 (get(int)get() 및)는 NamingException 던진다.

실례

LdapContext ctx = new InitialLdapContext(env, null); 

Attributes attributes = ctx.getAttributes("", new String[] { "supportedSASLMechanisms" }); 
System.out.println(attributes); // {supportedsaslmechanisms=supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5} 

Attribute a = atts.get("supportedsaslmechanisms"); 
System.out.println(a); // supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5 

System.out.println(a.get()); // GSSAPI 

for (int i = 0; i < a.size(); i++) { 
    System.out.print(a.get(i) + " "); // GSSAPI EXTERNAL DIGEST-MD5 
} 
+0

@Downvoter, 귀하의 결정에 대한 설명을 추가하십시오 ... 나는 이것이 매우 좋은 대답이라고 생각합니다. –

관련 문제