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

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

 2010-04-19 00:00:00 来源:WEB开发网   
核心提示: 清单 11. LDAP4JavaObjectspublicclassLDAP4JavaObjects{protectedStringcommonName=null;protectedStringsurName=null;protectedStringuserID=null;protectedSt

清单 11. LDAP4JavaObjects

public class LDAP4JavaObjects { 
 
  protected String commonName = null; 
  protected String surName = null; 
  protected String userID = null; 
  protected String javaObjectCN = null; 
  protected String userName = null; 
  protected String password = null; 
  protected String initialContext = null; 
  private String workingContext = null;   
  protected DirContext dirContext = null; 
  protected Properties properties = null; 
  protected Object javaObject = null; 
  protected Attributes attributes = null; 
 
  public LDAP4JavaObjects() { 
    properties = new Properties(); 
    attributes = new BasicAttributes(true); 
 
    workingContext = "ou=users"; 
    initialContext = workingContext; 
     
    try { 
      InputStream inputStream = new FileInputStream( "ApacheDS.properties"); 
      properties.load(inputStream); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  }//LDAPJavaObjects 
   
  public void setUserName(String userName){ 
    properties.setProperty("java.naming.security.principal", userName); 
    this.userName = userName; 
  } 
 
  public void setPassword(String password) { 
    properties.setProperty("java.naming.security.credentials", password); 
    this.password = password; 
  } 
 
  protected void connect() { 
    try { 
      // Fetch the directory context. 
      dirContext= new InitialDirContext(properties); 
    } catch (NamingException e) { 
      System.out.println("Getting initial context operation failed: " + e); 
    } 
  } 
   
  protected void close() { 
    try { 
      // Close the directory context. 
      dirContext.close(); 
    } catch (NamingException e) { 
      System.out.println("Closing initial context operation failed: " + e); 
    } 
  } 
   
  public void setJavaObject(Object obj) throws java.io.NotSerializableException { 
    if (obj instanceof java.io.Serializable) 
      javaObject = obj; 
  } 
 
  protected boolean isObjectPresent(String uid, String cn, String workContext) { 
    NamingEnumeration ne = search(uid, cn, workContext); 
    if (ne != null) 
      return true; 
    else 
      return false; 
  }//isObjectPresent 
 
  protected NamingEnumeration search(String user, String object, String workContext) { 
 
    NamingEnumeration ne = null; 
    String filter = new String(); 
    SearchControls ctls = new SearchControls(); 
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
     
    try { 
      if (user != null && object == null) { 
        addUserAttribute ("uid", user); 
        addUserAttribute ("objectClass", "person"); 
        ne = dirContext.search(workContext, attributes); 
        if (ne != null && ne.hasMore()) 
          return ne; 
        else 
          return null; 
      }else if (user == null && object != null) 
      { 
        filter = "(cn="+object+")"; 
        ctls.setReturningObjFlag(true); 
        ne = dirContext.search(workContext, filter, ctls); 
 
        if (ne != null && ne.hasMore()) 
          return ne; 
        else 
          return null; 
      } 
    } catch (NamingException e) { 
      e.printStackTrace(); 
    } 
    return null; 
  }//search 
   
  protected void update(String uid, String cn, String workContext) { 
    if (uid != null && cn == null) 
    { 
      String[] objClassValues = {"top","person"}; 
      addUserAttribute ("uid", uid); 
 
      if (commonName != null) 
        addUserAttribute ("cn", commonName); 
 
      addUserAttributes ("objectClass", objClassValues); 
 
      try { 
        dirContext.rebind(workContext, null, attributes); 
      } catch (NamingException e) { 
        System.out.println("Storing operation failed: " + e); 
      } 
    } else if (uid == null && cn != null) { 
      addUserAttribute ("cn", cn); 
       
      try { 
        dirContext.rebind(workContext, javaObject, attributes); 
      } catch (NamingException e) { 
        e.printStackTrace(); 
      } 
    } 
  }//update 
   
   
  protected void store(String uid, String cn, String workContext) { 
    if (uid != null && cn == null) 
    { 
      String[] objClassValues = {"top","person"}; 
      addUserAttribute ("uid", uid); 
 
      if (commonName != null) 
        addUserAttribute ("cn", commonName); 
 
      addUserAttributes ("objectClass", objClassValues); 
      try  
      { 
        dirContext.bind(workContext, null, attributes); 
      } catch (NamingException e) { 
        e.printStackTrace(); 
      } 
    } else if (uid == null && cn != null) { 
      addUserAttribute ("cn", cn);   
      try { 
        dirContext.bind(workContext, javaObject, attributes); 
      } catch (NamingException e) { 
        e.printStackTrace(); 
      } 
    } 
  }//store 
 
   
  public void addUserAttribute (String name, String value){ 
    Attribute attr = new BasicAttribute(name); 
    attr.add(value); 
    attributes.put(attr); 
  }//addUserAttribute 
 
 
  public void addUserAttributes(String name, String[] value) { 
    Attribute attr = new BasicAttribute (name); 
    if (value != null) { 
      for (int i =0; i < value.length; i++) 
        attr.add(value[i]); 
    } 
    attributes.put(attr); 
  }//addUserAttribute 
   
  public void writeToServer () { 
    connect(); 
 
    if (userID == null && commonName != null) 
      userID = commonName; 
 
    //Check if the cn of the Java object is set. 
    //If not, use commonName as object cn. 
    if (javaObject != null) 
    { 
      if (javaObjectCN == null && commonName != null)   
        javaObjectCN = commonName; 
    } 
 
    if (!(isObjectPresent(userID, null, initialContext))) 
    { 
     workingContext = "uid="+userID+","+initialContext; 
     store(userID, null, workingContext); 
     workingContext = "cn="+javaObjectCN +",uid="+userID+","+initialContext; 
     store( null, javaObjectCN, workingContext); 
    } else if (isObjectPresent(null, javaObjectCN, 
     "uid="+userID+","+initialContext)) { 
     workingContext = "cn="+javaObjectCN +",uid="+userID +","+ initialContext; 
     update(null, javaObjectCN, workingContext); 
    } else { 
     workingContext = "cn="+javaObjectCN +",uid="+userID +","+ initialContext; 
     store(null, javaObjectCN, workingContext); 
    } 
    close(); 
  }//writeToServer() 
 
 
  public void searchFromServer(String user, String object) { 
    connect(); 
    NamingEnumeration ne = search(user, object, initialContext); 
 
    if (ne != null) 
      processSearchResult(ne); 
    else 
      System.out.println ("searchFromServer... failed"); 
    close(); 
  }//searchFromServer 
 
  private void processSearchResult (NamingEnumeration ne){ 
    try { 
      if (ne!=null) 
      {    
        while (ne.hasMore()) { 
          SearchResult sr = (SearchResult) ne.next(); 
          Object obj = sr.getObject(); 
 
          if (obj != null) { 
            javaObject = obj; 
            javaObjectCN = sr.getName(); 
          } else { 
            userID = sr.getName(); 
            processAttributes(sr.getAttributes()); 
          }   
        }//while 
      }//if (ne != null) 
    } catch (javax.naming.NamingException e) { 
      e.printStackTrace(); 
    } 
  }//processSearchResult 
 
  private void processAttributes(Attributes attrs){ 
    try { 
      for (Enumeration e = attrs.getAll() ; e.hasMoreElements() ;) { 
        Attribute attr = (Attribute) e.nextElement(); 
        if (attr.getID().equals("cn")) 
          commonName = (String)attr.get(); 
        else if (attr.getID().equals("sn")) 
          surName = (String)attr.get(); 
      } 
    } catch (javax.naming.NamingException ne){ 
      ne.printStackTrace(); 
    } 
  }//processAttributes 
 
  public void setUserID(String userID) { 
    this.userID = userID; 
  } 
 
  public String getUserID() { 
    return userID; 
  } 
 
  public void setJavaObjectCN(String objectCN) { 
    this.javaObjectCN = objectCN; 
  } 
 
  public String getJavaObjectCN() { 
    return javaObjectCN; 
  } 
 
  public void setCommonName (String cn) { 
    commonName = cn; 
  } 
 
  public void setSurName (String sn) { 
    surName = sn; 
  } 
 
  public String getCommonName() { 
    return commonName; 
  } 
 
  public String getSurName() { 
    return surName; 
  } 
   
  public static void main(String[] args) { 
    LDAP4JavaObjects javaLdapClient = new LDAP4JavaObjects(); 
    javaLdapClient.setPassword("secret"); 
    javaLdapClient.setUserID("Alice"); 
 
    //Instantiating a Java object. 
    MessagingPreferences msgPreferences = 
      new MessagingPreferences (); 
    
    //Setting a Java object. 
    try { 
      javaLdapClient.setJavaObject (msgPreferences); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
           
    javaLdapClient.setJavaObjectCN ("preferences"); 
    javaLdapClient.writeToServer(); 
    System.out.println ("Entry stored in ApacheDS..");     
  }//main 
 }    

上一页  5 6 7 8 9 10 

Tags:Apache 目录 服务器

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接