How to Manage Internet Directories Using Beans for Novell Services
Articles and Tips: article
Senior Software Engineer
Novell, Inc. nrathna@novell.com
01 Jan 2002
Beans for Novell Services provide easy-to-use Java components for rapid and effective development of network applications. This AppNote discusses how to manage Internet directories using Beans for Novell Services.
- Introduction
- Novell Internet Directory User Group Bean
- Novell Internet Directory Query Bean
- Novell Internet Directory Entries Bean
- Novell Internet Directory Selector Bean
- Conclusion
Topics |
Beans for Novell Services, Java Beans, eDirectory |
Products |
Beans for Novell Services, eDirectory |
Audience |
administrators, developers |
Level |
intermediate |
Prerequisite Skills |
familiarity with LDAP, eDirectory (formerly known as NDS), and Beans for Novell Services |
Operating System |
NetWare 5 and above, Windows 95/98/NT/2K, UNIX, Solaris, Linux |
Tools |
JDK 1.7.X or higher, Novell Java LDAP SDK |
Sample Code |
yes |
Introduction
Before we get in to how to manage Internet directories using Beans for Novell Services let's go over a few terms.
RAD |
Rapid Application Development |
API |
Application Programming Interface |
NDS |
Novell Directory Services is now known as eDirectory, we will use eDirectory through out this article whenever reference is made to NDS. |
NDK |
Novell Developer Kit |
Directory |
A directory consists of entries which contain descriptive information of network resources like people, printers, fax machines, servers, etc. |
LDAP |
Lightweight Directory Access Protocol |
Novell eDirectory was used on NetWare to keep track of network entries like users, printers, and servers. This was done through Novell's proprietary protocol NDAP which accesses and manages eDirectory. Once LDAP was defined by IETF, Novell added it as a second access mechanism to eDirectory, which serves as a Gateway.
LDAP defines a light weight access mechanism, where clients send requests to and receive responses from LDAP servers. It is an emerging Internet standard for accessing directory information and allowing LDAP-enabled applications to access multiple directories.
To write the directory enabled applications you will need an LDAP SDK. Novell's LDAP client SDK can be downloaded from Novell Developer Net at http://developer.novell.com/ndk. The following are five different SDKs that can be used while writing LDAP client applications:
LDAP Libraries for C
LDAP Libraries for Java
LDAP Service Provider for JNDI
LDAP ActiveX Controls
LDAP Java Beans
LDAP Java Beans are part of Beans for Novell Services. They provide easy-to-use Java components for rapid and effective development of network applications. They have several components, which allow RAD programmers to easily write applications to do many operations on directories with out knowledge of internal directory APIs. It reduces programming time and the complexities of learning APIs.
As we know a Directory is a collection of entries, generally arranged in a hierarchical way. Directory Information Tree (DIT) Entries that contain other entries are called Containers. To be consistent throughout this article, we will use the tree shown in Figure 1 as our example.
Example tree.
Our example company, XX-SoftInc is divided into different departments: Accounts, Admin, Engineering, and Support each with a different number of employees and groups.
We will use this tree as our example as we explain the sample code written using Novell LDAP Beans and will focus on only the salient features. For more information refer to the help documentation provided with Beans for Novell Services.
Novell Internet Directory User Group Bean
The NWIDirUsrGrp bean is packaged in NWIDirUsrGrp.jar. It is a nonvisual component and can be used to manipulate User and Groups entries in an LDAP Directory. You can also use this component to manage users and groups in a network.
The following table lists the properties and methods of its Objects.
NWIDirUsrGrpBean |
void setFullName()NWGroup findGroup()NWUser findUser()NWGroups getGroups()NWUsers getUsers() |
NWUsers |
NWUser getElement()NWUser addElement()void removeElement() |
NWUser |
void setFieldValue()void addFieldValue()void removeFieldValue()NWNetworkNames getGroupMembership()void setUserPassword() |
NWGroups |
NWGroup getElement()NWGroup addElement()void removeElement() |
NWGroup |
NWNetworkNames getGroupMembers() void setFieldValue()void addFieldValue()void removeFieldValue() |
Retrieve Users
The following program can be used to retrieve Users in the admin department.
1. //Import Section 2. import java.lang.*; 3. import com.novell.beans.NWIDirUsrGrp.*; 4. import com.novell.beans.idirutil.*; 5. import com.novell.ldap.*; 6. public class GetUsersGroups{ 7. public static void main(String a[]) throws NWBeanException{ 8. NWIDirUsrGrp usrGrp=new NWIDirUsrGrp(); //Instance of Bean 9. usrGrp.setFullName("ldap://XX-SoftInc.com/ o=XX-SoftInc/ou=admin"); 10. try{ 11. usrGrp.authenticate(); //Authenticating to server 12. NWUsers users= usrGrp.getUsers(); //Get Users collection 13. while(users.hasMoreElements()){ 14. NWUser user=users.next(); 15. System.out.println(user.getFullName()); 16. }//Users While End 17. }// Try end 18. catch(Exception e){ 19. System.out.println("From the Exception handler"); 20. e.getMessage(); 21. }//catch block end 22. }//Main End 23. }//Class end
Line 2-5 imports required classes in to the application.
Line 8 instantiates the Bean.
Line 9 sets the FullName when the Users are retrieved.
Line 11 authenticates to the server through anonymous bind. Through anonymous bind you can only view the users. If you want to modify something you will need sufficient rights and must authenticate through the Simple bind. You can use setUserName() and setPassoword() methods to set UserName and password before calling UsrGrp.Authenticate().
Line 12 gets the collection of NWUsers Objects.
Line 13-14 are standard enumeration methods used to iterate through the collection.
The out put of the program will be ldap://XX-SoftInc.com/o=XX-SoftInc/ou=admin/cn= Mary Thomson ldap://XX-SoftInc.com/o=XX-SoftInc/ou=admin/cn= Johnson ldap://XX-SoftInc.com/o=XX-SoftInc/ou=admin/cn= Jones
Retrieve Groups
In order to retrieve the groups under the engineering department we only have to modify the above code in a few places.
In line 9 set the FullName to "ldap://XX-SoftInc.com/o=XX-SoftInc/ou=engineering".
Replace line 12-16 with the following code:
NWGroups groups=usrGroup.getgroups(); //get groups Collection while(groups.hasMoreElements()){ NWGroup group =groups.next(); System.out.println(group.getFullName()); }//while end
Out put of the above code will be ldap://XX-SoftInc.com/o=XX-SoftInc/ou=engineering/cn=EngGroup ldap://XX-SoftInc.com/o=XX-SoftInc/ou=engineering/cn=WritersGroup ldap://XX-SoftInc.com/o=XX-SoftInc/ou=engineering/cn=ManagersGroup
Add User
To add a user to the admin departmentReplace line from 12-16 with the following code:
NWUser user,AddedUser; user=new NWUser("ldap://XX-SoftInc.com/o=XX-SoftInc/ou=admin/cn=Joseph"); user.setLastName("Rahul"); AddedUser=users.addElement(user); System.out.println("Getting User After Addition"); user=users.getElement("ldap://XX-SoftInc.com/o=XX-SoftInc/ou=admin/cn=Joseph"); System.out.println(user.getFullName());
To add a user to the directory:
-
Create the instance of the new user
-
Set some mandatory values for the user (LastName).
-
Get the existing collection.
-
Add to the existing collection by calling addElement() method of NWUsers. You can get the Added User by calling getElement() of NWUsers.
It is simple to retrieve users, groups, and add users to directory. We can use this bean to add groups, modify attributes of user and groups using getFieldValue(), SetFieldValue(), AddFieldValue() of NWUser and NWGroup. Use setUserFilter(), setGroupFilter() of the bean to get the particular User or Group. Use setSearchScope(), setSearchMode() to specify where and how you want to search. There are many more useful properties and methods for more information refer to the Java documentation provided with these beans.
Novell Internet Directory Query Bean
The NWIDirQuery bean is a nonvisual bean that can be used to query LDAP directories to get entries. This bean can only be used for retrieval operations.
NWIDirQuery is the main class of the bean providing the functionality of searching the directory for entries satisfying the specified filter criteria. You can also specify the fields to be searched. The bean returns the search results in a structured form, which can be used to populate the list box; or they can also be provided as an input to the Internet Directory Entries Bean for populating the NWEntries collection.
The table lists the properties and methods of its Objects.
NWIDirQuery |
void setFields()void setFilter ()void setMaximumResults()void setSearchScope()void setSortKeys()NWQueryResult[] Search() void addNWIDirQueryListener()void removeNWIDirQueryListener()NWQueryResult Find()NWQueryResult[] FindNext()NWQueryResult[] FindPrev() |
The following program can be used to retrieve all the entries in XX-SoftInc.
1. import com.novell.ldap.*; 2. import com.novell.beans.idirutil.*; 3. import com.novell.beans.NWIDirQuery.*; 4. public class QuerySearch implements NWIDirQueryListener{ 5. public static void main(String a[]){ 6. QuerySearch QSearch = new QuerySearch(); // Instance of the class 7. NWIDirQuery Query=new NWIDirQuery(); // Instance of the bean 8. try{ 9. Query.addNWIDirQueryListener(QSearch); 10. Query.setFullName("ldap://XX-SoftInc.com/o=XX-SoftInc "); 11. Query.authenticate(); 12. Query.setSearchScope(2); 13. Query.Search(); 14. } 15. catch(Exception e){ 16. System.out.println(e.getMessage()); 17. } 18. } //End of Main 19. public void SearchCompleted(NWIDirQueryEvent QueryEvent){ 20. try{ 21. NWQueryResult[ ] qResult = QueryEvent.getResults(); 22. for(int i=0; i<qResult.length; i++){ 23. System.out.println(" "+qResult[i].getShortName()); 24. } 25. catch(Exception e){ 26. System.out.println(e.getMessage()); 27. } 28. }// End of Search Completed 29. }//End of class
Line 1-3 imports required classes in to the application.
Line 6-7 instantiates the Class and Bean.
Line 9 registers the listener to the application, SearchCompleted event will be thrown after completion of search operation.
Line 10 sets the FullName where entries are needed to be retrieved.
Line 11 authenticates to the server through anonymous bind.
Line 12 sets the search scope to subtree.
Line 19 implements the searchCompleted event of NWIDirQuery interface.
Line 21 gets the result of search operation after completion of search.
Line 22-25 displays the entries FullName and ShortName.
The output of the above program is
cn= Mary Thomson cn= Johnson cn= Jones cn=EngGroup cn=WritesGroup cn=ManagersGroup
Use this bean to query for entries under different contexts, by setting different properties like Search Scope, Search Mode, Maximum Results, Sort Keys, Filters, Fields, etc. Applications that involve only displaying information to user for viewing, can be written using this bean.
For detailed explanations about these properties and methods please refer to the Java documention provided with this bean.
Novell Internet Directory Entries Bean
The NWIDirEntries bean is packaged in NWIDirEntries.jar. You can set the fullName of the server and fetch all the entries in that context, with anonymous authentication. You can also update any entry, add a new entry, delete an existing entry, rename an entry, and move an entry to a different tree location in the directory, with user authentication. Use this bean to update entries that are found by using NWIDirQuery Bean.
The following table lists the properties and methods of its Objects.
NWIDirEntries |
void setFields() void setMaxResults() NWEntries getEntries() void addNWIDirEntriesListener() void removeNWIDirEntriesListener() |
NWEntries |
NWEntry addElement() NWEntry moveElement() NWEntry renameElement() void removeElement() |
NWEntry |
java.lang.String getLayout() java.lang.Object getFieldValue() void setFieldValue() void addFieldValue() void removeFieldValue() Boolean compareFieldValue() |
The following program will update the telephone number of an entry. Entries collection will be constructed from NWQueryResults of NWIDirQuery Bean, setFieldValue() method in NWEntry is used to set the telephone number with new value.
1. import com.novell.ldap.*; 2. import com.novell.beans.idirutil.*; 3. import com.novell.beans.NWIDirQuery.*; 4. import com.novell.beans.nwidirentries.*; 5. public class QueryEntries{ 6. public static void main (String args[]) throws Exception 7. { 8. NWIDirQuery Query = new NWIDirQuery(); 9. Query.setFullName("ldap://XX-SoftInc.com/o=XX-SoftInc "); 10. // Anonymous authentication. 11. query.authenticate(); 12. String[] fields = {"telephoneNumber" , "cn" , "sn" }; 13. query.setFields( fields ); 14. NWQueryResults Results = query.Search(); 15. //Unauthenticate 16. query.unauthenticate(); 17. //Instance of NWIDirEntries 18. NWIDirEntries IDirEnt = new NWIDirEntries(); 19. NWEntries entries = IDirEnt.getEntries(Results, "ldap://XX-SoftInc.com/o=XX-SoftInc ", Query.getFields() ); 20. //Proper Authentication is required for Modification operation 21. IDirEnt.setUserName("ldap://XX-SoftInc.com/o=XX-SoftInc/ cn=admin "); 22. IDirEnt.setPassword("admin_password"); 23. IDirEnt.authenticate(); 24. NWEntry entry = entries.getElement("cn=mary"); 25. // set the field values with required values.... 26. String[] telephoneNumber = {"2322332", "2365567"}; 27. entry.setFieldValue("telephoneNumber", telephoneNumber); 28. entry.update(); 29. IDirEnt.unauthenticate(); 30. }//main 31. }//Class
Line 19 NWEntries are constructed from NWIDirQuery's NWQueryResults.
Line 23 NWIDirEntries Authenticates to the server with proper credentials. Modification of entries attribute values requires sufficient rights.
Lines 24-29 modify the telephone number entry of the user.
Line 28 saves the changes to the directory.
This bean has many other properties and methods which can be set with the appropriate values and used to write an application for management of all entries present in the LDAP aware Internet directories. You can write a full-fledged application for entries management in a directory using this bean.
Novell Internet Directory Selector Bean
The NWIDirSelect Bean is a visual bean that allows you to select a network object in LDAP directories.
When you initialize the bean object by setting the full name of the context and calling the show() method, the Novell Network Selector dialog box is displayed. From the dialog box, you can select a network object and also get its full name. It also allows context menus to be added for the objects. You can specify the menu items to be listed for objects corresponding to each layout type. To display the menu items select the object and right-click in the dialog box. You can also select the required menu item.
The following table lists the properties and methods of its Objects.
NWIDirSelect |
void addNWIDirSelectListener()NWIDirContextMenus getContextMenus() java.lang.String getCurrentContext() java.lang.String getNameFilter() java.lang.String[] getTypeFilter() void mouseClicked / mouseEntered / mouseExited / mousePressed / mouseReleased()void removeNWIDirSelectListener(.)void setCurrentContext()void setDialogTitle()void setNameFilter()void setTypeFilter() Boolean show() |
The following program shows how the dialog is displayed.
1. import java.lang.*; 2. import java.io.*; 3. import com.novell.beans.nwidirselect.*; 4. public class FullName{ 5. public static void main(String[] x){ 6. try{ 7. NWIDirSelect sel = new NWIDirSelect(); // instance of the bean Sel.setFullName("ldap://blr-dileep/o=novell"); 8. sel.show(); //Displays the dialog 9. } 10. catch(Exception e){ 11. System.out.println(e.getMessage()); 12. } 13. }
The dialog display.
The type of entries to be displayed can be specified in the Type Filter property. The Name Filter property can be specified to display the entries starting with specified name.
You can write full-fledged applications by using the appropriate combination of other Novell LDAP Beans.
Conclusion
Beans for Novell Services provide easy-to-use Java components for rapid and effective development of network applications. These LDAP Java Beans are in Early Access as le124, le156. A few of them are expected to be available on the NDK by February 2002.
Look at the readme for Dependencies setup information for last minute updates. Post your queries on Beans for Novell Services to the News Group at http://developer-forums.novell.com/category/index.tpt#ad.
Important URLs
* Originally published in Novell AppNotes
Disclaimer
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.