Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: dropwizard/dropwizard
base: 63c60f9671
...
head fork: dropwizard/dropwizard
compare: 5ac1ad5c39
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
4 docs/source/about/release-notes.rst
@@ -31,6 +31,10 @@ v0.4.0-SNAPSHOT
* Upgraded to Jackson 1.9.6.
* Improved syslog logging.
* Fixed template paths for views.
+* Upgraded to Guava 12.0 RC1.
+* Added support for deserializing ``CacheBuilderSpec`` instances from JSON/YAML.
+* Switched ``AssetsBundle`` and servlet to using cache builder specs.
+* Switched ``CachingAuthenticator`` to using cache builder specs.
.. __: http://logging.apache.org/log4j/1.2/
.. __: http://logback.qos.ch/
View
20 dropwizard-auth/src/main/java/com/yammer/dropwizard/auth/CachingAuthenticator.java
@@ -1,10 +1,7 @@
package com.yammer.dropwizard.auth;
import com.google.common.base.Optional;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.CacheStats;
-import com.google.common.cache.LoadingCache;
+import com.google.common.cache.*;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Meter;
import com.yammer.metrics.core.Timer;
@@ -21,18 +18,19 @@
* @param <P> the type of principals the authenticator returns
*/
public class CachingAuthenticator<C, P> implements Authenticator<C, P> {
+
/**
* Wraps an underlying authenticator with a cache.
*
- * @param authenticator the underlying authenticator
- * @param builder a fully-configured {@link CacheBuilder}
- * @param <C> the type of credentials the authenticator can authenticate
- * @param <P> the type of principals the authenticator returns
+ * @param authenticator the underlying authenticator
+ * @param cacheSpec a {@link CacheBuilderSpec}
+ * @param <C> the type of credentials the authenticator can authenticate
+ * @param <P> the type of principals the authenticator returns
* @return a cached version of {@code authenticator}
*/
public static <C, P> CachingAuthenticator<C, P> wrap(Authenticator<C, P> authenticator,
- CacheBuilder<Object, Object> builder) {
- return new CachingAuthenticator<C, P>(authenticator, builder);
+ CacheBuilderSpec cacheSpec) {
+ return new CachingAuthenticator<C, P>(authenticator, CacheBuilder.from(cacheSpec));
}
private final Authenticator<C, P> underlying;
@@ -41,7 +39,7 @@
private final Timer gets;
private CachingAuthenticator(Authenticator<C, P> authenticator,
- CacheBuilder<Object, Object> builder) {
+ CacheBuilder<Object, Object> builder) {
this.underlying = authenticator;
this.cacheMisses = Metrics.newMeter(authenticator.getClass(),
"cache-misses",
View
8 dropwizard-auth/src/test/java/com/yammer/dropwizard/auth/tests/CachingAuthenticatorTest.java
@@ -1,7 +1,7 @@
package com.yammer.dropwizard.auth.tests;
import com.google.common.base.Optional;
-import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheStats;
import com.google.common.collect.ImmutableSet;
import com.yammer.dropwizard.auth.Authenticator;
@@ -10,8 +10,8 @@
import org.junit.Test;
import org.mockito.InOrder;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.*;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class CachingAuthenticatorTest {
@@ -19,7 +19,7 @@
private final Authenticator<String, String> underlying = mock(Authenticator.class);
private final CachingAuthenticator<String, String> cached =
- CachingAuthenticator.wrap(underlying, CacheBuilder.newBuilder().maximumSize(1));
+ CachingAuthenticator.wrap(underlying, CacheBuilderSpec.parse("maximumSize=1"));
@Before
public void setUp() throws Exception {
View
2  dropwizard-core/pom.xml
@@ -122,7 +122,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
- <version>11.0.2</version>
+ <version>12.0-rc1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
View
37 dropwizard-core/src/main/java/com/yammer/dropwizard/bundles/AssetsBundle.java
@@ -1,5 +1,6 @@
package com.yammer.dropwizard.bundles;
+import com.google.common.cache.CacheBuilderSpec;
import com.yammer.dropwizard.Bundle;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.servlets.AssetServlet;
@@ -11,20 +12,20 @@
*/
public class AssetsBundle implements Bundle {
public static final String DEFAULT_PATH = "/assets";
- public static final int DEFAULT_MAX_CACHE_SIZE = 100;
+ public static final CacheBuilderSpec DEFAULT_CACHE_SPEC = CacheBuilderSpec.parse("maximumSize=100");
private final String resourcePath;
private final String uriPath;
- private final int maxCacheSize;
+ private final CacheBuilderSpec cacheBuilderSpec;
/**
* Creates a new {@link AssetsBundle} which serves up static assets from
* {@code src/main/resources/assets/*} as {@code /assets/*}.
*
- * @see AssetsBundle#AssetsBundle(String, int)
+ * @see AssetsBundle#AssetsBundle(String, CacheBuilderSpec)
*/
public AssetsBundle() {
- this(DEFAULT_PATH, DEFAULT_MAX_CACHE_SIZE);
+ this(DEFAULT_PATH, DEFAULT_CACHE_SPEC);
}
/**
@@ -34,10 +35,10 @@ public AssetsBundle() {
* served up from {@code /assets/example.js}.
*
* @param path the classpath and URI root of the static asset files
- * @see AssetsBundle#AssetsBundle(String, int)
+ * @see AssetsBundle#AssetsBundle(String, CacheBuilderSpec)
*/
public AssetsBundle(String path) {
- this(path, DEFAULT_MAX_CACHE_SIZE, path);
+ this(path, DEFAULT_CACHE_SPEC, path);
}
/**
@@ -48,10 +49,10 @@ public AssetsBundle(String path) {
*
* @param resourcePath the resource path (in the classpath) of the static asset files
* @param uriPath the uri path for the static asset files
- * @see AssetsBundle#AssetsBundle(String, int)
+ * @see AssetsBundle#AssetsBundle(String, CacheBuilderSpec)
*/
public AssetsBundle(String resourcePath, String uriPath) {
- this(resourcePath, DEFAULT_MAX_CACHE_SIZE, uriPath);
+ this(resourcePath, DEFAULT_CACHE_SPEC, uriPath);
}
/**
@@ -60,11 +61,11 @@ public AssetsBundle(String resourcePath, String uriPath) {
* {@code path} of {@code "/assets"}, {@code src/main/resources/assets/example.js} would be
* served up from {@code /assets/example.js}.
*
- * @param resourcePath the resource path (in the classpath) of the static asset files
- * @param maxCacheSize the maximum number of resources to cache
+ * @param resourcePath the resource path (in the classpath) of the static asset files
+ * @param cacheBuilderSpec the spec for the cache builder
*/
- public AssetsBundle(String resourcePath, int maxCacheSize) {
- this(resourcePath, maxCacheSize, resourcePath);
+ public AssetsBundle(String resourcePath, CacheBuilderSpec cacheBuilderSpec) {
+ this(resourcePath, cacheBuilderSpec, resourcePath);
}
/**
@@ -73,20 +74,20 @@ public AssetsBundle(String resourcePath, int maxCacheSize) {
* {@code resourcePath} of {@code "/assets"} and a uriPath of {@code "/js"},
* {@code src/main/resources/assets/example.js} would be served up from {@code /js/example.js}.
*
- * @param resourcePath the resource path (in the classpath) of the static asset files
- * @param maxCacheSize the maximum number of resources to cache
- * @param uriPath the uri path for the static asset files
+ * @param resourcePath the resource path (in the classpath) of the static asset files
+ * @param cacheBuilderSpec the spec for the cache builder
+ * @param uriPath the uri path for the static asset files
*/
- public AssetsBundle(String resourcePath, int maxCacheSize, String uriPath) {
+ public AssetsBundle(String resourcePath, CacheBuilderSpec cacheBuilderSpec, String uriPath) {
checkArgument(resourcePath.startsWith("/"), "%s is not an absolute path", resourcePath);
checkArgument(!"/".equals(resourcePath), "%s is the classpath root");
this.resourcePath = resourcePath.endsWith("/") ? resourcePath : (resourcePath + '/');
this.uriPath = uriPath.endsWith("/") ? uriPath : (uriPath + '/');
- this.maxCacheSize = maxCacheSize;
+ this.cacheBuilderSpec = cacheBuilderSpec;
}
@Override
public void initialize(Environment environment) {
- environment.addServlet(new AssetServlet(resourcePath, maxCacheSize, uriPath), uriPath + '*');
+ environment.addServlet(new AssetServlet(resourcePath, cacheBuilderSpec, uriPath), uriPath + '*');
}
}
View
54 dropwizard-core/src/main/java/com/yammer/dropwizard/json/GuavaExtrasModule.java
@@ -0,0 +1,54 @@
+package com.yammer.dropwizard.json;
+
+import com.google.common.cache.CacheBuilderSpec;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.Version;
+import org.codehaus.jackson.map.*;
+import org.codehaus.jackson.map.annotate.JsonCachable;
+import org.codehaus.jackson.type.JavaType;
+
+import java.io.IOException;
+
+public class GuavaExtrasModule extends Module {
+ @JsonCachable
+ private static class CacheBuilderSpecDeserializer extends JsonDeserializer<CacheBuilderSpec> {
+ @Override
+ public CacheBuilderSpec deserialize(JsonParser jp,
+ DeserializationContext ctxt) throws IOException {
+ final String text = jp.getText();
+ if ("off".equalsIgnoreCase(text) || "disabled".equalsIgnoreCase(text)) {
+ return CacheBuilderSpec.disableCaching();
+ }
+ return CacheBuilderSpec.parse(text);
+ }
+ }
+
+ private static class GuavaExtrasDeserializers extends Deserializers.Base {
+ @Override
+ public JsonDeserializer<?> findBeanDeserializer(JavaType type,
+ DeserializationConfig config,
+ DeserializerProvider provider,
+ BeanDescription beanDesc,
+ BeanProperty property) throws JsonMappingException {
+ if (CacheBuilderSpec.class.isAssignableFrom(type.getRawClass())) {
+ return new CacheBuilderSpecDeserializer();
+ }
+ return super.findBeanDeserializer(type, config, provider, beanDesc, property);
+ }
+ }
+
+ @Override
+ public String getModuleName() {
+ return "guava-extras";
+ }
+
+ @Override
+ public Version version() {
+ return Version.unknownVersion();
+ }
+
+ @Override
+ public void setupModule(SetupContext context) {
+ context.addDeserializers(new GuavaExtrasDeserializers());
+ }
+}
View
1  dropwizard-core/src/main/java/com/yammer/dropwizard/json/Json.java
@@ -48,6 +48,7 @@ public Json() {
mapper.disable(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING);
mapper.registerModule(new GuavaModule());
mapper.registerModule(new LogbackModule());
+ mapper.registerModule(new GuavaExtrasModule());
this.typeFactory = mapper.getTypeFactory();
}
View
2  dropwizard-core/src/main/java/com/yammer/dropwizard/json/LogbackModule.java
@@ -4,11 +4,13 @@
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.*;
+import org.codehaus.jackson.map.annotate.JsonCachable;
import org.codehaus.jackson.type.JavaType;
import java.io.IOException;
class LogbackModule extends Module {
+ @JsonCachable
private static class LevelDeserializer extends JsonDeserializer<Level> {
@Override
public Level deserialize(JsonParser jp,
View
6 dropwizard-core/src/main/java/com/yammer/dropwizard/servlets/AssetServlet.java
@@ -1,6 +1,7 @@
package com.yammer.dropwizard.servlets;
import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.hash.Hashing;
@@ -77,10 +78,9 @@ public long getLastModifiedTime() {
private final transient LoadingCache<String, CachedAsset> cache;
private final transient MimeTypes mimeTypes;
- public AssetServlet(String resourcePath, int maxCacheSize, String uriPath) {
+ public AssetServlet(String resourcePath, CacheBuilderSpec cacheBuilderSpec, String uriPath) {
// TODO: 3/20/12 <coda> -- make the default filename here configurable
- this.cache = CacheBuilder.newBuilder()
- .maximumSize(maxCacheSize)
+ this.cache = CacheBuilder.from(cacheBuilderSpec)
.build(new AssetLoader(resourcePath, uriPath, "index.htm"));
this.mimeTypes = new MimeTypes();
}

No commit comments for this range

Something went wrong with that request. Please try again.