Skip to content

Commit

Permalink
Switch AssetsBundle and servlet to using cache builder specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
codahale committed Mar 30, 2012
1 parent 09cc6e6 commit 392a4b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
@@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 + '*');
}
}
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 392a4b2

Please sign in to comment.