WEB开发网
开发学院软件开发Java 在 Apache 目录服务器中存储 Java 对象,第 2 部分... 阅读

在 Apache 目录服务器中存储 Java 对象,第 2 部分:在 ApacheDS 中存储、搜索和检索 Java 对象(下)

 2010-04-19 00:00:00 来源:WEB开发网   
核心提示:应用程序 4. 搜索存储的数据先从 ApacheDS 上的一个简单搜索开始,假设在 ApacheDS 中有许多用户,在 Apache 目录服务器中存储 Java 对象,第 2 部分:在 ApacheDS 中存储、搜索和检索 Java 对象(下),而您想要找到用户 Alice 的所有详细信息,下面列出了关于 Alice

应用程序 4. 搜索存储的数据

先从 ApacheDS 上的一个简单搜索开始。假设在 ApacheDS 中有许多用户。而您想要找到用户 Alice 的所有详细信息。下面列出了关于 Alice 的所有已知事项:

Alice 是一名用户,所以在用户的数据组织单元中应当可以查找她的数据条目。(在第 1 部分已经介绍了组织单元或 “ou” 的概念。)

Alice 的用户名是 “alice”(不区分大小写)。

Alice 是一个人,所以她的数据条目使用的对象类必须直接或间接地扩展 person 对象类。

现在请参见清单 6,它显示了名为 SearchForAlice 的应用程序。SearchForAlice 演示了一个非常简单的搜索场景;后面我将扩展这个应用程序,使其适合更高级的搜索场景。

清单 6. SearchForAlice

public class SearchForAlice { 
  public SearchForAlice() { 
    try 
    { 
      //------------------------------------------     
      //Step1: Setting up JNDI properties for ApacheDS 
      //------------------------------------------       
      InputStream inputStream = new FileInputStream( "ApacheDS.properties"); 
      Properties properties = new Properties(); 
      properties.load(inputStream); 
      properties.setProperty("java.naming.security.credentials", "secret"); 
 
      //------------------------------------------     
      // Step2: Fetching a DirContext object 
      //------------------------------------------       
      DirContext ctx = new InitialDirContext(properties); 
   
      //--------------------------------------------- 
      //Step3: Setting search context 
      //--------------------------------------------- 
      String searchContext = "ou=users"; 
 
      //-------------------------------------------- 
      //Step4: Creating search attributes for Alice 
      //-------------------------------------------- 
      Attribute uid = new BasicAttribute("uid"); 
      Attribute objclass = new BasicAttribute("objectClass"); 
 
      //adding attribute values 
      uid.add("Alice"); 
      objclass.add("person"); 
 
      //Instantiate Attributes object and put search attributes in it. 
      Attributes attrs = new BasicAttributes(true); 
      attrs.put(uid);       
      attrs.put(objclass); 
 
      //------------------------------------------ 
      //Step5: Executing search 
      //------------------------------------------ 
      NamingEnumeration ne = ctx.search(searchContext, attrs); 
       
      if (ne != null) 
      { 
        //Step 6: Iterating through SearchResults 
        while (ne.hasMore()) { 
          //Step 7: Getting individual SearchResult object 
          SearchResult sr = (SearchResult) ne.next(); 
 
          //Step 8: 
          String entryRDN = sr.getName(); 
          System.out.println("RDN of the Searched entry: "+entryRDN); 
 
          //Step 9: 
          Attributes srAttrs = sr.getAttributes(); 
 
          if (srAttrs != null) { 
             //Step 10: 
             for (Enumeration e = attrs.getAll() ; e.hasMoreElements() ;) 
             { 
               Attribute attr = (Attribute) e.nextElement(); 
 
               //Step 11: 
               String attrID = attr.getID(); 
               System.out.println("Attribute Name: "+attrID); 
               System.out.println("Attribute Value(s):"); 
 
               NamingEnumeration e1 = attr.getAll(); 
               while (e1.hasMore()) 
                 System.out.println("\t\t"+e1.nextElement()); 
             }//for() 
          }//if (srAttrs) 
        }  
      }//if (ne != null) 
         
    } catch (Exception e) { 
      System.out.println("Operation failed: " + e); 
    } 
  } 
 
  public static void main(String[] args) { 
   SearchForAlice searchAlice = new SearchForAlice(); 
  } 
 }    

1 2 3 4 5 6  下一页

Tags:Apache 目录 服务器

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接