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

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

 2010-04-19 00:00:00 来源:WEB开发网   
核心提示: publicclassFetchAliceMessagingPreferences{publicFetchAliceMessagingPreferences(){try{//--//Step1:SettingupJNDIpropertiesforApacheDS//--InputStreamin

public class FetchAliceMessagingPreferences { 
  public FetchAliceMessagingPreferences() { 
    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 cn = new BasicAttribute("cn"); 
      Attribute objclass = new BasicAttribute("objectClass"); 
 
      //putting attribute values 
      cn.add("Alice"); 
      objclass.add("person"); 
 
      //Instantiate an Attributes object and put search attributes in it 
      Attributes attrs = new BasicAttributes(true); 
      attrs.put(cn); 
      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(); 
  
          //--------------------------------------------- 
          //Step9: Setting a new search context 
          //--------------------------------------------- 
          searchContext = entryRDN + "," + searchContext; 
 
          //--------------------------------------------- 
          //Step10: Creating search controls 
          //--------------------------------------------- 
          SearchControls ctls = new SearchControls(); 
          ctls.setReturningObjFlag(true); 
          ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
 
          //--------------------------------------------- 
          //Step11: Creating filter 
          //--------------------------------------------- 
          String filter = "(|(javaClassName=MessagingPreferences) 
                    (javaClassName=ShippingPreferences))"; 
 
          //------------------------------------------ 
          //Step12: Executing search 
          //------------------------------------------ 
          NamingEnumeration ne1 = ctx.search(searchContext, filter, ctls); 
       
          if (ne != null) 
          { 
            //Step13: Iterating through SearchResults 
            while (ne1.hasMore()) { 
              //Step14: Getting individual SearchResult object 
              SearchResult sr1 = (SearchResult) ne1.next(); 
    
              //Step15: Getting preferences object 
              Object obj = sr1.getObject(); 
 
              if (obj != null) { 
                if (obj instanceof MessagingPreferences){ 
                  MessagingPreferences pref = 
                   (MessagingPreferences) obj; 
                } 
                else if (obj instanceof ShippingPreferences) { 
                 ShippingPreferences pref = (ShippingPreferences) obj; 
                } 
              } 
            }//while(  
          }//if 
        }//while 
      }//if (ne != null) 
    } catch (Exception e) { 
      System.out.println("Operation failed: " + e); 
    } 
  } 
 
  public static void main(String[] args) { 
    FetchAliceMessagingPreferences searchAlicePreferences = 
       new FetchAliceMessagingPreferences(); 
  } 
 }    

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:Apache 目录 服务器

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