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 org.apache.archiva.redback.configuration.UserConfiguration;
23 import org.springframework.stereotype.Service;
25 import javax.annotation.PostConstruct;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.InvalidNameException;
29 import javax.naming.ldap.LdapName;
30 import javax.naming.ldap.Rdn;
31 import javax.naming.spi.ObjectFactory;
32 import javax.naming.spi.StateFactory;
33 import java.util.Properties;
36 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
39 @Service( "ldapConnectionFactory#configurable" )
40 public class ConfigurableLdapConnectionFactory
41 implements LdapConnectionFactory
46 private String hostname;
61 private String baseDn;
66 private String contextFactory;
71 private String bindDn;
76 private String password;
81 private String authenticationMethod;
86 private Properties extraProperties;
88 private LdapConnectionConfiguration configuration;
92 @Named( value = "userConfiguration" )
93 private UserConfiguration userConf;
95 // ----------------------------------------------------------------------
96 // Component Lifecycle
97 // ----------------------------------------------------------------------
99 public void initialize()
103 configuration = new LdapConnectionConfiguration();
104 configuration.setHostname( userConf.getString( "ldap.config.hostname", hostname ) );
105 configuration.setPort( userConf.getInt( "ldap.config.port", port ) );
106 configuration.setSsl( userConf.getBoolean( "ldap.config.ssl", ssl ) );
107 configuration.setBaseDn( userConf.getConcatenatedList( "ldap.config.base.dn", baseDn ) );
108 configuration.setContextFactory( userConf.getString( "ldap.config.context.factory", contextFactory ) );
109 configuration.setBindDn( userConf.getConcatenatedList( "ldap.config.bind.dn", bindDn ) );
110 configuration.setPassword( userConf.getString( "ldap.config.password", password ) );
111 configuration.setAuthenticationMethod(
112 userConf.getString( "ldap.config.authentication.method", authenticationMethod ) );
113 configuration.setExtraProperties( extraProperties );
115 catch ( InvalidNameException e )
117 throw new RuntimeException( "Error while initializing connection factory.", e );
121 // ----------------------------------------------------------------------
122 // LdapConnectionFactory Implementation
123 // ----------------------------------------------------------------------
125 public LdapConnection getConnection()
128 return new LdapConnection( configuration, null );
131 public LdapConnection getConnection( Rdn subRdn )
134 return new LdapConnection( configuration, subRdn );
137 public LdapConnection getConnection( String bindDn, String password )
140 return new LdapConnection( configuration, bindDn, password );
143 public LdapName getBaseDnLdapName()
148 return new LdapName( baseDn );
150 catch ( InvalidNameException e )
152 throw new LdapException( "The base DN is not a valid name.", e );
156 public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
158 configuration.getObjectFactories().add( objectFactoryClass );
161 public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
163 configuration.getStateFactories().add( stateFactoryClass );
166 // ----------------------------------------------------------------------
168 // ----------------------------------------------------------------------
170 public String toString()
172 return "{ConfigurableLdapConnectionFactory: configuration: " + configuration + "}";
175 public LdapConnectionConfiguration getConfiguration()
177 return configuration;
180 public String getHostname()
185 public void setHostname( String hostname )
187 this.hostname = hostname;
195 public void setPort( int port )
200 public boolean isSsl()
205 public void setSsl( boolean ssl )
210 public String getBaseDn()
215 public void setBaseDn( String baseDn )
217 this.baseDn = baseDn;
220 public String getContextFactory()
222 return contextFactory;
225 public void setContextFactory( String contextFactory )
227 this.contextFactory = contextFactory;
230 public String getBindDn()
235 public void setBindDn( String bindDn )
237 this.bindDn = bindDn;
240 public String getPassword()
245 public void setPassword( String password )
247 this.password = password;
250 public String getAuthenticationMethod()
252 return authenticationMethod;
255 public void setAuthenticationMethod( String authenticationMethod )
257 this.authenticationMethod = authenticationMethod;
260 public Properties getExtraProperties()
262 return extraProperties;
265 public void setExtraProperties( Properties extraProperties )
267 this.extraProperties = extraProperties;
270 public UserConfiguration getUserConf()
275 public void setUserConf( UserConfiguration userConf )
277 this.userConf = userConf;