Skip to content
Permalink
Browse files

Improved the initialization of JettyServerCore

  • Loading branch information
Ian Emmons
Ian Emmons committed Dec 11, 2019
1 parent 109846d commit 2d21c047023294cfae6cec391160867f41a8f3f8
@@ -81,7 +81,6 @@ public void run() {
/** Default entry point. */
public static void main(String[] args) {
try {
JettyServerCore.initialize();
JettyServerCore.getInstance().start();

Thread.sleep(4000);
@@ -23,14 +23,8 @@
* configuration files, create log files, create server sockets, create
* threads, etc.
*/
@SuppressWarnings("static-method")
public void init(String[] args) {
try {
LOG.info("Initializing Jetty Daemon");
JettyServerCore.initialize();
} catch (ServerInitException ex) {
LOG.error("Parliament server encountered an exception", ex);
}
// Do nothing
}

/** Start the server running */
@@ -21,12 +21,23 @@
import org.slf4j.LoggerFactory;

public class JettyServerCore {
private static class JettyServerCoreHolder {
private static final JettyServerCore INSTANCE;

static {
try {
INSTANCE = new JettyServerCore();
} catch (Exception ex) {
LOG.error("Parliament server encountered an exception", ex);
throw new IllegalStateException("Parliament server encountered an exception", ex);
}
}
}

private static final String JETTY_CONF_SYS_PROP_NAME = "jettyConfig";
private static final String CONF_JETTY_XML = "conf/jetty.xml";
private static final Logger LOG = LoggerFactory.getLogger(JettyServerCore.class);

private static JettyServerCore instance = null;

private final Server server;

//================================================================
@@ -35,21 +46,15 @@
//
//================================================================

/** Initialize this instance. */
public static void initialize() throws ServerInitException {
instance = new JettyServerCore();
}

/**
* Returns the singleton instance of the JettyServerCore class.
* Get the singleton instance of JettyServerCore. This follows the "lazy
* initialization holder class" idiom for lazy initialization of a static field.
* See Item 83 of Effective Java, Third Edition, by Joshua Bloch for details.
*
* @throws IllegalStateException if the initialize method has not been called yet.
* @return the instance
*/
public static JettyServerCore getInstance() {
if (instance == null) {
throw new IllegalStateException("The JettyServerCore class has not been initialized yet");
}
return instance;
return JettyServerCoreHolder.INSTANCE;
}

//================================================================
@@ -80,7 +85,7 @@ public void stop() {
//================================================================

/** This is private because JettyServerCore is a singleton class. */
private JettyServerCore() throws ServerInitException {
private JettyServerCore() throws Exception {
server = new Server();

String configPath = System.getProperty(JETTY_CONF_SYS_PROP_NAME, CONF_JETTY_XML);
@@ -89,8 +94,6 @@ private JettyServerCore() throws ServerInitException {
XmlConfiguration configuration = new XmlConfiguration(strm);
configuration.configure(server);
validateAndCreateTempDir(getTempDir());
} catch (Exception ex) {
throw new ServerInitException(ex, "Unable to apply server configuration \"%1$s\"", configPath);
}
}

@@ -24,7 +24,6 @@
/** Entry point for running as a Windows service. */
public static void start(String[] args) {
try {
JettyServerCore.initialize();
JettyServerCore.getInstance().start();
LOG.info("Starting Parliament server");
synchronized (lock) {
@@ -19,7 +19,6 @@ public TestServerThread() {
@Override
public void run() {
try {
JettyServerCore.initialize();
JettyServerCore.getInstance().start();
serverHasStarted.countDown();
serverShouldShutDown.await();

0 comments on commit 2d21c04

Please sign in to comment.
You can’t perform that action at this time.