KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > ldap > LDAPAddressBook


1 package net.suberic.pooka.ldap;
2 import net.suberic.pooka.*;
3 import net.suberic.util.VariableBundle;
4
5 import java.util.Hashtable JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.LinkedList JavaDoc;
8
9 import javax.naming.*;
10 import javax.naming.directory.*;
11
12
13 /**
14  * An Address Book that is accessed through LDAP.
15  */

16 public class LDAPAddressBook implements AddressBook, AddressMatcher {
17
18   String JavaDoc addressBookID;
19
20   InitialDirContext initialContext;
21   Hashtable JavaDoc env;
22
23   String JavaDoc ldapUrl;
24   String JavaDoc username;
25   String JavaDoc password;
26
27   /**
28    * Creates a new LDAPAddressBook.
29    */

30   public LDAPAddressBook() {
31
32   }
33
34   /**
35    * Configures the LDAPAddressBook using the given ID.
36    */

37   public void configureAddressBook(String JavaDoc newAddressBookID) {
38     addressBookID = newAddressBookID;
39     ldapUrl = Pooka.getProperty("AddressBook." + addressBookID + ".url", "");
40     username = Pooka.getProperty("AddressBook." + addressBookID + ".username", "");
41     password = Pooka.getProperty("AddressBook." + addressBookID + ".password", "");
42     if (!password.equals(""))
43       password = net.suberic.util.gui.propedit.PasswordEditorPane.descrambleString(password);
44
45     // make the connection.
46

47     env = new Hashtable JavaDoc(5, 0.75f);
48     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
49
50     /* Specify host and port to use for directory service */
51     env.put(Context.PROVIDER_URL, ldapUrl);
52
53     try {
54       /* get a handle to an Initial DirContext */
55       initialContext = new InitialDirContext(env);
56     } catch (NamingException ne) {
57       Pooka.getUIFactory().showError(Pooka.getProperty("error.AddressBook.ldap.gettingInitialContext", "Failed to connect to LDAP server: ") + ne.getMessage());
58     }
59   }
60
61   // AddressMatcher
62

63   /**
64    * Returns all of the AddressBookEntries which match the given String.
65    */

66   public AddressBookEntry[] match(String JavaDoc matchString) {
67     // create the search string.
68
String JavaDoc searchString = "(sn=" + matchString + ")";
69
70     try {
71       return doSearch(searchString);
72     } catch (NamingException ne) {
73       ne.printStackTrace();
74
75       return new AddressBookEntry[0];
76     }
77   }
78
79   /**
80    * Returns all of the AddressBookEntries which match the given String.
81    */

82   public AddressBookEntry[] matchExactly(String JavaDoc matchString) {
83     return match(matchString);
84   }
85
86   /**
87    * Returns all of the AddressBookEntries whose FirstName matches the given
88    * String.
89    */

90   public AddressBookEntry[] matchFirstName(String JavaDoc matchString) {
91     // create the search string.
92
String JavaDoc searchString = "(sn=" + matchString + ")";
93
94     try {
95       return doSearch(searchString);
96     } catch (NamingException ne) {
97       ne.printStackTrace();
98
99       return new AddressBookEntry[0];
100     }
101   }
102
103   /**
104    * Returns all of the AddressBookEntries whose LastName matches the given
105    * String.
106    */

107   public AddressBookEntry[] matchLastName(String JavaDoc matchString) {
108     // create the search string.
109
String JavaDoc searchString = "(sn=" + matchString + ")";
110
111     try {
112       return doSearch(searchString);
113     } catch (NamingException ne) {
114       ne.printStackTrace();
115
116       return new AddressBookEntry[0];
117     }
118   }
119
120   /**
121    * Returns all of the AddressBookEntries whose email addresses match the
122    * given String.
123    */

124   public AddressBookEntry[] matchEmailAddress(String JavaDoc matchString) {
125     // create the search string.
126
String JavaDoc searchString = "(sn=" + matchString + ")";
127
128     try {
129       return doSearch(searchString);
130     } catch (NamingException ne) {
131       ne.printStackTrace();
132
133       return new AddressBookEntry[0];
134     }
135   }
136
137   /**
138    * Returns the AddressBookEntry which follows the given String alphabetically.
139    */

140   public AddressBookEntry getNextMatch(String JavaDoc matchString) {
141     return null;
142   }
143
144   /**
145    * Returns the AddressBookEntry which precedes the given String
146    * alphabetically.
147    */

148   public AddressBookEntry getPreviousMatch(String JavaDoc matchString) {
149     return null;
150   }
151
152   // AddressBook
153

154   /**
155    * Gets and appropriate AddressMatcher.
156    */

157   public AddressMatcher getAddressMatcher() {
158     return this;
159   }
160
161   /**
162    * Adds an AddressBookEntry to the AddressBook.
163    */

164   public void addAddress(AddressBookEntry newEntry) {
165
166   }
167
168   /**
169    * Removes an AddressBookEntry from the AddressBook.
170    */

171   public void removeAddress(AddressBookEntry removeEntry) {
172
173   }
174
175   /**
176    * Gets the ID for this address book.
177    */

178   public String JavaDoc getAddressBookID() {
179     return addressBookID;
180   }
181
182   /**
183    * Loads the AddressBook.
184    */

185   public void loadAddressBook() throws java.io.IOException JavaDoc, java.text.ParseException JavaDoc {
186
187   }
188
189   /**
190    * Saves the AddressBook.
191    */

192   public void saveAddressBook() throws java.io.IOException JavaDoc {
193
194   }
195
196   public AddressBookEntry[] doSearch(String JavaDoc searchString) throws NamingException {
197
198     /* specify search constraints to search subtree */
199     SearchControls constraints = new SearchControls();
200     constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
201
202     NamingEnumeration results
203       = initialContext.search("o=Ace Industry, c=US", "(sn=" + searchString + ")", constraints);
204
205     LinkedList JavaDoc resultList = new LinkedList JavaDoc();
206
207     while (results != null && results.hasMore()) {
208       SearchResult si = (SearchResult)results.next();
209
210       Attributes att = si.getAttributes();
211
212       LDAPAddressEntry entry = new LDAPAddressEntry(att);
213
214       resultList.add(entry);
215     }
216
217     AddressBookEntry[] returnValue = new AddressBookEntry[resultList.size()];
218
219     resultList.toArray(returnValue);
220
221     return returnValue;
222
223   }
224
225   /**
226    * Creates a new, empty AddressBookEntry.
227    */

228   public AddressBookEntry newAddressBookEntry() {
229     return new LDAPAddressEntry(null);
230   }
231
232 }
233
Popular Tags