3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.httpinvoker
;
12 import java
.io
.BufferedReader
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStreamReader
;
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
;
25 import javax
.sql
.DataSource
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.springframework
.core
.io
.ClassPathResource
;
29 import org
.springframework
.core
.io
.Resource
;
30 import org
.unitils
.database
.annotations
.TestDataSource
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceBase
;
37 * (Singleton) Server instance which manages a compatible cdmlib-webapp-war.
38 * This is done by launching a jetty instance (using jetty-runner) as an
39 * executed process. The choice of the external process over a more
40 * preferable 'embedded jetty' instance is due to problems arising from the
41 * class loading of classes (e.g. from hibernate core) which are needed
42 * for both the webapp as well as the remoting client.
49 public class CDMServer
{
51 public static final Logger logger
= Logger
.getLogger(CDMServer
.class);
54 protected DataSource dataSource
;
56 private final String name
= "default";
57 private final String host
= "127.0.0.1";
58 private int httpPort
= 9090;
59 private int stopPort
= 9191;
60 private String stopKey
= "jetty-cdm-server";
61 private final String contextPath
= "";
63 public static final Resource DEFAULT_CDM_WEBAPP_RESOURCE
=
64 new ClassPathResource("/etc/jetty/cdmlib-remote-webapp.war");
66 public static final Resource DEFAULT_DATASOURCE_FILE
=
67 new ClassPathResource("datasources.xml");
69 public static final Resource DEFAULT_JETTY_RUNNER_RESOURCE
=
70 new ClassPathResource("/etc/jetty/jetty-runner-9.2.3.v20140905.jar");
72 public static final Resource DEFAULT_JETTY_RESOURCE
=
73 new ClassPathResource("/etc/jetty/start-9.2.3.v20140905.jar");
75 private static CDMServer cdmServer
= null;
76 private static CDMServerException cdmse
= null;
78 private void CDMServer() {}
80 public static CDMServer
getInstance() {
81 if(cdmServer
== null) {
82 cdmServer
= new CDMServer();
87 public String
getName() {
91 public String
getHost() {
95 public int getPort() {
99 public String
getContextPath() {
103 public void setHttpPort(int port
) {
104 this.httpPort
= port
;
107 public void setStopPort(int stopPort
) {
108 this.stopPort
= stopPort
;
111 public void setStopKey(String stopKey
) {
112 this.stopKey
= stopKey
;
116 public static boolean isRunningInEclipse() {
117 return (System
.getProperty("sun.java.command") != null &&
118 System
.getProperty("sun.java.command").startsWith("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner"));
121 private String
getVMArgs() throws IOException
{
122 StringBuilder sb
= new StringBuilder();
123 sb
.append(" -Dspring.profiles.active=remoting");
124 sb
.append(" -Dcdm.beanDefinitionFile=" + DEFAULT_DATASOURCE_FILE
.getFile().getAbsolutePath());
125 sb
.append(" -Dcdm.datasource=cdmTest");
126 return sb
.toString();
129 private String
getStartServerArgs() throws IOException
{
130 StringBuilder sb
= new StringBuilder();
131 sb
.append(" --port " + httpPort
);
132 return sb
.toString();
135 private String
getStopServerSettings() {
136 StringBuilder sb
= new StringBuilder();
137 sb
.append(" --stop-port ");
139 sb
.append(" --stop-key ");
141 return sb
.toString();
144 private String
getStopServerArgs() {
145 StringBuilder sb
= new StringBuilder();
146 sb
.append(" STOP.PORT=");
148 sb
.append(" STOP.KEY=");
150 return sb
.toString();
154 public void start() throws CDMServerException
{
157 * First check if the CDM server responds to a service request, which implies that
158 * the server has started properly. If no response is received then check if the
159 * server is listening on specific host / port, which implies that the server
160 * has started but incorrectly, in which case we try to force stop it (if we can)
161 * and start a new server.
164 logger
.info("[CDM-Server] Server already running @ " + host
+ ":" + httpPort
);
168 Thread t
= new Thread() {
172 StringBuffer output
= new StringBuffer();
175 String command
= "java "
178 + DEFAULT_JETTY_RUNNER_RESOURCE
.getFile().getAbsolutePath()
179 + getStartServerArgs()
180 + getStopServerSettings()
182 + DEFAULT_CDM_WEBAPP_RESOURCE
.getFile().getAbsolutePath();
183 logger
.info("[CDM-Server] Starting server with Command : " + command
);
184 p
= Runtime
.getRuntime().exec(command
);
186 BufferedReader inpReader
=
187 new BufferedReader(new InputStreamReader(p
.getInputStream()));
189 BufferedReader errReader
=
190 new BufferedReader(new InputStreamReader(p
.getErrorStream()));
193 while ((line
= inpReader
.readLine())!= null) {
194 logger
.info("[CDM-Server Start] : " + line
);
197 while ((line
= errReader
.readLine())!= null) {
198 logger
.info("[CDM-Server Start] : " + line
);
201 } catch (Exception e
) {
203 cdmse
= new CDMServerException(e
);
213 logger
.info("[CDM-Server] Server running @ " + host
+ ":" + httpPort
);
215 logger
.info("[CDM-Server] Server not started within given interval");
216 // making sure to kill server if it is not started correctly
219 } catch (Exception e
) {
220 throw new CDMServerException("CDM Server could not be stopped : " + e
.getMessage());
222 throw new CDMServerException("CDM Server not started : ");
228 public boolean isStarted(int checkingIntervals
) throws CDMServerException
{
229 CdmRemoteSourceBase crsb
= new CdmRemoteSourceBase("local-cdm-server",
234 int intervalsCount
= 0;
240 boolean check
= crsb
.checkConnection();
242 logger
.info("[CDM-Server] Running @ " + host
+ ":" + httpPort
);
245 } catch (Exception e
) {
250 } catch (InterruptedException ie
) {
251 throw new CDMServerException("Error checking CDM Server status", ie
);
254 } while (intervalsCount
< checkingIntervals
);
258 public void stop() throws Exception
{
262 public void stop(boolean force
) throws Exception
{
265 if(!getInstance().isStarted(1)) {
266 logger
.info("[CDM-Server] Server already stopped @ " + host
+ ":" + httpPort
);
271 Thread t
= new Thread() {
274 StringBuffer output
= new StringBuffer();
277 String command
= "java -jar " + DEFAULT_JETTY_RESOURCE
.getFile().getAbsolutePath()
278 + getStopServerArgs() + " --stop ";
279 logger
.info("[CDM-Server] Stop Command : " + command
);
280 p
= Runtime
.getRuntime().exec(command
);
282 BufferedReader inpReader
=
283 new BufferedReader(new InputStreamReader(p
.getInputStream()));
285 BufferedReader errReader
=
286 new BufferedReader(new InputStreamReader(p
.getErrorStream()));
289 while ((line
= inpReader
.readLine())!= null) {
290 logger
.info("[CDM-Server Stop] : " + line
);
293 while ((line
= errReader
.readLine())!= null) {
294 logger
.info("[CDM-Server Stop] : " + line
);
296 logger
.info("CDM-Server Stopped : ");
297 } catch (Exception e
) {
298 logger
.info("[CDM-Server] Could not stop @ " + host
+ ":" + httpPort
+ ". Please kill it manually");
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 ... ");
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
);