GroovyScriptEngine fails to load imports of running groovy script - java

Background :
I started playing with Groovy recently and am trying to embed a groovy script engine in an eclipse plugin to let my customers develop their own GUI extensions inside my eclipse-based product. This is very similar to the success story published on codehaus's website.
Problem
The groovy script (let's call it "main_eclipse.groovy") run from the eclipse plugin by a GroovyScriptEngine throws when trying to load a groovy class ("SwtGuiBuilder"), with the following error :
BUG! Queuing new source whilst already iterating. Queued source is 'file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.groovy'
Question
Did anyone run into the same problem ? How can it be fixed ?
Any help will be highly appreciated !
Some observations :
When using the groovy interpreter instead of a GroovyScriptEngine java object, I have no problem using my SwtGuiBuilder class (see script "main_groovy" here below).
My problem does not seem to be a classpath issue, since the file containing my SwtGuiBuilder class is mentioned in the thrown exception.
The error message is mentioned in two reported groovy bugs, GRECLIPSE-429 and GRECLIPSE-1037. I did not fully get the technicals details, but those bugs seemed to be related to performance issues when loading lots of classes, which is not relevant in my situation...
Details
SampleView.java
public class SampleView
{
public SampleView() { super(); }
public void createPartControl(Composite parent)
{
String groovyScript = null;
String [] groovyPath = null;
boolean shall_exit = false;
do
{ // ask user for params
GroovyLocationDialog groovyLocationDialog= new GroovyLocationDialog(parent.getShell() );
int return_code = groovyLocationDialog.open();
if ( return_code != Window.OK )
shall_exit = true;
else
{
groovyScript= groovyLocationDialog.getInputScriptName();
groovyPath = groovyLocationDialog.getInputScriptPath();
// run it
ScriptConnector scriptConnector = new ScriptConnector(parent);
try { scriptConnector.runGuiComponentScript( groovyPath, groovyScript); }
catch (Exception e) { e.printStackTrace(); }
System.out.println("script finished");
}
}
while ( ! shall_exit );
}
ScriptConnector.java
public class ScriptConnector
{
private String[] roots;
private Composite window;
private Binding binding;
public ScriptConnector( Composite window )
{
this.window = window;
Binding scriptenv = new Binding(); // A new Binding is created ...
scriptenv.setVariable("SDE", this);
scriptenv.setVariable("WINDOW", this.window); // ref to current window
this.binding = scriptenv;
}
public void runGuiComponentScript(final String[] groovyPath, final String scriptName)
{
GroovyScriptEngine gse = null;
this.roots = groovyPath;
try
{
// instanciating the script engine with current classpath
gse = new GroovyScriptEngine( roots, this.getClass().getClassLoader() );
gse.run(scriptName, binding); // ... and run specified script
}
catch (Exception e) { e.printStackTrace(); }
catch (Throwable t) { t.printStackTrace(); }
}
}
main_eclipse.groovy
package launcher;
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.RowLayout as Layout
// This import will fail...
import gui.SwtGuiBuilder;
WINDOW.layout = new Layout(SWT.VERTICAL);
def builder = new SwtGuiBuilder(WINDOW);
builder.Label ( style=SWT.NONE, text = 'Simple demo of Groovy and SWT')
builder.Button( style=SWT.PUSH, text = 'Click me' , action = { println "Click !" } )
SwtGuiBuilder.groovy
package gui;
import org.eclipse.swt.events.*
import org.eclipse.swt.widgets.Button
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Label
class SwtGuiBuilder
{
private Composite _parent
public SwtGuiBuilder(Composite parent) { _parent = parent }
public void Button( style = SWT.PUSH, text= null, action = null )
{
def btn = new Button(_parent, style)
if ( text != null )
btn.text = text
if (action != null)
btn.addSelectionListener( new SelectionAdapter() { void widgetSelected( SelectionEvent event ) { action(); } } );
}
public void Label( style = SWT.NONE, text = '' )
{
def lbl = new Label(_parent, style)
lbl.text = text
}
}
main_groovy.groovy
package launcher;
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.*
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.RowLayout as Layout
// ... But this import is handled properly !
import gui.SwtGuiBuilder;
def display = new Display()
def WINDOW = new Shell(display)
WINDOW.text = 'Groovy / SWT Test';
WINDOW.layout = new Layout(SWT.VERTICAL);
def builder = new SwtGuiBuilder(WINDOW);
builder.Label ( style=SWT.NONE, text = 'Simple demo of Groovy and SWT')
builder.Button( style=SWT.PUSH, text = 'Click me' , action = { println "Ya clicked me !" } )
WINDOW.pack();
WINDOW.open();
while (!WINDOW.disposed) {
if (!WINDOW.display.readAndDispatch())
WINDOW.display.sleep();
}
Stack trace
BUG! Queuing new source whilst already iterating. Queued source is 'file:/home/nicolas/workspace/groovy-test/src/gui/SwtGuiBuilder.groovy'
at org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:460)
at org.codehaus.groovy.control.CompilationUnit.addSource(CompilationUnit.java:433)
at groovy.util.GroovyScriptEngine$ScriptClassLoader$3.findClassNode(GroovyScriptEngine.java:195)
at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:124)
at org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(ResolveVisitor.java:863)
at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:377)
at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1407)
at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:202)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:713)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:1015)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:647)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:596)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
at groovy.util.GroovyScriptEngine$ScriptClassLoader.doParseClass(GroovyScriptEngine.java:247)
at groovy.util.GroovyScriptEngine$ScriptClassLoader.parseClass(GroovyScriptEngine.java:229)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:202)
at groovy.util.GroovyScriptEngine.loadScriptByName(GroovyScriptEngine.java:514)
at groovy.util.GroovyScriptEngine.createScript(GroovyScriptEngine.java:564)
at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:551)
My configuration :
Linux Ubuntu 14.04 x86
Groovy Version: 2.3.2
JVM: 1.7.0_55
Eclipse Kepler SR2 - Build 20140224-0627
Eclipse Groovy plugin v2.0.7

Instead of GroovyScriptEngine, I've used the GroovyShell class (groovy code below but easy enough to change back to java), CompilerConfiguration allows you to specify the classpath.
def config = new CompilerConfiguration(classpath: classpath)
def binding = new Binding()
def result = new GroovyShell(binding, config).evaluate("""
def foo='bar'
""")

Related

java Gherkin parser stream does not release file locks

I am using Gherkin parser to parse feature files and returning the list of Gherkin documents see the function below:
import io.cucumber.gherkin.Gherkin;
import io.cucumber.messages.IdGenerator;
import io.cucumber.messages.Messages;
import io.cucumber.messages.Messages.Envelope;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class GherkinUtils {
private static final Logger LOG = LogManager.getLogger(GherkinUtils.class);
public static ArrayList<Messages.GherkinDocument> getGherkinDocumentsFromFiles() {
IdGenerator idGenerator = new IdGenerator.Incrementing();
ArrayList<Messages.GherkinDocument> listOfGherkinDocuments = new ArrayList<>();
String pathFolderFrameworkFeatures = SettingsUtils.getPathFolderFrameworkFeatures();
List<String> listOfPathsForFeatureFiles = FileUtils.getAllFilePathsFromFolder(pathFolderFrameworkFeatures);
try (Stream<Envelope> dataStream = Gherkin.fromPaths(listOfPathsForFeatureFiles, false, true, false, idGenerator)){
List<Envelope> envelopes = dataStream.collect(Collectors.toList());
for (Envelope env : envelopes) {
Messages.GherkinDocument gherkinDocument = env.getGherkinDocument();
listOfGherkinDocuments.add(gherkinDocument);
}
} catch (Exception e) {
LOG.error("Error occurred while trying to read the feature files", new Exception(e));
}
FileUtils.renameAllFeatureFiles("b");
return listOfGherkinDocuments;
}
}
Just before the return statement, you can see the function that will update the name for all feature files just to check if they are not locked.
The problem is that only the first file is always renamed and the rest of them are always locked.
If I will place the rename function at the top, then all the files are successfully renamed...
My understanding is that the try statement will automatically close the stream. Also, I tried to close it manually inside the try block but the results are the same.
What am I missing? How can I make it to release the file locks?
Update 1:
This exact line is making the files (except the first one to be locked):
List<Envelope> envelopes = dataStream.collect(Collectors.toList());
Here is the file name update function definition in case you want to test it:
public static void renameAllFeatureFiles(String fileName) {
String pathFeaturesFolder = SettingsUtils.getPathFolderFrameworkFeatures();
List<String> pathList = FileUtils.getAllFilePathsFromFolder(pathFeaturesFolder);
int counter = 0;
for (String path : pathList) {
counter ++;
File file = new File(path);
File newFile = new File(pathFeaturesFolder + "\\" + fileName +counter+".feature");
System.out.println("File: " + path + " locked: " + !file.renameTo(newFile));
}
}
And here is a sample feature file content:
Feature: Test
Scenario: test 1
Given User will do something
And User will do something
Update 2:
Tried with separate thread using javafx Task, still the same issue :(
Except for one file (this is really strange) all files are locked...
public static void runInNewThread() {
// define the execution task that will run in a new thread
Task<Void> newTask = new Task<>() {
#Override
protected Void call() {
ArrayList<Messages.GherkinDocument> listOfGherkinDocuments = GherkinUtils.getGherkinDocumentsFromFiles();
return null;
}
};
// run the task in a new thread
Thread th = new Thread(newTask);
th.setDaemon(true);
th.start();
}
For now, I have used workaround with creating copies of the specific files and using parser on the copies to prevent locking of the original versions...

Issues trying to execute Form Class in Netbeans

Good evening.
I have a problem using NetBeans v 11.1 for Windows.
I try to open two Frame Forms when the user select an item from a Combo Box.
The forms I want open have a string param in their constructors.
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
Visualizza visualizza = new Visualizza(this.auth);
Inserisci inserisci = new Inserisci(this.auth);
var getItem = jComboBox1.getSelectedItem();
switch(getItem.toString()){
case ("Visualizza Iscritti nel Sistema"):
visualizza.setVisible(true);
break;
case ("Inserisci Atleti nel Sistema"):
inserisci.setVisible(true);
break;
default:
break;
}
} catch (IOException ex) {
Logger.getLogger(Select.class.getName()).log(Level.SEVERE, null, ex);
}
}
The constructors code is:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import org.json.*;
public class Visualizza extends javax.swing.JFrame {
//Attributes declaration
...
public Visualizza(String auth) throws IOException {
initComponents();
btnRicerca.setVisible(false);
txtField.setVisible(false);
this.auth = auth;
Player[] player;
Requests r = new Requests("https://www.kingofthecage.it/API/getAllPlayers.php", auth);
r.sendGet();
if (r.res.equals("Errore") || r.res.equals("[]"))
{
List.removeAll();
List.add(errorMessage);
}
else
{
JSONArray arr = new JSONArray(r.res);
player = new Player[arr.length()];
String[] list = new String[arr.length()];
for (int i = 0; i < arr.length(); i++)
{
String id = arr.getJSONObject(i).getString("ID");
String name = arr.getJSONObject(i).getString("NOME");
String surname = arr.getJSONObject(i).getString("COGNOME");
String date = arr.getJSONObject(i).getString("DATA_NASCITA");
String birthplace = arr.getJSONObject(i).getString("LUOGO_NASCITA");
String residence = arr.getJSONObject(i).getString("RESIDENZA");
String cf = arr.getJSONObject(i).getString("CODICE_FISCALE");
String mail = arr.getJSONObject(i).getString("MAIL");
String mobile = arr.getJSONObject(i).getString("CELLULARE");
String team = arr.getJSONObject(i).getString("NOME_SQUADRA");
player[i] = new Player(id, name, surname, date, birthplace, residence, cf, mobile, mail, team);
List.add(list[i] = player[i].getPlayerString());
}
}
I import the library org.json to parse and interact with a response from a server, called with an HTTP Request and in the for cycle I returned params in a class called Player.
If I run the project from NetBeans works perfectly, the issue come when I build the project and I try to execute the .jar file saved in "./dist" folder. Substantially when I select a value from the ComboBox nothing happens but, as i've said, if I try to run the same code in NetBeans IDE it works.
I specify that I never change any setting from NetBeans IDE.
I hope you can solve my problem!

Rhino debugger - can't hit a breakpoint?

I have this legacy web application on inspection, that utilizes Rhino to script some database import tasks. Scripted task is executed properly.
However, I can't seem to hit a breakpoint in the JS script file. I'm not even sure if it's possible to debug JS script this way, so if anyone can give some more insight or advice... The core setup is as follows:
Debugger (listener):
// executes when the app is launched...
if (!ContextFactory.hasExplicitGlobal()) {
ContextFactory cxf = new ContextFactory();
ContextFactory.initGlobal(cxf);
String rhino = "transport=socket,suspend=y,address=9999";
System.out.println(">>>>>> RHINO <<<<<<");
RhinoDebugger debugger = new RhinoDebugger(rhino);
debugger.start();
cxf.addListener(debugger);
}
...
Scripting context:
#Component("importDebugMockup")
public class ImportDebugMockup extends Import {
#Autowired private SomeDAO someDAO;
#Autowired private SomeOtherDAO someOtherDAO;
...
private ContextFactory cxf;
private Document doc;
public ImportDebugMockup() {
this.cxf = ContextFactory.getGlobal();
}
...
#Transactional
public Map<String, Object> doImport(final String scriptName, final String filePath)
throws ScriptException, IOException {
final Map<String, Object> results = new HashMap<>();
final String scriptSource = this.readJSFileToString(filePath, Charset.forName("UTF-8"));
Context context = this.cxf.enterContext();
try {
Scriptable scope = new ImporterTopLevel(context);
ScriptableObject.putProperty(scope, "doc", doc);
ScriptableObject.putProperty(scope, "someDAO", this.someDAO);
ScriptableObject.putProperty(scope, "someOtherDAO", this.someOtherDAO);
...
ScriptableObject.putProperty(scope, "results", results);
Object functionArgs[] = { "null" };
String scriptExecName = "script" + scriptName + ".js";
context.evaluateString(scope, scriptSource, scriptExecName, 1, null);
Function fct = (Function) scope.get("doImport", scope); // call doImport()
Object result = fct.call(context, scope, scope, functionArgs);
} finally {
Context.exit();
}
return results;
}
}
The script:
importPackage(java.io);
importPackage(some.package);
// ...
// some utility functions here
// ...
function doImport() {
...
var i = 0; // set breakpoint here - can't hit it
someDAO.doSomething(...); // using propagated java object
...
someOtherDAO.doSomethingElse();
...
}
EDIT
Remote JS Debug configuration (with Mozilla Rhino - Attaching Connector at port 9999) is set up, like in this article, for example. Source is configured to point to directory where the JS script is located, however it does not suspend on breakpoint...

How to hook into the internal Eclipse browser?

For my eclipse plugin I want to track every URL that is opened with the internal (and if possible also external) Eclipse browser.
So far I use
org.eclipse.swt.browser.Browser;
and
addLocationListener(...)
But I would prefer that it works also for the internal Eclipse browser. How can I achieve that?
One possible solution for the Eclipse Internal Browser would be to create an eclipse plugin that registers an IStartup extension. In your earlyStartup() method you would register an IPartListener on the workbenchPage. Then when the internal browser part is created, you will receive a callback with a reference to the WebBrowserEditor (or WebBrowserView). Since there is no direct API you will have to hack a bit and use reflection to grab the internal SWT Browser instance. Once you have that, you can add your location listener.
Sometimes during early startup there is no active Workbench window yet so you have to loop through all existing workbench windows (usually just one) and each of their workbench pages to add part listeners also.
Here is the snippet of code for the earlyStartup() routine. Note that I have omitted any cleanup of listeners during dispose for windows/pages so that still needs to be done.
//Add this code to an IStartup.earlyStartup() method
final IPartListener partListener = new IPartListener() {
#Override
public void partOpened(IWorkbenchPart part) {
if (part instanceof WebBrowserEditor)
{
WebBrowserEditor editor = (WebBrowserEditor) part;
try {
Field webBrowser = editor.getClass().getDeclaredField("webBrowser");
webBrowser.setAccessible(true);
BrowserViewer viewer = (BrowserViewer)webBrowser.get(editor);
Field browser = viewer.getClass().getDeclaredField("browser");
browser.setAccessible(true);
Browser swtBrowser = (Browser) browser.get(viewer);
swtBrowser.addLocationListener(new LocationListener() {
#Override
public void changed(LocationEvent event) {
System.out.println(event.location);
}
});
} catch (Exception e) {
}
}
else if (part instanceof WebBrowserView)
{
WebBrowserView view = (WebBrowserView) part;
try {
Field webBrowser = editor.getClass().getDeclaredField("viewer");
webBrowser.setAccessible(true);
BrowserViewer viewer = (BrowserViewer)webBrowser.get(view);
Field browser = viewer.getClass().getDeclaredField("browser");
browser.setAccessible(true);
Browser swtBrowser = (Browser) browser.get(viewer);
swtBrowser.addLocationListener(new LocationListener() {
#Override
public void changed(LocationEvent event) {
System.out.println(event.location);
}
});
} catch (Exception e) {
}
}
}
...
};
final IPageListener pageListener = new IPageListener() {
#Override
public void pageOpened(IWorkbenchPage page) {
page.addPartListener(partListener);
}
...
};
final IWindowListener windowListener = new IWindowListener() {
#Override
public void windowOpened(IWorkbenchWindow window) {
window.addPageListener(pageListener);
}
...
};
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (activeWindow != null)
{
IWorkbenchPage activePage = activeWindow.getActivePage();
if (activePage != null)
{
activePage.addPartListener(partListener);
}
else
{
activeWindow.addPageListener(pageListener);
}
}
else
{
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows())
{
for (IWorkbenchPage page : window.getPages()) {
page.addPartListener(partListener);
}
window.addPageListener(pageListener);
}
PlatformUI.getWorkbench().addWindowListener(windowListener);
}
One last detail about this code snippet is that it requires a dependency on the org.eclipse.ui.browser plugin to have access to the WebBrowserEditor class.

java and air integration ,the error is "3214 NativeProcessStartupInfo.executable does not specify a valid executable file. " on Windows machine

Below is my JavaProcess.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.controls.Alert;
protected var process:NativeProcess;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();// holds all info about native process
info.executable = new File("C:/Program Files/Java/jre1.6.0_22/bin"); // unix "/usr/bin/java"
//window program files/java
info.workingDirectory= File.applicationDirectory;
var args:Vector.<String> = new Vector.<String>();
args.push("-cp","../bin","net.riaspace.Main");
info.arguments = args;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onDataOutput);
// this method will be fired when anything comes from java
process.start(info);
}
private function onDataOutput(event:ProgressEvent):void
{
var message:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
Alert.show(message);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button label="call java" click="process.standardInput.writeUTFBytes('hello\n')"/>
</s:WindowedApplication>
And My Main.Java is
package net.riaspace;
import java.util.Scanner;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext("Hello|stop"))
{
input = scanner.next();
if(input.equals("hello"))
{
System.out.println("Hello Flex");
}
else if(input.equals("stop"))
{
return;
}
}
}
}
NativeProcessStartupInfo.info The
File object that references an
executable on the host operating
system. This should be the full path
to the executable including any
extension required.
It looks like you are missing a reference to your .exe file.
I found an open source project " Flerry " to integrate Flex and Java Integration
Below is the link , it works for me.
http://www.riaspace.com/tag/java/

Categories