From 474c071b96ffde900ff60ae0e1e95c32d7c09a80 Mon Sep 17 00:00:00 2001 From: boncey Date: Sat, 20 May 2017 18:30:52 +0100 Subject: [PATCH 1/5] Provide optional access to Configuration when constructing a VersionSupplier --- readme.md | 25 +++++++++ .../version/ConfiguredVersionBundle.java | 55 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/io/dropwizard/bundles/version/ConfiguredVersionBundle.java diff --git a/readme.md b/readme.md index e8ccaa0..31c4e18 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,31 @@ public class MyApplication extends Application { } ``` + +Or if you need access to your configuration in your supplier use the `ConfiguredVersionBundle` as follows: + +```java +public class MyApplication extends Application { + @Override + public void initialize(Bootstrap bootstrap) { + bootstrap.addBundle(new ConfiguredVersionBundle() + { + @Override + public VersionSupplier provideSupplier(Configuration configuration) + { + return new MyConfiguredVersionSupplier(configuration); + } + }); + } + + @Override + public void run(Configuration cfg, Environment env) throws Exception { + // ... + } +} +``` + + Now you can access the the `/version` URL on the admin port of your application to see the version of your application as well as its dependencies. diff --git a/src/main/java/io/dropwizard/bundles/version/ConfiguredVersionBundle.java b/src/main/java/io/dropwizard/bundles/version/ConfiguredVersionBundle.java new file mode 100644 index 0000000..efb71cc --- /dev/null +++ b/src/main/java/io/dropwizard/bundles/version/ConfiguredVersionBundle.java @@ -0,0 +1,55 @@ +package io.dropwizard.bundles.version; + +import io.dropwizard.Configuration; +import io.dropwizard.ConfiguredBundle; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A Dropwizard bundle that will expose a version number of the application via a servlet on the + * admin console port. The way that the bundle discovers the application's version number is + * configurable via a {@code VersionSupplier}. The provided {@code VersionSupplier} implementation + * will be called a single time and the value it returns will be memoized for the life of the JVM. + */ +public abstract class ConfiguredVersionBundle + implements ConfiguredBundle { + private static final String DEFAULT_URL = "/version"; + + private final String url; + + /** + * Construct the VersionBundle. The version number + * will be exposed on the Dropwizard admin port on the default URL. + */ + public ConfiguredVersionBundle() { + this(DEFAULT_URL); + } + + /** + * Construct a VersionBundle. The version number will + * be exposed on the Dropwizard admin port at the specified URL. + * + * @param url The URL to expose the version number on. + */ + public ConfiguredVersionBundle(String url) { + checkNotNull(url); + + this.url = url; + } + + public abstract VersionSupplier provideSupplier(T configuration); + + @Override + public void initialize(Bootstrap bootstrap) { + // Nothing to do here + } + + @Override + public void run(T configuration, Environment environment) { + VersionServlet servlet = new VersionServlet(provideSupplier(configuration), + environment.getObjectMapper()); + environment.admin().addServlet("version", servlet).addMapping(url); + } +} From f50626b69a0feda93b76951209688f7232fe1c82 Mon Sep 17 00:00:00 2001 From: stan svec Date: Sun, 15 Oct 2023 22:27:33 +0700 Subject: [PATCH 2/5] Made compatible with dw4.x --- pom.xml | 89 +++++++++---------- .../bundles/version/VersionBundle.java | 12 +-- .../bundles/version/VersionServlet.java | 10 +-- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/pom.xml b/pom.xml index 6e84963..c8a265d 100644 --- a/pom.xml +++ b/pom.xml @@ -2,14 +2,9 @@ 4.0.0 - - io.dropwizard-bundles - parent-pom - 1.3.5 - - + io.dropwizard-bundles dropwizard-version-bundle - 1.3.5-1-SNAPSHOT + 4.0.0 jar dropwizard-version-bundle @@ -23,13 +18,6 @@ - - http://github.com/dropwizard-bundles/dropwizard-version-bundle - scm:git:https://github.com/dropwizard-bundles/dropwizard-version-bundle - scm:git:https://github.com/dropwizard-bundles/dropwizard-version-bundle - HEAD - - bbeck @@ -45,49 +33,35 @@ 0.9.11 + 4.0.2 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 16 + true + + + + + io.dropwizard dropwizard-core + ${dw.version} + provided - - io.dropwizard - dropwizard-testing - test - - - junit - junit - test - - - org.eclipse.jetty - jetty-http - tests - test - - - org.eclipse.jetty - jetty-servlet - test - - - org.eclipse.jetty - jetty-servlet - tests - test - - - org.mockito - mockito-core - test - + org.reflections reflections - ${reflections.version} + 0.9.11