FileDocCategorySizeDatePackage
LdapPersonBean.javaAPI DocExample4875Sun Jul 06 01:05:36 BST 2003antipatterns.ejbs

LdapPersonBean

public class LdapPersonBean extends Object implements EntityBean

Fields Summary
private EntityContext
context
private String
firstName
private String
lastName
private String
phoneNumber
private DirContext
peopleContext
Constructors Summary
Methods Summary
public voidejbActivate()

        getPeopleContext();
    
public java.util.CollectionejbFindByName(java.lang.String firstName, java.lang.String lastName)

        try {
            String ldapString = null;
            
            if (firstName != null  && lastName != null) {
                ldapString = "& (cn=" + firstName + ") (sn=" + lastName + ")";
            } else if (firstName != null) {
                ldapString = "(cn=" + firstName + ")";
            } else if (lastName != null) {
                ldapString = "(sn=" + lastName + ")";
            } else {
                ldapString = "(objectClass=person)";
            }
            
            return ldapFind(ldapString);
        } catch(NamingException ne) {
            throw new FinderException("Error in find: " + ne);
        }
    
public java.lang.StringejbFindByPrimaryKey(java.lang.String key)

        try {
            loadFromLdap(key);
            return (key);
        } catch(NamingException ne) {
            throw new FinderException("Error finding " + key + " : " + ne);
        }
    
public voidejbLoad()

        String key = (String)context.getPrimaryKey();
        try {
            loadFromLdap(key);
        } catch(NamingException ne) {
            throw new EJBException("Error loading " + key, ne);
        }
    
public voidejbPassivate()

        if (peopleContext != null) {
            try {
                peopleContext.close();
            } catch(NamingException ne) {
                throw new EJBException("Error passivating bean: " + ne, ne);
            } finally {
                peopleContext = null;
            }
        }
    
public voidejbRemove()

public voidejbStore()

public java.lang.StringgetFirstName()

        return firstName;
    
public java.lang.StringgetLastName()

        return lastName;
    
private javax.naming.directory.DirContextgetPeopleContext()

   
        if (peopleContext != null)
            return (peopleContext);
        
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost/o=jndiTest");
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager, o=jndiTest");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        
        try {
            DirContext initalContext = new InitialDirContext(env);
            peopleContext = (DirContext)initalContext.lookup("ou=people");
            return peopleContext;
        } catch(NamingException ne) {
            throw new EJBException("Error activating bean: " + ne, ne);
        }
    
public java.lang.StringgetPhoneNumber()

        return phoneNumber;
    
private java.util.CollectionldapFind(java.lang.String filter)

        Collection out = new Vector();
        
        NamingEnumeration people = ldapSearch(filter);
        while(people.hasMore()) {
            NameClassPair personName = (NameClassPair)people.next();
            Attributes personAttrs = 
                peopleContext.getAttributes(personName.getName());
            Attribute cn = personAttrs.get("cn");    
            out.add(cn.get());
         }
         
         return out;
    
private javax.naming.NamingEnumerationldapSearch(java.lang.String filter)

        SearchControls sc = new SearchControls();
        sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
  
        NamingEnumeration people = getPeopleContext().search("", filter, sc);
        return people;
    
private voidloadFromLdap(java.lang.String key)

        NamingEnumeration people = ldapSearch("cn=" + key);
        if (!people.hasMore())
            throw new NamingException("No entries found matching " + key);
        
        NameClassPair personName = (NameClassPair)people.next();
        Attributes personAttrs = 
            peopleContext.getAttributes(personName.getName());
        Attribute cn = personAttrs.get("cn");    
        Attribute sn = personAttrs.get("sn");
        Attribute phone = personAttrs.get("telephoneNumber");
    
        firstName = (String)cn.get();
        lastName = (String)sn.get();
        phoneNumber = (String)phone.get();
    
public voidsetEntityContext(EntityContext aContext)

        context=aContext;
    
public voidunsetEntityContext()

        context=null;