1 package com.atlassian.user.impl.ldap.properties.factory;
2
3 import com.atlassian.user.impl.ldap.properties.LdapConnectionProperties;
4 import com.atlassian.user.impl.ldap.properties.DefaultLdapConnectionProperties;
5 import com.atlassian.user.configuration.Configuration;
6
7 import java.util.Properties;
8
9 import org.apache.log4j.Logger;
10
11 public class LdapConnectionPropertiesFactory
12 {
13 protected final Logger log = Logger.getLogger(this.getClass());
14
15 public LdapConnectionProperties createInstance(Properties properties)
16 {
17 DefaultLdapConnectionProperties result = new DefaultLdapConnectionProperties();
18 result.setHost(properties.getProperty(Configuration.HOST, "localhost"));
19 result.setJndiInitialContextFactoryIdentifier(
20 properties.getProperty(Configuration.INITIAL_CONTEXT_FACTORY_JNDI));
21 result.setPort(getInt(properties, Configuration.PORT,
22 DefaultLdapConnectionProperties.DEFAULT_LDAP_PORT, "LDAP server port"));
23 result.setSearchBatchSize(getInt(properties, Configuration.BATCH_SIZE,
24 DefaultLdapConnectionProperties.DEFAULT_BATCH_SIZE, "LDAP search batch size"));
25 result.setConnectTimeoutMillis(getInt(properties, Configuration.CONNECT_TIMEOUT,
26 DefaultLdapConnectionProperties.DEFAULT_CONNECT_TIMEOUT_MILLIS, "LDAP connection timeout"));
27 result.setReadTimeoutMillis(getInt(properties, Configuration.READ_TIMEOUT,
28 DefaultLdapConnectionProperties.DEFAULT_READ_TIMEOUT_MILLIS, "LDAP read timeout"));
29 result.setSecurityAuthentication(properties.getProperty(Configuration.AUTHENTICATION, DefaultLdapConnectionProperties.DEFAULT_AUTHENTICATION));
30 result.setSecurityCredential(properties.getProperty(Configuration.SECURITY_CREDENTIAL));
31 result.setSecurityPrincipal(properties.getProperty(Configuration.SECURITY_PRINCIPAL));
32 result.setSecurityProtocol(properties.getProperty(Configuration.SECURITY_PROTOCOL));
33 String poolingOn = properties.getProperty(Configuration.POOLING_ON, "true");
34 result.setPoolingOn(Boolean.valueOf(poolingOn).booleanValue());
35 result.setProviderURL(properties.getProperty(Configuration.PROVIDER_URL));
36 return result;
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 private int getInt(Properties properties, String key, int defaultValue, String description)
53 {
54 String value = properties.getProperty(key, Integer.toString(defaultValue));
55 try
56 {
57 return Integer.parseInt(value);
58 }
59 catch (NumberFormatException e)
60 {
61 log.warn("Error parsing " + description + " in configuration file, using default value", e);
62 return defaultValue;
63 }
64 }
65 }