arun wrote:

Hi,

Can i store/retrive  data in LDAP(openLDAP)
using  CMP entity bean? as can be done with
the databases like DB2, oracle etc.

If yes, can any one please provide a
sample example.

Thanks

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".



I don't think so but I do it with a BMP.

/**
* This is an LDAP backed user bean
*
* @ejb.bean
*      name="UserEJB"
*      type="BMP"
*      view-type="local"
*      primkey-field="username"
*      local-jndi-name="local/UserEJB"
*
* @ejb.interface
*      local-extends="javax.ejb.EJBLocalObject, au.com.ucw.cop.bi.User"
*
* @ejb.pk class="java.lang.String"
*
* @ejb.transaction-type
*      type="Container"
*
* @jboss.read-only
*      read-only="true"
*
* @ejb.transaction
*      type="NotSupported"
*
* @ejb.value-object name="UserEJB" match="*"
*/
public abstract class UserBean implements User, EntityBean
{
   /**
    * This is the username used to record actions taken by the system.
    */
   public static final String SYSTEM_USERNAME = "system";

   private EntityContext context;
   private DirContext ldapContext;
   private SearchControls constraints;


private String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory"; private String MY_HOST = "ldap://192.168.1.6/";; private String MY_SEARCHBASE = ",ou=People,dc=ucw,dc=com,dc=au"; private String MY_FILTER = "(objectclass=person)";

   protected String username;
   protected String name;
   protected String phoneNumber;
   protected String email;

   /**
    * Generated bulk accessor.
    *
    * @ejb.interface-method
    */
   public abstract UserEJBData getUserEJBData();

   /**
    * Generated bulk accessor.
    */
   public abstract void setUserEJBData(UserEJBData data);

   /**
    * @ejb.persistent-field
    * @ejb.interface-method
    */
   public String getUsername()
   {
       return username;
   }

   /**
    * @ejb.persistent-field
    * @ejb.interface-method
    */
   public String getName()
   {
       return name;
   }

   /**
    * @ejb.home-method
    */
   public UserEJBLocal ejbHomeGetCurrentUser()
   {
       try {
           return
UserEJBUtil.getLocalHome().findByPrimaryKey(context.getCallerPrincipal().getName());
       } catch (Exception e) {
           Debug.fatal("UserBean.getCurrentUser", e);
           throw new EJBException("FATAL ERROR at
UserBean.getCurrentUser", e);
       }
   }

   public void setPhoneNumber(String phoneNumber)
   {
       this.phoneNumber = phoneNumber;
   }

   public void setName(String name)
   {
       this.name = name;
   }

   public void setUsername(String username)
   {
       this.username = username;
   }

   /**
    * @ejb.persistent-field
    * @ejb.interface-method
    */
   public String getPhoneNumber()
   {
       return phoneNumber;
   }

   /**
    * @ejb.persistent-field
    * @ejb.interface-method
    */
   public String getEmail()
   {
       return email;
   }

   public void setEmail(String email)
   {
       this.email = email;
   }

   public String ejbFindByPrimaryKey(String theUserName) throws
FinderException
   {
       //Debug.debug("UserBean.ejbFindByPrimaryKey");
       boolean result;
       try {
           result = selectByPrimaryKey(theUserName);
       } catch (Exception ex) {
           throw new EJBException("ejbFindByPrimaryKey: " +
ex.getMessage());
       }
       if (result) {
           return theUserName;
       } else {
           throw new ObjectNotFoundException("Row for number " +
theUserName + " not found.");
       }

}

   public void setEntityContext(EntityContext context)
   {
       this.context = context;
       try {
           Hashtable env = new Hashtable();
           env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
           env.put(Context.PROVIDER_URL, MY_HOST);
           ldapContext = new InitialDirContext(env);
           constraints = new SearchControls();
           constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
       } catch (NamingException e) {
           throw new EJBException("Error initializaing ldap context", e);
       }
   }

   public void unsetEntityContext()
   {
       ldapContext = null;
       constraints = null;
   }

   public void ejbActivate()
   {
       username = (String)context.getPrimaryKey();
   }

   public void ejbPassivate()
   {
       username = null;
   }

   public void ejbLoad()
   {
       try {
           loadRow();
       } catch (Exception ex) {
           throw new EJBException("ejbLoad: " + ex.getMessage());
       }
   }

   /*********************** Database Routines *************************/
   private boolean selectByPrimaryKey(String theUserName) throws
SQLException
   {
       //System.out.println("theUserName = " + theUserName);
       try {
           NamingEnumeration results = ldapContext.search("uid=" +
theUserName + MY_SEARCHBASE, MY_FILTER, constraints);

           return (results != null && results.hasMore());
       } catch (NamingException e) {
           Debug.fatal("UserBean.selectByPrimaryKey", e);
           throw new EJBException("FATAL ERROR at
UserBean.selectByPrimaryKey", e);
       }
   }

   private void loadRow() throws SQLException
   {

       try {
           NamingEnumeration searchResults = ldapContext.search("uid="
+ username + MY_SEARCHBASE, MY_FILTER, constraints);

           if (searchResults.hasMore()) {
               SearchResult searchResult =
(SearchResult)searchResults.next();
               Attributes attributes = searchResult.getAttributes();
               this.name = (String)attributes.get("cn").get();
               this.email = (String)attributes.get("mail").get();
               // phone numbers aren't generated by the passwd
migration script
               // so we need to null check here
               Attribute attribute = attributes.get("telephoneNumber");
               if (attribute != null) {
                   this.phoneNumber = (String)attribute.get();
               }
           } else {
               throw new NoSuchEntityException("Entry for user name " +
username + " not found in ldap database.");
           }
       } catch (NamingException e) {
           Debug.fatal("UserBean.loadRow", e);
           throw new EJBException("FATAL ERROR at UserBean.loadRow", e);
       }
   }
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".



Reply via email to