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 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2014 ForgeRock AS 026 */ 027package org.opends.server.util; 028 029 030 031import org.forgerock.opendj.config.server.ConfigException; 032import org.opends.server.core.DirectoryServer; 033import org.opends.server.types.DirectoryEnvironmentConfig; 034import org.opends.server.types.InitializationException; 035 036import static org.opends.messages.UtilityMessages.*; 037 038import org.forgerock.i18n.LocalizableMessage; 039 040 041 042/** 043 * This class provides a number of utility methods for using OpenDS in an 044 * embedded manner (i.e., running within the same JVM as another application and 045 * controlled by that application). 046 */ 047@org.opends.server.types.PublicAPI( 048 stability=org.opends.server.types.StabilityLevel.UNCOMMITTED, 049 mayInstantiate=false, 050 mayExtend=false, 051 mayInvoke=true) 052public final class EmbeddedUtils 053{ 054 /** 055 * Indicates whether the Directory Server is currently running. 056 * 057 * @return {@code true} if the server is currently running, or {@code false} 058 * if not. 059 */ 060 public static boolean isRunning() 061 { 062 return DirectoryServer.isRunning(); 063 } 064 065 066 067 /** 068 * Attempts to start the Directory Server. 069 * 070 * @param config The environment configuration to use for the server. 071 * 072 * @throws InitializationException If the Directory Server is already 073 * running, or if an error occurs during 074 * server initialization or startup. 075 */ 076 public static void startServer(DirectoryEnvironmentConfig config) 077 throws InitializationException 078 { 079 if (DirectoryServer.isRunning()) 080 { 081 throw new InitializationException( 082 ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING.get()); 083 } 084 085 DirectoryServer directoryServer = DirectoryServer.reinitialize(config); 086 try 087 { 088 directoryServer.startServer(); 089 } 090 catch (ConfigException e) 091 { 092 throw new InitializationException(e.getMessageObject(), e); 093 } 094 } 095 096 097 098 /** 099 * Attempts to stop the Directory Server. 100 * 101 * @param className The name of the class that initiated the shutdown. 102 * @param reason A message explaining the reason for the shutdown. 103 */ 104 public static void stopServer(String className, LocalizableMessage reason) 105 { 106 DirectoryServer.shutDown(className, reason); 107 } 108 109 110 111 /** 112 * Attempts to restart the Directory Server. This will perform an in-core 113 * restart in which the existing server instance will be shut down, a new 114 * instance will be created, and it will be reinitialized and restarted. 115 * 116 * @param className The name of the class that initiated the restart. 117 * @param reason A message explaining the reason for the retart. 118 * @param config The environment configuration to use for the new server 119 * instance. 120 */ 121 public static void restartServer(String className, LocalizableMessage reason, 122 DirectoryEnvironmentConfig config) 123 { 124 DirectoryServer.restart(className, reason, config); 125 } 126 127 128 129 /** 130 * Sets up a number of internal server data structures to ensure that they are 131 * properly initialized for use. This is necessary if server libraries are 132 * going to be used without the server running (e.g., to facilitate use in an 133 * LDAP client API, for DN processing, etc.). This will have no effect if the 134 * server has already been initialized for client use. 135 */ 136 public static void initializeForClientUse() 137 { 138 DirectoryServer.getInstance(); 139 DirectoryServer.bootstrapClient(); 140 } 141} 142