This project has retired. For details please refer to its Attic page.
LdapConnectionConfiguration 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.commons.lang.StringUtils;
23  
24  import javax.naming.InvalidNameException;
25  import javax.naming.ldap.LdapName;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Properties;
29  import java.util.TreeMap;
30  
31  /**
32   * This class contains the configuration for a ldap connection.
33   * <p/>
34   * Properties of a ldap connection:
35   * <ul>
36   * <li>Hostname - String, required.
37   * <li>Port - int, not required. If 0 then the default value is used by the ldap driver.
38   * <li>Ssl - boolean, not required. If true then the ldaps will be used.
39   * <li>Base DN - String, required.
40   * <li>Context factory - String, required.
41   * <li>Bind DN - String, not required.
42   * <li>Password - String, not required.
43   * </ul>
44   * Note that both the bind dn and password must be set if any are set.
45   *
46   * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
47   *
48   */
49  public class LdapConnectionConfiguration
50  {
51      private String hostname;
52  
53      private int port;
54  
55      private boolean ssl;
56  
57      private LdapName baseDn;
58  
59      private String contextFactory;
60  
61      private LdapName bindDn;
62  
63      private String password;
64  
65      private String authenticationMethod;
66  
67      private List<Class<?>> objectFactories;
68  
69      private List<Class<?>> stateFactories;
70  
71      private Properties extraProperties;
72  
73      public LdapConnectionConfiguration()
74      {
75          // no op
76      }
77  
78      public LdapConnectionConfiguration( String hostname, int port, String baseDn, String contextFactory, String bindDn,
79                                          String password, String authenticationMethod, Properties extraProperties )
80          throws InvalidNameException, LdapException
81      {
82          this.hostname = hostname;
83          this.port = port;
84  
85          if ( baseDn != null )
86          {
87              this.baseDn = new LdapName( baseDn );
88          }
89  
90          if ( bindDn != null )
91          {
92              this.bindDn = new LdapName( bindDn );
93          }
94  
95          this.contextFactory = contextFactory;
96  
97          this.password = password;
98  
99          this.authenticationMethod = authenticationMethod;
100 
101         this.extraProperties = extraProperties;
102 
103         check();
104     }
105 
106     public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory )
107         throws LdapException
108     {
109         this.hostname = hostname;
110 
111         this.port = port;
112 
113         this.baseDn = baseDn;
114 
115         this.contextFactory = contextFactory;
116 
117         check();
118     }
119 
120     // ----------------------------------------------------------------------
121     // Accessors
122     // ----------------------------------------------------------------------
123 
124     public String getHostname()
125     {
126         return hostname;
127     }
128 
129     public void setHostname( String hostname )
130     {
131         this.hostname = hostname;
132     }
133 
134     public int getPort()
135     {
136         return port;
137     }
138 
139     public void setPort( int port )
140     {
141         this.port = port;
142     }
143 
144     public boolean isSsl()
145     {
146         return ssl;
147     }
148 
149     public void setSsl( boolean ssl )
150     {
151         this.ssl = ssl;
152     }
153 
154     public LdapName getBaseDn()
155     {
156         return baseDn;
157     }
158 
159     public void setBaseDn( LdapName baseDn )
160     {
161         this.baseDn = baseDn;
162     }
163 
164     public void setBaseDn( String baseDn )
165         throws InvalidNameException
166     {
167         if ( baseDn != null )
168         {
169             this.baseDn = new LdapName( baseDn );
170         }
171     }
172 
173     public String getContextFactory()
174     {
175         return contextFactory;
176     }
177 
178     public void setContextFactory( String contextFactory )
179     {
180         this.contextFactory = contextFactory;
181     }
182 
183     public LdapName getBindDn()
184     {
185         return bindDn;
186     }
187 
188     public void setBindDn( LdapName bindDn )
189     {
190         this.bindDn = bindDn;
191     }
192 
193     public void setBindDn( String bindDn )
194         throws InvalidNameException
195     {
196         if ( bindDn != null )
197         {
198             this.bindDn = new LdapName( bindDn );
199         }
200     }
201 
202     public String getPassword()
203     {
204         return password;
205     }
206 
207     public void setPassword( String password )
208     {
209         this.password = password;
210     }
211 
212     public String getAuthenticationMethod()
213     {
214         return authenticationMethod;
215     }
216 
217     public void setAuthenticationMethod( String authenticationMethod )
218     {
219         this.authenticationMethod = authenticationMethod;
220     }
221 
222     public List<Class<?>> getObjectFactories()
223     {
224         if ( objectFactories == null )
225         {
226             objectFactories = new ArrayList<Class<?>>( 0 );
227         }
228 
229         return objectFactories;
230     }
231 
232     public void setObjectFactories( List<Class<?>> objectFactories )
233     {
234         this.objectFactories = objectFactories;
235     }
236 
237     public List<Class<?>> getStateFactories()
238     {
239         if ( stateFactories == null )
240         {
241             stateFactories = new ArrayList<Class<?>>( 0 );
242         }
243 
244         return stateFactories;
245     }
246 
247     public void setStateFactories( List<Class<?>> stateFactories )
248     {
249         this.stateFactories = stateFactories;
250     }
251 
252     public Properties getExtraProperties()
253     {
254         if ( extraProperties == null )
255         {
256             extraProperties = new Properties();
257         }
258 
259         return extraProperties;
260     }
261 
262     public void setExtraProperties( Properties extraProperties )
263     {
264         this.extraProperties = extraProperties;
265     }
266 
267     // ----------------------------------------------------------------------
268     //
269     // ----------------------------------------------------------------------
270 
271     public void check()
272         throws LdapException
273     {
274         if ( port < 0 || port > 65535 )
275         {
276             throw new LdapException( "The port must be between 1 and 65535." );
277         }
278         if ( baseDn == null )
279         {
280             throw new LdapException( "The base DN must be set." );
281         }
282         if ( StringUtils.isEmpty( contextFactory ) )
283         {
284             throw new LdapException( "The context factory must be set." );
285         }
286         if ( password != null && bindDn == null )
287         {
288             throw new LdapException( "The password cant be set unless the bind dn is." );
289         }
290 
291         if ( extraProperties == null )
292         {
293             extraProperties = new Properties();
294         }
295     }
296 
297     // ----------------------------------------------------------------------
298     //
299     // ----------------------------------------------------------------------
300 
301     public String toString()
302     {
303         return "{LdapConnectionConfiguration: " +
304             "hostname: " + getHostname() + ", " +
305             "port: " + getPort() + ", " +
306             "ssl: " + isSsl() + ", " +
307             "baseDn: " + getBaseDn() + ", " +
308             "contextFactory: " + getContextFactory() + ", " +
309             "bindDn: " + getBindDn() + ", " +
310             // not display password
311             //"password: " + getPassword() + ", " +
312             "authenticationMethod: " + getAuthenticationMethod() + ", " +
313             "objectFactories: " + getObjectFactories() + ", " +
314             "stateFactories: " + getStateFactories() + ", " +
315             "extraProperties: " + new TreeMap<Object, Object>( extraProperties ).toString() + "}";
316     }
317 }