1 12 13 19 package com.openedit.modules.scriptrunner; 20 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.StringWriter ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.servlet.ServletContext ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.apache.bsf.BSFException; 34 import org.apache.bsf.BSFManager; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.openedit.repository.ContentItem; 38 import org.openedit.repository.filesystem.StringItem; 39 import org.springframework.beans.factory.BeanFactory; 40 41 import com.openedit.OpenEditException; 42 import com.openedit.WebPageRequest; 43 import com.openedit.modules.BaseModule; 44 import com.openedit.page.Page; 45 import com.openedit.page.PageAction; 46 import com.openedit.page.PageRequestKeys; 47 import com.openedit.page.PageStreamer; 48 import com.openedit.util.OutputFiller; 49 import com.openedit.util.PathUtilities; 50 import com.openedit.util.URLUtilities; 51 52 81 public class ScriptModule extends BaseModule implements PageRequestKeys 82 { 83 84 public static final String SCRIPT_TAG = "script"; 85 private static Log log = LogFactory.getLog(ScriptModule.class); 86 87 public ScriptModule() 88 { 89 super(); 90 } 91 92 99 public synchronized void run( WebPageRequest context) throws OpenEditException 100 { 101 PageAction inAction = context.getCurrentAction(); 102 String code = inAction.getConfig().getChildValue(SCRIPT_TAG); 103 105 ScriptLogger logger = new ScriptLogger(); 106 context.putPageValue("log", logger); 107 108 Map variableMap = context.getPageMap(); 109 111 String filepath = context.getPath(); 112 code = PathUtilities.resolveRelativePath(code, filepath); 113 114 execScript(variableMap, code); 115 } 116 117 public void execScript(Map variableMap, String code) throws OpenEditException 118 { 119 variableMap.put("userManager", getUserManager() ); 120 variableMap.put("moduleManager", getModuleManager() ); 121 variableMap.put("pageManager", getPageManager() ); 122 variableMap.put("root", getRoot() ); 123 124 Script script = loadScript(code); 125 log.info("Executing script: " + script.getDescription()); 126 127 try 128 { 129 if (script.getDescription().endsWith(".js")) 130 { 131 execWithRhino(script, variableMap); 132 133 } 135 else 136 { 137 execWithBsf(script, variableMap); 138 } 139 } 140 catch (Exception e) 141 { 142 if ( e instanceof OpenEditException ) 143 { 144 throw (OpenEditException)e; 145 } 146 throw new OpenEditException(e); 147 } 148 } 149 150 public Script loadScript(String code) throws OpenEditException 151 { 152 try 153 { 154 155 Page scriptPage = getPageManager().getPage( code ); 156 157 Reader in = scriptPage.getReader(); 158 StringWriter out = new StringWriter (); 159 new OutputFiller().fill(in,out); 160 in.close(); 161 out.close(); 162 String text = out.toString(); 163 Script script = new Script(text, code); 164 return script; 165 } 166 catch (Exception ex) 167 { 168 throw new OpenEditException(ex); 169 } 170 } 171 172 public void saveScript(String code, String filepath, String inScript) throws OpenEditException 173 { 174 try 175 { 176 String relativecode = PathUtilities.buildRelative(code, filepath); 177 Page scriptPage = getPageManager().getPage( relativecode ); 178 ContentItem scriptItem = new StringItem( relativecode, inScript,scriptPage.getCharacterEncoding() ); 179 scriptPage.setContentItem( scriptItem ); 180 getPageManager().putPage( scriptPage ); 181 } 182 catch (Exception ex) 183 { 184 throw new OpenEditException(ex); 185 } 186 } 187 188 196 protected void declareAll(ScriptRunner runner, Map context) throws OpenEditException 197 { 198 if (context != null) 199 { 200 for (Iterator iter = context.keySet().iterator(); iter.hasNext();) 201 { 202 String element = (String ) iter.next(); 203 runner.declareBean((String ) element, context.get(element)); 204 } 205 } 206 runner.declareBean( "beanFactory", getBeanFactory()); 207 } 208 209 protected void execWithBsf(Script script, Map context) 210 throws BSFException, IOException , FileNotFoundException  211 { 212 BSFManager bsfManager = new BSFManager(); 213 214 if (context != null) 216 { 217 ServletContext application = (ServletContext ) context.get(SERVLET_CONTEXT); 219 HttpServletRequest request = (HttpServletRequest ) context.get(REQUEST); 220 HttpServletResponse response = (HttpServletResponse ) context.get(RESPONSE); 221 HttpSession session = (HttpSession ) context.get(SESSION); 222 Page page = (Page) context.get(PAGE); 223 PageStreamer pages = (PageStreamer) context.get(PAGES); 224 225 bsfManager.declareBean("context", context.get("context"), WebPageRequest.class); 227 bsfManager.declareBean(URL_UTILITIES, context.get(URL_UTILITIES), URLUtilities.class); 228 229 if (page == null) 231 { 232 log.debug("Page request is null"); 233 } 234 else 235 { 236 bsfManager.declareBean(PAGE, page, Page.class); 237 } 238 239 if (pages == null) 241 { 242 log.debug("Pages request is null"); 243 } 244 else 245 { 246 bsfManager.declareBean(PAGES, pages, PageStreamer.class); 247 } 248 249 if (request == null) 251 { 252 log.debug("HTTP request is null"); 253 } 254 else 255 { 256 bsfManager.declareBean(REQUEST, request, HttpServletRequest .class); 257 } 258 259 if (response == null) 260 { 261 log.debug("HTTP response is null"); 262 } 263 else 264 { 265 bsfManager.declareBean(RESPONSE, response, HttpServletResponse .class); 266 } 267 268 if (session == null) 269 { 270 log.debug("HTTP session is null"); 271 } 272 else 273 { 274 bsfManager.declareBean(SESSION, session, HttpSession .class); 275 } 276 277 if (application == null) 278 { 279 log.debug("ServletContext is null"); 280 } 281 else 282 { 283 bsfManager.declareBean(SERVLET_CONTEXT, application, ServletContext .class); 284 } 285 286 bsfManager.declareBean( "beanFactory", getBeanFactory(), BeanFactory.class ); 289 } 290 291 297 bsfManager.exec( 303 BSFManager.getLangFromFilename(script.getDescription()), 304 script.getDescription(), 305 0, 306 0, 307 script.getScriptText()); 308 } 309 310 protected void execWithRhino(Script inScript, Map inState) throws Exception  311 { 312 313 ScriptRunner runner = new RhinoScriptRunner(inScript); 314 315 declareAll(runner, inState); 317 runner.init(); 318 runner.terminate(); 319 320 } 323 324 325 } 326 | Popular Tags |