Skip to content

Commit 6ef43b1

Browse files
committed
refactor
1 parent c0d00f5 commit 6ef43b1

7 files changed

Lines changed: 87 additions & 35 deletions

File tree

src/main/java/de/rwth/idsg/steve/Application.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
* @since 14.01.2015
1212
*/
1313
@Slf4j
14-
public class Application {
14+
public class Application implements ApplicationStarter, AutoCloseable {
1515

16-
public static void main(String[] args) throws Exception {
16+
private final ApplicationStarter delegate;
1717

18+
public Application() {
1819
// For Hibernate validator
1920
System.setProperty("org.jboss.logging.provider", "slf4j");
2021

@@ -27,9 +28,35 @@ public static void main(String[] args) throws Exception {
2728
log.info("Loaded the properties. Starting with the '{}' profile", sc.getProfile());
2829

2930
if (sc.getProfile().isProd()) {
30-
new SteveProdStarter().start();
31+
delegate = new SteveProdStarter();
3132
} else {
32-
new SteveDevStarter().start();
33+
delegate = new SteveDevStarter();
3334
}
3435
}
36+
37+
public static void main(String[] args) throws Exception {
38+
Application app = new Application();
39+
app.start();
40+
app.join();
41+
}
42+
43+
@Override
44+
public void start() throws Exception {
45+
delegate.start();
46+
}
47+
48+
@Override
49+
public void join() throws Exception {
50+
delegate.join();
51+
}
52+
53+
@Override
54+
public void stop() throws Exception {
55+
delegate.stop();
56+
}
57+
58+
@Override
59+
public void close() throws Exception {
60+
stop();
61+
}
3562
}

src/main/java/de/rwth/idsg/steve/ApplicationStarter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
*/
77
public interface ApplicationStarter {
88
void start() throws Exception;
9+
void join() throws Exception;
10+
void stop() throws Exception;
911
}

src/main/java/de/rwth/idsg/steve/JettyServer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class JettyServer {
4343
/**
4444
* A fully configured Jetty Server instance
4545
*/
46-
public void prepare() throws Exception {
46+
private void prepare() {
4747

4848
// === jetty.xml ===
4949
// Setup Threadpool
@@ -129,6 +129,8 @@ private ServerConnector httpsConnector(HttpConfiguration httpConfig) {
129129
* Starts the Jetty Server instance
130130
*/
131131
public void start() throws Exception {
132+
prepare();
133+
132134
if (server != null) {
133135
server.start();
134136
}
@@ -143,6 +145,12 @@ public void join() throws Exception {
143145
}
144146
}
145147

148+
public void stop() throws Exception {
149+
if (server != null) {
150+
server.stop();
151+
}
152+
}
153+
146154
public boolean isStarted() {
147155
return server != null && server.isStarted();
148156
}

src/main/java/de/rwth/idsg/steve/SteveAppContext.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public SteveAppContext() {
4545
springContext.scan("de.rwth.idsg.steve.config");
4646
}
4747

48-
public HandlerCollection getHandlers() throws IOException {
48+
public HandlerCollection getHandlers() {
4949
HandlerList handlerList = new HandlerList();
5050
handlerList.setHandlers(
5151
new Handler[]{
@@ -55,7 +55,7 @@ public HandlerCollection getHandlers() throws IOException {
5555
return handlerList;
5656
}
5757

58-
private Handler getWebApp() throws IOException {
58+
private Handler getWebApp() {
5959
if (CONFIG.getJetty().isGzipEnabled()) {
6060
return enableGzip(initWebApp());
6161
} else {
@@ -74,10 +74,10 @@ private Handler enableGzip(WebAppContext ctx) {
7474
return gzipHandler;
7575
}
7676

77-
private WebAppContext initWebApp() throws IOException {
77+
private WebAppContext initWebApp() {
7878
WebAppContext ctx = new WebAppContext();
7979
ctx.setContextPath(CONFIG.getContextPath());
80-
ctx.setResourceBase(new ClassPathResource("webapp").getURI().toString());
80+
ctx.setResourceBase(getWebAppURIAsString());
8181

8282
// Disable directory listings if no index.html is found.
8383
ctx.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
@@ -139,6 +139,14 @@ private HashSet<String> getRedirectSet() {
139139
return redirectSet;
140140
}
141141

142+
private static String getWebAppURIAsString() {
143+
try {
144+
return new ClassPathResource("webapp").getURI().toString();
145+
} catch (IOException e) {
146+
throw new RuntimeException(e);
147+
}
148+
}
149+
142150
// -------------------------------------------------------------------------
143151
// JSP stuff
144152
//
@@ -149,7 +157,7 @@ private HashSet<String> getRedirectSet() {
149157
// http://examples.javacodegeeks.com/enterprise-java/jetty/jetty-jsp-example
150158
// -------------------------------------------------------------------------
151159

152-
private void initJSP(WebAppContext ctx) throws IOException {
160+
private void initJSP(WebAppContext ctx) {
153161
ctx.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
154162
ctx.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
155163
ctx.addBean(new ServletContainerInitializersStarter(ctx), true);

src/main/java/de/rwth/idsg/steve/SteveDevStarter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@
88
*/
99
public class SteveDevStarter implements ApplicationStarter {
1010

11+
private final JettyServer jettyServer;
12+
13+
SteveDevStarter() {
14+
this.jettyServer = new JettyServer();
15+
}
16+
1117
@Override
1218
public void start() throws Exception {
13-
JettyServer jettyServer = new JettyServer();
14-
jettyServer.prepare();
1519
jettyServer.start();
20+
}
21+
22+
@Override
23+
public void stop() throws Exception {
24+
jettyServer.stop();
25+
}
26+
27+
@Override
28+
public void join() throws Exception {
1629
jettyServer.join();
1730
}
1831
}

src/main/java/de/rwth/idsg/steve/SteveProdStarter.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ public class SteveProdStarter implements ApplicationStarter {
2222
private static final String HINT = "Hint: You can stop the application by pressing CTRL+C" + sep();
2323
private static final String REFER = "Please refer to the log file for details";
2424

25-
private JettyServer jettyServer;
25+
private final JettyServer jettyServer;
2626
private Thread dotThread;
2727

28+
SteveProdStarter() {
29+
this.jettyServer = new JettyServer();
30+
}
31+
2832
@Override
2933
public void start() throws Exception {
30-
3134
starting();
32-
jettyServer = new JettyServer();
3335

3436
try {
35-
jettyServer.prepare();
3637
jettyServer.start();
3738
started();
3839

@@ -49,10 +50,18 @@ public void start() throws Exception {
4950
throw e;
5051
}
5152
}
53+
}
5254

55+
@Override
56+
public void join() throws Exception {
5357
jettyServer.join();
5458
}
5559

60+
@Override
61+
public void stop() throws Exception {
62+
jettyServer.stop();
63+
}
64+
5665
// -------------------------------------------------------------------------
5766
// Private helpers
5867
// -------------------------------------------------------------------------

src/test/java/de/rwth/idsg/steve/ApplicationTest.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
import ocpp.cs._2010._08.BootNotificationResponse;
99
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
1010
import org.apache.cxf.ws.addressing.WSAddressingFeature;
11-
import org.joda.time.DateTime;
12-
import org.joda.time.DateTimeZone;
1311
import org.junit.Assert;
1412
import org.junit.Ignore;
1513
import org.junit.Test;
1614

1715
import javax.xml.ws.soap.SOAPBinding;
18-
import java.util.TimeZone;
1916
import java.util.UUID;
2017

2118
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
@@ -68,22 +65,10 @@ public void testSoapPipeline() throws Exception {
6865
Assert.assertEquals(ocpp.cs._2012._06.AuthorizationStatus.INVALID, auth15.getIdTagInfo().getStatus());
6966
}
7067

71-
private JettyServer startApp() throws Exception {
72-
// For Hibernate validator
73-
System.setProperty("org.jboss.logging.provider", "slf4j");
74-
75-
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
76-
DateTimeZone.setDefault(DateTimeZone.UTC);
77-
log.info("Date/time zone of the application is set to UTC. Current date/time: {}", DateTime.now());
78-
79-
SteveConfiguration sc = CONFIG;
80-
81-
log.info("Loaded the properties. Starting with the '{}' profile", sc.getProfile());
82-
83-
JettyServer jettyServer = new JettyServer();
84-
jettyServer.prepare();
85-
jettyServer.start();
86-
return jettyServer;
68+
private Application startApp() throws Exception {
69+
Application app = new Application();
70+
app.start();
71+
return app;
8772
}
8873

8974
private static String getRandomString() {

0 commit comments

Comments
 (0)