001package org.andromda.core.server;
002
003import java.io.DataInputStream;
004import java.io.IOException;
005import java.io.ObjectInputStream;
006import java.io.ObjectOutputStream;
007import java.net.ConnectException;
008import java.net.Socket;
009import java.net.UnknownHostException;
010import org.andromda.core.configuration.Configuration;
011import org.andromda.core.configuration.Server;
012
013/**
014 * The default AndroMDA {@link Client} instance.
015 *
016 * @author Chad Brandon
017 */
018public class DefaultClient
019    implements Client
020{
021    /**
022     * @see org.andromda.core.server.Client#start(org.andromda.core.configuration.Configuration)
023     */
024    public void start(final Configuration configuration)
025        throws ConnectException
026    {
027        this.performServerOperation(configuration, configuration);
028    }
029
030    /**
031     * @see org.andromda.core.server.Client#stop(org.andromda.core.configuration.Configuration)
032     */
033    public void stop(final Configuration configuration)
034        throws ConnectException
035    {
036        this.performServerOperation(configuration, DefaultServer.STOP);
037    }
038
039    /**
040     * Connects to the server and passes the <code>object</code>
041     * to the server.  The server will take the appropriate action based
042     * on the value of the <code>object</code>
043     * @param configuration the AndroMDA configuration (contains the server information).
044     * @param object the object to pass to the server.
045     * @throws ConnectException if an error occurs while attempting to connect to the server.
046     */
047    @SuppressWarnings("static-method")
048    private void performServerOperation(
049        final Configuration configuration,
050        final Object object)
051        throws ConnectException
052    {
053        if (configuration != null && configuration.getServer() != null)
054        {
055            final Server serverConfiguration = configuration.getServer();
056            if (serverConfiguration != null)
057            {
058                Socket server = null;
059                try
060                {
061                    ObjectInputStream objectInput;
062                    ObjectOutputStream out;
063                    final String host = serverConfiguration.getHost();
064                    try
065                    {
066                        server = new Socket(
067                                host,
068                                serverConfiguration.getPort());
069                        objectInput = new ObjectInputStream(new DataInputStream(server.getInputStream()));
070                        out = new ObjectOutputStream(server.getOutputStream());
071                    }
072                    catch (final UnknownHostException exception)
073                    {
074                        throw new ClientException("Can't connect to host '" + host + '\'');
075                    }
076                    out.writeObject(object);
077                    if (object instanceof Configuration)
078                    {
079                        final Object input = objectInput.readObject();
080                        if (input instanceof Throwable)
081                        {
082                            throw new ClientException((Throwable)input);
083                        }
084                    }
085                    objectInput.close();
086                    server.close();
087                    out.flush();
088                    out.close();
089                }
090                catch (final ConnectException exception)
091                {
092                    // just re-throw since we need to detect when
093                    // we can't communicate with the server
094                    throw exception;
095                }
096                catch (final Throwable throwable)
097                {
098                    throw new ClientException(throwable);
099                }
100                finally
101                {
102                    if (server != null)
103                    {
104                        try
105                        {
106                            server.close();
107                        }
108                        catch (IOException ex)
109                        {
110                            // Ignore
111                        }
112                    }
113                    
114                }
115            }
116        }
117    }
118}