]> source.dussan.org Git - archiva.git/blob
e5438b9c0a615936042d4082d8d9bffba29083b9
[archiva.git] /
1 package org.apache.archiva.redback.common.ldap.connection;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.redback.configuration.UserConfiguration;
23 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
24 import org.springframework.stereotype.Service;
25
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;
35
36 /**
37  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
38  */
39 @Service( "ldapConnectionFactory#configurable" )
40 public class ConfigurableLdapConnectionFactory
41     implements LdapConnectionFactory
42 {
43
44     private String hostname;
45
46     private int port;
47
48     private boolean ssl;
49
50     private String baseDn;
51
52     private String contextFactory;
53
54     private String bindDn;
55
56     private String password;
57
58     private String authenticationMethod;
59
60     private Properties extraProperties;
61
62     private LdapConnectionConfiguration configuration;
63
64
65     @Inject
66     @Named( value = "userConfiguration" )
67     private UserConfiguration userConf;
68
69     // ----------------------------------------------------------------------
70     // Component Lifecycle
71     // ----------------------------------------------------------------------
72     @PostConstruct
73     public void initialize()
74     {
75         try
76         {
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 );
89         }
90         catch ( InvalidNameException e )
91         {
92             throw new RuntimeException( "Error while initializing connection factory.", e );
93         }
94     }
95
96     // ----------------------------------------------------------------------
97     // LdapConnectionFactory Implementation
98     // ----------------------------------------------------------------------
99
100     public LdapConnection getConnection()
101         throws LdapException
102     {
103         return new LdapConnection( configuration, null );
104     }
105
106     public LdapConnection getConnection( Rdn subRdn )
107         throws LdapException
108     {
109         return new LdapConnection( configuration, subRdn );
110     }
111
112     public LdapConnection getConnection( String bindDn, String password )
113         throws LdapException
114     {
115         return new LdapConnection( configuration, bindDn, password );
116     }
117
118     public LdapName getBaseDnLdapName()
119         throws LdapException
120     {
121         try
122         {
123             return new LdapName( baseDn );
124         }
125         catch ( InvalidNameException e )
126         {
127             throw new LdapException( "The base DN is not a valid name.", e );
128         }
129     }
130
131     public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
132     {
133         configuration.getObjectFactories().add( objectFactoryClass );
134     }
135
136     public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
137     {
138         configuration.getStateFactories().add( stateFactoryClass );
139     }
140
141     // ----------------------------------------------------------------------
142     //
143     // ----------------------------------------------------------------------
144
145     public String toString()
146     {
147         return "{ConfigurableLdapConnectionFactory: configuration: " + configuration + "}";
148     }
149
150     public LdapConnectionConfiguration getConfiguration()
151     {
152         return configuration;
153     }
154
155     public String getHostname()
156     {
157         return hostname;
158     }
159
160     public void setHostname( String hostname )
161     {
162         this.hostname = hostname;
163     }
164
165     public int getPort()
166     {
167         return port;
168     }
169
170     public void setPort( int port )
171     {
172         this.port = port;
173     }
174
175     public boolean isSsl()
176     {
177         return ssl;
178     }
179
180     public void setSsl( boolean ssl )
181     {
182         this.ssl = ssl;
183     }
184
185     public String getBaseDn()
186     {
187         return baseDn;
188     }
189
190     public void setBaseDn( String baseDn )
191     {
192         this.baseDn = baseDn;
193     }
194
195     public String getContextFactory()
196     {
197         return contextFactory;
198     }
199
200     public void setContextFactory( String contextFactory )
201     {
202         this.contextFactory = contextFactory;
203     }
204
205     public String getBindDn()
206     {
207         return bindDn;
208     }
209
210     public void setBindDn( String bindDn )
211     {
212         this.bindDn = bindDn;
213     }
214
215     public String getPassword()
216     {
217         return password;
218     }
219
220     public void setPassword( String password )
221     {
222         this.password = password;
223     }
224
225     public String getAuthenticationMethod()
226     {
227         return authenticationMethod;
228     }
229
230     public void setAuthenticationMethod( String authenticationMethod )
231     {
232         this.authenticationMethod = authenticationMethod;
233     }
234
235     public Properties getExtraProperties()
236     {
237         return extraProperties;
238     }
239
240     public void setExtraProperties( Properties extraProperties )
241     {
242         this.extraProperties = extraProperties;
243     }
244
245     public UserConfiguration getUserConf()
246     {
247         return userConf;
248     }
249
250     public void setUserConf( UserConfiguration userConf )
251     {
252         this.userConf = userConf;
253     }
254 }