PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/main/java/com/google/sitebricks/DebugModePageBook.java

http://github.com/dhanji/sitebricks
Java | 125 lines | 86 code | 25 blank | 14 comment | 5 complexity | 6e250e03f38cebcc0091aaf523b32b68 MD5 | raw file
  1package com.google.sitebricks;
  2
  3import com.google.inject.Inject;
  4import com.google.inject.Provider;
  5import com.google.inject.Singleton;
  6import com.google.inject.servlet.RequestScoped;
  7import com.google.sitebricks.compiler.Compilers;
  8import com.google.sitebricks.routing.PageBook;
  9import com.google.sitebricks.routing.Production;
 10import com.google.sitebricks.routing.SystemMetrics;
 11import net.jcip.annotations.ThreadSafe;
 12
 13import java.lang.annotation.Annotation;
 14import java.util.Collection;
 15import java.util.HashSet;
 16import java.util.List;
 17import java.util.Map;
 18import java.util.Set;
 19
 20/**
 21 * Used in the development stage to intercept the real pagebook so we can reload
 22 * & recompile templates on demand.
 23 *
 24 * @author Dhanji R. Prasanna (dhanji@gmail com)
 25 */
 26@ThreadSafe
 27@Singleton
 28class DebugModePageBook implements PageBook {
 29  private final PageBook book;
 30  private final SystemMetrics metrics;
 31  private final Compilers compilers;
 32  private final Provider<Memo> memo;
 33
 34  @Inject
 35  public DebugModePageBook(@Production PageBook book,
 36                           SystemMetrics metrics, Compilers compilers,
 37                           Provider<Memo> memo) {
 38    this.book = book;
 39    this.metrics = metrics;
 40    this.compilers = compilers;
 41    this.memo = memo;
 42  }
 43
 44  public Page at(String uri, Class<?> myPageClass) {
 45    return book.at(uri, myPageClass);
 46  }
 47
 48  public Page get(String uri) {
 49    final Page page = book.get(uri);
 50
 51    //reload template
 52    reload(uri, page);
 53
 54    return page;
 55  }
 56
 57  public Page forName(String name) {
 58    final Page page = book.forName(name);
 59
 60    //reload template
 61    reload(name, page);
 62
 63    return page;
 64  }
 65
 66  public Page embedAs(Class<?> page, String as) {
 67    return book.embedAs(page, as);
 68  }
 69  
 70  @Override
 71  public Page decorate(Class<?> pageClass) {
 72    return book.decorate(pageClass);
 73  }
 74  
 75  public Page nonCompilingGet(String uri) {
 76    // Simply delegate thru to the real page book.
 77    return book.get(uri);
 78  }
 79
 80  public Page forInstance(Object instance) {
 81    return book.forInstance(instance);
 82  }
 83
 84  public Page forClass(Class<?> pageClass) {
 85    return book.forClass(pageClass);
 86  }
 87
 88  public Page serviceAt(String uri, Class<?> pageClass) {
 89    return book.serviceAt(uri, pageClass);
 90  }
 91
 92  public Collection<List<Page>> getPageMap() {
 93    return book.getPageMap();
 94  }
 95
 96  @Override
 97  public void at(String uri, List<ActionDescriptor> actionDescriptor,
 98                 Map<Class<? extends Annotation>, String> methodSet) {
 99    book.at(uri, actionDescriptor, methodSet);
100  }
101
102  private void reload(String identifier, Page page) {
103
104    // Do nothing on the first pass since the page is already compiled.
105    // Also skips static resources and headless web services.
106    if (null == page || !metrics.isActive() || page.isHeadless())
107      return;
108
109    // Ensure we reload only once per request, per identifier.
110    final Memo memo = this.memo.get();
111    if (memo.uris.contains(identifier))
112      return;
113
114    // Otherwise, remember that we already loaded it in this request.
115    memo.uris.add(identifier);
116
117    // load template and compile
118    compilers.compilePage(page);
119  }
120
121  @RequestScoped
122  private static class Memo {
123    private final Set<String> uris = new HashSet<String>();
124  }
125}