View Javadoc
1   package org.andromda.core.server;
2   
3   import java.io.DataInputStream;
4   import java.io.IOException;
5   import java.io.ObjectInputStream;
6   import java.io.ObjectOutputStream;
7   import java.net.ConnectException;
8   import java.net.Socket;
9   import java.net.UnknownHostException;
10  import org.andromda.core.configuration.Configuration;
11  import org.andromda.core.configuration.Server;
12  
13  /**
14   * The default AndroMDA {@link Client} instance.
15   *
16   * @author Chad Brandon
17   */
18  public class DefaultClient
19      implements Client
20  {
21      /**
22       * @see org.andromda.core.server.Client#start(org.andromda.core.configuration.Configuration)
23       */
24      public void start(final Configuration configuration)
25          throws ConnectException
26      {
27          this.performServerOperation(configuration, configuration);
28      }
29  
30      /**
31       * @see org.andromda.core.server.Client#stop(org.andromda.core.configuration.Configuration)
32       */
33      public void stop(final Configuration configuration)
34          throws ConnectException
35      {
36          this.performServerOperation(configuration, DefaultServer.STOP);
37      }
38  
39      /**
40       * Connects to the server and passes the <code>object</code>
41       * to the server.  The server will take the appropriate action based
42       * on the value of the <code>object</code>
43       * @param configuration the AndroMDA configuration (contains the server information).
44       * @param object the object to pass to the server.
45       * @throws ConnectException if an error occurs while attempting to connect to the server.
46       */
47      @SuppressWarnings("static-method")
48      private void performServerOperation(
49          final Configuration configuration,
50          final Object object)
51          throws ConnectException
52      {
53          if (configuration != null && configuration.getServer() != null)
54          {
55              final Server serverConfiguration = configuration.getServer();
56              if (serverConfiguration != null)
57              {
58                  Socket server = null;
59                  try
60                  {
61                      ObjectInputStream objectInput;
62                      ObjectOutputStream out;
63                      final String host = serverConfiguration.getHost();
64                      try
65                      {
66                          server = new Socket(
67                                  host,
68                                  serverConfiguration.getPort());
69                          objectInput = new ObjectInputStream(new DataInputStream(server.getInputStream()));
70                          out = new ObjectOutputStream(server.getOutputStream());
71                      }
72                      catch (final UnknownHostException exception)
73                      {
74                          throw new ClientException("Can't connect to host '" + host + '\'');
75                      }
76                      out.writeObject(object);
77                      if (object instanceof Configuration)
78                      {
79                          final Object input = objectInput.readObject();
80                          if (input instanceof Throwable)
81                          {
82                              throw new ClientException((Throwable)input);
83                          }
84                      }
85                      objectInput.close();
86                      server.close();
87                      out.flush();
88                      out.close();
89                  }
90                  catch (final ConnectException exception)
91                  {
92                      // just re-throw since we need to detect when
93                      // we can't communicate with the server
94                      throw exception;
95                  }
96                  catch (final Throwable throwable)
97                  {
98                      throw new ClientException(throwable);
99                  }
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 }