diff --git a/.gitignore b/.gitignore index fc058f7..33751f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -.idea -*.iml +**/.idea/ +**/*.iml *.classpath *.project -target +**/target/ diff --git a/README.md b/README.md index a977b7d..9ab2b22 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,7 @@ Dropwizard/Spring =================================== -[ ![Codeship Status for nhuray/dropwizard-spring](https://www.codeship.io/projects/aaabb970-44fc-0132-2a3e-3e77b90e182a/status)](https://www.codeship.io/projects/44910) - -Welcome to the Dropwizard/Spring project +Welcome to the Dropwizard/Spring project. This is a updated version of the work done by Nicolas Huray at [https://github.com/nhuray/dropwizard-spring](https://github.com/nhuray/dropwizard-spring) . Introduction @@ -19,20 +17,22 @@ This project provide a simple method for integrating Spring with Dropwizard. Versions ------------ -The current version of the project is **0.2**. +The latest version of the project available on Maven repository is **0.2**. | dropwizard-spring | Dropwizard | Spring | |:------------------:|:------------:|:-------------:| +| master (0.3.4) | 0.8.1 | 4.1.6.RELEASE | | master (0.3.2) | 0.7.0 | 4.0.5.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 | +The latest available release is **0.3.4**. + Installation ------------ - To install Dropwizard/Spring you just have to add this Maven dependency in your project : ```xml @@ -43,6 +43,12 @@ To install Dropwizard/Spring you just have to add this Maven dependency in your ``` +To install a newer release, download the jar and added it to your local maven repository. Execute this command in the same folder as the jar: + +``` +mvn install:install-file -Dfile=dropwizard-spring-0.3.4-RELEASE.jar -DgroupId=com.github.raduciumag -DartifactId=dropwizard-spring -Dversion=0.3.4-RELEASE -Dpackaging=jar +``` + Usage ------------ @@ -53,31 +59,17 @@ To use Dropwizard/Spring you just have to add a ```SpringBundle``` and create yo For example : ```java -public class HelloApp extends Service { - - private static final String CONFIGURATION_FILE = "src/test/resources/hello/hello.yml"; - - public static void main(String[] args) throws Exception { - new HelloApp().run(new String[]{"server", CONFIGURATION_FILE}); - } - - @Override - public void initialize(Bootstrap bootstrap) { - // register configuration, environment and placeholder - bootstrap.addBundle(new SpringBundle(applicationContext(), true, true, true)); - } - - @Override - public void run(HelloAppConfiguration configuration, Environment environment) throws Exception { - // doing nothing - } +@Override +public void initialize(Bootstrap bootstrap) { + // register configuration, environment and placeholder + bootstrap.addBundle(new SpringBundle(applicationContext(), true, true, true)); +} - private ConfigurableApplicationContext applicationContext() throws BeansException { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.scan("hello"); - return context; - } +private ConfigurableApplicationContext applicationContext() throws BeansException { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.scan("hello"); + return context; } ``` @@ -87,11 +79,11 @@ The ```SpringBundle``` class use the application context to initialize Dropwizar Moreover the ```SpringBundle``` class register : - - a ```ConfigurationPlaceholderConfigurer``` to resolve Dropwizard configuration as [Spring placeholders](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer) (For example : ```${http.port}```). + - a ```ConfigurationPlaceholderConfigurer``` to resolve Dropwizard configuration (For example : ```${http.port}```). - - the Dropwizard configuration with the name ```dw``` to retrieve complex configuration with [Spring Expression Language](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/expressions.html) (For example : ```#{dw.httpConfiguration}```). + - the Dropwizard configuration with the name ```dw``` to retrieve complex configuration (For example : ```#{dw.httpConfiguration}```). - - the Dropwizard environment with the name ```dwEnv``` to retrieve complex configuration with [Spring Expression Language](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/expressions.html) (For example : ```#{dwEnv.validator}```). + - the Dropwizard environment with the name ```dwEnv``` to retrieve complex configuration (For example : ```#{dwEnv.validator}```). Please take a look at the hello application located in ```src/test/java/hello```. diff --git a/pom.xml b/pom.xml index ed782bd..a49581f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,33 +1,21 @@ - + 4.0.0 + org.sonatype.oss oss-parent 7 - com.github.nhuray + + com.github.raduciumag dropwizard-spring - 0.3.2-SNAPSHOT jar + 0.3.5-SNAPSHOT + Dropwizard/Spring integration Dropwizard/Spring integration - - scm:git:git@github.com:41quickbuild/dropwizard-spring.git - git@github.com:41quickbuild/dropwizard-spring - scm:git:git@github.com:41quickbuild/dropwizard-spring.git - - - - JIRA - http://jira - - - - Bamboo - http://bamboo/bamboo - - The Apache Software License, Version 2.0 @@ -43,39 +31,36 @@ https://github.com/nhuray/ +1 + + raduciumag + Radu Ciumag + https://github.com/RaduCiumag + +2 + - - - nexus - http://nexus.the41.internal/content/repositories/releases/ - - - nexus - http://nexus.the41.internal/content/repositories/snapshots/ - - - - 0.7.0 - 4.0.5.RELEASE UTF-8 - 17.0 + + 0.8.1 + 18.0 + 4.1.6.RELEASE - io.dropwizard dropwizard-core ${dropwizard.version} + io.dropwizard dropwizard-lifecycle ${dropwizard.version} + io.dropwizard dropwizard-servlets @@ -102,12 +87,14 @@ 4.10 test + org.mockito mockito-all 1.9.5 test + cglib @@ -115,7 +102,6 @@ 2.2 test - @@ -127,7 +113,6 @@ 1.6 1.6 - UTF-8 diff --git a/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java b/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java index eddb0d2..97138f7 100644 --- a/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java +++ b/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java @@ -4,7 +4,6 @@ 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 io.dropwizard.Configuration; import io.dropwizard.ConfiguredBundle; import io.dropwizard.lifecycle.Managed; @@ -13,10 +12,12 @@ import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import org.eclipse.jetty.util.component.LifeCycle; +import org.glassfish.hk2.api.InjectionResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import javax.ws.rs.Path; import javax.ws.rs.ext.Provider; @@ -37,6 +38,7 @@ public class SpringBundle implements ConfiguredBundle springConfigurationClass; private boolean registerConfiguration; private boolean registerEnvironment; @@ -59,14 +61,20 @@ public SpringBundle(ConfigurableApplicationContext context) { * @param registerEnvironment register Dropwizard environment as a Spring Bean. * @param registerPlaceholder resolve Dropwizard configuration as properties. */ - public SpringBundle(ConfigurableApplicationContext context, boolean registerConfiguration, boolean registerEnvironment, boolean registerPlaceholder) { - if (registerConfiguration || registerEnvironment || registerPlaceholder) { - Preconditions.checkArgument(!context.isActive(), "Context must be not active in order to register configuration, environment or placeholder"); + public SpringBundle(ConfigurableApplicationContext context, boolean registerConfiguration, boolean registerEnvironment, + boolean registerPlaceholder) { + if (registerConfiguration || registerEnvironment || registerPlaceholder) { + Preconditions.checkArgument(!context.isActive(), "Context must be not active in order to register configuration, " + + "environment or placeholder"); } + this.context = context; this.registerConfiguration = registerConfiguration; this.registerEnvironment = registerEnvironment; - if (registerPlaceholder) this.placeholderConfigurer = new ConfigurationPlaceholderConfigurer(); + + if (registerPlaceholder) { + this.placeholderConfigurer = new ConfigurationPlaceholderConfigurer(); + } } /** @@ -78,40 +86,89 @@ public SpringBundle(ConfigurableApplicationContext context, boolean registerConf * @param registerEnvironment register Dropwizard environment as a Spring Bean. * @param placeholderConfigurer placeholderConfigurer to resolve Dropwizard configuration as properties. */ - public SpringBundle(ConfigurableApplicationContext context, boolean registerConfiguration, boolean registerEnvironment, ConfigurationPlaceholderConfigurer placeholderConfigurer) { + public SpringBundle(final ConfigurableApplicationContext context, final boolean registerConfiguration, + final boolean registerEnvironment, final ConfigurationPlaceholderConfigurer placeholderConfigurer) { Preconditions.checkArgument(placeholderConfigurer != null, "PlaceholderConfigurer is required"); - if (registerConfiguration || registerEnvironment || placeholderConfigurer != null) { - Preconditions.checkArgument(!context.isActive(), "Context must be not active in order to register configuration, environment or placeholder"); + + if (registerConfiguration || registerEnvironment || placeholderConfigurer != null) { + Preconditions.checkArgument(!context.isActive(), "Context must be not active in order to register configuration, " + + "environment or placeholder"); } + this.context = context; this.registerConfiguration = registerConfiguration; this.registerEnvironment = registerEnvironment; this.placeholderConfigurer = placeholderConfigurer; } + /** + * Creates a new SpringBundle to automatically initialize Dropwizard {@link Environment} + *

+ * + * @param context the application context to load + * @param springConfigurationClass an annotated spring configuration class to register after configuration, environment + * and placeholderConfigurer got injected into the context. + * @param registerConfiguration register Dropwizard configuration as a Spring Bean. + * @param registerEnvironment register Dropwizard environment as a Spring Bean. + * @param registerPlaceholder resolve Dropwizard configuration as properties. + */ + public SpringBundle(final AnnotationConfigApplicationContext context, final Class springConfigurationClass, + final boolean registerConfiguration, final boolean registerEnvironment, final boolean registerPlaceholder) { + Preconditions.checkNotNull(springConfigurationClass, "Spring configuration class is required"); + + if (registerConfiguration || registerEnvironment || registerPlaceholder) { + Preconditions.checkArgument(!context.isActive(), "Context must be not active in order to register configuration, " + + "environment or placeholder"); + } + + this.context = context; + this.registerConfiguration = registerConfiguration; + this.registerEnvironment = registerEnvironment; + this.springConfigurationClass = springConfigurationClass; + + if (registerPlaceholder) { + this.placeholderConfigurer = new ConfigurationPlaceholderConfigurer(); + } + } + @Override public void run(T configuration, Environment environment) throws Exception { // Register Dropwizard Configuration as a Bean Spring. - if (registerConfiguration) registerConfiguration(configuration, context); + if (registerConfiguration) { + registerConfiguration(configuration, context); + } // Register the Dropwizard environment - if (registerEnvironment) registerEnvironment(environment, context); + if (registerEnvironment) { + registerEnvironment(environment, context); + } // Register a placeholder to resolve Dropwizard Configuration as properties. - if (placeholderConfigurer != null) registerPlaceholder(environment, configuration, context); + if (placeholderConfigurer != null) { + registerPlaceholder(environment, configuration, context); + } + + /* After configuration, environment and placeholder got configured, + register the spring configuration that instantiate beans that need them */ + if (springConfigurationClass != null && context instanceof AnnotationConfigApplicationContext) { + ((AnnotationConfigApplicationContext) context).register(springConfigurationClass); + } // Refresh context if is not active - if (!context.isActive()) context.refresh(); + if (!context.isActive()) { + context.refresh(); + } - // Initialize Dropwizard environment + /* Initialize Dropwizard environment */ registerManaged(environment, context); registerLifecycle(environment, context); registerServerLifecycleListeners(environment, context); registerTasks(environment, context); registerHealthChecks(environment, context); - registerInjectableProviders(environment, context); + registerInjectionResolver(environment, context); registerProviders(environment, context); registerResources(environment, context); + environment.lifecycle().manage(new SpringContextManaged(context)); } @@ -138,8 +195,6 @@ public void setRegisterEnvironment(boolean registerEnvironment) { this.registerEnvironment = registerEnvironment; } - // ~ Dropwizard Environment initialization methods ----------------------------------------------------------------- - /** * Register {@link Managed}s in Dropwizard {@link Environment} from Spring application context. * @@ -156,7 +211,6 @@ private void registerManaged(Environment environment, ConfigurableApplicationCon } } - /** * Register {@link LifeCycle}s in Dropwizard {@link Environment} from Spring application context. * @@ -175,7 +229,6 @@ private void registerLifecycle(Environment environment, ConfigurableApplicationC } } - /** * Register {@link ServerLifecycleListener}s in Dropwizard {@link Environment} from Spring application context. * @@ -183,19 +236,18 @@ private void registerLifecycle(Environment environment, ConfigurableApplicationC * @param context the Spring application context */ private void registerServerLifecycleListeners(Environment environment, ConfigurableApplicationContext context) { - Map beansOfType = context.getBeansOfType(ServerLifecycleListener.class); - for (String beanName : beansOfType.keySet()) { - // Add serverLifecycleListener to Dropwizard environment - if (!beanName.equals(ENVIRONMENT_BEAN_NAME)) { - ServerLifecycleListener serverLifecycleListener = beansOfType.get(beanName); - environment.lifecycle().addServerLifecycleListener(serverLifecycleListener); - LOG.info("Registering serverLifecycleListener: " + serverLifecycleListener.getClass().getName()); + Map beansOfType = context.getBeansOfType(ServerLifecycleListener.class); + for (String beanName : beansOfType.keySet()) { + // Add serverLifecycleListener to Dropwizard environment + if (!beanName.equals(ENVIRONMENT_BEAN_NAME)) { + ServerLifecycleListener serverLifecycleListener = beansOfType.get(beanName); + environment.lifecycle().addServerLifecycleListener(serverLifecycleListener); + LOG.info("Registering serverLifecycleListener: " + serverLifecycleListener.getClass().getName()); + } } - } } - - /** + /** * Register {@link Task}s in Dropwizard {@link Environment} from Spring application context. * * @param environment the Dropwizard environment @@ -211,7 +263,6 @@ private void registerTasks(Environment environment, ConfigurableApplicationConte } } - /** * Register {@link HealthCheck}s in Dropwizard {@link Environment} from Spring application context. * @@ -228,20 +279,19 @@ private void registerHealthChecks(Environment environment, ConfigurableApplicati } } - /** - * Register {@link InjectableProvider}s in Dropwizard {@link Environment} from Spring application context. + * Register {@link InjectionResolver}s in Dropwizard {@link Environment} from Spring application context. * * @param environment the Dropwizard environment * @param context the Spring application context */ - private void registerInjectableProviders(Environment environment, ConfigurableApplicationContext context) { - final Map beansOfType = context.getBeansOfType(InjectableProvider.class); + private void registerInjectionResolver(Environment environment, ConfigurableApplicationContext context) { + final Map beansOfType = context.getBeansOfType(InjectionResolver.class); for (String beanName : beansOfType.keySet()) { - // Add injectableProvider to Dropwizard environment - InjectableProvider injectableProvider = beansOfType.get(beanName); - environment.jersey().register(injectableProvider); - LOG.info("Registering injectable provider: " + injectableProvider.getClass().getName()); + // Add injectionResolver to Dropwizard environment + InjectionResolver injectionResolver = beansOfType.get(beanName); + environment.jersey().register(injectionResolver); + LOG.info("Registering injection resolver: " + injectionResolver.getClass().getName()); } } @@ -261,7 +311,6 @@ private void registerProviders(Environment environment, ConfigurableApplicationC } } - /** * Register resources annotated with {@link Path} in Dropwizard {@link Environment} from Spring application context. * @@ -290,7 +339,6 @@ private void registerConfiguration(T configuration, ConfigurableApplicationConte LOG.info("Registering Dropwizard Configuration under name : " + CONFIGURATION_BEAN_NAME); } - /** * Register Dropwizard {@link Environment} as a Bean Spring. * @@ -303,7 +351,6 @@ private void registerEnvironment(Environment environment, ConfigurableApplicatio LOG.info("Registering Dropwizard Environment under name : " + ENVIRONMENT_BEAN_NAME); } - /** * Register a placeholder to resolve Dropwizard Configuration as properties. * @@ -313,7 +360,7 @@ private void registerEnvironment(Environment environment, ConfigurableApplicatio private void registerPlaceholder(Environment environment, T configuration, ConfigurableApplicationContext context) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); final ObjectMapper objectMapper = environment.getObjectMapper(); - placeholderConfigurer.setObjectMapper(objectMapper); + placeholderConfigurer.setObjectMapper(objectMapper); placeholderConfigurer.setConfiguration(configuration); beanFactory.registerSingleton(PLACEHOLDER_BEAN_NAME, placeholderConfigurer); beanFactory.registerSingleton(OBJECT_MAPPER_BEAN_NAME, objectMapper);