1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.authn;
18
19
20 import org.apache.ldap.common.util.StringTools;
21 import org.apache.ldap.server.jndi.EnvKeys;
22
23 import javax.naming.NamingException;
24 import java.io.FileInputStream;
25 import java.util.Hashtable;
26 import java.util.Properties;
27
28
29 /***
30 * An authenticator configuration builder which produces AuthenticatorConfig
31 * objects from various configuration formats, namely Hashtables.
32 *
33 * @author <a href="mailto:endisd@vergenet.com">Endi S. Dewata</a>
34 */
35 public class AuthenticatorConfigBuilder
36 {
37 /*** keep this so we do not have create empty ones over and over again */
38 private final static GenericAuthenticatorConfig[] EMPTY = new GenericAuthenticatorConfig[0];
39
40
41 /***
42 * Extracts properties from a Hashtable and builds a configuration bean for
43 * an AuthenticationService.
44 *
45 * @param authenticatorName the name of the authenticator to extract configs for
46 * @param env the Hastable containing usually JNDI environment settings
47 * @return the extracted configuration object
48 * @throws javax.naming.NamingException
49 */
50 public static GenericAuthenticatorConfig getAuthenticatorConfig( String authenticatorName, Hashtable env )
51 throws NamingException
52 {
53 final StringBuffer buf = new StringBuffer();
54 final GenericAuthenticatorConfig config = new GenericAuthenticatorConfig();
55
56
57
58
59
60 config.setAuthenticatorName( authenticatorName );
61
62
63
64
65
66 buf.setLength( 0 );
67 buf.append( EnvKeys.AUTHENTICATOR_CLASS ).append( authenticatorName );
68 String authenticatorClass = ( String ) env.get( buf.toString() );
69
70 if ( authenticatorClass != null )
71 {
72 config.setAuthenticatorClass( authenticatorClass );
73 }
74
75
76
77
78
79 buf.setLength( 0 );
80 buf.append( EnvKeys.AUTHENTICATOR_PROPERTIES ).append( authenticatorName );
81 String propertiesFile = ( String ) env.get( buf.toString() );
82
83 if ( propertiesFile != null )
84 {
85 try
86 {
87 Properties properties = config.getProperties();
88 properties.load( new FileInputStream( propertiesFile ) );
89 config.setProperties( properties );
90 }
91 catch ( Exception e )
92 {
93 throw new NamingException( e.getMessage() );
94 }
95 }
96
97 return config;
98 }
99
100
101 /***
102 * Extracts properties from a Hashtable and builds a set of configurations
103 * bean for Authenticators.
104 *
105 * @param env the Hastable containing usually JNDI environment settings
106 * @return all the extracted configuration objects configured
107 * @throws javax.naming.NamingException
108 */
109 public static GenericAuthenticatorConfig[] getAuthenticatorConfigs( Hashtable env )
110 throws NamingException
111 {
112 String idList = ( String ) env.get( EnvKeys.AUTHENTICATORS );
113
114
115 if ( idList == null || idList.trim().length() == 0 )
116 {
117 return EMPTY;
118 }
119
120 idList = StringTools.deepTrim( idList );
121 final String[] ids = idList.split( " " );
122 final GenericAuthenticatorConfig[] configs = new GenericAuthenticatorConfig[ids.length];
123 for ( int ii = 0; ii < configs.length; ii++ )
124 {
125 configs[ii] = getAuthenticatorConfig( ids[ii], env );
126 }
127
128 return configs;
129 }
130 }