/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

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