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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.util;
028    
029    
030    
031    import org.opends.server.config.ConfigException;
032    import org.opends.server.core.DirectoryServer;
033    import org.opends.server.types.DirectoryEnvironmentConfig;
034    import org.opends.server.types.InitializationException;
035    
036    import static org.opends.messages.UtilityMessages.*;
037    import org.opends.messages.Message;
038    import static org.opends.server.util.ServerConstants.*;
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)
052    public 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  ConfigException  If a configuration problem is detected during
073       *                           the server initialization or startup process.
074       *
075       * @throws  InitializationException  If the Directory Server is already
076       *                                   running, or if an error occurs during
077       *                                   server initialization or startup.
078       */
079      public static void startServer(DirectoryEnvironmentConfig config)
080             throws ConfigException, InitializationException
081      {
082        if (DirectoryServer.isRunning())
083        {
084          throw new InitializationException(
085                  ERR_EMBEDUTILS_SERVER_ALREADY_RUNNING.get());
086        }
087    
088        DirectoryServer directoryServer = DirectoryServer.reinitialize(config);
089        directoryServer.startServer();
090      }
091    
092    
093    
094      /**
095       * Attempts to stop the Directory Server.
096       *
097       * @param  className  The name of the class that initiated the shutdown.
098       * @param  reason     A message explaining the reason for the shutdown.
099       */
100      public static void stopServer(String className, Message reason)
101      {
102        DirectoryServer.shutDown(className, reason);
103      }
104    
105    
106    
107      /**
108       * Attempts to restart the Directory Server.  This will perform an in-core
109       * restart in which the existing server instance will be shut down, a new
110       * instance will be created, and it will be reinitialized and restarted.
111       *
112       * @param  className  The name of the class that initiated the restart.
113       * @param  reason     A message explaining the reason for the retart.
114       * @param  config     The environment configuration to use for the new server
115       *                    instance.
116       */
117      public static void restartServer(String className, Message reason,
118                                       DirectoryEnvironmentConfig config)
119      {
120        DirectoryServer.restart(className, reason, config);
121      }
122    
123    
124    
125      /**
126       * Sets up a number of internal server data structures to ensure that they are
127       * properly initialized for use.  This is necessary if server libraries are
128       * going to be used without the server running (e.g., to facilitate use in an
129       * LDAP client API, for DN processing, etc.).  This will have no effect if the
130       * server has already been initialized for client use.
131       */
132      public static void initializeForClientUse()
133      {
134        DirectoryServer directoryServer = DirectoryServer.getInstance();
135        directoryServer.bootstrapClient();
136      }
137    }
138