001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2014 ForgeRock AS. 025 */ 026package org.forgerock.opendj.server.setup.model; 027 028import java.io.File; 029import java.io.IOException; 030import java.net.InetSocketAddress; 031import java.net.ServerSocket; 032import static com.forgerock.opendj.cli.CliConstants.*; 033 034/** 035 * This class provides listener settings for the OpenDJ3 setup. 036 */ 037public class ListenerSettings { 038 039 private String hostName; 040 private int ldapPort; 041 private int ldapsPort; 042 private int adminPort; 043 private boolean isJMXConnectionHandlerEnabled; 044 private int jmxPort; 045 private boolean isHTTPConnectionHandlerEnabled; 046 private int httpPort; 047 private boolean isSNMPConnectionHandlerEnabled; 048 private int snmpPort; 049 private String rootUserDN; 050 private char[] password; 051 private File passwordFile; 052 private boolean isSSLEnabled; 053 private boolean isTLSEnabled; 054 private int sslPortNumber; 055 private Certificate certificate; 056 057 /** 058 * Default constructor. 059 */ 060 public ListenerSettings() { 061 hostName = ""; 062 ldapPort = DEFAULT_LDAP_PORT; 063 ldapsPort = DEFAULT_LDAPS_PORT; 064 adminPort = DEFAULT_ADMIN_PORT; 065 jmxPort = DEFAULT_JMX_PORT; 066 isJMXConnectionHandlerEnabled = false; 067 httpPort = DEFAULT_HTTP_PORT; 068 isHTTPConnectionHandlerEnabled = true; 069 snmpPort = DEFAULT_SNMP_PORT; 070 isSNMPConnectionHandlerEnabled = false; 071 rootUserDN = DEFAULT_ROOT_USER_DN; 072 isSSLEnabled = false; 073 isTLSEnabled = false; 074 sslPortNumber = DEFAULT_SSL_PORT; 075 certificate = null; 076 } 077 078 /** 079 * Returns the host name. 080 * 081 * @return The host name of the local machine. 082 */ 083 public String getHostName() { 084 return hostName; 085 } 086 087 /** 088 * Sets the host name of the machine. 089 * 090 * @param hostName 091 * The host name of the current machine. 092 */ 093 public void setHostName(String hostName) { 094 this.hostName = hostName; 095 } 096 097 /** 098 * Returns the value of the LDAP port. 099 * 100 * @return The value of the LDAP port. 101 */ 102 public int getLdapPort() { 103 return ldapPort; 104 } 105 106 /** 107 * Sets the value of the LDAP port. 108 * 109 * @param ldapPort 110 * The LDAP port's value to set. 111 */ 112 public void setLdapPort(int ldapPort) { 113 this.ldapPort = ldapPort; 114 } 115 116 /** 117 * Return the LDAPs port. 118 * 119 * @return The LDAPs port's value. 120 */ 121 public int getLdapsPort() { 122 return ldapsPort; 123 } 124 125 /** 126 * Sets the LDAPs port value. 127 * 128 * @param ldapsPort 129 * The LDAPs port's value to set. 130 */ 131 public void setLdapsPort(int ldapsPort) { 132 this.ldapsPort = ldapsPort; 133 } 134 135 /** 136 * Returns the administration connector port. 137 * 138 * @return The administration connector's port 139 */ 140 public int getAdminPort() { 141 return adminPort; 142 } 143 144 /** 145 * Sets the administration connector 's port. 146 * 147 * @param adminPort 148 * The administration connector. 149 */ 150 public void setAdminPort(int adminPort) { 151 this.adminPort = adminPort; 152 } 153 154 /** 155 * Returns the JMX's port value. 156 * 157 * @return The JMX's port value. 158 */ 159 public int getJMXPort() { 160 return jmxPort; 161 } 162 163 /** 164 * Sets the JMX port's value. 165 * 166 * @param jmxPort 167 * The JMX port's value. 168 */ 169 public void setJMXPort(int jmxPort) { 170 this.jmxPort = jmxPort; 171 } 172 173 /** 174 * Returns {@code true} if the JMX connection handler is enabled. 175 * 176 * @return {@code true} if the JMX connection handler is enabled. 177 */ 178 public boolean isJMXConnectionHandlerEnabled() { 179 return isJMXConnectionHandlerEnabled; 180 } 181 182 /** 183 * Sets the status of the JMX connection handler. 184 * 185 * @param isJMXConnectionHandlerEnabled 186 * true} if the JMX connection handler is enabled. 187 */ 188 public void setJMXConnectionHandlerEnabled(boolean isJMXConnectionHandlerEnabled) { 189 this.isJMXConnectionHandlerEnabled = isJMXConnectionHandlerEnabled; 190 } 191 192 /** 193 * Returns the value of the HTTP connection handler port. 194 * 195 * @return The value of the HTTP connection handler port. 196 */ 197 public int getHTTPPort() { 198 return httpPort; 199 } 200 201 /** 202 * Sets the value of the port which is going to be used bu the HTTP connection handler. 203 * 204 * @param httpPort 205 * The value of the HTTP port. 206 */ 207 public void setHTTPPort(int httpPort) { 208 this.httpPort = httpPort; 209 } 210 211 /** 212 * Returns {@code true} if the HTTP connection handler is enabled. 213 * 214 * @return {@code true} if the HTTP connection handler is enabled. 215 */ 216 public boolean isHTTPConnectionHandlerEnabled() { 217 return isHTTPConnectionHandlerEnabled; 218 } 219 220 /** 221 * Sets the status of the HTTP connection handler. 222 * 223 * @param isHTTPConnectionHandlerEnabled 224 * true} if the HTTP connection handler is enabled. 225 */ 226 public void setHTTPConnectionHandlerEnabled(boolean isHTTPConnectionHandlerEnabled) { 227 this.isHTTPConnectionHandlerEnabled = isHTTPConnectionHandlerEnabled; 228 } 229 230 /** 231 * Returns the value of the port used by SNMP. 232 * 233 * @return The value of the port used by SNMP. 234 */ 235 public int getSNMPPort() { 236 return snmpPort; 237 } 238 239 /** 240 * Sets the value of the port used by SNMP. 241 * 242 * @param snmpPort 243 * The value of the port used by SNMP. 244 */ 245 public void setSNMPPort(int snmpPort) { 246 this.snmpPort = snmpPort; 247 } 248 249 /** 250 * Returns {@code true} if the SNMP connection handler is enabled. 251 * 252 * @return {@code true} if the SNMP connection handler is enabled. {@code false} otherwise. 253 */ 254 public boolean isSNMPConnectionHandlerEnabled() { 255 return isSNMPConnectionHandlerEnabled; 256 } 257 258 /** 259 * Sets the status of the HTTP connection handler. 260 * 261 * @param isSNMPConnectionHandlerEnabled 262 * {@code true} if the HTTP connection handler is enabled. 263 */ 264 public void setSNMPConnectionHandlerEnabled(boolean isSNMPConnectionHandlerEnabled) { 265 this.isSNMPConnectionHandlerEnabled = isSNMPConnectionHandlerEnabled; 266 } 267 268 /** 269 * Returns the root user DN. 270 * 271 * @return The root user DN. 272 */ 273 public String getRootUserDN() { 274 return rootUserDN; 275 } 276 277 /** 278 * Sets the root user DN. 279 * 280 * @param rootUserDN 281 * The root user DN. 282 */ 283 public void setRootUserDN(String rootUserDN) { 284 this.rootUserDN = rootUserDN; 285 } 286 287 /** 288 * Returns the password linked to this root user DN. 289 * 290 * @return The password linked to this root user DN. 291 */ 292 public String getPassword() { 293 if (password != null) { 294 return String.valueOf(password); 295 } 296 return null; 297 } 298 299 /** 300 * Sets the user root's password. 301 * 302 * @param password 303 * The password to set to this user root DN. 304 */ 305 public void setPassword(String password) { 306 this.password = password.toCharArray(); 307 } 308 309 /** 310 * The file containing the password for the initial root user for the directory server. 311 * 312 * @return The file containing the password for the initial root user. 313 */ 314 public File getPasswordFile() { 315 return passwordFile; 316 } 317 318 /** 319 * Sets the file containing the password for the initial root user for the directory server. 320 * 321 * @param pwdFile 322 * The file containing the password for the initial root user for the directory server. 323 */ 324 public void setPasswordFile(File pwdFile) { 325 this.passwordFile = pwdFile; 326 } 327 328 /** 329 * Returns {@code true} is SSL is enabled. 330 * 331 * @return {@code true} is SSL is enabled, {@code false} otherwise. 332 */ 333 public boolean isSSLEnabled() { 334 return isSSLEnabled; 335 } 336 337 /** 338 * Sets a flag is SSL is enabled. 339 * 340 * @param isSSLEnabled 341 * {@code true} is SSL is enabled, {@code false} otherwise. 342 */ 343 public void setSSLEnabled(boolean isSSLEnabled) { 344 this.isSSLEnabled = isSSLEnabled; 345 } 346 347 /** 348 * Returns {@code true} is TLS is enabled. 349 * 350 * @return {@code true} is TLS is enabled, {@code false} otherwise. 351 */ 352 public boolean isTLSEnabled() { 353 return isTLSEnabled; 354 } 355 356 /** 357 * Sets a flag is TLS is enabled. 358 * 359 * @param isTLSEnabled 360 * {@code true} is TLS is enabled, {@code false} otherwise. 361 */ 362 public void setTLSEnabled(boolean isTLSEnabled) { 363 this.isTLSEnabled = isTLSEnabled; 364 } 365 366 /** 367 * Returns the port number which is used with SSL. 368 * 369 * @return The SSL port's number. 370 */ 371 public int getSSLPortNumber() { 372 return sslPortNumber; 373 } 374 375 /** 376 * Sets the SSL's port number. 377 * 378 * @param sslPortNumber 379 * The port number which should be used with SSL. 380 */ 381 public void setSSLPortNumber(int sslPortNumber) { 382 this.sslPortNumber = sslPortNumber; 383 } 384 385 /** 386 * Returns the certificate linked to this setup. 387 * 388 * @return The certificate linked to this setup. 389 */ 390 public Certificate getCertificate() { 391 return certificate; 392 } 393 394 /** 395 * Sets the certificate used in this setup. 396 * 397 * @param certificate 398 * The certificate used in this setup. 399 */ 400 public void setCertificate(Certificate certificate) { 401 this.certificate = certificate; 402 } 403 404 /** 405 * Returns the port number which is currently free. 406 * @param startPortNumber The port number to start with. 407 * @return The port number which is currently free. 408 */ 409 static int getFreeSocketPort(int startPortNumber) { 410 return getFreeSocketPort(startPortNumber, new TestPortImpl()); 411 } 412 413 private static int getFreeSocketPort(int startPortNumber, TestPort testPort) { 414 int port = startPortNumber; 415 while (port >= 0 && port <= 65535) { 416 try { 417 testPort.canBindToPort(port); 418 return port; 419 } catch (IOException e) { 420 port = port + PORT_INCREMENT; 421 } 422 } 423 throw new IllegalArgumentException("Invalid port."); 424 } 425 426 interface TestPort { 427 void canBindToPort(int portNumber) throws IOException; 428 } 429 430 static class TestPortImpl implements TestPort { 431 public void canBindToPort(int portNumber) throws IOException { 432 ServerSocket socket = null; 433 try { 434 socket = new ServerSocket(); 435 socket.setReuseAddress(true); 436 socket.bind(new InetSocketAddress(portNumber)); 437 } finally { 438 close(socket); 439 } 440 } 441 442 private void close(ServerSocket socket) { 443 try { 444 if (socket != null) { 445 socket.close(); 446 } 447 } catch (final IOException ignored) { 448 // Ignore. 449 } 450 } 451 } 452}