Skip to content

Commit a96e77b

Browse files
committed
do away with 'public static' config fields
because good OO design and making checkstyle happy
1 parent ba5ca67 commit a96e77b

10 files changed

Lines changed: 142 additions & 126 deletions
Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package de.rwth.idsg.steve;
22

3-
import de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategyEnum;
4-
import de.rwth.idsg.steve.utils.PropertiesFileLoader;
53
import lombok.extern.slf4j.Slf4j;
64
import org.joda.time.DateTime;
75
import org.joda.time.DateTimeZone;
86

9-
import java.io.IOException;
107
import java.util.TimeZone;
118

12-
import static de.rwth.idsg.steve.SteveConfiguration.Auth;
13-
import static de.rwth.idsg.steve.SteveConfiguration.DB;
14-
import static de.rwth.idsg.steve.SteveConfiguration.Jetty;
15-
import static de.rwth.idsg.steve.SteveConfiguration.Ocpp;
16-
import static de.rwth.idsg.steve.SteveConfiguration.PROFILE;
17-
import static de.rwth.idsg.steve.SteveConfiguration.STEVE_VERSION;
18-
199
/**
2010
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
2111
* @since 14.01.2015
@@ -32,49 +22,14 @@ public static void main(String[] args) throws Exception {
3222
DateTimeZone.setDefault(DateTimeZone.UTC);
3323
log.info("Date/time zone of the application is set to UTC. Current date/time: {}", DateTime.now());
3424

35-
loadProperties();
25+
SteveConfiguration sc = SteveConfiguration.CONFIG;
3626

37-
log.info("Loaded the properties. Starting with the '{}' profile", PROFILE);
27+
log.info("Loaded the properties. Starting with the '{}' profile", sc.getProfile());
3828

39-
if (PROFILE.isProd()) {
29+
if (sc.getProfile().isProd()) {
4030
new SteveProdStarter().start();
4131
} else {
4232
new SteveDevStarter().start();
4333
}
4434
}
45-
46-
private static void loadProperties() throws IOException {
47-
PropertiesFileLoader prop = new PropertiesFileLoader("main.properties");
48-
49-
STEVE_VERSION = prop.getString("steve.version");
50-
PROFILE = ApplicationProfile.fromName(prop.getString("profile"));
51-
52-
DB.IP = prop.getString("db.ip");
53-
DB.PORT = prop.getInt("db.port");
54-
DB.SCHEMA = prop.getString("db.schema");
55-
DB.USERNAME = prop.getString("db.user");
56-
DB.PASSWORD = prop.getString("db.password");
57-
DB.SQL_LOGGING = prop.getBoolean("db.sql.logging");
58-
59-
Auth.USERNAME = prop.getString("auth.user");
60-
Auth.PASSWORD = prop.getString("auth.password");
61-
62-
Jetty.SERVER_HOST = prop.getString("server.host");
63-
Jetty.GZIP_ENABLED = prop.getBoolean("server.gzip.enabled");
64-
65-
Jetty.HTTP_ENABLED = prop.getBoolean("http.enabled");
66-
Jetty.HTTP_PORT = prop.getInt("http.port");
67-
Jetty.HTTPS_ENABLED = prop.getBoolean("https.enabled");
68-
Jetty.HTTPS_PORT = prop.getInt("https.port");
69-
Jetty.KEY_STORE_PATH = prop.getString("keystore.path");
70-
Jetty.KEY_STORE_PASSWORD = prop.getString("keystore.password");
71-
72-
Ocpp.WS_SESSION_SELECT_STRATEGY =
73-
WsSessionSelectStrategyEnum.fromName(prop.getString("ws.session.select.strategy"));
74-
75-
if (!(Jetty.HTTP_ENABLED || Jetty.HTTPS_ENABLED)) {
76-
throw new IllegalArgumentException(
77-
"HTTP and HTTPS are both disabled. Well, how do you want to access the server, then?");
78-
}
79-
}
8035
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.concurrent.TimeUnit;
2424

25+
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
26+
2527
/**
2628
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
2729
* @since 12.12.2014
@@ -57,7 +59,7 @@ public void prepare() throws Exception {
5759
// HTTP Configuration
5860
HttpConfiguration httpConfig = new HttpConfiguration();
5961
httpConfig.setSecureScheme(HttpScheme.HTTPS.asString());
60-
httpConfig.setSecurePort(SteveConfiguration.Jetty.HTTPS_PORT);
62+
httpConfig.setSecurePort(CONFIG.getJetty().getHttpsPort());
6163
httpConfig.setOutputBufferSize(32768);
6264
httpConfig.setRequestHeaderSize(8192);
6365
httpConfig.setResponseHeaderSize(8192);
@@ -71,11 +73,11 @@ public void prepare() throws Exception {
7173
server.setStopAtShutdown(true);
7274
server.setStopTimeout(STOP_TIMEOUT);
7375

74-
if (SteveConfiguration.Jetty.HTTP_ENABLED) {
76+
if (CONFIG.getJetty().isHttpEnabled()) {
7577
server.addConnector(httpConnector(httpConfig));
7678
}
7779

78-
if (SteveConfiguration.Jetty.HTTPS_ENABLED) {
80+
if (CONFIG.getJetty().isHttpsEnabled()) {
7981
server.addConnector(httpsConnector(httpConfig));
8082
}
8183

@@ -86,8 +88,8 @@ public void prepare() throws Exception {
8688
private ServerConnector httpConnector(HttpConfiguration httpConfig) {
8789
// === jetty-http.xml ===
8890
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
89-
http.setHost(SteveConfiguration.Jetty.SERVER_HOST);
90-
http.setPort(SteveConfiguration.Jetty.HTTP_PORT);
91+
http.setHost(CONFIG.getJetty().getServerHost());
92+
http.setPort(CONFIG.getJetty().getHttpPort());
9193
http.setIdleTimeout(IDLE_TIMEOUT);
9294
return http;
9395
}
@@ -96,9 +98,9 @@ private ServerConnector httpsConnector(HttpConfiguration httpConfig) {
9698
// === jetty-https.xml ===
9799
// SSL Context Factory
98100
SslContextFactory sslContextFactory = new SslContextFactory();
99-
sslContextFactory.setKeyStorePath(SteveConfiguration.Jetty.KEY_STORE_PATH);
100-
sslContextFactory.setKeyStorePassword(SteveConfiguration.Jetty.KEY_STORE_PASSWORD);
101-
sslContextFactory.setKeyManagerPassword(SteveConfiguration.Jetty.KEY_STORE_PASSWORD);
101+
sslContextFactory.setKeyStorePath(CONFIG.getJetty().getKeyStorePath());
102+
sslContextFactory.setKeyStorePassword(CONFIG.getJetty().getKeyStorePassword());
103+
sslContextFactory.setKeyManagerPassword(CONFIG.getJetty().getKeyStorePassword());
102104
sslContextFactory.setExcludeCipherSuites(
103105
"SSL_RSA_WITH_DES_CBC_SHA",
104106
"SSL_DHE_RSA_WITH_DES_CBC_SHA",
@@ -116,8 +118,8 @@ private ServerConnector httpsConnector(HttpConfiguration httpConfig) {
116118
ServerConnector https = new ServerConnector(server,
117119
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
118120
new HttpConnectionFactory(httpsConfig));
119-
https.setHost(SteveConfiguration.Jetty.SERVER_HOST);
120-
https.setPort(SteveConfiguration.Jetty.HTTPS_PORT);
121+
https.setHost(CONFIG.getJetty().getServerHost());
122+
https.setPort(CONFIG.getJetty().getHttpsPort());
121123
https.setIdleTimeout(IDLE_TIMEOUT);
122124
return https;
123125
}
@@ -173,11 +175,11 @@ private String getConnectorPath(ServerConnector sc) {
173175
} catch (UnknownHostException e) {
174176
// Well, we failed to read from system, fall back to main.properties.
175177
// Better than nothing
176-
host = SteveConfiguration.Jetty.SERVER_HOST;
178+
host = CONFIG.getJetty().getServerHost();
177179
}
178180
}
179181

180-
String layout = "%s://%s:%d" + SteveConfiguration.CONTEXT_PATH;
182+
String layout = "%s://%s:%d" + CONFIG.getContextPath();
181183

182184
return String.format(layout, prefix, host, port);
183185
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.EnumSet;
2828
import java.util.List;
2929

30+
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
31+
3032
/**
3133
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
3234
* @since 07.04.2015
@@ -51,7 +53,7 @@ public HandlerCollection getHandlers() throws IOException {
5153
}
5254

5355
private Handler getWebApp() throws IOException {
54-
if (SteveConfiguration.Jetty.GZIP_ENABLED) {
56+
if (CONFIG.getJetty().isGzipEnabled()) {
5557
return enableGzip(initWebApp());
5658
} else {
5759
return initWebApp();
@@ -71,7 +73,7 @@ private Handler enableGzip(WebAppContext ctx) {
7173

7274
private WebAppContext initWebApp() throws IOException {
7375
WebAppContext ctx = new WebAppContext();
74-
ctx.setContextPath(SteveConfiguration.CONTEXT_PATH);
76+
ctx.setContextPath(CONFIG.getContextPath());
7577
ctx.setResourceBase(new ClassPathResource("webapp").getURI().toString());
7678

7779
// Disable directory listings if no index.html is found.
@@ -81,13 +83,13 @@ private WebAppContext initWebApp() throws IOException {
8183
ServletHolder cxf = new ServletHolder("cxf", new CXFServlet());
8284

8385
ctx.addEventListener(new ContextLoaderListener(springContext));
84-
ctx.addServlet(web, SteveConfiguration.SPRING_MAPPING);
85-
ctx.addServlet(cxf, SteveConfiguration.CXF_MAPPING);
86+
ctx.addServlet(web, CONFIG.getSpringMapping());
87+
ctx.addServlet(cxf, CONFIG.getCxfMapping());
8688

8789
// Register Spring's filter chain for security. The name is not arbitrary, but is as expected by Spring.
8890
ctx.addFilter(
8991
new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")),
90-
SteveConfiguration.SPRING_MANAGER_MAPPING,
92+
CONFIG.getSpringManagerMapping(),
9193
EnumSet.allOf(DispatcherType.class)
9294
);
9395

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,131 @@
11
package de.rwth.idsg.steve;
22

33
import de.rwth.idsg.steve.ocpp.ws.custom.WsSessionSelectStrategyEnum;
4+
import de.rwth.idsg.steve.utils.PropertiesFileLoader;
5+
import lombok.Builder;
6+
import lombok.Getter;
47

58
/**
69
* @author Sevket Goekay <goekay@dbis.rwth-aachen.de>
710
* @since 19.08.2014
811
*/
12+
@Getter
913
public final class SteveConfiguration {
10-
private SteveConfiguration() { }
14+
public static final SteveConfiguration CONFIG = new SteveConfiguration();
1115

1216
// Root mapping for Spring
13-
public static final String SPRING_MAPPING = "/";
17+
private String springMapping = "/";
1418
// Web frontend
15-
public static final String SPRING_MANAGER_MAPPING = "/manager/*";
19+
private String springManagerMapping = "/manager/*";
1620
// Mapping for CXF SOAP services
17-
public static final String CXF_MAPPING = "/services/*";
21+
private String cxfMapping = "/services/*";
1822
// Just to be backwards compatible with previous versions of Steve,
1923
// since there might be already configured chargepoints expecting the older path.
2024
// Otherwise, might as well be "/".
21-
public static final String CONTEXT_PATH = "/steve";
25+
private String contextPath = "/steve";
2226
// Dummy service path
23-
public static final String ROUTER_ENDPOINT_PATH = "/CentralSystemService";
27+
private String routerEndpointPath = "/CentralSystemService";
2428

2529
// -------------------------------------------------------------------------
2630
// main.properties
2731
// -------------------------------------------------------------------------
2832

29-
public static String STEVE_VERSION;
30-
public static ApplicationProfile PROFILE;
33+
private String steveVersion;
34+
private ApplicationProfile profile;
35+
private Ocpp ocpp;
36+
private Auth auth;
37+
private DB db;
38+
private Jetty jetty;
3139

32-
/**
33-
* Jetty configuration
34-
*/
35-
public static final class Jetty {
36-
public static String SERVER_HOST;
37-
public static boolean GZIP_ENABLED;
40+
private SteveConfiguration() {
41+
PropertiesFileLoader p = new PropertiesFileLoader("main.properties");
42+
43+
steveVersion = p.getString("steve.version");
44+
profile = ApplicationProfile.fromName(p.getString("profile"));
45+
46+
jetty = Jetty.builder()
47+
.serverHost(p.getString("server.host"))
48+
.gzipEnabled(p.getBoolean("server.gzip.enabled"))
49+
.httpEnabled(p.getBoolean("http.enabled"))
50+
.httpPort(p.getInt("http.port"))
51+
.httpsEnabled(p.getBoolean("https.enabled"))
52+
.httpsPort(p.getInt("https.port"))
53+
.keyStorePath(p.getString("keystore.path"))
54+
.keyStorePassword(p.getString("keystore.password"))
55+
.build();
56+
57+
db = DB.builder()
58+
.ip(p.getString("db.ip"))
59+
.port(p.getInt("db.port"))
60+
.schema(p.getString("db.schema"))
61+
.userName(p.getString("db.user"))
62+
.password(p.getString("db.password"))
63+
.sqlLogging(p.getBoolean("db.sql.logging"))
64+
.build();
65+
66+
auth = Auth.builder()
67+
.userName(p.getString("auth.user"))
68+
.password(p.getString("auth.password"))
69+
.build();
70+
71+
ocpp = Ocpp.builder()
72+
.wsSessionSelectStrategy(
73+
WsSessionSelectStrategyEnum.fromName(p.getString("ws.session.select.strategy")))
74+
.build();
75+
76+
validate();
77+
}
78+
79+
private void validate() {
80+
if (!(jetty.httpEnabled || jetty.httpsEnabled)) {
81+
throw new IllegalArgumentException(
82+
"HTTP and HTTPS are both disabled. Well, how do you want to access the server, then?");
83+
}
84+
}
85+
86+
// -------------------------------------------------------------------------
87+
// Class declarations
88+
// -------------------------------------------------------------------------
89+
90+
// Jetty configuration
91+
@Builder @Getter
92+
public static class Jetty {
93+
private String serverHost;
94+
private boolean gzipEnabled;
3895

3996
// HTTP
40-
public static boolean HTTP_ENABLED;
41-
public static int HTTP_PORT;
97+
private boolean httpEnabled;
98+
private int httpPort;
4299

43100
// HTTPS
44-
public static boolean HTTPS_ENABLED;
45-
public static int HTTPS_PORT;
46-
public static String KEY_STORE_PATH;
47-
public static String KEY_STORE_PASSWORD;
101+
private boolean httpsEnabled;
102+
private int httpsPort;
103+
private String keyStorePath;
104+
private String keyStorePassword;
48105
}
49106

50-
/**
51-
* Database configuration
52-
*/
53-
public static final class DB {
54-
public static String IP;
55-
public static int PORT;
56-
public static String SCHEMA;
57-
public static String USERNAME;
58-
public static String PASSWORD;
59-
public static boolean SQL_LOGGING;
107+
// Database configuration
108+
@Builder @Getter
109+
public static class DB {
110+
private String ip;
111+
private int port;
112+
private String schema;
113+
private String userName;
114+
private String password;
115+
private boolean sqlLogging;
60116
}
61117

62-
/**
63-
* Credentials for Web interface access
64-
*/
65-
public static final class Auth {
66-
public static String USERNAME;
67-
public static String PASSWORD;
118+
// Credentials for Web interface access
119+
@Builder @Getter
120+
public static class Auth {
121+
private String userName;
122+
private String password;
68123
}
69124

70-
/**
71-
* OCPP-related configuration
72-
*/
73-
public static final class Ocpp {
74-
public static WsSessionSelectStrategyEnum WS_SESSION_SELECT_STRATEGY;
125+
// OCPP-related configuration
126+
@Builder @Getter
127+
public static class Ocpp {
128+
private WsSessionSelectStrategyEnum wsSessionSelectStrategy;
75129
}
76130

77131
}

0 commit comments

Comments
 (0)