/framework/src/play/templates/BaseTemplate.java

https://github.com/hsablonniere/play · Java · 118 lines · 101 code · 14 blank · 3 comment · 21 complexity · 4e9187a6356207eb7dd3c3becfb889a2 MD5 · raw file

  1. package play.templates;
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import play.Logger;
  8. import play.Play;
  9. import play.classloading.BytecodeCache;
  10. import play.exceptions.JavaExecutionException;
  11. import play.exceptions.NoRouteFoundException;
  12. import play.exceptions.TagInternalException;
  13. import play.exceptions.TemplateExecutionException;
  14. import play.exceptions.TemplateExecutionException.DoBodyException;
  15. import play.libs.Codec;
  16. import play.libs.IO;
  17. /**
  18. * A template
  19. */
  20. public abstract class BaseTemplate extends Template {
  21. public String compiledSource;
  22. public Map<Integer, Integer> linesMatrix = new HashMap<Integer, Integer>();
  23. public Set<Integer> doBodyLines = new HashSet<Integer>();
  24. public Class compiledTemplate;
  25. public String compiledTemplateName;
  26. public Long timestamp = System.currentTimeMillis();
  27. public BaseTemplate(String name, String source) {
  28. this.name = name;
  29. this.source = source;
  30. }
  31. public BaseTemplate(String source) {
  32. this.name = Codec.UUID();
  33. this.source = source;
  34. }
  35. public void loadPrecompiled() {
  36. try {
  37. File file = Play.getFile("precompiled/templates/" + name);
  38. byte[] code = IO.readContent(file);
  39. directLoad(code);
  40. } catch (Exception e) {
  41. throw new RuntimeException("Cannot load precompiled template " + name);
  42. }
  43. }
  44. public boolean loadFromCache() {
  45. try {
  46. long start = System.currentTimeMillis();
  47. byte[] bc = BytecodeCache.getBytecode(name, source);
  48. if (bc != null) {
  49. directLoad(bc);
  50. if (Logger.isTraceEnabled()) {
  51. Logger.trace("%sms to load template %s from cache", System.currentTimeMillis() - start, name);
  52. }
  53. return true;
  54. }
  55. } catch (Exception e) {
  56. Logger.warn(e, "Cannot load %s from cache", name);
  57. }
  58. return false;
  59. }
  60. abstract void directLoad(byte[] code) throws Exception;
  61. void throwException(Throwable e) {
  62. for (StackTraceElement stackTraceElement : e.getStackTrace()) {
  63. if (stackTraceElement.getClassName().equals(compiledTemplateName) || stackTraceElement.getClassName().startsWith(compiledTemplateName + "$_run_closure")) {
  64. if (doBodyLines.contains(stackTraceElement.getLineNumber())) {
  65. throw new DoBodyException(e);
  66. } else if (e instanceof TagInternalException) {
  67. throw (TagInternalException) cleanStackTrace(e);
  68. } else if (e instanceof NoRouteFoundException) {
  69. NoRouteFoundException ex = (NoRouteFoundException) cleanStackTrace(e);
  70. if (ex.getFile() != null) {
  71. throw new NoRouteFoundException(ex.getFile(), this, this.linesMatrix.get(stackTraceElement.getLineNumber()));
  72. }
  73. throw new NoRouteFoundException(ex.getAction(), ex.getArgs(), this, this.linesMatrix.get(stackTraceElement.getLineNumber()));
  74. } else if (e instanceof TemplateExecutionException) {
  75. throw (TemplateExecutionException) cleanStackTrace(e);
  76. } else {
  77. throw new TemplateExecutionException(this, this.linesMatrix.get(stackTraceElement.getLineNumber()), e.getMessage(), cleanStackTrace(e));
  78. }
  79. }
  80. if (stackTraceElement.getLineNumber() > 0 && Play.classes.hasClass(stackTraceElement.getClassName())) {
  81. throw new JavaExecutionException(Play.classes.getApplicationClass(stackTraceElement.getClassName()), stackTraceElement.getLineNumber(), cleanStackTrace(e));
  82. }
  83. }
  84. throw new RuntimeException(e);
  85. }
  86. abstract Throwable cleanStackTrace(Throwable e);
  87. public static ThreadLocal<BaseTemplate> layout = new ThreadLocal<BaseTemplate>();
  88. public static ThreadLocal<Map<Object, Object>> layoutData = new ThreadLocal<Map<Object, Object>>();
  89. public static ThreadLocal<BaseTemplate> currentTemplate = new ThreadLocal<BaseTemplate>();
  90. public static class RawData {
  91. public String data;
  92. public RawData(Object val) {
  93. if (val == null) {
  94. data = "";
  95. } else {
  96. data = val.toString();
  97. }
  98. }
  99. @Override
  100. public String toString() {
  101. return data;
  102. }
  103. }
  104. }