| 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.beanshell; |
| 19 |
|
| 20 |
import bsh.Interpreter; |
| 21 |
|
| 22 |
import com.sun.star.comp.loader.FactoryHelper; |
| 23 |
|
| 24 |
import com.sun.star.document.XScriptInvocationContext; |
| 25 |
|
| 26 |
import com.sun.star.frame.XModel; |
| 27 |
|
| 28 |
import com.sun.star.lang.XMultiComponentFactory; |
| 29 |
import com.sun.star.lang.XMultiServiceFactory; |
| 30 |
import com.sun.star.lang.XSingleServiceFactory; |
| 31 |
|
| 32 |
import com.sun.star.reflection.InvocationTargetException; |
| 33 |
|
| 34 |
import com.sun.star.registry.XRegistryKey; |
| 35 |
|
| 36 |
import com.sun.star.script.framework.container.ScriptMetaData; |
| 37 |
import com.sun.star.script.framework.log.LogUtils; |
| 38 |
import com.sun.star.script.framework.provider.ClassLoaderFactory; |
| 39 |
import com.sun.star.script.framework.provider.ScriptContext; |
| 40 |
import com.sun.star.script.framework.provider.ScriptEditor; |
| 41 |
import com.sun.star.script.framework.provider.ScriptProvider; |
| 42 |
import com.sun.star.script.provider.ScriptErrorRaisedException; |
| 43 |
import com.sun.star.script.provider.ScriptExceptionRaisedException; |
| 44 |
import com.sun.star.script.provider.ScriptFrameworkErrorException; |
| 45 |
import com.sun.star.script.provider.ScriptFrameworkErrorType; |
| 46 |
import com.sun.star.script.provider.XScript; |
| 47 |
|
| 48 |
import com.sun.star.uno.Any; |
| 49 |
import com.sun.star.uno.Type; |
| 50 |
import com.sun.star.uno.XComponentContext; |
| 51 |
|
| 52 |
import java.net.URL; |
| 53 |
|
| 54 |
import java.util.StringTokenizer; |
| 55 |
|
| 56 |
public class ScriptProviderForBeanShell { |
| 57 |
|
| 58 |
public static class ScriptProviderForBeanShell_2 extends ScriptProvider { |
| 59 |
|
| 60 |
public ScriptProviderForBeanShell_2(XComponentContext ctx) { |
| 61 |
super(ctx, "BeanShell"); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public XScript getScript(/*IN*/String scriptURI) throws |
| 66 |
com.sun.star.uno.RuntimeException, ScriptFrameworkErrorException { |
| 67 |
|
| 68 |
ScriptMetaData scriptData = getScriptData(scriptURI); |
| 69 |
|
| 70 |
try { |
| 71 |
|
| 72 |
ScriptImpl script = |
| 73 |
new ScriptImpl(m_xContext, scriptData, m_xModel, m_xInvocContext); |
| 74 |
|
| 75 |
return script; |
| 76 |
} catch (com.sun.star.uno.RuntimeException re) { |
| 77 |
throw new ScriptFrameworkErrorException( |
| 78 |
"Failed to create script object: " + re.getMessage(), |
| 79 |
null, scriptData.getLanguageName(), language, |
| 80 |
ScriptFrameworkErrorType.UNKNOWN); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public boolean hasScriptEditor() { |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public ScriptEditor getScriptEditor() { |
| 91 |
return ScriptEditorForBeanShell.getEditor(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns a factory for creating the service. |
| 97 |
* This method is called by the <code>JavaLoader</code> |
| 98 |
* <p> |
| 99 |
* |
| 100 |
* @param implName the name of the implementation for which a service is desired |
| 101 |
* @param multiFactory the service manager to be used if needed |
| 102 |
* @param regKey the registryKey |
| 103 |
* @return returns a <code>XSingleServiceFactory</code> for creating |
| 104 |
* the component |
| 105 |
* @see com.sun.star.comp.loader.JavaLoader |
| 106 |
*/ |
| 107 |
public static XSingleServiceFactory __getServiceFactory( |
| 108 |
String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey) { |
| 109 |
|
| 110 |
XSingleServiceFactory xSingleServiceFactory = null; |
| 111 |
|
| 112 |
if (implName.equals( |
| 113 |
ScriptProviderForBeanShell.ScriptProviderForBeanShell_2.class.getName())) { |
| 114 |
|
| 115 |
xSingleServiceFactory = |
| 116 |
FactoryHelper.getServiceFactory( |
| 117 |
ScriptProviderForBeanShell.ScriptProviderForBeanShell_2.class, |
| 118 |
"com.sun.star.script.provider.ScriptProviderForBeanShell", |
| 119 |
multiFactory, regKey); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
return xSingleServiceFactory; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
class ScriptImpl implements XScript { |
| 128 |
|
| 129 |
private final ScriptMetaData metaData; |
| 130 |
private final XComponentContext m_xContext; |
| 131 |
private XMultiComponentFactory m_xMultiComponentFactory; |
| 132 |
private final XModel m_xModel; |
| 133 |
private final XScriptInvocationContext m_xInvocContext; |
| 134 |
|
| 135 |
ScriptImpl(XComponentContext ctx, ScriptMetaData metaData, XModel xModel, |
| 136 |
XScriptInvocationContext xContext) throws |
| 137 |
com.sun.star.uno.RuntimeException { |
| 138 |
|
| 139 |
this.metaData = metaData; |
| 140 |
this.m_xContext = ctx; |
| 141 |
this.m_xModel = xModel; |
| 142 |
this.m_xInvocContext = xContext; |
| 143 |
|
| 144 |
try { |
| 145 |
this.m_xMultiComponentFactory = m_xContext.getServiceManager(); |
| 146 |
} catch (Exception e) { |
| 147 |
throw new com.sun.star.uno.RuntimeException(e); |
| 148 |
} |
| 149 |
|
| 150 |
LogUtils.DEBUG("ScriptImpl [beanshell] script data = " + metaData); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* documentStorageID and document reference |
| 155 |
* for use in script name resolving |
| 156 |
* |
| 157 |
* @param aParams 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 |
* @return The value returned from the function |
| 166 |
* being invoked |
| 167 |
* |
| 168 |
* @throws IllegalArgumentException If there is no matching script name |
| 169 |
* |
| 170 |
* @throws InvocationTargetException If the running script throws |
| 171 |
* an exception this information |
| 172 |
* is captured and rethrown as |
| 173 |
* this exception type. |
| 174 |
*/ |
| 175 |
|
| 176 |
public Object invoke(/*IN*/Object[] aParams, |
| 177 |
/*OUT*/short[][] aOutParamIndex, |
| 178 |
/*OUT*/Object[][] aOutParam) throws |
| 179 |
ScriptFrameworkErrorException, InvocationTargetException { |
| 180 |
|
| 181 |
// Initialise the out parameters - not used at the moment |
| 182 |
aOutParamIndex[0] = new short[0]; |
| 183 |
aOutParam[0] = new Object[0]; |
| 184 |
|
| 185 |
ClassLoader cl = null; |
| 186 |
URL sourceUrl = null; |
| 187 |
|
| 188 |
try { |
| 189 |
cl = ClassLoaderFactory.getURLClassLoader(metaData); |
| 190 |
sourceUrl = metaData.getSourceURL(); |
| 191 |
} catch (java.net.MalformedURLException mfu) { |
| 192 |
// Framework error |
| 193 |
throw new ScriptFrameworkErrorException( |
| 194 |
mfu.getMessage(), null, |
| 195 |
metaData.getLanguageName(), metaData.getLanguage(), |
| 196 |
ScriptFrameworkErrorType.MALFORMED_URL); |
| 197 |
} |
| 198 |
|
| 199 |
// Set class loader to be used for class files |
| 200 |
// and jar files |
| 201 |
Thread.currentThread().setContextClassLoader(cl); |
| 202 |
Interpreter interpreter = new Interpreter(); |
| 203 |
|
| 204 |
interpreter.getNameSpace().clear(); |
| 205 |
// Set class loader to be used by interpreter |
| 206 |
// to look for classes by source e.g. interpreter |
| 207 |
// will use this classloader to search classpath |
| 208 |
// for source file ( bla.java ) on import or reference |
| 209 |
interpreter.setClassLoader(cl); |
| 210 |
|
| 211 |
try { |
| 212 |
|
| 213 |
interpreter.set("XSCRIPTCONTEXT", |
| 214 |
ScriptContext.createContext(m_xModel, m_xInvocContext, |
| 215 |
m_xContext, m_xMultiComponentFactory)); |
| 216 |
|
| 217 |
interpreter.set("ARGUMENTS", aParams); |
| 218 |
} catch (bsh.EvalError e) { |
| 219 |
// Framework error setting up context |
| 220 |
throw new ScriptFrameworkErrorException( |
| 221 |
e.getMessage(), null, |
| 222 |
metaData.getLanguageName(), metaData.getLanguage(), |
| 223 |
ScriptFrameworkErrorType.UNKNOWN); |
| 224 |
} |
| 225 |
|
| 226 |
try { |
| 227 |
Object result; |
| 228 |
|
| 229 |
ScriptEditorForBeanShell editor = |
| 230 |
ScriptEditorForBeanShell.getEditor(sourceUrl); |
| 231 |
|
| 232 |
if (editor != null) { |
| 233 |
result = editor.execute(); |
| 234 |
|
| 235 |
if (result == null) { |
| 236 |
return new Any(new Type(), null); |
| 237 |
} |
| 238 |
|
| 239 |
return result; |
| 240 |
} |
| 241 |
|
| 242 |
metaData.loadSource(); |
| 243 |
String source = metaData.getSource(); |
| 244 |
|
| 245 |
if (source == null || source.length() == 0) { |
| 246 |
throw new ScriptFrameworkErrorException( |
| 247 |
"Failed to read script", null, |
| 248 |
metaData.getLanguageName(), metaData.getLanguage(), |
| 249 |
ScriptFrameworkErrorType.NO_SUCH_SCRIPT); |
| 250 |
} |
| 251 |
|
| 252 |
result = interpreter.eval(source); |
| 253 |
|
| 254 |
if (result == null) { |
| 255 |
return new Any(new Type(), null); |
| 256 |
} |
| 257 |
|
| 258 |
return result; |
| 259 |
} catch (bsh.ParseException pe) { |
| 260 |
throw new InvocationTargetException( |
| 261 |
"Beanshell failed to parse " + metaData.getLanguageName(), |
| 262 |
null, processBshException(pe, metaData.getLanguageName())); |
| 263 |
} catch (bsh.TargetError te) { |
| 264 |
throw new InvocationTargetException( |
| 265 |
"Beanshell uncaught exception for " + metaData.getLanguageName(), |
| 266 |
null, processBshException(te, metaData.getLanguageName())); |
| 267 |
} catch (bsh.EvalError ex) { |
| 268 |
throw new InvocationTargetException( |
| 269 |
"Beanshell error for " + metaData.getLanguageName(), |
| 270 |
null, processBshException(ex, metaData.getLanguageName())); |
| 271 |
} catch (Exception e) { |
| 272 |
throw new ScriptFrameworkErrorException( |
| 273 |
"Failed to read script", null, metaData.getLanguageName(), |
| 274 |
metaData.getLanguage(), ScriptFrameworkErrorType.UNKNOWN); |
| 275 |
} |
| 276 |
} |
| 277 |
private void raiseEditor(int lineNum) { |
| 278 |
try { |
| 279 |
URL sourceUrl = metaData.getSourceURL(); |
| 280 |
|
| 281 |
ScriptEditorForBeanShell editor = |
| 282 |
ScriptEditorForBeanShell.getEditor(sourceUrl); |
| 283 |
|
| 284 |
if (editor == null) { |
| 285 |
editor = ScriptEditorForBeanShell.getEditor(); |
| 286 |
|
| 287 |
editor.edit( |
| 288 |
ScriptContext.createContext(m_xModel, m_xInvocContext, m_xContext, |
| 289 |
m_xMultiComponentFactory), metaData); |
| 290 |
|
| 291 |
editor = ScriptEditorForBeanShell.getEditor(sourceUrl); |
| 292 |
} |
| 293 |
|
| 294 |
if (editor != null) { |
| 295 |
editor.indicateErrorLine(lineNum); |
| 296 |
} |
| 297 |
} catch (java.net.MalformedURLException ignore) { |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
private ScriptErrorRaisedException processBshException( |
| 302 |
bsh.EvalError e, String script) { |
| 303 |
|
| 304 |
LogUtils.DEBUG("Beanshell error RAW message " + e.getMessage()); |
| 305 |
String message = e.getMessage(); |
| 306 |
int usefulInfoIndex = message.lastIndexOf("\' :"); |
| 307 |
int lineNum = e.getErrorLineNumber(); |
| 308 |
|
| 309 |
raiseEditor(lineNum); |
| 310 |
|
| 311 |
if (usefulInfoIndex > -1) { |
| 312 |
message = message.substring(usefulInfoIndex + 2); |
| 313 |
} |
| 314 |
|
| 315 |
if (e instanceof bsh.TargetError) { |
| 316 |
LogUtils.DEBUG("got instance of TargetError"); |
| 317 |
|
| 318 |
if (usefulInfoIndex == -1) { |
| 319 |
message = ((bsh.TargetError)e).getTarget().getMessage(); |
| 320 |
} |
| 321 |
|
| 322 |
String wrappedException = ""; |
| 323 |
String full = e.toString(); |
| 324 |
int index = full.indexOf("Target exception:"); |
| 325 |
|
| 326 |
if (index > -1) { |
| 327 |
String toParse = full.substring(index); |
| 328 |
LogUtils.DEBUG("About to parse " + toParse); |
| 329 |
|
| 330 |
StringTokenizer tokenizer = |
| 331 |
new StringTokenizer(full.substring(index), ":"); |
| 332 |
|
| 333 |
if (tokenizer.countTokens() > 2) { |
| 334 |
LogUtils.DEBUG("First token = " + tokenizer.nextToken()); |
| 335 |
wrappedException = tokenizer.nextToken(); |
| 336 |
LogUtils.DEBUG("wrapped exception = = " + wrappedException); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
ScriptExceptionRaisedException se = |
| 341 |
new ScriptExceptionRaisedException(message); |
| 342 |
|
| 343 |
se.lineNum = lineNum; |
| 344 |
se.scriptName = script; |
| 345 |
se.exceptionType = wrappedException; |
| 346 |
se.language = "BeanShell"; |
| 347 |
|
| 348 |
LogUtils.DEBUG("UnCaught Exception error: "); |
| 349 |
LogUtils.DEBUG("\tscript: " + script); |
| 350 |
LogUtils.DEBUG("\tline: " + lineNum); |
| 351 |
LogUtils.DEBUG("\twrapped exception: " + wrappedException); |
| 352 |
LogUtils.DEBUG("\tmessage: " + message); |
| 353 |
|
| 354 |
return se; |
| 355 |
|
| 356 |
} else { |
| 357 |
LogUtils.DEBUG("Error or ParseError Exception error: "); |
| 358 |
LogUtils.DEBUG("\tscript: " + script); |
| 359 |
LogUtils.DEBUG("\tline: " + lineNum); |
| 360 |
LogUtils.DEBUG("\tmessage: " + message); |
| 361 |
|
| 362 |
return new ScriptErrorRaisedException(message, null, script, |
| 363 |
"BeanShell", lineNum); |
| 364 |
|
| 365 |
} |
| 366 |
} |
| 367 |
} |