Project

General

Profile

« Previous | Next » 

Revision 608f7100

Added by Cherian Mathew almost 9 years ago

corrected merge to latest snapshot version
updated hibernate config file and test db
setup testing framework with jetty and maven

View differences:

eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java
12 12
import java.io.BufferedReader;
13 13
import java.io.IOException;
14 14
import java.io.InputStreamReader;
15
import java.net.Socket;
16 15

  
16
import javax.management.InstanceNotFoundException;
17
import javax.management.MBeanException;
18
import javax.management.MBeanServerConnection;
19
import javax.management.MalformedObjectNameException;
20
import javax.management.ObjectName;
21
import javax.management.ReflectionException;
22
import javax.management.remote.JMXConnector;
23
import javax.management.remote.JMXConnectorFactory;
24
import javax.management.remote.JMXServiceURL;
17 25
import javax.sql.DataSource;
18 26

  
19
import org.apache.log4j.Level;
20 27
import org.apache.log4j.Logger;
21 28
import org.springframework.core.io.ClassPathResource;
22 29
import org.springframework.core.io.Resource;
......
48 55

  
49 56
    private final String name = "default";
50 57
    private final String host = "127.0.0.1";
51
    private final int port = 9090;
52
    private final int stopPort = 9191;
53
    private final String stopKey = "jetty-cdm-server";
58
    private int httpPort = 9090;
59
    private int stopPort = 9191;
60
    private String stopKey = "jetty-cdm-server";
54 61
    private final String contextPath = "";
55 62

  
56
    private boolean keepServerRunning = false;
57

  
58 63
    public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE =
59 64
            new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
60 65

  
61
    public static final Resource DEFAULT_JETTY_CONFIG_RESOURCE =
62
            new ClassPathResource("/etc/jetty/jetty.xml");
63

  
64
    public static final Resource DEFAULT_JETTY_TEMP_RESOURCE =
65
            new ClassPathResource("/etc/jetty/temp");
66

  
67
    public static final Resource DEFAULT_JETTY_TEMP_WAR_LIB_RESOURCE =
68
            new ClassPathResource("/etc/jetty/temp/webapp/WEB-INF/lib");
69

  
70 66
    public static final Resource DEFAULT_DATASOURCE_FILE =
71 67
            new ClassPathResource("datasources.xml");
72 68

  
......
76 72
    public static final Resource DEFAULT_JETTY_RESOURCE =
77 73
            new ClassPathResource("/etc/jetty/start-9.2.3.v20140905.jar");
78 74

  
79

  
80

  
81 75
    private static CDMServer cdmServer = null;
82 76
    private static CDMServerException cdmse = null;
83 77

  
84
    private void CDMServer() {
85
        logger.setLevel(Level.INFO);
86
    }
78
    private void CDMServer() {}
87 79

  
88 80
    public static CDMServer getInstance() {
89 81
        if(cdmServer == null) {
......
101 93
    }
102 94

  
103 95
    public int getPort() {
104
        return port;
96
        return httpPort;
105 97
    }
106 98

  
107 99
    public String getContextPath() {
108 100
        return contextPath;
109 101
    }
110 102

  
111
    public boolean getKeepServerRunning() {
112
        return keepServerRunning;
103
    public void setHttpPort(int port) {
104
        this.httpPort = port;
105
    }
106

  
107
    public void setStopPort(int stopPort) {
108
        this.stopPort = stopPort;
113 109
    }
114 110

  
115
    public void setKeepServerRunning(boolean keepServerRunning) {
116
        this.keepServerRunning = keepServerRunning;
111
    public void setStopKey(String stopKey) {
112
        this.stopKey = stopKey;
117 113
    }
118 114

  
115

  
119 116
    public static boolean isRunningInEclipse() {
120 117
        return (System.getProperty("sun.java.command") != null &&
121 118
                System.getProperty("sun.java.command").startsWith("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner"));
......
129 126
        return sb.toString();
130 127
    }
131 128

  
132
    private String getStartServerArgs() {
129
    private String getStartServerArgs() throws IOException {
133 130
        StringBuilder sb = new StringBuilder();
134
        sb.append(" --port ");
135
        sb.append(port);
131
        sb.append(" --port " + httpPort);
136 132
        return sb.toString();
137 133
    }
138 134

  
......
154 150
        return sb.toString();
155 151
    }
156 152

  
153

  
157 154
    public void start() throws CDMServerException {
158 155

  
159 156
        /**
......
163 160
         * has started but incorrectly, in which case we try to force stop it (if we can)
164 161
         * and start a new server.
165 162
         */
166
        if(isRunning(1)) {
167
            logger.info("[CDM-Server] Server already running @ " + host + ":" + port );
163
        if(isStarted(1)) {
164
            logger.info("[CDM-Server] Server already running @ " + host + ":" + httpPort );
168 165
            return;
169
        } else if (isAvailable(1)){
170
            logger.info("[CDM-Server] Server available, but not started correctly @ " + host + ":" + port );
171
            logger.info("[CDM-Server] .... trying to force stop server @ " + host + ":" + port );
172
            try {
173
                stop(true);
174
            } catch (Exception e) {
175
                throw new CDMServerException("CDM Server could not be stopped : " + e.getMessage());
176
            }
177 166
        }
178 167

  
179 168
        Thread t = new Thread() {
180 169
            @Override
181 170
            public void run() {
171

  
182 172
                StringBuffer output = new StringBuffer();
183 173
                try{
184 174
                    Process p;
......
193 183
                    logger.info("[CDM-Server] Starting server with Command : " + command);
194 184
                    p = Runtime.getRuntime().exec(command);
195 185

  
196
                    BufferedReader reader =
186
                    BufferedReader inpReader =
197 187
                            new BufferedReader(new InputStreamReader(p.getInputStream()));
198 188

  
189
                    BufferedReader errReader =
190
                            new BufferedReader(new InputStreamReader(p.getErrorStream()));
191

  
199 192
                    String line = "";
200
                    while ((line = reader.readLine())!= null) {
201
                        logger.info("[CDM-Server] Start : " + line);
193
                    while ((line = inpReader.readLine())!= null) {
194
                        logger.info("[CDM-Server Start] : " + line);
195
                    }
196

  
197
                    while ((line = errReader.readLine())!= null) {
198
                        logger.info("[CDM-Server Start] : " + line);
202 199
                    }
203 200

  
204 201
                } catch (Exception e) {
205 202
                    e.printStackTrace();
206 203
                    cdmse = new CDMServerException(e);
207 204
                }
208

  
209 205
            }
210 206
        };
211 207

  
......
213 209
        cdmse = null;
214 210
        t.start();
215 211

  
216
        if(isRunning(60)) {
217
            logger.info("[CDM-Server] Started Server @ " + host + ":" + port );
212
        if(isStarted(50)) {
213
            logger.info("[CDM-Server] Server running @ " + host + ":" + httpPort );
218 214
        } else {
219 215
            logger.info("[CDM-Server] Server not started within given interval");
220 216
            // making sure to kill server if it is not started correctly
......
228 224

  
229 225
    }
230 226

  
231
    public boolean isAvailable(int checkingIntervals) throws CDMServerException {
232
        int intervalsCount = 0;
233
        do {
234
            try {
235
                Socket s = new Socket(host, port);
236
                logger.info("[CDM-Server] Available @ " + host + ":" + port );
237
                return true;
238
            } catch (IOException ioe) {
239 227

  
240
            }
241
            try {
242
                Thread.sleep(1000);
243
            } catch (InterruptedException ie) {
244
                throw new CDMServerException("Error checking CDM Server status", ie);
245
            }
246
            intervalsCount++;
247
        } while (intervalsCount < checkingIntervals);
248

  
249
        return false;
250
    }
251

  
252
    public boolean isStopped(int checkingIntervals) throws CDMServerException {
253
        int intervalsCount = 0;
254
        do {
255
            try {
256
                Socket s = new Socket(host, port);
257
            } catch (IOException ioe) {
258
                return true;
259
            }
260
            try {
261
                Thread.sleep(1000);
262
            } catch (InterruptedException ie) {
263
                throw new CDMServerException("Error checking CDM Server status", ie);
264
            }
265
            intervalsCount++;
266
        } while (intervalsCount < checkingIntervals);
267

  
268
        return false;
269
    }
270

  
271
    public boolean isRunning(int checkingIntervals) throws CDMServerException  {
228
    public boolean isStarted(int checkingIntervals) throws CDMServerException  {
272 229
        CdmRemoteSourceBase crsb = new CdmRemoteSourceBase("local-cdm-server",
273 230
                host,
274
                port,
231
                httpPort,
275 232
                contextPath,
276 233
                null);
277 234
        int intervalsCount = 0;
......
282 239
                }
283 240
                boolean check = crsb.checkConnection();
284 241
                if(check) {
285
                    logger.info("[CDM-Server] Running @ " + host + ":" + port );
242
                    logger.info("[CDM-Server] Running @ " + host + ":" + httpPort );
286 243
                    return true;
287 244
                }
288 245
            } catch (Exception e) {
......
301 258
    public void stop() throws Exception {
302 259
        stop(false);
303 260
    }
261

  
304 262
    public void stop(boolean force) throws Exception {
305 263

  
306 264
        if(!force) {
307
            if(!getInstance().isAvailable(1)) {
308
                logger.info("[CDM-Server] Server already stopped @ " + host + ":" + port );
309
                return;
310
            }
311

  
312
            if(getInstance().getKeepServerRunning()) {
313
                logger.info("[CDM-Server] Server @ " + host + ":" + port + " is set to keep running");
265
            if(!getInstance().isStarted(1)) {
266
                logger.info("[CDM-Server] Server already stopped @ " + host + ":" + httpPort );
314 267
                return;
315 268
            }
316 269
        }
270

  
317 271
        Thread t = new Thread() {
318 272
            @Override
319 273
            public void run() {
......
324 278
                            + getStopServerArgs() + " --stop ";
325 279
                    logger.info("[CDM-Server] Stop Command : " + command);
326 280
                    p = Runtime.getRuntime().exec(command);
327
                    p.waitFor();
328
                    BufferedReader reader =
281

  
282
                    BufferedReader inpReader =
329 283
                            new BufferedReader(new InputStreamReader(p.getInputStream()));
284

  
285
                    BufferedReader errReader =
286
                            new BufferedReader(new InputStreamReader(p.getErrorStream()));
287

  
330 288
                    String line = "";
331
                    while ((line = reader.readLine())!= null) {
332
                        logger.info("CDM-Server Stop : " + line + "\n");
289
                    while ((line = inpReader.readLine())!= null) {
290
                        logger.info("[CDM-Server Stop] : " + line);
291
                    }
292

  
293
                    while ((line = errReader.readLine())!= null) {
294
                        logger.info("[CDM-Server Stop] : " + line);
333 295
                    }
296
                    logger.info("CDM-Server Stopped : ");
334 297
                } catch (Exception e) {
335
                    e.printStackTrace();
298
                    logger.info("[CDM-Server] Could not stop @ " + host + ":" + httpPort + ". Please kill it manually");
299

  
336 300
                }
337 301

  
338 302
            }
......
341 305
        t.setDaemon(true);
342 306
        t.start();
343 307

  
344
        if(isStopped(5)) {
345
            logger.info("[CDM-Server] Stopped Server @ " + host + ":" + port );
346
        } else {
347
            logger.info("[CDM-Server] Could not stop @ " + host + ":" + port + ". Please kill it manually");
308
    }
309

  
310
    public static void stopServerViaJMX(int jmxPort) throws CDMServerException  {
311
        String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:" + jmxPort + "/jmxrmi";
312
        logger.warn("Shutting down Jetty instance ... ");
313

  
314
        try {
315
            JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), null);
316
            connector.connect(null);
317
            MBeanServerConnection connection = connector.getMBeanServerConnection();
318
            ObjectName objectName = new ObjectName("org.eclipse.jetty.server:type=server,id=0");
319
            connection.invoke(objectName, "stop", null, null);
320
            logger.warn("Shutdown command sent");
321
        } catch (InstanceNotFoundException e) {
322
            throw new CDMServerException(e);
323
        } catch (MBeanException e) {
324
            throw new CDMServerException(e);
325
        } catch (ReflectionException e) {
326
            throw new CDMServerException(e);
327
        } catch (IOException e) {
328
            throw new CDMServerException(e);
329
        } catch (MalformedObjectNameException e) {
330
            throw new CDMServerException(e);
348 331
        }
332

  
333

  
334

  
349 335
    }
350 336
}

Also available in: Unified diff