?? ejbldapbean.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 + -