1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package com.sun.star.script.framework.provider.javascript; 24 25 import com.sun.star.uno.XComponentContext; 26 import com.sun.star.lang.XMultiComponentFactory; 27 import com.sun.star.lang.XMultiServiceFactory; 28 import com.sun.star.lang.XSingleServiceFactory; 29 import com.sun.star.frame.XModel; 30 import com.sun.star.registry.XRegistryKey; 31 import com.sun.star.comp.loader.FactoryHelper; 32 33 import com.sun.star.document.XScriptInvocationContext; 34 import com.sun.star.reflection.InvocationTargetException; 35 36 import java.net.URL; 37 38 import com.sun.star.script.provider.XScript; 39 40 import com.sun.star.script.provider.ScriptExceptionRaisedException; 41 import com.sun.star.script.provider.ScriptFrameworkErrorException; 42 import com.sun.star.script.provider.ScriptFrameworkErrorType; 43 44 import com.sun.star.script.framework.log.LogUtils; 45 import com.sun.star.script.framework.provider.ScriptContext; 46 import com.sun.star.script.framework.provider.ClassLoaderFactory; 47 import com.sun.star.script.framework.provider.ScriptProvider; 48 import com.sun.star.script.framework.provider.ScriptEditor; 49 import com.sun.star.script.framework.container.ScriptMetaData; 50 51 import org.mozilla.javascript.Context; 52 import org.mozilla.javascript.ImporterTopLevel; 53 import org.mozilla.javascript.Scriptable; 54 import org.mozilla.javascript.JavaScriptException; 55 56 public class ScriptProviderForJavaScript 57 { 58 public static class _ScriptProviderForJavaScript extends ScriptProvider 59 { _ScriptProviderForJavaScript(XComponentContext ctx)60 public _ScriptProviderForJavaScript(XComponentContext ctx) 61 { 62 super(ctx, "JavaScript"); 63 } 64 getScript( String scriptURI )65 public XScript getScript( /*IN*/String scriptURI ) 66 throws com.sun.star.uno.RuntimeException, 67 ScriptFrameworkErrorException 68 { 69 ScriptMetaData scriptData = null; 70 try 71 { 72 scriptData = getScriptData( scriptURI ); 73 ScriptImpl script = new ScriptImpl( m_xContext, scriptData, m_xModel, m_xInvocContext ); 74 return script; 75 } 76 catch ( com.sun.star.uno.RuntimeException re ) 77 { 78 throw new ScriptFrameworkErrorException( "Failed to create script object: " + re.getMessage(), 79 null, scriptData.getLanguageName(), language, ScriptFrameworkErrorType.UNKNOWN ); 80 } 81 } 82 hasScriptEditor()83 public boolean hasScriptEditor() 84 { 85 return true; 86 } 87 getScriptEditor()88 public ScriptEditor getScriptEditor() 89 { 90 return ScriptEditorForJavaScript.getEditor(); 91 } 92 } 93 94 /** 95 * Returns a factory for creating the service. 96 * This method is called by the <code>JavaLoader</code> 97 * <p> 98 * 99 * @param implName the name of the implementation for which a service is desired 100 * @param multiFactory the service manager to be used if needed 101 * @param regKey the registryKey 102 * @return returns a <code>XSingleServiceFactory</code> for creating 103 * the component 104 * @see com.sun.star.comp.loader.JavaLoader 105 */ __getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey )106 public static XSingleServiceFactory __getServiceFactory( String implName, 107 XMultiServiceFactory multiFactory, 108 XRegistryKey regKey ) 109 { 110 XSingleServiceFactory xSingleServiceFactory = null; 111 112 if ( implName.equals( ScriptProviderForJavaScript._ScriptProviderForJavaScript.class.getName() ) ) 113 { 114 xSingleServiceFactory = FactoryHelper.getServiceFactory( 115 ScriptProviderForJavaScript._ScriptProviderForJavaScript.class, 116 "com.sun.star.script.provider.ScriptProviderForJavaScript", 117 multiFactory, 118 regKey ); 119 } 120 121 return xSingleServiceFactory; 122 } 123 } 124 class ScriptImpl implements XScript 125 { 126 private ScriptMetaData metaData; 127 private XComponentContext m_xContext; 128 private XMultiComponentFactory m_xMultiComponentFactory; 129 private XModel m_xModel; 130 private XScriptInvocationContext m_xInvocContext; 131 ScriptImpl( XComponentContext ctx, ScriptMetaData metaData, XModel xModel, XScriptInvocationContext xInvocContext )132 ScriptImpl( XComponentContext ctx, ScriptMetaData metaData, XModel xModel, XScriptInvocationContext xInvocContext ) throws com.sun.star.uno.RuntimeException 133 { 134 this.metaData = metaData; 135 this.m_xContext = ctx; 136 this.m_xModel = xModel; 137 this.m_xInvocContext = xInvocContext; 138 try 139 { 140 this.m_xMultiComponentFactory = m_xContext.getServiceManager(); 141 } 142 catch ( Exception e ) 143 { 144 LogUtils.DEBUG( LogUtils.getTrace( e ) ); 145 throw new com.sun.star.uno.RuntimeException( 146 "Error constructing ScriptImpl: [javascript]"); 147 } 148 LogUtils.DEBUG("ScriptImpl [javascript] script data = " + metaData ); 149 } 150 151 /** 152 * The invoke method of the ScriptProviderForJavaScript runs the 153 * JavaScript script specified in the URI 154 * 155 * 156 * 157 * @param params All parameters; pure, out params are 158 * undefined in sequence, i.e., the value 159 * has to be ignored by the callee 160 * 161 * @param aOutParamIndex Out indices 162 * 163 * @param aOutParam Out parameters 164 * 165 * @returns The value returned from the function 166 * being invoked 167 * 168 * @throws ScriptFrameworkErrorException If there is no matching script name 169 * 170 * 171 * @throws InvocationTargetException If the running script throws 172 * an exception this information 173 * is captured and rethrown as 174 * ScriptErrorRaisedException or 175 * ScriptExceptionRaisedException 176 */ 177 invoke( Object[] params, short[][] aOutParamIndex, Object[][] aOutParam )178 public Object invoke( 179 /*IN*/Object[] params, 180 /*OUT*/short[][] aOutParamIndex, 181 /*OUT*/Object[][] aOutParam ) 182 183 throws ScriptFrameworkErrorException, InvocationTargetException 184 { 185 // Initialise the out parameters - not used at the moment 186 aOutParamIndex[0] = new short[0]; 187 aOutParam[0] = new Object[0]; 188 189 190 191 ClassLoader cl = null; 192 URL sourceUrl = null; 193 try { 194 cl = ClassLoaderFactory.getURLClassLoader( metaData ); 195 sourceUrl = metaData.getSourceURL(); 196 } 197 catch ( java.net.MalformedURLException mfu ) 198 { 199 throw new ScriptFrameworkErrorException( 200 mfu.getMessage(), null, 201 metaData.getLanguageName(), metaData.getLanguage(), 202 ScriptFrameworkErrorType.MALFORMED_URL ); 203 } 204 catch ( com.sun.star.script.framework.provider.NoSuitableClassLoaderException nsc ) 205 { 206 // Framework error 207 throw new ScriptFrameworkErrorException( 208 nsc.getMessage(), null, 209 metaData.getLanguageName(), metaData.getLanguage(), 210 ScriptFrameworkErrorType.UNKNOWN ); 211 } 212 Context ctxt = null; 213 ClassLoader prevCl = Thread.currentThread().getContextClassLoader(); 214 215 try 216 { 217 String editorURL = sourceUrl.toString(); 218 Object result = null; 219 String source = null; 220 ScriptEditorForJavaScript editor = 221 ScriptEditorForJavaScript.getEditor( 222 metaData.getSourceURL() ); 223 224 if (editor != null) 225 { 226 editorURL = editor.getURL(); 227 result = editor.execute(); 228 if ( result != null && 229 result.getClass().getName().equals( "org.mozilla.javascript.Undefined" ) ) 230 { 231 // Always return a string 232 // TODO revisit 233 return Context.toString( result ); 234 } 235 else if ( result != null ) 236 { 237 // editor returned a non-Undefined result; do not execute again 238 return Context.toString( result ); 239 } 240 else 241 { 242 // editor executed the script (result may legitimately be null); do not execute again 243 return null; 244 } 245 246 } 247 248 if (editor != null && editor.isModified() == true) 249 { 250 LogUtils.DEBUG("GOT A MODIFIED SOURCE"); 251 source = editor.getText(); 252 } 253 else 254 { 255 metaData.loadSource(); 256 source = metaData.getSource(); 257 258 } 259 260 if ( source == null || source.length() == 0 ) { 261 throw new ScriptFrameworkErrorException( 262 "Failed to read source data for script", null, 263 metaData.getLanguageName(), metaData.getLanguage(), 264 ScriptFrameworkErrorType.UNKNOWN ); 265 } 266 267 /* Set the context ClassLoader on the current thread to 268 be our custom ClassLoader. This is the suggested method 269 for setting up a ClassLoader to be used by the Rhino 270 interpreter 271 */ 272 if (cl != null) { 273 Thread.currentThread().setContextClassLoader(cl); 274 } 275 276 // Initialize a Rhino Context object 277 ctxt = Context.enter(); 278 ctxt.setLanguageVersion(Context.VERSION_ES6); 279 ctxt.setOptimizationLevel(9); 280 281 /* The ImporterTopLevel ensures that importClass and 282 importPackage statements work in Javascript scripts 283 Make the XScriptContext available as a global variable 284 to the script 285 */ 286 ImporterTopLevel scope = new ImporterTopLevel(ctxt); 287 288 Scriptable jsCtxt = Context.toObject( 289 ScriptContext.createContext( 290 m_xModel, m_xInvocContext, m_xContext, 291 m_xMultiComponentFactory), scope); 292 scope.put("XSCRIPTCONTEXT", scope, jsCtxt); 293 294 Scriptable jsArgs = Context.toObject(params, scope); 295 scope.put("ARGUMENTS", scope, jsArgs); 296 297 result = ctxt.evaluateString(scope, 298 source, editorURL, 1, null); 299 result = ctxt.toString(result); 300 return result; 301 } 302 catch (JavaScriptException jse) { 303 LogUtils.DEBUG( "Caught JavaScriptException exception for JavaScript type = " + jse.getClass() ); 304 String message = jse.getMessage(); 305 int lineNo = jse.lineNumber(); 306 Object wrap = jse.getValue(); 307 LogUtils.DEBUG( "\t message " + message ); 308 LogUtils.DEBUG( "\t wrapped type " + (wrap != null ? wrap.getClass().getName() : "null") ); 309 LogUtils.DEBUG( "\t wrapped toString " + (wrap != null ? wrap.toString() : "null") ); 310 ScriptExceptionRaisedException se = new 311 ScriptExceptionRaisedException( message ); 312 se.lineNum = lineNo; 313 se.language = "JavaScript"; 314 se.scriptName = metaData.getLanguageName(); 315 se.exceptionType = (wrap != null) ? wrap.getClass().getName() : "null"; 316 se.language = metaData.getLanguage(); 317 LogUtils.DEBUG( "ExceptionRaised exception " ); 318 LogUtils.DEBUG( "\t message " + se.getMessage() ); 319 LogUtils.DEBUG( "\t lineNum " + se.lineNum ); 320 LogUtils.DEBUG( "\t language " + se.language ); 321 LogUtils.DEBUG( "\t scriptName " + se.scriptName ); 322 raiseEditor( se.lineNum ); 323 throw new InvocationTargetException( "JavaScript uncaught exception" + metaData.getLanguageName(), null, se ); 324 } 325 catch (Exception ex) { 326 LogUtils.DEBUG("Caught Exception " + ex ); 327 LogUtils.DEBUG("rethrowing as ScriptFramework error" ); 328 throw new ScriptFrameworkErrorException( 329 ex.getMessage(), null, 330 metaData.getLanguageName(), metaData.getLanguage(), 331 ScriptFrameworkErrorType.UNKNOWN ); 332 } 333 finally { 334 if ( ctxt != null ) 335 { 336 Context.exit(); 337 } 338 if (prevCl != null) { 339 Thread.currentThread().setContextClassLoader(prevCl); 340 } 341 } 342 } 343 raiseEditor( int lineNum )344 private void raiseEditor( int lineNum ) 345 { 346 ScriptEditorForJavaScript editor = null; 347 try 348 { 349 URL sourceUrl = metaData.getSourceURL(); 350 editor = ScriptEditorForJavaScript.getEditor( sourceUrl ); 351 if ( editor == null ) 352 { 353 editor = ScriptEditorForJavaScript.getEditor(); 354 editor.edit( 355 ScriptContext.createContext(m_xModel, m_xInvocContext, 356 m_xContext, m_xMultiComponentFactory), metaData ); 357 editor = ScriptEditorForJavaScript.getEditor( sourceUrl ); 358 } 359 if ( editor != null ) 360 { 361 System.out.println("** Have raised IDE for JavaScript, calling indicateErrorLine for line " + lineNum ); 362 editor.indicateErrorLine( lineNum ); 363 } 364 } 365 catch( Exception ignore ) 366 { 367 } 368 } 369 } 370