⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ejbldapbean.java

📁 EJB应用开发的源码,是学习EJB的好帮助!EJB应用开发的源码,是学习EJB的好帮助
💻 JAVA
字号:
/* * LdapEjbbean.java */package HelloWorldLdap;import java.util.*;import javax.ejb.*;import java.rmi.Remote;import java.rmi.RemoteException;import java.io.*;import netscape.ldap.*;public class EjbLdapbean implements SessionBean {  LDAPConnection ld=null;  File logfile ;  FileOutputStream fos ;  DataOutputStream dos ;  SessionContext sc;  public void ejbCreate() throws RemoteException{    try{      logfile = new File("ldaplog1.txt");      fos = new FileOutputStream(logfile);      dos = new DataOutputStream(fos);    }catch(Exception e){System.out.println(e.toString());}  }  public void setSessionContext(SessionContext sc)throws RemoteException {    this.sc = sc;  }    public void ejbRemove() throws RemoteException {}    public void ejbActivate() throws RemoteException{}    public void ejbPassivate() throws RemoteException{}    public EjbLdapbean() throws RemoteException{  }    public boolean getConnected(String host, int port) throws RemoteException{    if (ld!=null){      writelog("trying to connect while already connected");      return true;    }//if    else{      writelog("trying to connect to "+host + " ...");      try{	ld=new LDAPConnection();	ld.connect(host,port);	writelog("Got Connected...");      }catch(Exception e){	ld=null;	writelog("exception in getConnected");	writelog(e.toString());	return false;      }      return true;    }//else  }//get connected host port      public Hashtable getHashedEntry(String uid ,String basedn) throws RemoteException{        Hashtable hashedEntry=new Hashtable();    try{      LDAPAttributeSet entryAttrs = this.getEntry(uid,basedn).getAttributeSet();      writelog("there are "+ entryAttrs.size()+" attributes for this entry");      Enumeration enumAttrs = entryAttrs.getAttributes();      while (enumAttrs.hasMoreElements()){	LDAPAttribute attribute=(LDAPAttribute)enumAttrs.nextElement();	String attrKey=attribute.getName();	String[] attrVals=attribute.getStringValueArray();	hashedEntry.put(attrKey,attrVals);      }//while hasmoreelements    }catch(Exception e){      writelog("exception in gethashed entry.");      writelog(e.toString());}    return hashedEntry;  }//getHashedEntry    public LDAPEntry getEntry(String uid, String basedn) throws RemoteException{    LDAPEntry entry=new LDAPEntry();    LDAPSearchResults results=null;        if (uid.indexOf("*")!=-1){      writelog("No wild cards accepted");      return entry;    }//if        String FILTERBASE="mail = "+uid;    try{      if (ld==null){	writelog("Not Connected to an LDAP server");	return entry;      }//if      writelog("Searching for " +uid +" with this dn" +basedn);      results=ld.search(basedn,LDAPConnection.SCOPE_SUB,FILTERBASE,null,false);      writelog("there  are "+ results.getCount() +" results returning the first one only");      entry=results.next();    }catch(Exception e){      writelog("exception in getEntry");      writelog(e.toString());}    return entry;  }//get entry    private void  writelog(String message)throws RemoteException{    try{      synchronized(logfile){	dos.writeChars(message);	dos.writeBytes("\r\n");	dos.flush();      }    }catch(Exception e){e.printStackTrace();}  }//writelog}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -