Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [dash-dev] Groovy Monkey Plugin

I just redid the patch and its small enough to be included I think. The modified org.eclipse.eclipsemonkey plugin just references a plugin I called org.codehaus.groovy based on the groovy-1.0-jsr-05-all.jar archive.

James E. Ervin
--
A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.

-Robert A. Heinlein



### Eclipse Workspace Patch 1.0
#P org.eclipse.eclipsemonkey
Index: src/org/eclipse/eclipsemonkey/RunMonkeyScript.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.dash/org.eclipse.eclipsemonkey/src/org/eclipse/eclipsemonkey/RunMonkeyScript.java,v
retrieving revision 1.8
diff -u -r1.8 RunMonkeyScript.java
--- src/org/eclipse/eclipsemonkey/RunMonkeyScript.java	4 Apr 2006 17:05:20 -0000	1.8
+++ src/org/eclipse/eclipsemonkey/RunMonkeyScript.java	17 May 2006 02:56:54 -0000
@@ -12,9 +12,16 @@
 
 package org.eclipse.eclipsemonkey;
 
+import groovy.lang.Binding;
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyShell;
+import groovy.lang.Script;
+
 import java.io.IOException;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.codehaus.groovy.control.CompilationFailedException;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
@@ -52,7 +59,15 @@
 	}
 
 	public void run() {
-		StoredScript storedScript = null;
+	    if( Utilities.isJavaScript( file ) )
+            runJavaScript();
+        if( Utilities.isGroovy( file ) )
+            runGroovyScript();
+	}
+
+    private void runJavaScript()
+    {
+        StoredScript storedScript = null;
 		try {
 			Context cx = Context.enter();
 
@@ -74,7 +89,6 @@
 					scopeStore.put(sharedScopeName, sharedScope);
 				}
 			}
-
 			defineDynamicVariables(file);
 			defineClassLoader();
 			try {
@@ -84,7 +98,7 @@
 					if (sharedScope != null)
 						storedScript.compiledScope.setParentScope(sharedScope);
 				}
-				defineStandardGlobalVariables(storedScript.compiledScope);
+				defineStandardGlobalVariables(storedScript.compiledScope );
 				defineExtensionGlobalVariables(storedScript.compiledScope,
 						storedScript.metadata);
 
@@ -145,9 +159,82 @@
 			last_run = storedScript;
 			UpdateMonkeyActionsResourceChangeListener.createTheMonkeyMenu();
 		}
+    }
 
-	}
-
+    private void runGroovyScript()
+    {
+        StoredScript storedScript = null;
+        try
+        {
+            Context cx = Context.enter();
+
+            Scriptable sharedScope = null;
+            String fileName = this.file.getFullPath().toString();
+            Map scriptStore = EclipseMonkeyPlugin.getDefault().getScriptStore();
+            storedScript = ( StoredScript )( scriptStore.get( fileName ) );
+
+            if( !storedScript.metadata.ensure_doms_are_loaded( window ) )
+                return;
+            String sharedScopeName = storedScript.metadata.getScopeName();
+            if( sharedScopeName != null )
+            {
+                Map scopeStore = EclipseMonkeyPlugin.getDefault().getScopeStore();
+                sharedScope = ( Scriptable )scopeStore.get( sharedScopeName );
+                if( sharedScope == null )
+                {
+                    sharedScope = cx.initStandardObjects();
+                    scopeStore.put( sharedScopeName, sharedScope );
+                }
+            }
+            defineDynamicVariables( file );
+            defineClassLoader();
+            try
+            {
+                final Map< String, Object > vars = getExtensionGlobalVariables( storedScript.metadata );
+                vars.put( "window", window );
+                final GroovyShell shell = new GroovyShell( new GroovyClassLoader( classloader ), new Binding( vars ) );
+                final String className = file.getName().substring( 0, file.getName().lastIndexOf( "." ) );
+                final Script script = shell.parse( Utilities.getFileContents( file ), className );
+                script.run();
+            }
+            finally
+            {
+                undefineClassLoader();
+                undefineDynamicVariables( file );
+            }
+        }
+        catch( CompilationFailedException x )
+        {
+            error( x, x.getMessage() + ". " + x.getUnit().toString() );
+        }
+        catch( WrappedException x )
+        {
+            error( x, x.getWrappedException().toString() );
+        }
+        catch( EvaluatorException x )
+        {
+            error( x, x.lineSource() + "\n" + x.details() );
+        }
+        catch( RhinoException x )
+        {
+            error( x, x.details() );
+        }
+        catch( IOException x )
+        {
+            error( x, x.toString() );
+        }
+        catch( CoreException x )
+        {
+            error( x, x.toString() );
+        }
+        finally
+        {
+            Context.exit();
+            last_run = storedScript;
+            UpdateMonkeyActionsResourceChangeListener.createTheMonkeyMenu();
+        }
+    }
+    
 	private void defineDynamicVariables(IFile file) {
 		Utilities.state().begin(file);
 		Utilities.state().set(Utilities.SCRIPT_NAME,
@@ -161,7 +248,6 @@
 	private void defineStandardGlobalVariables(Scriptable scope) {
 		Object wrappedWindow = Context.javaToJS(window, scope);
 		ScriptableObject.putProperty(scope, "window", wrappedWindow);
-
 		// Object wrappedWorkspace = Context.javaToJS(ResourcesPlugin
 		// .getWorkspace(), scope);
 		// ScriptableObject.putProperty(scope, "workspace", wrappedWorkspace);
@@ -209,6 +295,48 @@
 		}
 	}
 
+    private Map< String, Object > getExtensionGlobalVariables( final ScriptMetadata metadata ) 
+    {
+        final IExtensionRegistry registry = Platform.getExtensionRegistry();
+        final IExtensionPoint point = registry.getExtensionPoint( "org.eclipse.eclipsemonkey.dom" );
+        final Map< String, Object > vars = new LinkedHashMap< String, Object >();
+        if( point == null )
+            return vars;
+        final IExtension[] extensions = point.getExtensions();
+        for( int i = 0; i < extensions.length; i++ )
+        {
+            final IExtension extension = extensions[ i ];
+            final IConfigurationElement[] configurations = extension.getConfigurationElements();
+            for( int j = 0; j < configurations.length; j++ )
+            {
+                final IConfigurationElement element = configurations[ j ];
+                try
+                {
+                    final IExtension declaring = element.getDeclaringExtension();
+                    final String declaring_plugin_id = declaring.getContributor().getName();
+                    if( metadata.containsDOM_by_plugin( declaring_plugin_id ) )
+                    {
+                        final String variableName = element.getAttribute( "variableName" );
+                        final IMonkeyDOMFactory factory = ( IMonkeyDOMFactory )element.createExecutableExtension( "class" );
+                        final Object rootObject = factory.getDOMroot();
+                        final ClassLoader rootLoader = rootObject.getClass().getClassLoader();
+                        classloader.add( rootLoader );
+                        vars.put( variableName, rootObject );
+                    }
+                }
+                catch( InvalidRegistryObjectException x )
+                {
+                    // ignore bad extensions
+                }
+                catch( CoreException x )
+                {
+                    // ignore bad extensions
+                }
+            }
+        }
+        return vars;
+    }
+    
 	private void error(RhinoException x, String string) {
 		MessageDialog.openError(window.getShell(), x.getClass().getName(),
 				string + "\n" + x.sourceName() + " #" + x.lineNumber());
@@ -220,7 +348,7 @@
 	}
 
 	private void defineClassLoader() {
-		classloader = new MonkeyClassLoader();
+		classloader = new MonkeyClassLoader( RunMonkeyScript.class.getClassLoader() );
 		old_classloader = Thread.currentThread().getContextClassLoader();
 		classloader.add(old_classloader);
 		Thread.currentThread().setContextClassLoader(classloader);
Index: src/org/eclipse/eclipsemonkey/UpdateMonkeyActionsResourceChangeListener.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.dash/org.eclipse.eclipsemonkey/src/org/eclipse/eclipsemonkey/UpdateMonkeyActionsResourceChangeListener.java,v
retrieving revision 1.4
diff -u -r1.4 UpdateMonkeyActionsResourceChangeListener.java
--- src/org/eclipse/eclipsemonkey/UpdateMonkeyActionsResourceChangeListener.java	29 Apr 2006 04:39:22 -0000	1.4
+++ src/org/eclipse/eclipsemonkey/UpdateMonkeyActionsResourceChangeListener.java	17 May 2006 02:56:54 -0000
@@ -27,6 +27,7 @@
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.eclipsemonkey.actions.RecreateMonkeyMenuAction;
+import org.eclipse.eclipsemonkey.dom.Utilities;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 
@@ -42,7 +43,7 @@
 
 			public boolean visit(IResourceDelta delta) {
 				String fullPath = delta.getFullPath().toString();
-				if (fullPath.endsWith(".em")) {
+				if (Utilities.isMonkeyScript( fullPath )) {
 					IFile file = (IFile) delta.getResource();
 					switch (delta.getKind()) {
 					case IResourceDelta.ADDED:
@@ -123,9 +124,8 @@
 					IResource resource = folder.members()[j];
 					if (resource instanceof IFile) {
 						IFile file = (IFile) resource;
-						if (file.getName().endsWith(".em")) {
-							processNewOrChangedScript(file.getFullPath()
-									.toString(), file);
+						if (Utilities.isMonkeyScript( file ) ) {
+							processNewOrChangedScript(file.getFullPath().toString(), file);
 						}
 					}
 				}
Index: src/org/eclipse/eclipsemonkey/MonkeyClassLoader.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.dash/org.eclipse.eclipsemonkey/src/org/eclipse/eclipsemonkey/MonkeyClassLoader.java,v
retrieving revision 1.1
diff -u -r1.1 MonkeyClassLoader.java
--- src/org/eclipse/eclipsemonkey/MonkeyClassLoader.java	4 Apr 2006 17:05:20 -0000	1.1
+++ src/org/eclipse/eclipsemonkey/MonkeyClassLoader.java	17 May 2006 02:56:53 -0000
@@ -7,7 +7,17 @@
 public class MonkeyClassLoader extends ClassLoader {
 	List loaders = new ArrayList();
 
-	public void add(ClassLoader loader) {
+	public MonkeyClassLoader()
+    {
+        super();
+    }
+
+    public MonkeyClassLoader( ClassLoader parent )
+    {
+        super( parent );
+    }
+
+    public void add(ClassLoader loader) {
 		loaders.add(loader);
 	}
 
@@ -25,6 +35,7 @@
 				// continue the loop
 			}
 		}
-		throw new ClassNotFoundException(name);
+        return super.loadClass( name );
+		//throw new ClassNotFoundException(name);
 	}
 }
Index: src/org/eclipse/eclipsemonkey/dom/Utilities.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.dash/org.eclipse.eclipsemonkey/src/org/eclipse/eclipsemonkey/dom/Utilities.java,v
retrieving revision 1.1
diff -u -r1.1 Utilities.java
--- src/org/eclipse/eclipsemonkey/dom/Utilities.java	13 Dec 2005 00:20:06 -0000	1.1
+++ src/org/eclipse/eclipsemonkey/dom/Utilities.java	17 May 2006 02:56:54 -0000
@@ -17,6 +17,7 @@
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.eclipsemonkey.internal.DynamicState;
 
 public class Utilities {
@@ -54,4 +55,45 @@
 
 
 	public static final String SCRIPT_NAME = "scriptName";
+    
+    public static boolean isMonkeyScript( final IFile file )
+    {
+        return isMonkeyScript( file.getFullPath() );
+    }
+    public static boolean isMonkeyScript( final IPath path )
+    {
+        return isMonkeyScript( path.toString() );
+    }
+    public static boolean isMonkeyScript( final String fullPath )
+    {
+        if( isJavaScript( fullPath ) )
+            return true;
+        if( fullPath.contains( "/monkey/" ) && isGroovy( fullPath ) )
+            return true;
+        return false;
+    }
+    public static boolean isJavaScript( final IFile file )
+    {
+        return isJavaScript( file.getFullPath() );
+    }
+    public static boolean isJavaScript( final IPath path )
+    {
+        return isJavaScript( path.toString() );
+    }
+    public static boolean isJavaScript( final String fullPath )
+    {
+        return fullPath.endsWith( ".em" );
+    }
+    public static boolean isGroovy( final IFile file )
+    {
+        return isGroovy( file.getFullPath() );
+    }
+    public static boolean isGroovy( final IPath path )
+    {
+        return isGroovy( path.toString() );
+    }
+    public static boolean isGroovy( final String fullPath )
+    {
+        return fullPath.endsWith( ".groovy" ) || fullPath.endsWith( ".gvy" );
+    }
 }
Index: META-INF/MANIFEST.MF
===================================================================
RCS file: /cvsroot/technology/org.eclipse.dash/org.eclipse.eclipsemonkey/META-INF/MANIFEST.MF,v
retrieving revision 1.7
diff -u -r1.7 MANIFEST.MF
--- META-INF/MANIFEST.MF	28 Mar 2006 18:17:16 -0000	1.7
+++ META-INF/MANIFEST.MF	17 May 2006 02:56:53 -0000
@@ -15,7 +15,8 @@
  org.junit,
  org.eclipse.update.core,
  org.eclipse.update.ui,
- org.mozilla.rhino
+ org.mozilla.rhino,
+ org.codehaus.groovy
 Eclipse-LazyStart: true
 Export-Package: org.eclipse.eclipsemonkey.dom
 Bundle-ClassPath: monkey.jar

Back to the top