1 package org.apache.archiva.redback.common.ldap.connection;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.sun.jndi.ldap.LdapCtxFactory;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
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;
37 * The configuration for a connection will not change.
39 * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
42 public class LdapConnection
45 private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory();
54 private Logger log = LoggerFactory.getLogger( getClass() );
56 private LdapConnectionConfiguration config;
58 private DirContext context;
60 private List<Rdn> baseDnRdns;
62 private static void initCtxFactory()
64 ctxFactory = new LdapCtxFactory();
67 public LdapConnection( LdapConnectionConfiguration config, Rdn subRdn )
72 LdapName baseDn = new LdapName( config.getBaseDn().getRdns() );
79 baseDnRdns = Collections.unmodifiableList( baseDn.getRdns() );
81 if ( context != null )
83 throw new LdapException( "Already connected." );
86 Hashtable<Object, Object> e = getEnvironment();
90 context = (DirContext) ctxFactory.getInitialContext( e );
92 catch ( NamingException ex )
94 throw new LdapException( "Could not connect to the server.", ex );
99 * This ldap connection will attempt to establish a connection using the configuration,
100 * replacing the principal and the password
105 * @throws LdapException
107 public LdapConnection( LdapConnectionConfiguration config, String bindDn, String password )
110 this.config = config;
112 Hashtable<Object, Object> e = getEnvironment();
114 e.put( Context.SECURITY_PRINCIPAL, bindDn );
115 e.put( Context.SECURITY_CREDENTIALS, password );
119 context = (DirContext) ctxFactory.getInitialContext( e );
121 catch ( NamingException ex )
123 throw new LdapException( "Could not connect to the server.", ex );
127 // ----------------------------------------------------------------------
128 // Connection Managment
129 // ----------------------------------------------------------------------
131 public Hashtable<Object, Object> getEnvironment()
134 Properties env = new Properties();
136 env.putAll( config.getExtraProperties() );
140 env.put( Context.INITIAL_CONTEXT_FACTORY, config.getContextFactory() );
142 // REDBACK-289/MRM-1488
143 // enable connection pooling when using Sun's LDAP context factory
144 if( config.getContextFactory().equals( "com.sun.jndi.ldap.LdapCtxFactory" ) )
146 env.put( "com.sun.jndi.ldap.connect.pool", "true");
148 env.put( "com.sun.jndi.ldap.connect.pool.timeout", "3600" );
151 if ( config.getHostname() != null )
153 String protocol = config.isSsl() ? "ldaps" : "ldap";
154 if ( config.getPort() != 0 )
156 env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + ":" + config.getPort() + "/" );
160 env.put( Context.PROVIDER_URL, protocol + "://" + config.getHostname() + "/" );
164 if ( config.getAuthenticationMethod() != null )
166 env.put( Context.SECURITY_AUTHENTICATION, config.getAuthenticationMethod() );
169 if ( config.getBindDn() != null )
171 env.put( Context.SECURITY_PRINCIPAL, config.getBindDn().toString() );
174 if ( config.getPassword() != null )
176 env.put( Context.SECURITY_CREDENTIALS, config.getPassword() );
179 // ----------------------------------------------------------------------
181 // ----------------------------------------------------------------------
183 String objectFactories = null;
185 for ( Class<?> objectFactoryClass : config.getObjectFactories() )
187 if ( objectFactories == null )
189 objectFactories = objectFactoryClass.getName();
193 objectFactories += ":" + objectFactoryClass.getName();
197 if ( objectFactories != null )
199 env.setProperty( Context.OBJECT_FACTORIES, objectFactories );
202 // ----------------------------------------------------------------------
204 // ----------------------------------------------------------------------
206 String stateFactories = null;
208 for ( Class<?> stateFactoryClass : config.getStateFactories() )
210 if ( stateFactories == null )
212 stateFactories = stateFactoryClass.getName();
216 stateFactories += ":" + stateFactoryClass.getName();
220 if ( stateFactories != null )
222 env.setProperty( Context.STATE_FACTORIES, stateFactories );
232 if ( context != null )
237 catch ( NamingException ex )
239 log.info( "skip error closing ldap connection {}", ex.getMessage() );
247 // ----------------------------------------------------------------------
249 // ----------------------------------------------------------------------
251 public LdapConnectionConfiguration getConfiguration()
256 public List<Rdn> getBaseDnRdns()
261 public DirContext getDirContext()