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