Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/26 update jruby version #27

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion use-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-core</artifactId>
<version>1.7.2</version>
<version>9.3.1.0</version>
</dependency>
<dependency>
<groupId>com.ximpleware</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package org.tzi.use.uml.ocl.extension;

import java.util.ArrayList;
import java.util.List;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.jruby.embed.EvalFailedException;
import org.tzi.use.uml.ocl.expr.EvalContext;
import org.tzi.use.uml.ocl.expr.operations.OpGeneric;
import org.tzi.use.uml.ocl.type.Type;
import org.tzi.use.uml.ocl.value.UndefinedValue;
import org.tzi.use.uml.ocl.value.Value;
import org.tzi.use.util.Log;
import org.tzi.use.util.NullWriter;
import org.tzi.use.util.rubyintegration.RubyHelper;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class ExtensionOperation extends OpGeneric {

public static class Parameter {
private String name;
private String typeName;
private final String name;
private final String typeName;
private Type type;

public Parameter(String name, String typeName) {
Expand Down Expand Up @@ -75,14 +74,15 @@ public void addParameter(String name, String typeName) {
public Value eval(EvalContext ctx, Value[] args, Type resultType) {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName("jruby");

StringWriter errorWriter = new StringWriter();

if (rubyEngine == null)
throw new RuntimeException("Did not find the ruby engine. Please verify your classpath");

ScriptContext context = rubyEngine.getContext();
context.setErrorWriter(new NullWriter());
context.setErrorWriter(errorWriter);

context.setAttribute("self", RubyHelper.useValueToRubyValue(args[0]), ScriptContext.ENGINE_SCOPE);
context.setAttribute("$self", RubyHelper.useValueToRubyValue(args[0]), ScriptContext.ENGINE_SCOPE);

for (int i = 0; i < parameter.size(); i++) {
context.setAttribute(parameter.get(i).getName(),
Expand All @@ -104,10 +104,12 @@ public Value eval(EvalContext ctx, Value[] args, Type resultType) {
return resultValue;
}

} catch (ScriptException e) {
Log.error(e.getMessage());
} catch (EvalFailedException e) {
Log.error(e.getMessage());
} catch (ScriptException | EvalFailedException e) {
Log.error(e.getMessage());

if (errorWriter.getBuffer().length() > 0) {
Log.error(errorWriter.toString());
}
}

return UndefinedValue.instance;
Expand Down Expand Up @@ -149,17 +151,17 @@ public String name() {
public void initialize() {
this.sourceType = ExtensionManager.getInstance().getType(this.sourceTypeName);
if (this.sourceType == null)
throw new RuntimeException("Unknown source type '" + this.sourceType + "'");
throw new RuntimeException("Unknown source type '" + this.sourceTypeName + "'");

this.resultType = ExtensionManager.getInstance().getType(this.resultTypeName);
if (this.resultType == null)
throw new RuntimeException("Unknown result type '" + this.resultType + "'");
throw new RuntimeException("Unknown result type '" + this.resultTypeName + "'");

for (Parameter par : this.parameter) {
par.setType(ExtensionManager.getInstance().getType(par.getTypeName()));

if (par.getType() == null)
throw new RuntimeException("Unknown parameter type '" + this.resultType + "'");
throw new RuntimeException("Unknown parameter type '" + par.getTypeName() + "'");
}
}
}