Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The current version of the project is **0.2**.

| dropwizard-spring | Dropwizard | Spring |
|:------------------:|:------------:|:-------------:|
| master (0.3.2) | 0.7.0-rc1 | 3.1.4.RELEASE |
| master (0.3.1) | 0.6.2 | 3.1.4.RELEASE |
| 0.2 | 0.6.0 | 3.1.3.RELEASE |
| 0.1 | 0.5.1 | 3.1.1.RELEASE |
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</distributionManagement>

<properties>
<dropwizard.version>0.6.2</dropwizard.version>
<dropwizard.version>0.7.0-rc1</dropwizard.version>
<spring.version>3.1.4.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand All @@ -66,7 +66,7 @@

<!-- Compile Dependencies -->
<dependency>
<groupId>com.yammer.dropwizard</groupId>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
<optional>true</optional>
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.github.nhuray.dropwizard.spring;

import com.codahale.metrics.health.HealthCheck;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.nhuray.dropwizard.spring.config.ConfigurationPlaceholderConfigurer;
import com.google.common.base.Preconditions;
import com.sun.jersey.spi.inject.InjectableProvider;
import com.yammer.dropwizard.ConfiguredBundle;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.lifecycle.Managed;
import com.yammer.dropwizard.lifecycle.ServerLifecycleListener;
import com.yammer.dropwizard.tasks.Task;
import com.yammer.metrics.core.HealthCheck;
import io.dropwizard.ConfiguredBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Environment;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.servlets.tasks.Task;
import org.eclipse.jetty.util.component.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -112,7 +112,7 @@ public void run(T configuration, Environment environment) throws Exception {
registerInjectableProviders(environment, context);
registerProviders(environment, context);
registerResources(environment, context);
environment.manage(new SpringContextManaged(context));
environment.lifecycle().manage(new SpringContextManaged(context));
}


Expand Down Expand Up @@ -151,7 +151,7 @@ private void registerManaged(Environment environment, ConfigurableApplicationCon
for (String beanName : beansOfType.keySet()) {
// Add managed to Dropwizard environment
Managed managed = beansOfType.get(beanName);
environment.manage(managed);
environment.lifecycle().manage(managed);
LOG.info("Registering managed: " + managed.getClass().getName());
}
}
Expand All @@ -169,7 +169,7 @@ private void registerLifecycle(Environment environment, ConfigurableApplicationC
// Add lifeCycle to Dropwizard environment
if (!beanName.equals(ENVIRONMENT_BEAN_NAME)) {
LifeCycle lifeCycle = beansOfType.get(beanName);
environment.manage(lifeCycle);
environment.lifecycle().manage(lifeCycle);
LOG.info("Registering lifeCycle: " + lifeCycle.getClass().getName());
}
}
Expand All @@ -188,7 +188,7 @@ private void registerServerLifecycleListeners(Environment environment, Configura
// Add serverLifecycleListener to Dropwizard environment
if (!beanName.equals(ENVIRONMENT_BEAN_NAME)) {
ServerLifecycleListener serverLifecycleListener = beansOfType.get(beanName);
environment.addServerLifecycleListener(serverLifecycleListener);
environment.servlets().addServletListeners(serverLifecycleListener);
LOG.info("Registering serverLifecycleListener: " + serverLifecycleListener.getClass().getName());
}
}
Expand All @@ -206,7 +206,7 @@ private void registerTasks(Environment environment, ConfigurableApplicationConte
for (String beanName : beansOfType.keySet()) {
// Add task to Dropwizard environment
Task task = beansOfType.get(beanName);
environment.addTask(task);
environment.admin().addTask(task);
LOG.info("Registering task: " + task.getClass().getName());
}
}
Expand All @@ -223,7 +223,7 @@ private void registerHealthChecks(Environment environment, ConfigurableApplicati
for (String beanName : beansOfType.keySet()) {
// Add healthCheck to Dropwizard environment
HealthCheck healthCheck = beansOfType.get(beanName);
environment.addHealthCheck(healthCheck);
environment.healthChecks().register(beanName, healthCheck);
LOG.info("Registering healthCheck: " + healthCheck.getClass().getName());
}
}
Expand All @@ -240,7 +240,7 @@ private void registerInjectableProviders(Environment environment, ConfigurableAp
for (String beanName : beansOfType.keySet()) {
// Add injectableProvider to Dropwizard environment
InjectableProvider injectableProvider = beansOfType.get(beanName);
environment.addProvider(injectableProvider);
environment.jersey().register(injectableProvider);
LOG.info("Registering injectable provider: " + injectableProvider.getClass().getName());
}
}
Expand All @@ -256,7 +256,7 @@ private void registerProviders(Environment environment, ConfigurableApplicationC
for (String beanName : beansWithAnnotation.keySet()) {
// Add injectableProvider to Dropwizard environment
Object provider = beansWithAnnotation.get(beanName);
environment.addProvider(provider);
environment.jersey().register(provider);
LOG.info("Registering provider : " + provider.getClass().getName());
}
}
Expand All @@ -273,7 +273,7 @@ private void registerResources(Environment environment, ConfigurableApplicationC
for (String beanName : beansWithAnnotation.keySet()) {
// Add injectableProvider to Dropwizard environment
Object resource = beansWithAnnotation.get(beanName);
environment.addResource(resource);
environment.jersey().register(resource);
LOG.info("Registering resource : " + resource.getClass().getName());
}
}
Expand Down Expand Up @@ -312,8 +312,8 @@ private void registerEnvironment(Environment environment, ConfigurableApplicatio
*/
private void registerPlaceholder(Environment environment, T configuration, ConfigurableApplicationContext context) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
final ObjectMapper objectMapper = environment.getObjectMapperFactory().build();
placeholderConfigurer.setObjectMapper(objectMapper);
final ObjectMapper objectMapper = environment.getObjectMapper();
placeholderConfigurer.setObjectMapper(objectMapper);
placeholderConfigurer.setConfiguration(configuration);
beanFactory.registerSingleton(PLACEHOLDER_BEAN_NAME, placeholderConfigurer);
beanFactory.registerSingleton(OBJECT_MAPPER_BEAN_NAME, objectMapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.nhuray.dropwizard.spring;

import io.dropwizard.lifecycle.Managed;
import org.springframework.context.ConfigurableApplicationContext;

import com.yammer.dropwizard.lifecycle.Managed;
import io.dropwizard.lifecycle.Managed;

public class SpringContextManaged implements Managed {
private final ConfigurableApplicationContext context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.nhuray.dropwizard.spring.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yammer.dropwizard.config.Configuration;
import io.dropwizard.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.github.nhuray.dropwizard.spring;

import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.servlets.HealthCheckServlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.json.ObjectMapperFactory;
import com.yammer.dropwizard.lifecycle.ServerLifecycleListener;
import com.yammer.dropwizard.tasks.Task;
import com.yammer.metrics.core.HealthCheck;
import io.dropwizard.Configuration;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.jetty.setup.ServletEnvironment;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.setup.AdminEnvironment;
import io.dropwizard.setup.Environment;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.servlets.tasks.Task;
import com.codahale.metrics.health.HealthCheck;

import hello.server_lifecycle_listeners.HelloServerLifecycleListener;
import hello.config.HelloAppConfiguration;
Expand All @@ -22,6 +28,7 @@
import org.mockito.MockitoAnnotations;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertNotNull;
Expand All @@ -35,7 +42,19 @@ public class SpringBundleTest {
private Environment environment;

@Mock
private ObjectMapperFactory objectMapperFactory;
private JerseyEnvironment jersey;

@Mock
private AdminEnvironment admin;

@Mock
private ServletEnvironment servlets;

@Mock
private LifecycleEnvironment lifecycle ;

@Mock
private HealthCheckRegistry healthChecks ;

private HelloAppConfiguration configuration;

Expand All @@ -57,8 +76,17 @@ public void setup() {
configuration = new HelloAppConfiguration();
configuration.setHello(hello);

when(environment.getObjectMapperFactory()).thenReturn(objectMapperFactory);
when(objectMapperFactory.build()).thenReturn(new ObjectMapper());
when(environment.getObjectMapper()).thenReturn(Jackson.newObjectMapper());

when(environment.jersey()).thenReturn(jersey);

when(environment.admin()).thenReturn(admin);

when(environment.servlets()).thenReturn(servlets);

when(environment.lifecycle()).thenReturn(lifecycle);

when(environment.healthChecks()).thenReturn(healthChecks);
}

@Test
Expand All @@ -68,7 +96,7 @@ public void registerResources() throws Exception {

// Then
ArgumentCaptor<HelloResource> resource = ArgumentCaptor.forClass(HelloResource.class);
verify(environment).addResource(resource.capture());
verify(jersey).register(resource.capture());
assertThat(resource.getValue(), is(HelloResource.class));
}

Expand All @@ -79,7 +107,10 @@ public void registerHealthChecks() throws Exception {

// Then
ArgumentCaptor<? extends HealthCheck> healthCheck = ArgumentCaptor.forClass(HealthCheck.class);
verify(environment).addHealthCheck(healthCheck.capture());
ArgumentCaptor<String> healthCheckName = ArgumentCaptor.forClass(String.class);
verify(healthChecks).register(healthCheckName.capture(), healthCheck.capture());

assertThat(healthCheckName.getValue(), is("helloHealthCheck")); // is the bean name
assertThat(healthCheck.getValue(), is(HelloHealthCheck.class));
}

Expand All @@ -90,7 +121,7 @@ public void registerTasks() throws Exception {

// Then
ArgumentCaptor<? extends Task> task = ArgumentCaptor.forClass(Task.class);
verify(environment).addTask(task.capture());
verify(admin).addTask(task.capture());
assertThat(task.getValue(), is(HelloTask.class));
}

Expand All @@ -101,7 +132,7 @@ public void registerServerLifecycleListener() throws Exception {

// Then
ArgumentCaptor<? extends ServerLifecycleListener> listener = ArgumentCaptor.forClass(ServerLifecycleListener.class);
verify(environment).addServerLifecycleListener(listener.capture());
verify(servlets).addServletListeners(listener.capture());
assertThat(listener.getValue(), is(HelloServerLifecycleListener.class));
}

Expand All @@ -112,10 +143,10 @@ public void registerConfiguration() throws Exception {

// Then
ArgumentCaptor<HelloResource> resource = ArgumentCaptor.forClass(HelloResource.class);
verify(environment).addResource(resource.capture());
verify(jersey).register(resource.capture());

HelloResource r = resource.getValue();
assertThat(r.getPort(), is(8080)); // Defaut port
assertThat(r.getPort(), is(8080)); // Default port
}

@Test
Expand All @@ -135,7 +166,7 @@ public void wiresUpDependencies() throws Exception {

// Then
ArgumentCaptor<HelloResource> resource = ArgumentCaptor.forClass(HelloResource.class);
verify(environment).addResource(resource.capture());
verify(jersey).register(resource.capture());

HelloResource r = resource.getValue();
final HelloService helloService = r.getHelloService();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.nhuray.dropwizard.spring.config;


import ch.qos.logback.classic.Level;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yammer.dropwizard.config.Configuration;
import io.dropwizard.Configuration;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void defaultPlaceholder() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean",
rootBeanDefinition(ConfigurationTestBean.class)
.addPropertyValue("connectorType", "${http.connectorType}")
.addPropertyValue("loggingLevel", "${logging.level.levelStr}")
.getBeanDefinition());
placeholder.setObjectMapper(new ObjectMapper());

Expand All @@ -45,7 +46,7 @@ public void defaultPlaceholder() {

// Then
ConfigurationTestBean bean = bf.getBean(ConfigurationTestBean.class);
assertThat(bean.getConnectorType(), equalTo("blocking"));
assertThat(bean.getLoggingLevel(), equalTo(Level.INFO));
}

@Test
Expand All @@ -58,7 +59,7 @@ public void customPlaceholderPrefixAndSuffix() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean",
rootBeanDefinition(ConfigurationTestBean.class)
.addPropertyValue("connectorType", "@<http.connectorType>")
.addPropertyValue("loggingLevel", "@<logging.level.levelStr>")
.addPropertyValue("rootPath", "${key2}")
.getBeanDefinition());
placeholder.setObjectMapper(new ObjectMapper());
Expand All @@ -68,7 +69,7 @@ public void customPlaceholderPrefixAndSuffix() {

// Then
ConfigurationTestBean bean = bf.getBean(ConfigurationTestBean.class);
assertThat(bean.getConnectorType(), is("blocking"));
assertThat(bean.getLoggingLevel(), is(Level.INFO));
assertThat(bean.getRootPath(), is("${key2}"));
}

Expand Down Expand Up @@ -99,4 +100,4 @@ private class ConfigurationWithNull extends Configuration {
}


}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.github.nhuray.dropwizard.spring.config;

import ch.qos.logback.classic.Level;

public class ConfigurationTestBean {

private String connectorType;
private String rootPath;

private Level loggingLevel;

public String getConnectorType() {
return connectorType;
}
Expand All @@ -20,4 +24,12 @@ public String getRootPath() {
public void setRootPath(String rootPath) {
this.rootPath = rootPath;
}
}

public Level getLoggingLevel() {
return loggingLevel;
}

public void setLoggingLevel(Level loggingLevel) {
this.loggingLevel = loggingLevel;
}
}
Loading