1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.tarsis.factory;
24
25 import java.util.Properties;
26 import javax.naming.Context;
27 import javax.naming.directory.DirContext;
28 import javax.naming.directory.InitialDirContext;
29 import javax.naming.NamingException;
30
31 /**
32 * JNDI LDAP <code>DirContext</code> factory implementation of <code>Factory</code> interface.<br>
33 * <br>
34 * Creates a <code>DirContext</code> object from given parameters.<br>
35 * <br>
36 * Parameters given to create <code>DirContext</code> are:
37 * <ul>
38 * <li>url - URL for LDAP connections.</li>
39 * <li>user - User name for LDAP connections.</li>
40 * <li>password - User assword for LDAP connections.</li>
41 * </ul>
42 *
43 * @author Jose M. Palomar
44 * @version $Revision: 269 $
45 * @see javax.naming.directory.DirContext
46 */
47 public final class LdapContextFactory extends FactoryService {
48
49
50
51
52 /**
53 * JNDI context init properties.
54 */
55 private Properties env = new Properties();
56
57
58 /**
59 * Creates a new <code>LdapContextFactory</code> object.
60 */
61 public LdapContextFactory() {
62 this.env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
63 }
64
65
66 /**
67 * Sets url parameter.
68 *
69 * @param url String URL for LDAP connections.
70 */
71 public void setUrl(String url) {
72 this.env.put(Context.PROVIDER_URL, url);
73 }
74
75 /**
76 * Sets user parameter.
77 *
78 * @param user String user name for LDAP connections.
79 */
80 public void setUser(String user) {
81 this.env.put(Context.SECURITY_PRINCIPAL, user);
82 }
83
84 /**
85 * Sets password parameter.
86 *
87 * @param password String user password for LDAP connections.
88 */
89 public void setPassword(String password) {
90 this.env.put(Context.SECURITY_CREDENTIALS, password);
91 }
92
93 /**
94 * Returns name of service.
95 *
96 * @return String name of service.
97 * @see org.talika.tarsis.service.Service#getName()
98 */
99 public String getName() {
100 return "LdapContextFactory";
101 }
102
103 /**
104 * Returns a new <code>DirContext</code> instance created by this factory.
105 *
106 * @return Object a new <code>DirContext</code> instance created by this factory.
107 * @see org.talika.tarsis.factory.Factory#getInstance()
108 */
109 public Object getInstance() {
110 return getLdapContext();
111 }
112
113 /**
114 * Returns a new <code>DirContext</code> instance created by this factory.
115 *
116 * @return DirContext a new <code>DirContext</code> instance created by this factory.
117 */
118 public DirContext getLdapContext() {
119
120 DirContext ldapContext = null;
121 try {
122 ldapContext = new InitialDirContext(this.env);
123 }
124 catch (NamingException ne) {
125 if (getLogger().isWarningEnabled()) {
126 getLogger().logWarning("Error creating Ldap Context (" + ne.getMessage() + ")");
127 }
128 }
129
130 return ldapContext;
131 }
132
133 }