| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package com.sun.star.script.framework.provider.javascript; |
| 19 |
|
| 20 |
import com.sun.star.comp.loader.FactoryHelper; |
| 21 |
|
| 22 |
import com.sun.star.document.XScriptInvocationContext; |
| 23 |
|
| 24 |
import com.sun.star.frame.XModel; |
| 25 |
|
| 26 |
import com.sun.star.lang.XMultiComponentFactory; |
| 27 |
import com.sun.star.lang.XMultiServiceFactory; |
| 28 |
import com.sun.star.lang.XSingleServiceFactory; |
| 29 |
|
| 30 |
import com.sun.star.reflection.InvocationTargetException; |
| 31 |
|
| 32 |
import com.sun.star.registry.XRegistryKey; |
| 33 |
|
| 34 |
import com.sun.star.script.framework.container.ScriptMetaData; |
| 35 |
import com.sun.star.script.framework.log.LogUtils; |
| 36 |
import com.sun.star.script.framework.provider.ClassLoaderFactory; |
| 37 |
import com.sun.star.script.framework.provider.ScriptContext; |
| 38 |
import com.sun.star.script.framework.provider.ScriptEditor; |
| 39 |
import com.sun.star.script.framework.provider.ScriptProvider; |
| 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 |
import com.sun.star.script.provider.XScript; |
| 44 |
|
| 45 |
import com.sun.star.uno.XComponentContext; |
| 46 |
|
| 47 |
import org.mozilla.javascript.Context; |
| 48 |
import org.mozilla.javascript.ImporterTopLevel; |
| 49 |
import org.mozilla.javascript.JavaScriptException; |
| 50 |
import org.mozilla.javascript.Scriptable; |
| 51 |
|
| 52 |
public class ScriptProviderForJavaScript { |
| 53 |
|
| 54 |
public static class ScriptProviderForJavaScript_2 extends ScriptProvider { |
| 55 |
|
| 56 |
public ScriptProviderForJavaScript_2(XComponentContext ctx) { |
| 57 |
super(ctx, "JavaScript"); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public XScript getScript(/*IN*/String scriptURI) |
| 62 |
throws com.sun.star.uno.RuntimeException, ScriptFrameworkErrorException { |
| 63 |
ScriptMetaData scriptData = null; |
| 64 |
|
| 65 |
try { |
| 66 |
scriptData = getScriptData(scriptURI); |
| 67 |
ScriptImpl script = new ScriptImpl(m_xContext, scriptData, m_xModel, |
| 68 |
m_xInvocContext); |
| 69 |
return script; |
| 70 |
} catch (com.sun.star.uno.RuntimeException re) { |
| 71 |
throw new ScriptFrameworkErrorException( |
| 72 |
"Failed to create script object: " + re.getMessage(), |
| 73 |
null, scriptData.getLanguageName(), language, ScriptFrameworkErrorType.UNKNOWN); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean hasScriptEditor() { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public ScriptEditor getScriptEditor() { |
| 84 |
return null; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns a factory for creating the service. |
| 90 |
* This method is called by the <code>JavaLoader</code> |
| 91 |
* <p> |
| 92 |
* |
| 93 |
* @param implName the name of the implementation for which a service is desired |
| 94 |
* @param multiFactory the service manager to be used if needed |
| 95 |
* @param regKey the registryKey |
| 96 |
* @return returns a <code>XSingleServiceFactory</code> for creating |
| 97 |
* the component |
| 98 |
* @see com.sun.star.comp.loader.JavaLoader |
| 99 |
*/ |
| 100 |
public static XSingleServiceFactory __getServiceFactory(String implName, |
| 101 |
XMultiServiceFactory multiFactory, XRegistryKey regKey) { |
| 102 |
|
| 103 |
XSingleServiceFactory xSingleServiceFactory = null; |
| 104 |
|
| 105 |
if (implName.equals( |
| 106 |
ScriptProviderForJavaScript.ScriptProviderForJavaScript_2.class.getName())) { |
| 107 |
|
| 108 |
xSingleServiceFactory = |
| 109 |
FactoryHelper.getServiceFactory( |
| 110 |
ScriptProviderForJavaScript.ScriptProviderForJavaScript_2.class, |
| 111 |
"com.sun.star.script.provider.ScriptProviderForJavaScript", |
| 112 |
multiFactory, regKey); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
return xSingleServiceFactory; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
class ScriptImpl implements XScript { |
| 121 |
|
| 122 |
private final ScriptMetaData metaData; |
| 123 |
private final XComponentContext m_xContext; |
| 124 |
private XMultiComponentFactory m_xMultiComponentFactory; |
| 125 |
private final XModel m_xModel; |
| 126 |
private final XScriptInvocationContext m_xInvocContext; |
| 127 |
|
| 128 |
ScriptImpl(XComponentContext ctx, ScriptMetaData metaData, XModel xModel, |
| 129 |
XScriptInvocationContext xInvocContext) throws |
| 130 |
com.sun.star.uno.RuntimeException { |
| 131 |
|
| 132 |
this.metaData = metaData; |
| 133 |
this.m_xContext = ctx; |
| 134 |
this.m_xModel = xModel; |
| 135 |
this.m_xInvocContext = xInvocContext; |
| 136 |
|
| 137 |
try { |
| 138 |
this.m_xMultiComponentFactory = m_xContext.getServiceManager(); |
| 139 |
} catch (Exception e) { |
| 140 |
throw new com.sun.star.uno.RuntimeException(e); |
| 141 |
} |
| 142 |
|
| 143 |
LogUtils.DEBUG("ScriptImpl [javascript] script data = " + metaData); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* The invoke method of the ScriptProviderForJavaScript runs the |
| 148 |
* JavaScript script specified in the URI |
| 149 |
* |
| 150 |
* |
| 151 |
* |
| 152 |
* @param params All parameters; pure, out params are |
| 153 |
* undefined in sequence, i.e., the value |
| 154 |
* has to be ignored by the callee |
| 155 |
* |
| 156 |
* @param aOutParamIndex Out indices |
| 157 |
* |
| 158 |
* @param aOutParam Out parameters |
| 159 |
* |
| 160 |
* @return The value returned from the function |
| 161 |
* being invoked |
| 162 |
* |
| 163 |
* @throws ScriptFrameworkErrorException If there is no matching script name |
| 164 |
* |
| 165 |
* |
| 166 |
* @throws InvocationTargetException If the running script throws |
| 167 |
* an exception this information |
| 168 |
* is captured and rethrown as |
| 169 |
* ScriptErrorRaisedException or |
| 170 |
* ScriptExceptionRaisedException |
| 171 |
*/ |
| 172 |
public Object invoke( |
| 173 |
/*IN*/Object[] params, |
| 174 |
/*OUT*/short[][] aOutParamIndex, |
| 175 |
/*OUT*/Object[][] aOutParam) |
| 176 |
throws ScriptFrameworkErrorException, InvocationTargetException { |
| 177 |
|
| 178 |
// Initialise the out parameters - not used at the moment |
| 179 |
aOutParamIndex[0] = new short[0]; |
| 180 |
aOutParam[0] = new Object[0]; |
| 181 |
|
| 182 |
ClassLoader cl = null; |
| 183 |
|
| 184 |
try { |
| 185 |
cl = ClassLoaderFactory.getURLClassLoader(metaData); |
| 186 |
metaData.getSourceURL(); |
| 187 |
} catch (java.net.MalformedURLException mfu) { |
| 188 |
throw new ScriptFrameworkErrorException( |
| 189 |
mfu.getMessage(), null, |
| 190 |
metaData.getLanguageName(), metaData.getLanguage(), |
| 191 |
ScriptFrameworkErrorType.MALFORMED_URL); |
| 192 |
} |
| 193 |
|
| 194 |
Context ctxt = null; |
| 195 |
|
| 196 |
try { |
| 197 |
Object result = null; |
| 198 |
|
| 199 |
String source; |
| 200 |
|
| 201 |
metaData.loadSource(); |
| 202 |
source = metaData.getSource(); |
| 203 |
|
| 204 |
if (source == null || source.length() == 0) { |
| 205 |
throw new ScriptFrameworkErrorException( |
| 206 |
"Failed to read source data for script", null, |
| 207 |
metaData.getLanguageName(), metaData.getLanguage(), |
| 208 |
ScriptFrameworkErrorType.UNKNOWN); |
| 209 |
} |
| 210 |
|
| 211 |
/* Set the context ClassLoader on the current thread to |
| 212 |
be our custom ClassLoader. This is the suggested method |
| 213 |
for setting up a ClassLoader to be used by the Rhino |
| 214 |
interpreter |
| 215 |
*/ |
| 216 |
if (cl != null) { |
| 217 |
Thread.currentThread().setContextClassLoader(cl); |
| 218 |
} |
| 219 |
|
| 220 |
// Initialize a Rhino Context object |
| 221 |
ctxt = Context.enter(); |
| 222 |
|
| 223 |
/* The ImporterTopLevel ensures that importClass and |
| 224 |
importPackage statements work in Javascript scripts |
| 225 |
Make the XScriptContext available as a global variable |
| 226 |
to the script |
| 227 |
*/ |
| 228 |
ImporterTopLevel scope = new ImporterTopLevel(ctxt); |
| 229 |
|
| 230 |
Scriptable jsCtxt = |
| 231 |
Context.toObject( |
| 232 |
ScriptContext.createContext( |
| 233 |
m_xModel, m_xInvocContext, m_xContext, |
| 234 |
m_xMultiComponentFactory), |
| 235 |
scope); |
| 236 |
|
| 237 |
scope.put("XSCRIPTCONTEXT", scope, jsCtxt); |
| 238 |
|
| 239 |
Scriptable jsArgs = Context.toObject(params, scope); |
| 240 |
scope.put("ARGUMENTS", scope, jsArgs); |
| 241 |
|
| 242 |
result = ctxt.evaluateString(scope, source, "<stdin>", 1, null); |
| 243 |
result = Context.toString(result); |
| 244 |
return result; |
| 245 |
} catch (JavaScriptException jse) { |
| 246 |
LogUtils.DEBUG("Caught JavaScriptException exception for JavaScript type = " |
| 247 |
+ jse.getClass()); |
| 248 |
String message = jse.getMessage(); |
| 249 |
Object wrap = jse.getValue(); |
| 250 |
LogUtils.DEBUG("\t message " + message); |
| 251 |
LogUtils.DEBUG("\t wrapped type " + wrap.getClass()); |
| 252 |
LogUtils.DEBUG("\t wrapped toString " + wrap.toString()); |
| 253 |
ScriptExceptionRaisedException se = new |
| 254 |
ScriptExceptionRaisedException(message); |
| 255 |
se.lineNum = -1; |
| 256 |
se.language = "JavaScript"; |
| 257 |
se.scriptName = metaData.getLanguageName(); |
| 258 |
se.exceptionType = wrap.getClass().getName(); |
| 259 |
se.language = metaData.getLanguage(); |
| 260 |
LogUtils.DEBUG("ExceptionRaised exception "); |
| 261 |
LogUtils.DEBUG("\t message " + se.getMessage()); |
| 262 |
LogUtils.DEBUG("\t lineNum " + se.lineNum); |
| 263 |
LogUtils.DEBUG("\t language " + se.language); |
| 264 |
LogUtils.DEBUG("\t scriptName " + se.scriptName); |
| 265 |
throw new InvocationTargetException( |
| 266 |
"JavaScript uncaught exception" + metaData.getLanguageName(), null, se); |
| 267 |
} catch (Exception ex) { |
| 268 |
LogUtils.DEBUG("Caught Exception " + ex); |
| 269 |
LogUtils.DEBUG("rethrowing as ScriptFramework error"); |
| 270 |
throw new ScriptFrameworkErrorException( |
| 271 |
ex.getMessage(), null, metaData.getLanguageName(), |
| 272 |
metaData.getLanguage(), ScriptFrameworkErrorType.UNKNOWN); |
| 273 |
} finally { |
| 274 |
if (ctxt != null) { |
| 275 |
Context.exit(); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |