001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2014-2016 ForgeRock AS. 015 */ 016package org.forgerock.opendj.ldap; 017 018import static com.forgerock.opendj.util.StaticUtils.getProvider; 019 020import org.forgerock.opendj.ldap.spi.TransportProvider; 021import org.forgerock.util.Option; 022import org.forgerock.util.Options; 023 024/** 025 * Common options for LDAP clients and listeners. 026 */ 027public abstract class CommonLDAPOptions { 028 /** 029 * Specifies the class loader which will be used to load the 030 * {@link org.forgerock.opendj.ldap.spi.TransportProvider TransportProvider}. 031 * <p> 032 * By default the default class loader will be used. 033 * <p> 034 * The transport provider is loaded using {@code java.util.ServiceLoader}, 035 * the JDK service-provider loading facility. The provider must be 036 * accessible from the same class loader that was initially queried to 037 * locate the configuration file; note that this is not necessarily the 038 * class loader from which the file was actually loaded. This method allows 039 * to provide a class loader to be used for loading the provider. 040 * 041 */ 042 public static final Option<ClassLoader> TRANSPORT_PROVIDER_CLASS_LOADER = Option.of(ClassLoader.class, null); 043 044 /** 045 * Specifies the name of the provider to use for transport. 046 * <p> 047 * Transport providers implement {@link org.forgerock.opendj.ldap.spi.TransportProvider TransportProvider} 048 * interface. 049 * <p> 050 * The name should correspond to the name of an existing provider, as 051 * returned by {@code TransportProvider#getName()} method. 052 */ 053 public static final Option<String> TRANSPORT_PROVIDER = Option.of(String.class, null); 054 055 /** 056 * Specifies the transport provider to use. This option is internal and only intended for testing. 057 */ 058 static final Option<TransportProvider> TRANSPORT_PROVIDER_INSTANCE = Option.of(TransportProvider.class, null); 059 060 /** 061 * Specifies the value of the {@link java.net.SocketOptions#TCP_NODELAY 062 * TCP_NODELAY} socket option for new connections. 063 * <p> 064 * The default setting is {@code true} and may be configured using the 065 * {@code org.forgerock.opendj.io.tcpNoDelay} property. 066 */ 067 public static final Option<Boolean> TCP_NO_DELAY = Option.withDefault( 068 getBooleanProperty("org.forgerock.opendj.io.tcpNoDelay", true)); 069 070 /** 071 * Specifies the value of the {@link java.net.SocketOptions#SO_REUSEADDR 072 * SO_REUSEADDR} socket option for new connections. 073 * <p> 074 * The default setting is {@code true} and may be configured using the 075 * {@code org.forgerock.opendj.io.reuseAddress} property. 076 * 077 */ 078 public static final Option<Boolean> SO_REUSE_ADDRESS = Option.withDefault( 079 getBooleanProperty("org.forgerock.opendj.io.reuseAddress", true)); 080 081 /** 082 * Specifies the value of the {@link java.net.SocketOptions#SO_LINGER 083 * SO_LINGER} socket option for new connections. 084 * <p> 085 * The default setting is {@code -1} (disabled) and may be configured using 086 * the {@code org.forgerock.opendj.io.linger} property. 087 */ 088 public static final Option<Integer> SO_LINGER_IN_SECONDS = Option.withDefault( 089 getIntProperty("org.forgerock.opendj.io.linger", -1)); 090 091 /** 092 * Specifies the value of the {@link java.net.SocketOptions#SO_KEEPALIVE 093 * SO_KEEPALIVE} socket option for new connections. 094 * <p> 095 * The default setting is {@code true} and may be configured using the 096 * {@code org.forgerock.opendj.io.keepAlive} property. 097 */ 098 public static final Option<Boolean> SO_KEEPALIVE = Option.withDefault( 099 getBooleanProperty("org.forgerock.opendj.io.keepAlive", true)); 100 101 /** Sets the decoding options which will be used to control how requests and responses are decoded. */ 102 public static final Option<DecodeOptions> LDAP_DECODE_OPTIONS = Option.withDefault(new DecodeOptions()); 103 104 static TransportProvider getTransportProvider(final Options options) { 105 final TransportProvider transportProvider = options.get(TRANSPORT_PROVIDER_INSTANCE); 106 if (transportProvider != null) { 107 return transportProvider; 108 } 109 return getProvider(TransportProvider.class, 110 options.get(TRANSPORT_PROVIDER), 111 options.get(TRANSPORT_PROVIDER_CLASS_LOADER)); 112 } 113 114 static boolean getBooleanProperty(final String name, final boolean defaultValue) { 115 final String value = System.getProperty(name); 116 return value != null ? Boolean.parseBoolean(value) : defaultValue; 117 } 118 119 static int getIntProperty(final String name, final int defaultValue) { 120 final String value = System.getProperty(name); 121 try { 122 return value != null ? Integer.parseInt(value) : defaultValue; 123 } catch (final NumberFormatException e) { 124 return defaultValue; 125 } 126 } 127}