This project has retired. For details please refer to its
Attic page.
DefaultLdapConnection xref
1 package org.apache.archiva.redback.common.ldap.connection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.sun.jndi.ldap.LdapCtxFactory;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.naming.Context;
27 import javax.naming.NamingException;
28 import javax.naming.directory.DirContext;
29 import javax.naming.ldap.LdapName;
30 import javax.naming.ldap.Rdn;
31 import java.util.Collections;
32 import java.util.Hashtable;
33 import java.util.List;
34 import java.util.Properties;
35
36
37
38
39
40
41
42 public class DefaultLdapConnection
43 implements LdapConnection
44 {
45
46 private static LdapCtxFactory ctxFactory;
47
48
49 static
50 {
51 initCtxFactory();
52 }
53
54
55 private Logger log = LoggerFactory.getLogger( getClass() );
56
57 private LdapConnectionConfiguration config;
58
59 private DirContext context;
60
61 private List<Rdn> baseDnRdns;
62
63 private static void initCtxFactory()
64 {
65 ctxFactory = new LdapCtxFactory();
66 }
67
68 public DefaultLdapConnection( LdapConnectionConfiguration config, Rdn subRdn )
69 throws LdapException
70 {
71 this.config = config;
72
73 LdapName baseDn = new LdapName( config.getBaseDn().getRdns() );
74
75 if ( subRdn != null )
76 {
77 baseDn.add( subRdn );
78 }
79
80 log.debug( "baseDn: {}", baseDn );
81
82 baseDnRdns = Collections.unmodifiableList( baseDn.getRdns() );
83
84 if ( context != null )
85 {
86 throw new LdapException( "Already connected." );
87 }
88
89 log.debug( "baseDnRdns: {}", baseDnRdns );
90
91 Hashtable<Object, Object> e = getEnvironment();
92
93 try
94 {
95 context = (DirContext) ctxFactory.getInitialContext( e );
96 }
97 catch ( NamingException ex )
98 {
99 throw new LdapException( "Could not connect to the server.", ex );
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public DefaultLdapConnection( LdapConnectionConfiguration config, String bindDn, String password )
113 throws LdapException
114 {
115 this.config = config;
116
117 Hashtable<Object, Object> e = getEnvironment();
118
119 e.put( Context.SECURITY_PRINCIPAL, bindDn );
120 e.put( Context.SECURITY_CREDENTIALS, password );
121
122 try
123 {
124 context = (DirContext) ctxFactory.getInitialContext( e );
125 }
126 catch ( NamingException ex )
127 {
128 throw new LdapException( "Could not connect to the server.", ex );
129 }
130 }
131
132
133
134
135
136 @Override
137 public Hashtable<Object, Object> getEnvironment()
138 throws LdapException
139 {
140 Properties env = new Properties();
141
142 env.putAll( config.getExtraProperties() );
143
144 config.check();
145
146 env.put( Context.INITIAL_CONTEXT_FACTORY, config.getContextFactory() );
147
148
149
150 if ( config.getContextFactory().equals( "com.sun.jndi.ldap.LdapCtxFactory" ) )
151 {
152 env.put( "com.sun.jndi.ldap.connect.pool", "true" );
153
154 env.put( "com.sun.jndi.ldap.connect.pool.timeout", "3600" );
155 }
156
157 if ( config.getHostname() != null )
158 {
159 String protocol = "ldap";
160 if ( config.getPort() != 0 )
161 {
162 env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + ":" + config.getPort() + "/" );
163 }
164 else
165 {
166 env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + "/" );
167 }
168 }
169
170 if ( config.isSsl() )
171 {
172 env.put( Context.SECURITY_PROTOCOL, "ssl" );
173 }
174
175 if ( config.getAuthenticationMethod() != null )
176 {
177 env.put( Context.SECURITY_AUTHENTICATION, config.getAuthenticationMethod() );
178 }
179
180 if ( config.getBindDn() != null )
181 {
182 env.put( Context.SECURITY_PRINCIPAL, config.getBindDn().toString() );
183 }
184
185 if ( config.getPassword() != null )
186 {
187 env.put( Context.SECURITY_CREDENTIALS, config.getPassword() );
188 }
189
190
191
192
193
194 String objectFactories = null;
195
196 for ( Class<?> objectFactoryClass : config.getObjectFactories() )
197 {
198 if ( objectFactories == null )
199 {
200 objectFactories = objectFactoryClass.getName();
201 }
202 else
203 {
204 objectFactories += ":" + objectFactoryClass.getName();
205 }
206 }
207
208 if ( objectFactories != null )
209 {
210 env.setProperty( Context.OBJECT_FACTORIES, objectFactories );
211 }
212
213
214
215
216
217 String stateFactories = null;
218
219 for ( Class<?> stateFactoryClass : config.getStateFactories() )
220 {
221 if ( stateFactories == null )
222 {
223 stateFactories = stateFactoryClass.getName();
224 }
225 else
226 {
227 stateFactories += ":" + stateFactoryClass.getName();
228 }
229 }
230
231 if ( stateFactories != null )
232 {
233 env.setProperty( Context.STATE_FACTORIES, stateFactories );
234 }
235
236 log.debug( "env properties: {}", env );
237
238 return env;
239 }
240
241 @Override
242 public void close()
243 {
244 try
245 {
246 if ( context != null )
247 {
248 context.close();
249 }
250 }
251 catch ( NamingException ex )
252 {
253 log.info( "skip error closing ldap connection {}", ex.getMessage() );
254 }
255 finally
256 {
257 context = null;
258 }
259 }
260
261
262
263
264
265 @Override
266 public LdapConnectionConfiguration getConfiguration()
267 {
268 return config;
269 }
270
271 @Override
272 public List<Rdn> getBaseDnRdns()
273 {
274 return baseDnRdns;
275 }
276
277 @Override
278 public DirContext getDirContext()
279 {
280 return context;
281 }
282 }