This project has retired. For details please refer to its Attic page.
ConfigurableLdapConnectionFactory xref
View Javadoc

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 ldapConnectionConfiguration;
63  
64  
65      @Inject
66      @Named(value = "userConfiguration#default")
67      private UserConfiguration userConf;
68  
69      // ----------------------------------------------------------------------
70      // Component Lifecycle
71      // ----------------------------------------------------------------------
72      @PostConstruct
73      public void initialize()
74      {
75          try
76          {
77              ldapConnectionConfiguration = new LdapConnectionConfiguration();
78              ldapConnectionConfiguration.setHostname(
79                  userConf.getString( UserConfigurationKeys.LDAP_HOSTNAME, hostname ) );
80              ldapConnectionConfiguration.setPort( userConf.getInt( UserConfigurationKeys.LDAP_PORT, port ) );
81              ldapConnectionConfiguration.setSsl( userConf.getBoolean( UserConfigurationKeys.LDAP_SSL, ssl ) );
82              ldapConnectionConfiguration.setBaseDn(
83                  userConf.getConcatenatedList( UserConfigurationKeys.LDAP_BASEDN, baseDn ) );
84              ldapConnectionConfiguration.setContextFactory(
85                  userConf.getString( UserConfigurationKeys.LDAP_CONTEX_FACTORY, contextFactory ) );
86              ldapConnectionConfiguration.setBindDn(
87                  userConf.getConcatenatedList( UserConfigurationKeys.LDAP_BINDDN, bindDn ) );
88              ldapConnectionConfiguration.setPassword(
89                  userConf.getString( UserConfigurationKeys.LDAP_PASSWORD, password ) );
90              ldapConnectionConfiguration.setAuthenticationMethod(
91                  userConf.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, authenticationMethod ) );
92              ldapConnectionConfiguration.setExtraProperties( extraProperties );
93          }
94          catch ( InvalidNameException e )
95          {
96              throw new RuntimeException( "Error while initializing connection factory.", e );
97          }
98      }
99  
100     // ----------------------------------------------------------------------
101     // LdapConnectionFactory Implementation
102     // ----------------------------------------------------------------------
103 
104     public DefaultLdapConnection getConnection()
105         throws LdapException
106     {
107         return new DefaultLdapConnection( getLdapConnectionConfiguration(), null );
108     }
109 
110     public DefaultLdapConnection getConnection( Rdn subRdn )
111         throws LdapException
112     {
113         return new DefaultLdapConnection( getLdapConnectionConfiguration(), subRdn );
114     }
115 
116     public DefaultLdapConnection getConnection( String bindDn, String password )
117         throws LdapException
118     {
119         return new DefaultLdapConnection( getLdapConnectionConfiguration(), bindDn, password );
120     }
121 
122     public DefaultLdapConnection getConnection( LdapConnectionConfiguration ldapConnectionConfiguration )
123         throws LdapException
124     {
125         return new DefaultLdapConnection( ldapConnectionConfiguration, null );
126     }
127 
128     public LdapName getBaseDnLdapName()
129         throws LdapException
130     {
131         try
132         {
133             return new LdapName( baseDn );
134         }
135         catch ( InvalidNameException e )
136         {
137             throw new LdapException( "The base DN is not a valid name.", e );
138         }
139     }
140 
141     public void addObjectFactory( Class<? extends ObjectFactory> objectFactoryClass )
142     {
143         getLdapConnectionConfiguration().getObjectFactories().add( objectFactoryClass );
144     }
145 
146     public void addStateFactory( Class<? extends StateFactory> stateFactoryClass )
147     {
148         getLdapConnectionConfiguration().getStateFactories().add( stateFactoryClass );
149     }
150 
151     // ----------------------------------------------------------------------
152     //
153     // ----------------------------------------------------------------------
154 
155     public String toString()
156     {
157         return "{ConfigurableLdapConnectionFactory: configuration: " + getLdapConnectionConfiguration() + "}";
158     }
159 
160     public LdapConnectionConfiguration getLdapConnectionConfiguration()
161     {
162         return ldapConnectionConfiguration;
163     }
164 
165     public void setLdapConnectionConfiguration( LdapConnectionConfiguration ldapConnectionConfiguration )
166     {
167         this.ldapConnectionConfiguration = ldapConnectionConfiguration;
168     }
169 
170     public String getHostname()
171     {
172         return hostname;
173     }
174 
175     public void setHostname( String hostname )
176     {
177         this.hostname = hostname;
178     }
179 
180     public int getPort()
181     {
182         return port;
183     }
184 
185     public void setPort( int port )
186     {
187         this.port = port;
188     }
189 
190     public boolean isSsl()
191     {
192         return ssl;
193     }
194 
195     public void setSsl( boolean ssl )
196     {
197         this.ssl = ssl;
198     }
199 
200     public String getBaseDn()
201     {
202         return baseDn;
203     }
204 
205     public void setBaseDn( String baseDn )
206     {
207         this.baseDn = baseDn;
208     }
209 
210     public String getContextFactory()
211     {
212         return contextFactory;
213     }
214 
215     public void setContextFactory( String contextFactory )
216     {
217         this.contextFactory = contextFactory;
218     }
219 
220     public String getBindDn()
221     {
222         return bindDn;
223     }
224 
225     public void setBindDn( String bindDn )
226     {
227         this.bindDn = bindDn;
228     }
229 
230     public String getPassword()
231     {
232         return password;
233     }
234 
235     public void setPassword( String password )
236     {
237         this.password = password;
238     }
239 
240     public String getAuthenticationMethod()
241     {
242         return authenticationMethod;
243     }
244 
245     public void setAuthenticationMethod( String authenticationMethod )
246     {
247         this.authenticationMethod = authenticationMethod;
248     }
249 
250     public Properties getExtraProperties()
251     {
252         return extraProperties;
253     }
254 
255     public void setExtraProperties( Properties extraProperties )
256     {
257         this.extraProperties = extraProperties;
258     }
259 
260     public UserConfiguration getUserConf()
261     {
262         return userConf;
263     }
264 
265     public void setUserConf( UserConfiguration userConf )
266     {
267         this.userConf = userConf;
268     }
269 }