Skip to content

Commit

Permalink
HH-80325 add NabJerseyTestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Khalikov authored and akhalikov committed Aug 7, 2018
1 parent 9bab9fe commit 7a311d4
Show file tree
Hide file tree
Showing 27 changed files with 391 additions and 249 deletions.
10 changes: 10 additions & 0 deletions nab-example/src/main/java/ru/hh/nab/example/ExampleConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.hh.nab.example;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import ru.hh.nab.starter.NabProdConfig;

@Configuration
@Import(NabProdConfig.class)
public class ExampleConfig {
}
7 changes: 1 addition & 6 deletions nab-example/src/main/java/ru/hh/nab/example/ExampleMain.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package ru.hh.nab.example;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import ru.hh.nab.starter.NabApplication;
import ru.hh.nab.starter.NabProdConfig;
import ru.hh.nab.starter.servlet.DefaultServletConfig;

@Configuration
@Import({NabProdConfig.class})
public class ExampleMain {

public static void main(String[] args) {
Expand All @@ -17,6 +12,6 @@ public static void main(String[] args) {
public void registerResources(ResourceConfig resourceConfig) {
resourceConfig.register(ExampleResource.class);
}
}, ExampleMain.class);
}, ExampleConfig.class);
}
}
6 changes: 6 additions & 0 deletions nab-hibernate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<version>${hibernate.version}</version>
</dependency>

<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b06</version>
</dependency>

<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
20 changes: 6 additions & 14 deletions nab-starter/src/main/java/ru/hh/nab/starter/AppMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.text.MessageFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

Expand All @@ -17,11 +13,11 @@ public class AppMetadata {

private static final String PROJECT_PROPERTIES = "/project.properties";

private final String name;
private final String serviceName;
private final String version;
private final long started;

AppMetadata(String name) {
AppMetadata(String serviceName) {
Properties projectProps = new Properties();

try (InputStream s = AppMetadata.class.getResourceAsStream(PROJECT_PROPERTIES)) {
Expand All @@ -31,18 +27,14 @@ public class AppMetadata {
LOGGER.warn("Failed to load {}, project version will be unknown, ignoring", PROJECT_PROPERTIES, e);
}

this.name = name;
this.serviceName = serviceName;

version = projectProps.getProperty("project.version", "unknown");
started = System.currentTimeMillis();
}

public String getStatus() {
LocalDateTime start = Instant.ofEpochMilli(started).atZone(ZoneId.systemDefault()).toLocalDateTime();
return MessageFormat.format("[{0}] {1} (ver. {2}) started at {3}", LocalDateTime.now(), name, version, start);
}

public String getName() {
return name;
public String getServiceName() {
return serviceName;
}

public String getVersion() {
Expand Down
21 changes: 16 additions & 5 deletions nab-starter/src/main/java/ru/hh/nab/starter/NabApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
import ru.hh.nab.starter.servlet.DefaultServletConfig;
import ru.hh.nab.starter.servlet.ServletConfig;

import java.lang.management.ManagementFactory;
import java.time.LocalDateTime;

public class NabApplication {
public final class NabApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(NabApplication.class);

public static ApplicationContext run(Class<?>... primarySources) {
return run(new DefaultServletConfig(), primarySources);
public static NabApplicationContext run(Class<?>... configurationClasses) {
return run(new DefaultServletConfig(), configurationClasses);
}

public static ApplicationContext run(ServletConfig servletConfig, Class<?>... primarySources) {
public static NabApplicationContext run(ServletConfig servletConfig, Class<?>... configurationClasses) {
configureLogger();
NabApplicationContext context = null;
try {
context = new NabApplicationContext(servletConfig, primarySources);
context = new NabApplicationContext(servletConfig, configurationClasses);
context.refresh();
logStartupInfo(context);
} catch (Exception e) {
logErrorAndExit(e);
}
Expand All @@ -34,6 +36,15 @@ public static void configureLogger() {
SLF4JBridgeHandler.install();
}

private static void logStartupInfo(ApplicationContext context) {
AppMetadata appMetadata = context.getBean(AppMetadata.class);
LOGGER.info("Started {} PID={} (version={})", appMetadata.getServiceName(), getCurrentPid(), appMetadata.getVersion());
}

private static String getCurrentPid() {
return ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
}

private static void logErrorAndExit(Exception e) {
LOGGER.error("Failed to start, shutting down", e);
System.err.println(format("[{0}] Failed to start, shutting down: {1}", LocalDateTime.now(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import ru.hh.nab.common.properties.FileSettings;
import ru.hh.nab.starter.jersey.DefaultResourceConfig;
import ru.hh.nab.starter.server.jetty.JettyServer;
import ru.hh.nab.starter.server.jetty.JettyServerFactory;
import ru.hh.nab.starter.servlet.ServletConfig;

import java.lang.management.ManagementFactory;

public class NabApplicationContext extends AnnotationConfigWebApplicationContext {
public final class NabApplicationContext extends AnnotationConfigWebApplicationContext {

private volatile JettyServer jettyServer;

private final ServletConfig servletConfig;

public NabApplicationContext(ServletConfig servletConfig, Class<?>... primarySources) {
NabApplicationContext(ServletConfig servletConfig, Class<?>... primarySources) {
this.servletConfig = servletConfig;
register(primarySources);
registerShutdownHook();
Expand All @@ -30,7 +29,6 @@ public NabApplicationContext(ServletConfig servletConfig, Class<?>... primarySou
protected void finishRefresh() {
super.finishRefresh();
startJettyServer();
printStartupInfo();
}

private void startJettyServer() {
Expand All @@ -39,7 +37,7 @@ private void startJettyServer() {
if (jettyServer == null) {
FileSettings jettySettings = getBean(FileSettings.class);
ThreadPool threadPool = getBean(ThreadPool.class);
ResourceConfig resourceConfig = getBean(ResourceConfig.class);
ResourceConfig resourceConfig = new DefaultResourceConfig();

this.jettyServer = JettyServerFactory.create(jettySettings, threadPool, resourceConfig, servletConfig, (contextHandler) -> {
configureServletContext(contextHandler, this, servletConfig);
Expand All @@ -61,13 +59,4 @@ public static void configureServletContext(ServletContextHandler handler, Applic
boolean isServerRunning() {
return jettyServer.isRunning();
}

private void printStartupInfo() {
AppMetadata appMetadata = getBean(AppMetadata.class);
System.out.println(appMetadata.getStatus() + ", pid " + getCurrentPid());
}

private static String getCurrentPid() {
return ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
}
}
32 changes: 3 additions & 29 deletions nab-starter/src/main/java/ru/hh/nab/starter/NabProdConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.StatsDClient;
import org.eclipse.jetty.servlet.FilterHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.support.MBeanServerFactoryBean;
import ru.hh.metrics.StatsDSender;
import ru.hh.nab.starter.filters.ResourceNameLoggingFilter;
import ru.hh.nab.starter.jersey.FilteredXmlElementProvider;
import ru.hh.nab.starter.jersey.FilteredXmlListElementProvider;
import ru.hh.nab.starter.jersey.FilteredXmlRootElementProvider;
import ru.hh.nab.starter.jmx.MBeanExporterFactory;
import ru.hh.nab.common.properties.FileSettings;
import static ru.hh.nab.common.properties.PropertiesUtils.fromFilesInSettingsDir;
import ru.hh.nab.starter.jmx.MBeanExporterFactory;
import static ru.hh.nab.starter.server.cache.HttpCacheFilterFactory.createCacheFilterHolder;

import javax.management.MBeanServer;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;

import ru.hh.nab.starter.resource.StatsResource;
import ru.hh.nab.starter.resource.StatusResource;
import static ru.hh.nab.starter.server.cache.HttpCacheFilterFactory.createCacheFilterHolder;
import static ru.hh.nab.common.properties.PropertiesUtils.fromFilesInSettingsDir;

@Configuration
@Import({NabCommonConfig.class})
public class NabProdConfig {
Expand All @@ -36,24 +28,6 @@ FileSettings fileSettings() throws Exception {
return new FileSettings(properties);
}

@Bean
ResourceConfig resourceConfig() {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(FilteredXmlRootElementProvider.App.class);
resourceConfig.register(FilteredXmlRootElementProvider.General.class);
resourceConfig.register(FilteredXmlRootElementProvider.Text.class);
resourceConfig.register(FilteredXmlElementProvider.App.class);
resourceConfig.register(FilteredXmlElementProvider.General.class);
resourceConfig.register(FilteredXmlElementProvider.Text.class);
resourceConfig.register(FilteredXmlListElementProvider.App.class);
resourceConfig.register(FilteredXmlListElementProvider.General.class);
resourceConfig.register(FilteredXmlListElementProvider.Text.class);
resourceConfig.register(new ResourceNameLoggingFilter());
resourceConfig.register(StatusResource.class);
resourceConfig.register(StatsResource.class);
return resourceConfig;
}

@Bean
StatsDClient statsDClient() {
return new NonBlockingStatsDClient(null, "localhost", 8125, 10000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected void doFilterInternal(HttpServletRequest request,
response.addHeader(RequestHeaders.REQUEST_ID, requestId);
}
MDC.setKey(REQUEST_ID_MDC_KEY, requestId);

filterChain.doFilter(request, response);

} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.hh.nab.starter.jersey;

import org.glassfish.jersey.server.ResourceConfig;
import ru.hh.nab.starter.filters.ResourceNameLoggingFilter;
import ru.hh.nab.starter.resource.StatsResource;
import ru.hh.nab.starter.resource.StatusResource;

public final class DefaultResourceConfig extends ResourceConfig {

public DefaultResourceConfig() {
register(FilteredXmlRootElementProvider.App.class);
register(FilteredXmlRootElementProvider.General.class);
register(FilteredXmlRootElementProvider.Text.class);
register(FilteredXmlElementProvider.App.class);
register(FilteredXmlElementProvider.General.class);
register(FilteredXmlElementProvider.Text.class);
register(FilteredXmlListElementProvider.App.class);
register(FilteredXmlListElementProvider.General.class);
register(FilteredXmlListElementProvider.Text.class);
register(ResourceNameLoggingFilter.class);
register(StatusResource.class);
register(StatsResource.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public StatusResource(AppMetadata appMetaData) {
@Produces(MediaType.TEXT_XML)
public String status() {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project name=\"" + appMetaData.getName() + "\">\n"
+ "<project name=\"" + appMetaData.getServiceName() + "\">\n"
+ " <version>" + appMetaData.getVersion() + "</version>\n"
+ " <uptime>" + appMetaData.getUpTimeSeconds() + "</uptime>\n"
+ "</project>\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public final class JettyServer {

private final FileSettings jettySettings;
private final Server server;
private final ServletContextHandler servletContextHandler;

JettyServer(ThreadPool threadPool, FileSettings jettySettings, ServletContextHandler servletContextHandler) {
this.jettySettings = jettySettings;
this.servletContextHandler = servletContextHandler;

server = new Server(threadPool);
configureConnector();
Expand All @@ -44,7 +42,7 @@ public void start() throws JettyServerException {
server.start();
server.setStopAtShutdown(true);

LOGGER.info(" Jetty started on port {}", getPort());
LOGGER.info("Jetty started on port {}", getPort());
} catch (Exception e) {
stopSilently();
throw new JettyServerException("Unable to start Jetty server", e);
Expand Down Expand Up @@ -134,8 +132,4 @@ private void stopSilently() {
public Server getServer() {
return server;
}

public ServletContextHandler getServletContextHandler() {
return servletContextHandler;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.hh.nab.starter.server.jetty;

class JettyServerException extends RuntimeException {
final class JettyServerException extends RuntimeException {

JettyServerException(String message, Throwable cause) {
super(message, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import javax.servlet.Servlet;

public class JettyServerFactory {
public final class JettyServerFactory {

public static JettyServer create(FileSettings fileSettings,
ThreadPool threadPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.util.ArrayList;
import java.util.List;

public final class JettyWebAppContext extends WebAppContext {
final class JettyWebAppContext extends WebAppContext {

public JettyWebAppContext(JerseyServletContextInitializer servletContextInitializer, boolean sessionEnabled) {
JettyWebAppContext(JerseyServletContextInitializer servletContextInitializer, boolean sessionEnabled) {
super(null, null, null, null, null, null,
sessionEnabled ? SESSIONS: 0);

Expand All @@ -20,7 +20,7 @@ public JettyWebAppContext(JerseyServletContextInitializer servletContextInitiali
setConfigurations(configurations.toArray(new Configuration[0]));
}

private static class ServletContextInitializerConfiguration extends AbstractConfiguration {
private static final class ServletContextInitializerConfiguration extends AbstractConfiguration {
private final JerseyServletContextInitializer servletContextInitializer;

ServletContextInitializerConfiguration(JerseyServletContextInitializer servletContextInitializer) {
Expand All @@ -32,7 +32,7 @@ public void configure(WebAppContext context) {
context.addBean(new InitializingLifeCycle(context), true);
}

class InitializingLifeCycle extends AbstractLifeCycle {
final class InitializingLifeCycle extends AbstractLifeCycle {
private final WebAppContext context;

InitializingLifeCycle(WebAppContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextListener;
import ru.hh.nab.starter.filters.RequestIdLoggingFilter;
Expand All @@ -24,8 +23,4 @@ public void configureServletContext(ServletContextHandler servletContextHandler,
}
}
}

@Override
public void registerResources(ResourceConfig resourceConfig) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ default String getServletMapping() {

void configureServletContext(ServletContextHandler servletContextHandler, ApplicationContext applicationContext);

void registerResources(ResourceConfig resourceConfig);
default void registerResources(ResourceConfig resourceConfig) {
}
}
Loading

0 comments on commit 7a311d4

Please sign in to comment.