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.apache.archiva.redback.configuration.UserConfigurationKeys;
24 import org.springframework.stereotype.Service;
26 import javax.annotation.PostConstruct;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.InvalidNameException;
30 import javax.naming.ldap.LdapName;
31 import javax.naming.ldap.Rdn;
32 import javax.naming.spi.ObjectFactory;
33 import javax.naming.spi.StateFactory;
34 import java.util.Properties;
37 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
39 @Service( "ldapConnectionFactory#configurable" )
40 public class ConfigurableLdapConnectionFactory
41 implements LdapConnectionFactory
44 private String hostname;
50 private String baseDn;
52 private String contextFactory;
54 private String bindDn;
56 private String password;
58 private String authenticationMethod;
60 private Properties extraProperties;
62 private LdapConnectionConfiguration configuration;
66 @Named( value = "userConfiguration" )
67 private UserConfiguration userConf;
69 // ----------------------------------------------------------------------
70 // Component Lifecycle
71 // ----------------------------------------------------------------------
73 public void initialize()
77 configuration = new LdapConnectionConfiguration();
78 configuration.setHostname( userConf.getString( UserConfigurationKeys.LDAP_HOSTNAME, hostname ) );
79 configuration.setPort( userConf.getInt( UserConfigurationKeys.LDAP_PORT, port ) );
80 configuration.setSsl( userConf.getBoolean( UserConfigurationKeys.LDAP_SSL, ssl ) );
81 configuration.setBaseDn( userConf.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, baseDn ) );
82 configuration.setContextFactory(
83 userConf.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, contextFactory ) );
84 configuration.setBindDn( userConf.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, bindDn ) );
85 configuration.setPassword( userConf.getString( UserConfigurationKeys.LDAP_PASSWORD, password ) );
86 configuration.setAuthenticationMethod(
87 userConf.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, authenticationMethod ) );
88 configuration.setExtraProperties( extraProperties );
90 catch ( InvalidNameException e )
92 throw new RuntimeException( "Error while initializing connection factory.", e );
96 // ----------------------------------------------------------------------
97 // LdapConnectionFactory Implementation
98 // ----------------------------------------------------------------------
100 public LdapConnection getConnection()
103 return new LdapConnection( configuration, null );
106 public LdapConnection getConnection( Rdn subRdn )
109 return new LdapConnection( configuration, subRdn );
112 public LdapConnection getConnection( String bindDn, String password )
115 return new LdapConnection( configuration, bindDn, password );
118 public LdapName getBaseDnLdapName()
123 return new LdapName( baseDn );
125 catch ( InvalidNameException e )
127 throw new LdapException( "The base DN is not a valid name.", e );
131 public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
133 configuration.getObjectFactories().add( objectFactoryClass );
136 public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
138 configuration.getStateFactories().add( stateFactoryClass );
141 // ----------------------------------------------------------------------
143 // ----------------------------------------------------------------------
145 public String toString()
147 return "{ConfigurableLdapConnectionFactory: configuration: " + configuration + "}";
150 public LdapConnectionConfiguration getConfiguration()
152 return configuration;
155 public String getHostname()
160 public void setHostname( String hostname )
162 this.hostname = hostname;
170 public void setPort( int port )
175 public boolean isSsl()
180 public void setSsl( boolean ssl )
185 public String getBaseDn()
190 public void setBaseDn( String baseDn )
192 this.baseDn = baseDn;
195 public String getContextFactory()
197 return contextFactory;
200 public void setContextFactory( String contextFactory )
202 this.contextFactory = contextFactory;
205 public String getBindDn()
210 public void setBindDn( String bindDn )
212 this.bindDn = bindDn;
215 public String getPassword()
220 public void setPassword( String password )
222 this.password = password;
225 public String getAuthenticationMethod()
227 return authenticationMethod;
230 public void setAuthenticationMethod( String authenticationMethod )
232 this.authenticationMethod = authenticationMethod;
235 public Properties getExtraProperties()
237 return extraProperties;
240 public void setExtraProperties( Properties extraProperties )
242 this.extraProperties = extraProperties;
245 public UserConfiguration getUserConf()
250 public void setUserConf( UserConfiguration userConf )
252 this.userConf = userConf;