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
15
16
17
18 public class DefaultClient
19 implements Client
20 {
21
22
23
24 public void start(final Configuration configuration)
25 throws ConnectException
26 {
27 this.performServerOperation(configuration, configuration);
28 }
29
30
31
32
33 public void stop(final Configuration configuration)
34 throws ConnectException
35 {
36 this.performServerOperation(configuration, DefaultServer.STOP);
37 }
38
39
40
41
42
43
44
45
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
93
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
111 }
112 }
113
114 }
115 }
116 }
117 }
118 }