Accessing LDAP Connections

If you create a property named ldapr of type LDAP Connection Resource Template, TIBCO Business Studio adds the following to the abstract implementation class:
import org.osoa.sca.annotations.Property;
import javax.naming.ldap.LdapContext;

private LdapContext ldapr;

	@Property(name = "ldapr")
	public void setLdapr(LdapContext ldapr) {
		this.ldapr = ldapr;
	}

	public LdapContext getLdapr() {
		return ldapr;
	}

Procedure

  1. To update the resource:
    ...
    	Attributes attr = new BasicAttributes(true);
    	Attribute objFact = new BasicAttribute("objectclass");
    	objFact.add("top");
    	objFact.add("person");
    	objFact.add("uidObject");
    	objFact.add("organizationalPerson");
    
    	attr.put(objFact);
    	attr.put("uid", uid);
    	attr.put("cn", commonName);
    	attr.put("sn", surname);
    	attr.put("userPassword", password);
    	Name name = new LdapName("uid=" + uid + ",ou=People,dc=tibco,dc=com");
    	getLdapr().createSubcontext(name, attr);
    	...
    	public void destroy() {
    		try {
    			getLdapContext().close();
    		} catch (NamingException e) {
    			e.printStackTrace();
    		}
    	}
  2. To query the resource:
    ...
    	StringBuffer sb = new StringBuffer(); 
    	NamingEnumeration<SearchResult> results = null;
    	try {
    		SearchControls controls = new SearchControls();
    		controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    		MessageFormat format = new MessageFormat("(&(uid={0})(objectclass=*))");
    		String format2 = format.format(new Object[] { uid });
    		results = getLdapr().search("uid=" + uid+ ",ou=People,dc=tibco,dc=com", 
                format2,controls);
    
    		while (results.hasMore()) {
    			SearchResult searchResult = (SearchResult) results.next();
    			Attributes attributes = searchResult.getAttributes();
    			NamingEnumeration<? extends Attribute> enumeration = attributes.getAll();
    			for (; enumeration.hasMoreElements();) {
    				Attribute object = (Attribute) enumeration.next();
    				sb.append(object.toString());
    				sb.append('\n');
    				if (logger.isInfoEnabled()) {
    					logger.info(object.toString() );
    				}
    			}
    		}
    			
    	} catch (NameNotFoundException e) {
    	 	...			
    	} catch (NamingException e) {
    	 	...
    	}
    
    	return sb.toString();
    	...