Skip to content

Commit

Permalink
add symbols to disable cache warming and/or slow minification logging (
Browse files Browse the repository at this point in the history
…fixes #2, #3)

 #
  • Loading branch information
jochenberger committed Mar 23, 2015
1 parent 02c6167 commit 7cc15a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.lang.reflect.Method;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.MethodAdviceReceiver;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Advise;
import org.apache.tapestry5.ioc.annotations.Contribute;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.services.FactoryDefaults;
import org.apache.tapestry5.ioc.services.SymbolProvider;
import org.apache.tapestry5.plastic.MethodAdvice;
import org.apache.tapestry5.plastic.MethodInvocation;
import org.apache.tapestry5.services.assets.ResourceMinimizer;
Expand All @@ -29,14 +32,21 @@ public static void bind(final ServiceBinder binder) {
binder.bind(MinificationCacheWarming.class, MinificationCacheWarmingImpl.class);
}

@Contribute(SymbolProvider.class)
@FactoryDefaults
public static void addDefaultConfiguration(final MappedConfiguration<String, Object> configuration) {
configuration.add(MinificationCacheWarmingSymbols.ENABLE_MINIFICATION_CACHE_WARMING, "${"
+ SymbolConstants.MINIFICATION_ENABLED + "}");
configuration.add(MinificationCacheWarmingSymbols.LOG_SLOW_MINIFICATION, Boolean.TRUE);
}

@Contribute(Runnable.class)
public static void warmMinifiedJavascriptCache(final MinificationCacheWarming minificationCacheWarming,
@Symbol(SymbolConstants.MINIFICATION_ENABLED) final boolean minificationEnabled,
public static void warmMinifiedJavascriptCache(
final MinificationCacheWarming minificationCacheWarming,
@Symbol(MinificationCacheWarmingSymbols.ENABLE_MINIFICATION_CACHE_WARMING) final boolean minificationCacheWarmingEnabled,
final OrderedConfiguration<Runnable> configuration) {

// TODO add separate symbol defaulting to
// SymbolConstants.MINIFICATION_ENABLED
if (minificationEnabled) {
if (minificationCacheWarmingEnabled) {
configuration.add("MinificationCacheWarming", new Runnable() {

@Override
Expand All @@ -53,30 +63,32 @@ public void run() {
}

@Advise(serviceInterface = ResourceMinimizer.class)
public static void logSlowMinification(final MethodAdviceReceiver adviceReceiver, final Logger logger)
public static void logSlowMinification(final MethodAdviceReceiver adviceReceiver, final Logger logger,
@Symbol(MinificationCacheWarmingSymbols.LOG_SLOW_MINIFICATION) final boolean logSlowMinification)
throws NoSuchMethodException, SecurityException {
// TODO add symbol to toggle logging behavior
@SuppressWarnings("unchecked")
Method m = adviceReceiver.getInterface().getMethod("minimize", StreamableResource.class);

adviceReceiver.adviseMethod(m, new MethodAdvice() {

@Override
public void advise(final MethodInvocation invocation) {

if (!startupComplete) {
invocation.proceed();
} else {
long start = System.currentTimeMillis();
invocation.proceed();
long end = System.currentTimeMillis();
if (end - start >= slowMinificationThresholdMillis) {
logger.warn("Minification of {} took {} ms, consider adding it to the cache warming process.",
invocation.getParameter(0), end - start);
if (logSlowMinification) {
@SuppressWarnings("unchecked")
Method m = adviceReceiver.getInterface().getMethod("minimize", StreamableResource.class);

adviceReceiver.adviseMethod(m, new MethodAdvice() {

@Override
public void advise(final MethodInvocation invocation) {

if (!startupComplete) {
invocation.proceed();
} else {
long start = System.currentTimeMillis();
invocation.proceed();
long end = System.currentTimeMillis();
if (end - start >= slowMinificationThresholdMillis) {
logger.warn("Minification of {} took {} ms, consider adding it to the cache warming process.",
invocation.getParameter(0), end - start);
}
}
}
}
});
});
}
}

private MinificationCacheWarmingModule() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.eddyson.tapestry.minificationcachewarming;

public final class MinificationCacheWarmingSymbols {

public static final String ENABLE_MINIFICATION_CACHE_WARMING = "tapestry.enable-minification-cache-warming";
public static final String LOG_SLOW_MINIFICATION = "tapestry.log-slow-minification";

private MinificationCacheWarmingSymbols() {
}
}

0 comments on commit 7cc15a5

Please sign in to comment.