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