Skip to content

Commit 443e4ed

Browse files
author
Ke Wang
committed
Environment bean needs to be created before refreshing the spring root
context as it needs to collect the ModuleBeans. No need for AccessLogLocationBean @component or @Autowired addAccessLog method in GrizzlyApplication class need to handle context where there are slashes in the context
1 parent b1db025 commit 443e4ed

File tree

7 files changed

+149
-162
lines changed

7 files changed

+149
-162
lines changed

micro-core/src/main/java/com/aol/micro/server/config/Classes.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import nonautoscan.com.aol.micro.server.ScheduleAndAsyncConfig;
77

88
import com.aol.micro.server.events.ConfigureActiveJobsAspect;
9+
import com.aol.micro.server.module.ConfigureEnviroment;
910
import com.aol.micro.server.rest.resources.ConfigureResources;
1011
import com.aol.micro.server.spring.datasource.DataSourceBuilder;
1112
import com.aol.micro.server.spring.datasource.JdbcConfig;
@@ -28,31 +29,25 @@
2829
*
2930
*/
3031
public enum Classes {
31-
32+
3233
/**
3334
* CORE CLASSES are the Core Microserver Spring Configuration classes
3435
* Property support, Guava Event Bus, Spring AOP & Scheduling
3536
* Codahale Metrics, Event tracking etc
3637
*/
37-
CORE_CLASSES(PropertyFileConfig.class,
38-
MiscellaneousConfig.class, AopConfig.class, CodahaleMetricsConfigurer.class,
39-
ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class,ConfigureResources.class),
40-
JDBC_CLASSES(JdbcConfig.class ,
41-
DAOProvider.class,DataSourceBuilder.class,SQL.class,SpringDataConfig.class),
42-
ROMA_ROW_MAPPER(RomaRowMapperConfig.class),
43-
HIBERNATE_CLASSES(HibernateConfig.class,JdbcConfig.class ,
44-
GenericHibernateService.class,DAOProvider.class,DataSourceBuilder.class,SQL.class),
38+
CORE_CLASSES(PropertyFileConfig.class, MiscellaneousConfig.class, AopConfig.class, CodahaleMetricsConfigurer.class,
39+
ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class, ConfigureResources.class, ConfigureEnviroment.class),
40+
JDBC_CLASSES(JdbcConfig.class, DAOProvider.class, DataSourceBuilder.class, SQL.class, SpringDataConfig.class),
41+
ROMA_ROW_MAPPER(RomaRowMapperConfig.class),
42+
HIBERNATE_CLASSES(HibernateConfig.class, JdbcConfig.class, GenericHibernateService.class, DAOProvider.class, DataSourceBuilder.class, SQL.class),
4543
SPRING_DATA_CLASSES(SpringDataConfig.class),
46-
DATASOURCE_CLASSES(JdbcConfig.class ,
47-
DataSourceBuilder.class);
48-
44+
DATASOURCE_CLASSES(JdbcConfig.class, DataSourceBuilder.class);
45+
4946
@Getter
5047
private final Class[] classes;
51-
52-
private Classes(Class... classes){
48+
49+
private Classes(Class... classes) {
5350
this.classes = classes;
5451
}
5552

56-
5753
}
58-
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.aol.micro.server.module;
2+
3+
import java.util.Collection;
4+
import java.util.Properties;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
@Configuration
12+
public class ConfigureEnviroment {
13+
14+
@Autowired(required = false)
15+
private Collection<ModuleBean> modules;
16+
17+
@Bean
18+
public Environment environment(@Qualifier("propertyFactory") Properties props) {
19+
if (modules == null) {
20+
return new Environment(props);
21+
}
22+
return new Environment(props, modules);
23+
}
24+
25+
}

micro-core/src/main/java/com/aol/micro/server/module/Environment.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,16 @@
66
import java.util.Properties;
77
import java.util.stream.Collectors;
88

9-
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.stereotype.Component;
11-
129
import com.google.common.collect.ImmutableMap;
1310
import com.google.common.collect.Maps;
1411

15-
//@Component
1612
public class Environment {
1713

1814
private volatile Map<String, ModuleBean> modulePort;
1915
private final Properties properties;
2016
private volatile int nextPort = 8080;
2117

22-
23-
public Environment(Properties propertyFactory,Collection<ModuleBean> modules) {
18+
public Environment(Properties propertyFactory, Collection<ModuleBean> modules) {
2419
modulePort = modules.stream().collect(Collectors.toMap(key -> key.getModule().getContext(), value -> value));
2520
this.properties = propertyFactory;
2621
}
@@ -30,24 +25,23 @@ public Environment(Properties propertyFactory) {
3025
this.properties = propertyFactory;
3126

3227
}
33-
28+
3429
public ModuleBean getModuleBean(Module module) {
3530
return modulePort.get(module.getContext());
3631
}
32+
3733
public void assureModule(Module module) {
38-
if(!modulePort.containsKey(module.getContext())){
34+
if (!modulePort.containsKey(module.getContext())) {
3935
Map<String, ModuleBean> builder = Maps.newHashMap();
4036
builder.putAll(modulePort);
41-
builder.put(module.getContext(),ModuleBean.builder().port(getPort(module)).build());
37+
builder.put(module.getContext(), ModuleBean.builder().port(getPort(module)).build());
4238
modulePort = ImmutableMap.copyOf(builder);
4339
}
44-
45-
40+
4641
}
42+
4743
private int getPort(Module module) {
48-
49-
return Integer.valueOf(Optional.ofNullable(
50-
properties.get(module.getContext()+".port"))
51-
.orElse(nextPort++).toString());
44+
45+
return Integer.valueOf(Optional.ofNullable(properties.get(module.getContext() + ".port")).orElse(nextPort++).toString());
5246
}
5347
}

micro-core/src/main/java/com/aol/micro/server/servers/AccessLogLocationBean.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22

33
import lombok.Getter;
44

5-
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.beans.factory.annotation.Value;
7-
import org.springframework.stereotype.Component;
8-
9-
@Component
105
@Getter
116
public class AccessLogLocationBean {
127

138
private final String accessLogLocation;
149

15-
@Autowired
16-
public AccessLogLocationBean(@Value("${access.log.output:./logs/}") String accessLogLocation) {
10+
public AccessLogLocationBean(String accessLogLocation) {
1711
this.accessLogLocation = accessLogLocation;
1812
}
1913

micro-core/src/main/java/com/aol/micro/server/servers/grizzly/GrizzlyApplication.java

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.glassfish.grizzly.http.server.accesslog.AccessLogBuilder;
1717
import org.glassfish.grizzly.servlet.ServletRegistration;
1818
import org.glassfish.grizzly.servlet.WebappContext;
19-
import org.glassfish.jersey.server.ResourceConfig;
2019
import org.glassfish.jersey.servlet.ServletContainer;
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
@@ -32,21 +31,21 @@
3231
import com.aol.simple.react.exceptions.ExceptionSoftener;
3332
import com.google.common.collect.ImmutableList;
3433

35-
@AllArgsConstructor(access=AccessLevel.PRIVATE)
36-
public class GrizzlyApplication implements ServerApplication {
34+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
35+
public class GrizzlyApplication implements ServerApplication {
3736

3837
private final Logger logger = LoggerFactory.getLogger(getClass());
3938
private final ExceptionSoftener softener = ExceptionSoftener.singleton.factory.getInstance();
4039

4140
@Getter
4241
private final ServerData serverData;
43-
42+
4443
private final ImmutableList<FilterData> filterData;
4544
private final ImmutableList<ServletData> servletData;
4645
private final ImmutableList<ServletContextListener> servletContextListenerData;
4746
@Wither
4847
private final SSLProperties SSLProperties;
49-
48+
5049
public GrizzlyApplication(AllData serverData) {
5150
this.serverData = serverData.getServerData();
5251
this.filterData = serverData.getFilterDataList();
@@ -55,45 +54,42 @@ public GrizzlyApplication(AllData serverData) {
5554
this.SSLProperties = null;
5655
}
5756

58-
public void run(CompletableFuture start,CompletableFuture end) {
57+
public void run(CompletableFuture start, CompletableFuture end) {
5958

6059
WebappContext webappContext = new WebappContext("WebappContext", "");
6160

62-
new ServletContextListenerConfigurer(serverData,servletContextListenerData);
63-
61+
new ServletContextListenerConfigurer(serverData, servletContextListenerData);
62+
6463
addServlet(webappContext);
6564

66-
new ServletConfigurer(serverData,servletData).addServlets(webappContext);
65+
new ServletConfigurer(serverData, servletData).addServlets(webappContext);
6766

68-
new FilterConfigurer(serverData,this.filterData).addFilters(webappContext);
67+
new FilterConfigurer(serverData, this.filterData).addFilters(webappContext);
6968

7069
addListeners(webappContext);
7170

72-
HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0",
73-
serverData.getPort());
71+
HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0", serverData.getPort());
7472

7573
addAccessLog(httpServer);
76-
if(SSLProperties!=null)
74+
if (SSLProperties != null)
7775
this.createSSLListener(serverData.getPort());
7876

79-
startServer(webappContext, httpServer,start,end);
77+
startServer(webappContext, httpServer, start, end);
8078
}
8179

82-
private void startServer(WebappContext webappContext, HttpServer httpServer, CompletableFuture start, CompletableFuture end) {
80+
private void startServer(WebappContext webappContext, HttpServer httpServer, CompletableFuture start, CompletableFuture end) {
8381
webappContext.deploy(httpServer);
8482
try {
85-
logger.info("Starting application {} on port {}", serverData
86-
.getModule().getContext(), serverData.getPort());
87-
logger.info("Browse to http://localhost:{}/{}/application.wadl",
88-
serverData.getPort(), serverData.getModule().getContext());
83+
logger.info("Starting application {} on port {}", serverData.getModule().getContext(), serverData.getPort());
84+
logger.info("Browse to http://localhost:{}/{}/application.wadl", serverData.getPort(), serverData.getModule().getContext());
8985
logger.info("Configured resource classes :-");
90-
serverData.extractResources()
91-
.forEach(t -> logger.info(t.v1 + " : " + "http://localhost:"+serverData.getPort()
92-
+"/"+serverData.getModule().getContext() + t.v2 ));;
86+
serverData.extractResources().forEach(
87+
t -> logger.info(t.v1 + " : " + "http://localhost:" + serverData.getPort() + "/" + serverData.getModule().getContext() + t.v2));
88+
;
9389
httpServer.start();
9490
start.complete(true);
9591
end.get();
96-
92+
9793
} catch (IOException e) {
9894
softener.throwSoftenedException(e);
9995
} catch (ExecutionException e) {
@@ -108,58 +104,47 @@ private void startServer(WebappContext webappContext, HttpServer httpServer, Com
108104

109105
private void addAccessLog(HttpServer httpServer) {
110106
try {
111-
String accessLogLocation = serverData.getRootContext()
112-
.getBean(AccessLogLocationBean.class)
113-
.getAccessLogLocation();
114-
final AccessLogBuilder builder = new AccessLogBuilder(
115-
accessLogLocation + serverData.getModule().getContext()
116-
+ "-access.log");
107+
String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation();
108+
109+
accessLogLocation = accessLogLocation
110+
+ serverData.getModule().getContext().substring(0, serverData.getModule().getContext().indexOf("/")) + "-access.log";
111+
final AccessLogBuilder builder = new AccessLogBuilder(accessLogLocation);
112+
117113
builder.rotatedDaily();
118114
builder.rotationPattern("yyyy-MM-dd");
119115
builder.instrument(httpServer.getServerConfiguration());
120116
} catch (Exception e) {
121-
logger.error(ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG
122-
.toString() + ": " + e.getMessage());
117+
logger.error(ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getMessage());
123118
if (e.getCause() != null)
124-
logger.error("CAUSED BY: "
125-
+ ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG
126-
.toString() + ": " + e.getCause().getMessage());
119+
logger.error("CAUSED BY: " + ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getCause().getMessage());
127120

128121
}
129122

130123
}
131124

132125
private void addServlet(WebappContext webappContext) {
133126
ServletContainer container = new ServletContainer();
134-
ServletRegistration servletRegistration = webappContext.addServlet(
135-
"Jersey Spring Web Application", container);
136-
servletRegistration.setInitParameter("javax.ws.rs.Application",
137-
this.serverData.getModule().getJaxWsRsApplication());
138-
servletRegistration.setInitParameter(
139-
"jersey.config.server.provider.packages", this.serverData
140-
.getModule().getProviders());
127+
ServletRegistration servletRegistration = webappContext.addServlet("Jersey Spring Web Application", container);
128+
servletRegistration.setInitParameter("javax.ws.rs.Application", this.serverData.getModule().getJaxWsRsApplication());
129+
servletRegistration.setInitParameter("jersey.config.server.provider.packages", this.serverData.getModule().getProviders());
141130
servletRegistration.setLoadOnStartup(1);
142131
servletRegistration.addMapping(serverData.getBaseUrlPattern());
143132
}
144133

134+
private NetworkListener createSSLListener(int port) {
145135

146-
147-
private NetworkListener createSSLListener(int port){
148-
149-
150-
151136
SSLConfigurationBuilder sslBuilder = new SSLConfigurationBuilder();
152137
NetworkListener listener = new NetworkListener("grizzly", "0.0.0.0", Integer.valueOf(port));
153138
listener.getFileCache().setEnabled(false);
154-
155-
listener.setSecure(true);
156-
listener.setSSLEngineConfig(sslBuilder.build(SSLProperties));
157-
139+
140+
listener.setSecure(true);
141+
listener.setSSLEngineConfig(sslBuilder.build(SSLProperties));
142+
158143
return listener;
159144
}
160145

161146
private void addListeners(WebappContext webappContext) {
162-
147+
163148
new ServletContextListenerConfigurer(serverData, servletContextListenerData).addListeners(webappContext);
164149
}
165150

0 commit comments

Comments
 (0)