View Javadoc

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       * Returns the integer value for the key from the properties, substituting the default value
41       * if the key is missing or not an integer. Parsing the string property into an integer is done via
42       * {@link Integer#parseInt(String)}.
43       *
44       * @param properties the properties to retrieve the value from
45       * @param key the key to look up
46       * @param defaultValue the value returned if the key does not exist in the properties or its value cannot
47       * be parsed into an integer by {@link Integer#parseInt(String)}.
48       * @param description a description of the key used for logging if the parsing fails
49       * @return the integer value of the key in the provided properties or the default value if the key is not found
50       * or its value is not a number
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  }