Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 83 84 85 86 87 88 < 89 > 90 91 92 93 94 95 .. 166 >> Next

38
39 public void setDB(String s) {
40
41 System.out.println("INSIDE setDB()...email= " + s);
42 // UPDATE entry in database with email address.
43 }
44 }
Listing 27.3 authenticateHelper
Transactional LDAP-Don't Make that Commitment 241
The ldapAuthenticate routine in Listing 27.4 receives the username and password from the login.jsp and searches the LDAP directory to determine if the user is a valid user that should be authenticated.
001 package org.javapitfalls.item27;
002
003 import java.io.*;
004 import java.net.*;
005 import java.util.*;
006 import java.text.*;
007 import java.util.Date;
008 import javax.naming.*;
009 import javax.naming.directory.*;
010
011 public class ldapAuthenticate {
012
013 String username;
014 String password;
015 String firstname;
016 String lastname;
017 String email;
018 int searchCount;
019
020 public ldapAuthenticate(String u String p)
021 {
022 username = u;
023 password = p;
024 setUsername(u);
025 setPassword(p);
026 }
027
028 public boolean validUser() {
029
030 try {
031 search();
032 }
033 catch(Exception e) {
034 System.out.println("ERROR: " + e.toString());
035 return false;
036 }
037 return true;
038 }
039
040 public boolean search() {
041
Listing 27.4 ldapAuthenticate.java (continued)
242 Item 27
The DirContext class allows our application to search the directory for the username specified in the login script. This operation is followed by the Attribute operation on the DirContext object, which is used for the username lookup.
042 try {
043 DirContext ctx = getDirContext();
044 Attributes matchAttrs = new BasicAttributes(true);
045 matchAttrs.put(new BasicAttribute("uid", username));
046
047 NamingEnumeration result = ctx.search("dc=avey",
048 matchAttrs);
049 int count = 0;
050 while (result.hasMore()) {
051 SearchResult sr = (SearchResult)result.next();
052 System.out.println("RESULT:" + sr.getName());
053 printAttributes(sr.getAttributes());
054 count++;
055 }
056 System.out.println("Search returned "+ count+ " results");
057 setSearchCount(count);
058 ctx.close();
059 }
060 catch(NamingException ne) {
061 System.out.println("ERROR: " + ne.toString());
062 return false;
063 }
064 catch(Exception e) {
065 System.out.println("ERROR: " + e.toString());
066 return false;
067 }
068 return true;
069 }
070
071 public void printAttributes(Attributes attrs)throws Exception {
072
073 if (attrs == null) {
074 System.out.println("This result has no attributes");
075 } else {
076
077 try {
078
079 for (NamingEnumeration enum = attrs.getAll();
080 enum.hasMore();) {
081 Attribute attrib = (Attribute)enum.next();
082
083 for(NamingEnumeration e = attrib.getAll();e.hasMore();) {
084 String s = e.next().toString();
085 System.out.println("attrib = " + s + "\n");
086
Listing 27.4 (continued)
Transactional LDAP-Don't Make that Commitment 243
087 if (attrib.getID().equals("mail")) {
088 setEmail(s);
089 } else if (attrib.getID().equals("givenName")) {
090 setFirstname(s);
091 } else if (attrib.getID().equals("surName")) {
092 setLastname(s);
093 }
094 }
095 }
096
097 } catch (NamingException ne) {
098 System.out.println("ERROR: " + ne.toString());
099 }
100
101 }
102
103 }
104
105 public DirContext getDirContext() throws Exception {
106
Listing 27.4 (continued)
The InitialDirContext class is the starting context for performing directory operations. The Hashtable items are the environment properties that allow the application to construct a directory context to be searched.
107 Hashtable env = new Hashtable(11);
108 env.put(Context.INITIAL_CONTEXT_FACTORY,
109 "com.sun.jndi.ldap.LdapCtxFactory");
110 env.put(Context.PROVIDER_URL, "ldap://localhost:389");
111 env.put(Context.SECURITY_PRINCIPAL, username);
112 env.put(Context.SECURITY_CREDENTIALS, password);
113 DirContext ctx = new InitialDirContext(env);
114 return ctx;
115
116 }
166 }
167
Listing 27.4 (continued)
Customarily, the personalization process is incorporated after several iterations of a program, because it is a difficult process to capture. Personalization preferences and matching algorithms often vacillate, which hampers their implementation. That is why role information, which is relatively stable, should be stored in a directory server and personalization preferences should be part of a database that can handle transactions in a more efficient manner.
244 Item 28
Consider the following. An LDAP directory is a lightweight database that acts as a central repository for object management and is searched far more often than it should be written to. Its real strength lies in its ability to replicate data across networks, which comes at the expense of transactional protections. Because of this, an RDBMS should be deployed to manage dynamic data, like personalization preferences, which often change regularly.
When you are building distributed business systems, keep in mind that transactional data should be handled with a database, and role information and static user profile information should be managed by an LDAP directory. LDAP directories serve as a complementary technology to database systems, because they can be used to organize and search for relatively static user information in an efficient manner, while database systems can accommodate frequent fluctuations in data.
Previous << 1 .. 83 84 85 86 87 88 < 89 > 90 91 92 93 94 95 .. 166 >> Next