PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/org/jpublish/action/ScriptAction.java

http://jpublish.googlecode.com/
Java | 218 lines | 113 code | 28 blank | 77 comment | 42 complexity | aae3a86cb7b8d27980150cf2ab84baec MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright 2004-2007 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.jpublish.action;
  18. import com.anthonyeden.lib.config.Configuration;
  19. import com.atlassian.util.profiling.UtilTimerStack;
  20. import org.apache.bsf.BSFManager;
  21. import org.apache.bsf.util.IOUtils;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.jpublish.JPublishContext;
  25. import org.jpublish.Page;
  26. import org.jpublish.SiteContext;
  27. import javax.servlet.ServletContext;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import javax.servlet.http.HttpSession;
  31. import java.io.File;
  32. import java.io.FileReader;
  33. /**
  34. * An action which is implemented in a BSF supported scripting language.
  35. * Script actions have access to several varibles:
  36. * <p/>
  37. * <p>These are always available:</p>
  38. * <p/>
  39. * <p/>
  40. * <b>site</b> - The SiteContext<br>
  41. * <b>syslog</b> - Standard logging stream (Log4J Category)<br>
  42. * </p>
  43. * <p/>
  44. * <p>If there is a context defined when the action is executed (all
  45. * actions excluding startup actions):</p>
  46. * <p/>
  47. * <p/>
  48. * <b>context</b> - The current context<br>
  49. * <b>application</b> - The ServletContext<br>
  50. * <b>request</b> - The HTTP request<br>
  51. * <b>response</b> - The HTTP response<br>
  52. * <b>session</b> - The HTTP session<br>
  53. * <b>page</b> - The Page object<br>
  54. * </p>
  55. * <p/>
  56. * <p>If the action is executed with a configuration object then that object
  57. * will be included as <code>configuration</code>.</p>
  58. *
  59. * @author Anthony Eden
  60. * @author David Jones
  61. */
  62. public class ScriptAction implements Action {
  63. private static Log log = LogFactory.getLog(ScriptAction.class);
  64. private SiteContext siteContext;
  65. private File script;
  66. private String scriptLang = null;
  67. private long timeLastLoaded = 0;
  68. private String scriptString = null;
  69. /**
  70. * Construct a new ScriptAction for the given script. The path to the
  71. * script should be an absolute path.
  72. *
  73. * @param siteContext The SiteContext
  74. * @param script The path to the script
  75. */
  76. public ScriptAction(SiteContext siteContext, String script) {
  77. this(siteContext, new File(script));
  78. }
  79. /**
  80. * Construct a new ScriptAction for the given script.
  81. *
  82. * @param siteContext The SiteContext
  83. * @param script The file representing the script
  84. */
  85. public ScriptAction(SiteContext siteContext, File script) {
  86. this.siteContext = siteContext;
  87. this.script = script;
  88. if (log.isDebugEnabled())
  89. log.debug("Creating new ScriptAction for " + script.getName());
  90. }
  91. /**
  92. * Execute the action script represented by this ScriptAction.
  93. *
  94. * @param context The current context
  95. * @param configuration The configuration object
  96. * @throws Exception
  97. */
  98. public void execute(JPublishContext context, Configuration configuration) throws Exception {
  99. if (log.isDebugEnabled())
  100. log.debug("Executing script: " + script);
  101. BSFManager bsfManager = new BSFManager();
  102. // expose standard items in the context
  103. if (context != null) {
  104. ServletContext application = (ServletContext) context.get("application");
  105. HttpServletRequest request = (HttpServletRequest) context.get("request");
  106. HttpServletResponse response = (HttpServletResponse) context.get("response");
  107. HttpSession session = (HttpSession) context.get("session");
  108. Page page = (Page) context.get("page");
  109. // expose the context
  110. bsfManager.declareBean("context", context, JPublishContext.class);
  111. // expose the context. This variable has been removed as of JP2.
  112. //bsfManager.declareBean("vc", context, JPublishContext.class);
  113. // expose the page object.
  114. if (page == null) {
  115. if (log.isDebugEnabled())
  116. log.debug("Page request is null");
  117. } else {
  118. bsfManager.declareBean("page", page, Page.class);
  119. }
  120. // expose standard HttpServlet objects
  121. if (request == null) {
  122. if (log.isDebugEnabled())
  123. log.debug("HTTP request is null");
  124. } else {
  125. bsfManager.declareBean("request", request, HttpServletRequest.class);
  126. }
  127. if (response == null) {
  128. if (log.isDebugEnabled())
  129. log.debug("HTTP response is null");
  130. } else {
  131. bsfManager.declareBean("response", response, HttpServletResponse.class);
  132. }
  133. if (session == null) {
  134. if (log.isDebugEnabled())
  135. log.debug("HTTP session is null");
  136. } else {
  137. bsfManager.declareBean("session", session, HttpSession.class);
  138. }
  139. if (application == null) {
  140. if (log.isDebugEnabled())
  141. log.debug("ServletContext is null");
  142. } else {
  143. bsfManager.declareBean("application", application, ServletContext.class);
  144. }
  145. }
  146. // these objects are exposed regardless if there is a context
  147. // object or not. In other words they are accesible to startup
  148. // actions
  149. bsfManager.declareBean("syslog", SiteContext.syslog, Log.class);
  150. if (siteContext == null) {
  151. if (log.isDebugEnabled())
  152. log.debug("SiteContext is null");
  153. } else {
  154. bsfManager.declareBean("site", siteContext, SiteContext.class);
  155. }
  156. if (configuration == null) {
  157. if (log.isDebugEnabled())
  158. log.debug("Configuration is null");
  159. } else {
  160. bsfManager.declareBean("configuration", configuration, Configuration.class);
  161. }
  162. // The following code was added by David Jones. This will have
  163. // to be rewritten to use an ActionContent object for getting the
  164. // last modified time once I get to writing it. -AE
  165. if (scriptLang == null) {
  166. scriptLang = BSFManager.getLangFromFilename(script.getName());
  167. }
  168. boolean reloadScript = false;
  169. long scriptLastModified = script.lastModified();
  170. if (scriptLastModified > timeLastLoaded) {
  171. if (log.isDebugEnabled())
  172. log.debug("Loading updated or new script: " + script.getName());
  173. reloadScript = true;
  174. }
  175. if (reloadScript || scriptString == null) {
  176. synchronized (this) {
  177. if (reloadScript || scriptString == null) {
  178. timeLastLoaded = System.currentTimeMillis();
  179. scriptString = IOUtils.getStringFromReader(new FileReader(script));
  180. }
  181. }
  182. }
  183. try {
  184. UtilTimerStack.push(script.getName());
  185. bsfManager.exec(scriptLang, script.getCanonicalPath(), 0, 0, scriptString);
  186. } finally {
  187. UtilTimerStack.pop(script.getName());
  188. }
  189. }
  190. }