PageRenderTime 21ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/scm-webapp/src/main/java/sonia/scm/template/ServletMustacheFactory.java

https://bitbucket.org/sdorra/scm-manager/
Java | 152 lines | 65 code | 28 blank | 59 comment | 12 complexity | ef39c047a1d87555e770065f4dbd56bb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**
  2. * Copyright (c) 2010, Sebastian Sdorra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. Neither the name of SCM-Manager; nor the names of its
  14. * contributors may be used to endorse or promote products derived from this
  15. * software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * http://bitbucket.org/sdorra/scm-manager
  29. *
  30. */
  31. package sonia.scm.template;
  32. //~--- non-JDK imports --------------------------------------------------------
  33. import com.github.mustachejava.DefaultMustacheFactory;
  34. import com.google.common.base.Charsets;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import sonia.scm.plugin.PluginLoader;
  38. //~--- JDK imports ------------------------------------------------------------
  39. import java.io.BufferedReader;
  40. import java.io.InputStream;
  41. import java.io.InputStreamReader;
  42. import java.io.Reader;
  43. import javax.servlet.ServletContext;
  44. /**
  45. *
  46. * @author Sebastian Sdorra
  47. */
  48. public class ServletMustacheFactory extends DefaultMustacheFactory
  49. {
  50. /**
  51. * the logger for ServletMustacheFactory
  52. */
  53. private static final Logger logger =
  54. LoggerFactory.getLogger(ServletMustacheFactory.class);
  55. //~--- constructors ---------------------------------------------------------
  56. /**
  57. * Constructs ...
  58. *
  59. *
  60. * @param servletContext
  61. * @param pluginLoader
  62. */
  63. public ServletMustacheFactory(ServletContext servletContext,
  64. PluginLoader pluginLoader)
  65. {
  66. this.servletContext = servletContext;
  67. this.pluginLoader = pluginLoader;
  68. }
  69. //~--- get methods ----------------------------------------------------------
  70. /**
  71. * Method description
  72. *
  73. *
  74. * @param resourceName
  75. *
  76. * @return
  77. */
  78. @Override
  79. public Reader getReader(String resourceName)
  80. {
  81. if (logger.isTraceEnabled())
  82. {
  83. logger.trace("try to find resource for {}", resourceName);
  84. }
  85. Reader reader = null;
  86. InputStream is = servletContext.getResourceAsStream(resourceName);
  87. if (is == null)
  88. {
  89. if (logger.isTraceEnabled())
  90. {
  91. logger.trace("could not find resource {} in ServletContext",
  92. resourceName);
  93. }
  94. if (resourceName.startsWith("/"))
  95. {
  96. resourceName = resourceName.substring(1);
  97. }
  98. is = pluginLoader.getUberClassLoader().getResourceAsStream(resourceName);
  99. }
  100. if (is != null)
  101. {
  102. if (logger.isTraceEnabled())
  103. {
  104. logger.trace("found resource for {}, return reader", resourceName);
  105. }
  106. reader = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));
  107. }
  108. else if (logger.isWarnEnabled())
  109. {
  110. logger.warn("could not find resource {}", resourceName);
  111. }
  112. if (reader == null)
  113. {
  114. throw new MustacheTemplateNotFoundException(
  115. "could not find template for resource ".concat(resourceName));
  116. }
  117. return reader;
  118. }
  119. //~--- fields ---------------------------------------------------------------
  120. /** Field description */
  121. private final PluginLoader pluginLoader;
  122. /** Field description */
  123. private ServletContext servletContext;
  124. }