Skip to content

Commit

Permalink
Environment bean needs to be created before refreshing the spring root
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ke Wang committed Mar 19, 2015
1 parent b1db025 commit 443e4ed
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 162 deletions.
27 changes: 11 additions & 16 deletions micro-core/src/main/java/com/aol/micro/server/config/Classes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import nonautoscan.com.aol.micro.server.ScheduleAndAsyncConfig;

import com.aol.micro.server.events.ConfigureActiveJobsAspect;
import com.aol.micro.server.module.ConfigureEnviroment;
import com.aol.micro.server.rest.resources.ConfigureResources;
import com.aol.micro.server.spring.datasource.DataSourceBuilder;
import com.aol.micro.server.spring.datasource.JdbcConfig;
Expand All @@ -28,31 +29,25 @@
*
*/
public enum Classes {

/**
* CORE CLASSES are the Core Microserver Spring Configuration classes
* Property support, Guava Event Bus, Spring AOP & Scheduling
* Codahale Metrics, Event tracking etc
*/
CORE_CLASSES(PropertyFileConfig.class,
MiscellaneousConfig.class, AopConfig.class, CodahaleMetricsConfigurer.class,
ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class,ConfigureResources.class),
JDBC_CLASSES(JdbcConfig.class ,
DAOProvider.class,DataSourceBuilder.class,SQL.class,SpringDataConfig.class),
ROMA_ROW_MAPPER(RomaRowMapperConfig.class),
HIBERNATE_CLASSES(HibernateConfig.class,JdbcConfig.class ,
GenericHibernateService.class,DAOProvider.class,DataSourceBuilder.class,SQL.class),
CORE_CLASSES(PropertyFileConfig.class, MiscellaneousConfig.class, AopConfig.class, CodahaleMetricsConfigurer.class,
ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class, ConfigureResources.class, ConfigureEnviroment.class),
JDBC_CLASSES(JdbcConfig.class, DAOProvider.class, DataSourceBuilder.class, SQL.class, SpringDataConfig.class),
ROMA_ROW_MAPPER(RomaRowMapperConfig.class),
HIBERNATE_CLASSES(HibernateConfig.class, JdbcConfig.class, GenericHibernateService.class, DAOProvider.class, DataSourceBuilder.class, SQL.class),
SPRING_DATA_CLASSES(SpringDataConfig.class),
DATASOURCE_CLASSES(JdbcConfig.class ,
DataSourceBuilder.class);

DATASOURCE_CLASSES(JdbcConfig.class, DataSourceBuilder.class);

@Getter
private final Class[] classes;
private Classes(Class... classes){

private Classes(Class... classes) {
this.classes = classes;
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.aol.micro.server.module;

import java.util.Collection;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigureEnviroment {

@Autowired(required = false)
private Collection<ModuleBean> modules;

@Bean
public Environment environment(@Qualifier("propertyFactory") Properties props) {
if (modules == null) {
return new Environment(props);
}
return new Environment(props, modules);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
import java.util.Properties;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

//@Component
public class Environment {

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


public Environment(Properties propertyFactory,Collection<ModuleBean> modules) {
public Environment(Properties propertyFactory, Collection<ModuleBean> modules) {
modulePort = modules.stream().collect(Collectors.toMap(key -> key.getModule().getContext(), value -> value));
this.properties = propertyFactory;
}
Expand All @@ -30,24 +25,23 @@ public Environment(Properties propertyFactory) {
this.properties = propertyFactory;

}

public ModuleBean getModuleBean(Module module) {
return modulePort.get(module.getContext());
}

public void assureModule(Module module) {
if(!modulePort.containsKey(module.getContext())){
if (!modulePort.containsKey(module.getContext())) {
Map<String, ModuleBean> builder = Maps.newHashMap();
builder.putAll(modulePort);
builder.put(module.getContext(),ModuleBean.builder().port(getPort(module)).build());
builder.put(module.getContext(), ModuleBean.builder().port(getPort(module)).build());
modulePort = ImmutableMap.copyOf(builder);
}



}

private int getPort(Module module) {

return Integer.valueOf(Optional.ofNullable(
properties.get(module.getContext()+".port"))
.orElse(nextPort++).toString());

return Integer.valueOf(Optional.ofNullable(properties.get(module.getContext() + ".port")).orElse(nextPort++).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import lombok.Getter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Getter
public class AccessLogLocationBean {

private final String accessLogLocation;

@Autowired
public AccessLogLocationBean(@Value("${access.log.output:./logs/}") String accessLogLocation) {
public AccessLogLocationBean(String accessLogLocation) {
this.accessLogLocation = accessLogLocation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.glassfish.grizzly.http.server.accesslog.AccessLogBuilder;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,21 +31,21 @@
import com.aol.simple.react.exceptions.ExceptionSoftener;
import com.google.common.collect.ImmutableList;

@AllArgsConstructor(access=AccessLevel.PRIVATE)
public class GrizzlyApplication implements ServerApplication {
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class GrizzlyApplication implements ServerApplication {

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

@Getter
private final ServerData serverData;

private final ImmutableList<FilterData> filterData;
private final ImmutableList<ServletData> servletData;
private final ImmutableList<ServletContextListener> servletContextListenerData;
@Wither
private final SSLProperties SSLProperties;

public GrizzlyApplication(AllData serverData) {
this.serverData = serverData.getServerData();
this.filterData = serverData.getFilterDataList();
Expand All @@ -55,45 +54,42 @@ public GrizzlyApplication(AllData serverData) {
this.SSLProperties = null;
}

public void run(CompletableFuture start,CompletableFuture end) {
public void run(CompletableFuture start, CompletableFuture end) {

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

new ServletContextListenerConfigurer(serverData,servletContextListenerData);
new ServletContextListenerConfigurer(serverData, servletContextListenerData);

addServlet(webappContext);

new ServletConfigurer(serverData,servletData).addServlets(webappContext);
new ServletConfigurer(serverData, servletData).addServlets(webappContext);

new FilterConfigurer(serverData,this.filterData).addFilters(webappContext);
new FilterConfigurer(serverData, this.filterData).addFilters(webappContext);

addListeners(webappContext);

HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0",
serverData.getPort());
HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0", serverData.getPort());

addAccessLog(httpServer);
if(SSLProperties!=null)
if (SSLProperties != null)
this.createSSLListener(serverData.getPort());

startServer(webappContext, httpServer,start,end);
startServer(webappContext, httpServer, start, end);
}

private void startServer(WebappContext webappContext, HttpServer httpServer, CompletableFuture start, CompletableFuture end) {
private void startServer(WebappContext webappContext, HttpServer httpServer, CompletableFuture start, CompletableFuture end) {
webappContext.deploy(httpServer);
try {
logger.info("Starting application {} on port {}", serverData
.getModule().getContext(), serverData.getPort());
logger.info("Browse to http://localhost:{}/{}/application.wadl",
serverData.getPort(), serverData.getModule().getContext());
logger.info("Starting application {} on port {}", serverData.getModule().getContext(), serverData.getPort());
logger.info("Browse to http://localhost:{}/{}/application.wadl", serverData.getPort(), serverData.getModule().getContext());
logger.info("Configured resource classes :-");
serverData.extractResources()
.forEach(t -> logger.info(t.v1 + " : " + "http://localhost:"+serverData.getPort()
+"/"+serverData.getModule().getContext() + t.v2 ));;
serverData.extractResources().forEach(
t -> logger.info(t.v1 + " : " + "http://localhost:" + serverData.getPort() + "/" + serverData.getModule().getContext() + t.v2));
;
httpServer.start();
start.complete(true);
end.get();

} catch (IOException e) {
softener.throwSoftenedException(e);
} catch (ExecutionException e) {
Expand All @@ -108,58 +104,47 @@ private void startServer(WebappContext webappContext, HttpServer httpServer, Com

private void addAccessLog(HttpServer httpServer) {
try {
String accessLogLocation = serverData.getRootContext()
.getBean(AccessLogLocationBean.class)
.getAccessLogLocation();
final AccessLogBuilder builder = new AccessLogBuilder(
accessLogLocation + serverData.getModule().getContext()
+ "-access.log");
String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation();

accessLogLocation = accessLogLocation
+ serverData.getModule().getContext().substring(0, serverData.getModule().getContext().indexOf("/")) + "-access.log";
final AccessLogBuilder builder = new AccessLogBuilder(accessLogLocation);

builder.rotatedDaily();
builder.rotationPattern("yyyy-MM-dd");
builder.instrument(httpServer.getServerConfiguration());
} catch (Exception e) {
logger.error(ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG
.toString() + ": " + e.getMessage());
logger.error(ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getMessage());
if (e.getCause() != null)
logger.error("CAUSED BY: "
+ ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG
.toString() + ": " + e.getCause().getMessage());
logger.error("CAUSED BY: " + ErrorCode.SERVER_STARTUP_FAILED_TO_CREATE_ACCESS_LOG.toString() + ": " + e.getCause().getMessage());

}

}

private void addServlet(WebappContext webappContext) {
ServletContainer container = new ServletContainer();
ServletRegistration servletRegistration = webappContext.addServlet(
"Jersey Spring Web Application", container);
servletRegistration.setInitParameter("javax.ws.rs.Application",
this.serverData.getModule().getJaxWsRsApplication());
servletRegistration.setInitParameter(
"jersey.config.server.provider.packages", this.serverData
.getModule().getProviders());
ServletRegistration servletRegistration = webappContext.addServlet("Jersey Spring Web Application", container);
servletRegistration.setInitParameter("javax.ws.rs.Application", this.serverData.getModule().getJaxWsRsApplication());
servletRegistration.setInitParameter("jersey.config.server.provider.packages", this.serverData.getModule().getProviders());
servletRegistration.setLoadOnStartup(1);
servletRegistration.addMapping(serverData.getBaseUrlPattern());
}

private NetworkListener createSSLListener(int port) {


private NetworkListener createSSLListener(int port){



SSLConfigurationBuilder sslBuilder = new SSLConfigurationBuilder();
NetworkListener listener = new NetworkListener("grizzly", "0.0.0.0", Integer.valueOf(port));
listener.getFileCache().setEnabled(false);
listener.setSecure(true);
listener.setSSLEngineConfig(sslBuilder.build(SSLProperties));

listener.setSecure(true);
listener.setSSLEngineConfig(sslBuilder.build(SSLProperties));

return listener;
}

private void addListeners(WebappContext webappContext) {

new ServletContextListenerConfigurer(serverData, servletContextListenerData).addListeners(webappContext);
}

Expand Down
Loading

0 comments on commit 443e4ed

Please sign in to comment.