001package org.andromda.core;
002
003import org.andromda.core.common.AndroMDALogger;
004import org.andromda.core.common.ComponentContainer;
005import org.andromda.core.configuration.Configuration;
006import org.andromda.core.server.Client;
007import org.andromda.core.server.ClientException;
008import org.andromda.core.server.Server;
009import org.andromda.core.server.ServerException;
010
011/**
012 * Provides the ability to manage an AndroMDA server instance.
013 *
014 * @see org.andromda.core.server.Server
015 * @author Chad Brandon
016 */
017public final class AndroMDAServer
018{
019    /**
020     * The actual server instance.
021     */
022    private Server server;
023
024    /**
025     * Creates a new instance of class.
026     *
027     * @return the new instance.
028     */
029    public static AndroMDAServer newInstance()
030    {
031        return new AndroMDAServer();
032    }
033
034    private AndroMDAServer()
035    {
036        AndroMDALogger.initialize();
037        this.server = (Server)ComponentContainer.instance().findRequiredComponent(Server.class);
038    }
039
040    /**
041     * Starts the AndroMDA server instance listening for requests.
042     *
043     * @param configuration the Configuration instance used to configure the server.
044     */
045    public void start(final Configuration configuration)
046    {
047        if (configuration == null)
048        {
049            throw new ServerException("You must specify a valid 'configuration' in order to start the server");
050        }
051        final org.andromda.core.configuration.Server serverConfiguration = configuration.getServer();
052        if (serverConfiguration == null)
053        {
054            AndroMDALogger.warn(
055                "Can not start the server, you must define the " + "server element within your AndroMDA configuration");
056        }
057        else
058        {
059            this.server.start(configuration);
060        }
061    }
062
063    /**
064     * Stops the AndroMDA server instance.
065     * @param configuration the Configuration instance used to configure the server.
066     */
067    public void stop(final Configuration configuration)
068    {
069        final ComponentContainer container = ComponentContainer.instance();
070        final Client serverClient = (Client)container.findComponent(Client.class);
071        if (serverClient != null)
072        {
073            try
074            {
075                serverClient.stop(configuration);
076            }
077            catch (final Throwable throwable)
078            {
079                throw new ClientException(throwable);
080            }
081        }
082    }
083}