[exo-jcr-commits] exo-jcr SVN: r3731 - in ws/trunk/exo.ws.rest.ext/src: test/java/org/exoplatform/services/rest/ext/groovy and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Dec 27 08:35:46 EST 2010


Author: aparfonov
Date: 2010-12-27 08:35:46 -0500 (Mon, 27 Dec 2010)
New Revision: 3731

Added:
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ExtendedGroovyClassLoader.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFile.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFolder.java
   ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/ExtendedClassLoaderTest.java
Removed:
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPath.java
Modified:
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java
   ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy
   ws/trunk/exo.ws.rest.ext/src/test/resources/groovy1.groovy
   ws/trunk/exo.ws.rest.ext/src/test/resources/groovy2.groovy
   ws/trunk/exo.ws.rest.ext/src/test/resources/groovy3.groovy
Log:
EXOJCR-1105

Deleted: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPath.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPath.java	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPath.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -1,46 +0,0 @@
-/*
- * Copyright (C) 2010 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.exoplatform.services.rest.ext.groovy;
-
-/**
- * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
- * @version $Id$
- */
-public class ClassPath
-{
-   private ClassPathEntry[] entries;
-
-   private String[] extensions;
-
-   public ClassPath(ClassPathEntry[] entries, String[] extensions)
-   {
-      this.entries = entries;
-      this.extensions = extensions;
-   }
-
-   public ClassPathEntry[] getEntries()
-   {
-      return entries;
-   }
-
-   public String[] getExtensions()
-   {
-      return extensions;
-   }
-}

Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -21,44 +21,22 @@
 import java.net.URL;
 
 /**
- * Item of Groovy classpath. It may describe source file of folder that contains
- * source files. If <code>ClassPathEntry</code> point to the folder then like
- * Java package structure is expected.
+ * Item of Groovy classpath.
  * 
  * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
  * @version $Id$
  */
-public class ClassPathEntry
+public abstract class ClassPathEntry
 {
-   /** Type of class-path entry. */
-   public enum EntryType {
-      SRC_DIR, FILE
-   }
+   private final URL path;
 
-   /** Type of entry. */
-   private EntryType type;
-
-   /** URL. */
-   private URL path;
-
-   public ClassPathEntry(EntryType type, URL path)
+   public ClassPathEntry(URL path)
    {
-      this.type = type;
       this.path = path;
    }
 
-   public EntryType getType()
-   {
-      return type;
-   }
-
    public URL getPath()
    {
       return path;
    }
-
-   public void setPath(URL path)
-   {
-      this.path = path;
-   }
 }

Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -39,27 +39,19 @@
  */
 public class DefaultGroovyResourceLoader implements GroovyResourceLoader
 {
-   private static final String[] DEFAULT_SCRIPT_EXTENSIONS = new String[]{".groovy"};
+   private static final String DEFAULT_SOURCE_FILE_EXTENSION = ".groovy";
 
-   protected final String[] extensions;
-
-   protected final URL[] roots;
-
-   protected final URL[] files;
-
    // TODO need configurable ?
    private int maxEntries = 512;
 
    protected final Map<String, URL> resources;
 
+   protected final URL[] roots;
+
    @SuppressWarnings("serial")
-   public DefaultGroovyResourceLoader(URL[] roots, URL[] files, String[] extensions) throws MalformedURLException
+   public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
    {
-      this.files = files;
       this.roots = new URL[roots.length];
-      if (extensions == null || extensions.length == 0)
-         extensions = DEFAULT_SCRIPT_EXTENSIONS;
-      this.extensions = extensions;
       for (int i = 0; i < roots.length; i++)
       {
          String str = roots[i].toString();
@@ -68,7 +60,8 @@
          else
             this.roots[i] = roots[i];
       }
-      resources = Collections.synchronizedMap(new LinkedHashMap<String, URL>() {
+      resources = Collections.synchronizedMap(new LinkedHashMap<String, URL>()
+      {
          protected boolean removeEldestEntry(Entry<String, URL> eldest)
          {
             return size() > maxEntries;
@@ -76,16 +69,6 @@
       });
    }
 
-   public DefaultGroovyResourceLoader(URL[] roots, URL[] files) throws MalformedURLException
-   {
-      this(roots, files, null);
-   }
-
-   public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
-   {
-      this(roots, new URL[0]);
-   }
-
    public DefaultGroovyResourceLoader(URL root) throws MalformedURLException
    {
       this(new URL[]{root});
@@ -94,30 +77,26 @@
    /**
     * {@inheritDoc}
     */
-   public final URL loadGroovySource(String classname) throws MalformedURLException
+   public final URL loadGroovySource(String filename) throws MalformedURLException
    {
       URL resource = null;
-      final String baseName = classname.replace('.', '/');
-      String[] extensions = getScriptExtensions();
-      for (int i = 0; i < extensions.length && resource == null; i++)
+      final String ffilename = filename.replace('.', '/') + getSourceFileExtension();
+      try
       {
-         final String ext = extensions[i];
-         try
+         resource = AccessController.doPrivileged(new PrivilegedExceptionAction<URL>()
          {
-            resource = AccessController.doPrivileged(new PrivilegedExceptionAction<URL>() {
-               public URL run() throws MalformedURLException
-               {
-                  return getResource(baseName + ext);
-               }
-            });
-         }
-         catch (PrivilegedActionException e)
-         {
-            Throwable cause = e.getCause();
-            // MalformedURLException
-            throw (MalformedURLException)cause;
-         }
+            public URL run() throws MalformedURLException
+            {
+               return getResource(ffilename);
+            }
+         });
       }
+      catch (PrivilegedActionException e)
+      {
+         Throwable cause = e.getCause();
+         // MalformedURLException
+         throw (MalformedURLException)cause;
+      }
       return resource;
    }
 
@@ -131,12 +110,6 @@
          boolean inCache = resource != null;
          if (inCache && !checkResource(resource))
             resource = null; // Resource in cache is unreachable.
-         for (int i = 0; i < files.length && resource == null; i++)
-         {
-            URL tmp = files[i];
-            if (tmp.toString().endsWith(filename) && checkResource(tmp))
-               resource = tmp;
-         }
          for (int i = 0; i < roots.length && resource == null; i++)
          {
             URL tmp = new URL(roots[i], filename);
@@ -148,10 +121,14 @@
          else if (inCache)
             resources.remove(filename);
       }
-
       return resource;
    }
 
+   protected String getSourceFileExtension()
+   {
+      return DEFAULT_SOURCE_FILE_EXTENSION;
+   }
+
    protected boolean checkResource(URL resource)
    {
       try
@@ -164,9 +141,4 @@
          return false;
       }
    }
-
-   protected String[] getScriptExtensions()
-   {
-      return extensions;
-   }
 }

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ExtendedGroovyClassLoader.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ExtendedGroovyClassLoader.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ExtendedGroovyClassLoader.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -0,0 +1,287 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.groovy;
+
+import groovy.lang.GroovyClassLoader;
+
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.ModuleNode;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilationUnit;
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.control.Phases;
+import org.codehaus.groovy.control.SourceUnit;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.CodeSource;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+ at SuppressWarnings({"rawtypes", "unchecked"})
+public class ExtendedGroovyClassLoader extends GroovyClassLoader
+{
+   public static final String CODE_BASE = "/groovy/script/jaxrs";
+
+   public static class SingleClassCollector extends GroovyClassLoader.ClassCollector
+   {
+      protected final CompilationUnit cunit;
+      protected final SourceUnit sunit;
+      protected Class target;
+
+      protected SingleClassCollector(ExtendedInnerLoader cl, CompilationUnit cunit, SourceUnit sunit)
+      {
+         super(cl, cunit, sunit);
+         this.cunit = cunit;
+         this.sunit = sunit;
+      }
+
+      @Override
+      protected Class createClass(byte[] code, ClassNode classNode)
+      {
+         ExtendedInnerLoader cl = (ExtendedInnerLoader)getDefiningClassLoader();
+         String classname = classNode.getName();
+         int i = classname.lastIndexOf('.');
+         if (i != -1)
+         {
+            String pkgname = classname.substring(0, i);
+            cl.definePackage(pkgname);
+         }
+         Class clazz = cl.defineClass(classNode.getName(), code, cunit.getAST().getCodeSource());
+         getLoadedClasses().add(clazz);
+         if (target == null)
+         {
+            ClassNode targetClassNode = null;
+            SourceUnit targetSunit = null;
+            ModuleNode module = classNode.getModule();
+            if (module != null)
+            {
+               targetClassNode = (ClassNode)module.getClasses().get(0);
+               targetSunit = module.getContext();
+            }
+            if (targetSunit == sunit && targetClassNode == classNode)
+               target = clazz;
+         }
+         return clazz;
+      }
+
+      public Class getTarget()
+      {
+         return target;
+      }
+   }
+
+   public static class MultipleClassCollector extends GroovyClassLoader.ClassCollector
+   {
+      protected final CompilationUnit cunit;
+      protected final Set<SourceUnit> sunitSet;
+      private final List<Class> compiledClasses;
+
+      protected MultipleClassCollector(ExtendedInnerLoader cl, CompilationUnit cunit, Set<SourceUnit> sunitSet)
+      {
+         super(cl, cunit, null);
+         this.cunit = cunit;
+         this.sunitSet = sunitSet;
+         this.compiledClasses = new ArrayList<Class>();
+      }
+
+      @Override
+      protected Class createClass(byte[] code, ClassNode classNode)
+      {
+         ExtendedInnerLoader cl = (ExtendedInnerLoader)getDefiningClassLoader();
+         String classname = classNode.getName();
+         int i = classname.lastIndexOf('.');
+         if (i != -1)
+         {
+            String pkgname = classname.substring(0, i);
+            cl.definePackage(pkgname);
+         }
+         Class clazz = cl.defineClass(classNode.getName(), code, cunit.getAST().getCodeSource());
+         getLoadedClasses().add(clazz);
+         ModuleNode module = classNode.getModule();
+         if (module != null)
+         {
+            SourceUnit currentSunit = module.getContext();
+            if (sunitSet.contains(currentSunit))
+               compiledClasses.add(clazz);
+         }
+         return clazz;
+      }
+
+      public List<Class> getCompiledClasses()
+      {
+         return compiledClasses;
+      }
+   }
+
+   public static class ExtendedInnerLoader extends GroovyClassLoader.InnerLoader
+   {
+      public ExtendedInnerLoader(ExtendedGroovyClassLoader parent)
+      {
+         super(parent);
+      }
+
+      protected Class defineClass(String name, byte[] code, CodeSource cs)
+      {
+         return super.defineClass(name, code, 0, code.length, cs);
+      }
+
+      protected void definePackage(String name) throws IllegalArgumentException
+      {
+         Package pkg = getPackage(name);
+         if (pkg == null)
+            super.definePackage(name, null, null, null, null, null, null, null);
+      }
+   }
+
+   public ExtendedGroovyClassLoader(ClassLoader classLoader)
+   {
+      super(classLoader);
+   }
+
+   public ExtendedGroovyClassLoader(GroovyClassLoader parent)
+   {
+      super(parent);
+   }
+
+   public Class parseClass(InputStream in, String fileName, SourceFile[] files) throws CompilationFailedException
+   {
+      return doParseClass(in, fileName, files, Phases.CLASS_GENERATION, null, false);
+   }
+
+   protected Class doParseClass(InputStream in, String fileName, SourceFile[] files, int phase,
+      CompilerConfiguration config, boolean shouldCacheSource) throws CompilationFailedException
+   {
+      synchronized (sourceCache)
+      {
+         Class target = (Class)sourceCache.get(fileName);
+         if (target == null)
+         {
+            CodeSource cs = new CodeSource(getCodeSource(), (java.security.cert.Certificate[])null);
+            CompilationUnit cunit = createCompilationUnit(config, cs);
+            SourceUnit targetSunit = cunit.addSource(fileName, in);
+            if (files != null)
+            {
+               for (int i = 0; i < files.length; i++)
+                  cunit.addSource(files[i].getPath());
+            }
+            SingleClassCollector collector = createSingleCollector(cunit, targetSunit);
+            cunit.setClassgenCallback(collector);
+            cunit.compile(phase);
+
+            for (Iterator iter = collector.getLoadedClasses().iterator(); iter.hasNext();)
+               setClassCacheEntry((Class)iter.next());
+
+            target = collector.getTarget();
+
+            if (shouldCacheSource)
+               sourceCache.put(fileName, target);
+         }
+
+         return target;
+      }
+   }
+
+   public Class[] parseClasses(SourceFile[] files)
+   {
+      return doParseClasses(files, Phases.CLASS_GENERATION, null);
+   }
+
+   protected Class[] doParseClasses(SourceFile[] sources, int phase, CompilerConfiguration config)
+   {
+      synchronized (classCache)
+      {
+         CodeSource cs = new CodeSource(getCodeSource(), (java.security.cert.Certificate[])null);
+         CompilationUnit cunit = createCompilationUnit(config, cs);
+         Set<SourceUnit> setSunit = new HashSet<SourceUnit>();
+         for (int i = 0; i < sources.length; i++)
+            setSunit.add(cunit.addSource(sources[i].getPath()));
+         MultipleClassCollector collector = createMultipleCollector(cunit, setSunit);
+         cunit.setClassgenCallback(collector);
+         cunit.compile(phase);
+
+         for (Iterator iter = collector.getLoadedClasses().iterator(); iter.hasNext();)
+         {
+            Class clazz = (Class)iter.next();
+            setClassCacheEntry(clazz);
+         }
+         List<Class> compiledClasses = collector.getCompiledClasses();
+         return compiledClasses.toArray(new Class[compiledClasses.size()]);
+      }
+   }
+
+   /**
+    * @see groovy.lang.GroovyClassLoader#createCompilationUnit(org.codehaus.groovy.control.CompilerConfiguration,
+    *      java.security.CodeSource)
+    */
+   @Override
+   protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource cs)
+   {
+      return new CompilationUnit(config, cs, this);
+   }
+
+   protected SingleClassCollector createSingleCollector(CompilationUnit unit, SourceUnit sunit)
+   {
+      ExtendedInnerLoader loader = AccessController.doPrivileged(new PrivilegedAction<ExtendedInnerLoader>() {
+         public ExtendedInnerLoader run()
+         {
+            return new ExtendedInnerLoader(ExtendedGroovyClassLoader.this);
+         }
+      });
+      return new SingleClassCollector(loader, unit, sunit);
+   }
+
+   protected MultipleClassCollector createMultipleCollector(CompilationUnit unit, Set<SourceUnit> setSunit)
+   {
+      ExtendedInnerLoader loader = AccessController.doPrivileged(new PrivilegedAction<ExtendedInnerLoader>() {
+         public ExtendedInnerLoader run()
+         {
+            return new ExtendedInnerLoader(ExtendedGroovyClassLoader.this);
+         }
+      });
+      return new MultipleClassCollector(loader, unit, setSunit);
+   }
+
+   protected URL getCodeSource()
+   {
+      return getCodeSource(CODE_BASE);
+   }
+
+   private URL getCodeSource(String codeBase)
+   {
+      try
+      {
+         return new URL("file", "", codeBase);
+      }
+      catch (MalformedURLException e)
+      {
+         throw new IllegalArgumentException("Unable create code source URL from: " + codeBase + ". " + e.getMessage());
+      }
+   }
+}
\ No newline at end of file


Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ExtendedGroovyClassLoader.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -20,14 +20,10 @@
 
 import groovy.lang.GroovyClassLoader;
 
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry.EntryType;
-
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
  * Factory of Groovy class loader. It can provide preset GroovyClassLoader
@@ -41,35 +37,31 @@
 public class GroovyClassLoaderProvider
 {
    /** Preset default GroovyClassLoader. */
-   private GroovyClassLoader defaultClassLoader;
+   private ExtendedGroovyClassLoader defaultClassLoader;
 
-   /**
-    * Create GroovyClassLoaderProvider that will use specified GroovyClassLoader
-    * as default.
-    * 
-    * @param defaultClassLoader GroovyClassLoader
-    */
-   public GroovyClassLoaderProvider(GroovyClassLoader defaultClassLoader)
-   {
-      this.defaultClassLoader = defaultClassLoader;
-   }
-
+   // TODO
    public GroovyClassLoaderProvider()
    {
-      defaultClassLoader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() {
-         public GroovyClassLoader run()
+      this(AccessController.doPrivileged(new PrivilegedAction<ExtendedGroovyClassLoader>()
+      {
+         public ExtendedGroovyClassLoader run()
          {
-            return new GroovyClassLoader(getClass().getClassLoader());
+            return new ExtendedGroovyClassLoader(getClass().getClassLoader());
          }
-      });
+      }));
    }
 
+   protected GroovyClassLoaderProvider(ExtendedGroovyClassLoader defaultClassLoader)
+   {
+      this.defaultClassLoader = defaultClassLoader;
+   }
+
    /**
     * Get default GroovyClassLoader.
     * 
     * @return default GroovyClassLoader
     */
-   public GroovyClassLoader getGroovyClassLoader()
+   public ExtendedGroovyClassLoader getGroovyClassLoader()
    {
       return defaultClassLoader;
    }
@@ -78,53 +70,30 @@
     * Get customized instance of GroovyClassLoader that able to resolve
     * additional Groovy source files.
     * 
-    * @param classPath additional Groovy sources
+    * @param sources additional Groovy sources
     * @return GroovyClassLoader
-    * @throws MalformedURLException if any of entries in <code>classPath</code>
+    * @throws MalformedURLException if any of entries in <code>sources</code>
     *            has invalid URL.
     */
-   public GroovyClassLoader getGroovyClassLoader(ClassPath classPath) throws MalformedURLException
+   public ExtendedGroovyClassLoader getGroovyClassLoader(SourceFolder[] sources) throws MalformedURLException
    {
-      List<URL> files = new ArrayList<URL>();
-      List<URL> roots = new ArrayList<URL>();
-      ClassPathEntry[] classPathEntries = classPath.getEntries();
-      if (classPathEntries != null && classPathEntries.length > 0)
-      {
-         for (int i = 0; i < classPathEntries.length; i++)
+      if (sources == null || sources.length == 0)
+         return getGroovyClassLoader();
+
+      URL[] roots = new URL[sources.length];
+      for (int i = 0; i < sources.length; i++)
+         roots[i] = sources[i].getPath();
+
+      final GroovyClassLoader parent = getGroovyClassLoader();
+      ExtendedGroovyClassLoader classLoader =
+         AccessController.doPrivileged(new PrivilegedAction<ExtendedGroovyClassLoader>()
          {
-            ClassPathEntry classPathEntry = classPathEntries[i];
-            if (EntryType.SRC_DIR == classPathEntry.getType())
+            public ExtendedGroovyClassLoader run()
             {
-               roots.add(classPathEntry.getPath());
+               return new ExtendedGroovyClassLoader(parent);
             }
-            else
-            {
-               files.add(classPathEntry.getPath());
-            }
-         }
-      }
-      final GroovyClassLoader parent = getGroovyClassLoader();
-      GroovyClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() {
-         public GroovyClassLoader run()
-         {
-            return new GroovyClassLoader(parent);
-         }
-      });
-      classLoader.setResourceLoader(new DefaultGroovyResourceLoader(roots.toArray(new URL[roots.size()]), files
-         .toArray(new URL[files.size()]), classPath.getExtensions()));
+         });
+      classLoader.setResourceLoader(new DefaultGroovyResourceLoader(roots));
       return classLoader;
    }
-
-   /**
-    * Set default Groovy class loader.
-    * 
-    * @param defaultClassLoader default Groovy class loader
-    * @throws NullPointerException if <code>defaultClassLoader == null</code>
-    */
-   public void setGroovyClassLoader(GroovyClassLoader defaultClassLoader)
-   {
-      if (defaultClassLoader == null)
-         throw new NullPointerException("GroovyClassLoader may not be null. ");
-      this.defaultClassLoader = defaultClassLoader;
-   }
 }

Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -23,6 +23,10 @@
 import groovy.lang.GroovyCodeSource;
 
 import org.codehaus.groovy.control.CompilationFailedException;
+import org.exoplatform.container.ExoContainer;
+import org.exoplatform.container.ExoContainerContext;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
 import org.exoplatform.services.rest.ObjectFactory;
 import org.exoplatform.services.rest.PerRequestObjectFactory;
 import org.exoplatform.services.rest.impl.ResourceBinder;
@@ -33,6 +37,8 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.net.MalformedURLException;
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
@@ -40,7 +46,9 @@
 import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -56,7 +64,23 @@
  */
 public class GroovyJaxrsPublisher
 {
+   private static final Log log = ExoLogger.getExoLogger(GroovyJaxrsPublisher.class);
 
+   @SuppressWarnings("rawtypes")
+   private static final Comparator<Constructor> constructorComparator = new Comparator<Constructor>()
+   {
+      public int compare(Constructor o1, Constructor o2)
+      {
+         int c1 = o1.getParameterTypes().length;
+         int c2 = o2.getParameterTypes().length;
+         if (c1 < c2)
+            return 1;
+         if (c1 > c2)
+            return -1;
+         return 0;
+      }
+   };
+
    /** Default character set name. */
    protected static final String DEFAULT_CHARSET_NAME = "UTF-8";
 
@@ -111,9 +135,7 @@
    @Deprecated
    public void setGroovyClassLoader(GroovyClassLoader gcl)
    {
-      if (gcl == null)
-         throw new NullPointerException("GroovyClassLoader may not be null. ");
-      classLoaderProvider.setGroovyClassLoader(gcl);
+      log.warn("Method setGroovyClassLoader is deprecated.");
    }
 
    /**
@@ -170,34 +192,36 @@
    public void publishPerRequest(final InputStream in, final ResourceId resourceId,
       MultivaluedMap<String, String> properties)
    {
-      publishPerRequest(in, resourceId, properties, null);
+      publishPerRequest(in, resourceId, properties, null, null);
    }
 
    /**
     * Parse given stream and publish result as per-request RESTful service.
     * 
-    * @param in stream which contains groovy source code of RESTful service
+    * @param in stream which contains Groovy source code of RESTful service
     * @param resourceId id to be assigned to resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
     *            {@link ResourceBinder#addResource(Class, MultivaluedMap)}
     * @throws CompilationFailedException if compilation fails from source errors
     */
    public void publishPerRequest(final InputStream in, final ResourceId resourceId,
-      final MultivaluedMap<String, String> properties, final ClassPath classPath)
+      final MultivaluedMap<String, String> properties, final SourceFolder[] src, final SourceFile[] files)
    {
-      Class<?> rc = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
+      Class<?> rc = AccessController.doPrivileged(new PrivilegedAction<Class<?>>()
+      {
          public Class<?> run()
          {
             try
             {
-               GroovyClassLoader cl =
-                  (classPath == null) ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider
-                     .getGroovyClassLoader(classPath);
-               return cl.parseClass(createCodeSource(in, resourceId.getId()));
+               ExtendedGroovyClassLoader cl =
+                  (src == null) ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider
+                     .getGroovyClassLoader(src);
+               return cl.parseClass(in, resourceId.getId(), files);
             }
             catch (MalformedURLException e)
             {
@@ -225,7 +249,7 @@
     */
    public final void publishPerRequest(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
    {
-      publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, null);
+      publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, null, null);
    }
 
    /**
@@ -236,16 +260,17 @@
     * @param resourceId id to be assigned to resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
     *            {@link ResourceBinder#addResource(Class, MultivaluedMap)}
     * @throws CompilationFailedException if compilation fails from source errors
     */
    public final void publishPerRequest(String source, ResourceId resourceId, MultivaluedMap<String, String> properties,
-      ClassPath classPath)
+      final SourceFolder[] src, final SourceFile[] files)
    {
-      publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, classPath);
+      publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, src, files);
    }
 
    /**
@@ -268,7 +293,7 @@
       MultivaluedMap<String, String> properties)
    {
       publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
-         null);
+         null, null);
    }
 
    /**
@@ -281,7 +306,8 @@
     * @param resourceId id to be assigned to resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>.
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws UnsupportedCharsetException if <code>charset</code> is unsupported
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
@@ -289,10 +315,10 @@
     * @throws CompilationFailedException if compilation fails from source errors
     */
    public final void publishPerRequest(String source, String charset, ResourceId resourceId,
-      MultivaluedMap<String, String> properties, ClassPath classPath)
+      MultivaluedMap<String, String> properties, SourceFolder[] src, SourceFile[] files)
    {
       publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
-         classPath);
+         src, files);
    }
 
    /**
@@ -309,7 +335,7 @@
     */
    public void publishSingleton(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties)
    {
-      publishSingleton(in, resourceId, properties, null);
+      publishSingleton(in, resourceId, properties, null, null);
    }
 
    /**
@@ -319,22 +345,41 @@
     * @param resourceId id to be assigned to resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
     *            {@link ResourceBinder#addResource(Object, MultivaluedMap)}
     * @throws CompilationFailedException if compilation fails from source errors
     */
-   public void publishSingleton(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties,
-      ClassPath classPath)
+   public void publishSingleton(final InputStream in, final ResourceId resourceId,
+      MultivaluedMap<String, String> properties, final SourceFolder[] src, final SourceFile[] files/*TODO*/)
    {
       Object resource;
       try
       {
-         resource =
-            instantiator.instantiateScript(createCodeSource(in, resourceId.getId()), (classPath == null)
-               ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider.getGroovyClassLoader(classPath));
+         ExtendedGroovyClassLoader cl =
+            (src == null) ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider.getGroovyClassLoader(src);
+         @SuppressWarnings("rawtypes")
+         Class clazz = cl.parseClass(in, resourceId.getId(), files);
+         resource = createInstance(clazz);
       }
+      catch (IllegalArgumentException e)
+      {
+         throw new ResourcePublicationException(e.getMessage());
+      }
+      catch (InstantiationException e)
+      {
+         throw new ResourcePublicationException(e.getMessage());
+      }
+      catch (IllegalAccessException e)
+      {
+         throw new ResourcePublicationException(e.getMessage());
+      }
+      catch (InvocationTargetException e)
+      {
+         throw new ResourcePublicationException(e.getMessage());
+      }
       catch (MalformedURLException e)
       {
          throw new IllegalArgumentException(e.getMessage());
@@ -358,7 +403,7 @@
     */
    public final void publishSingleton(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
    {
-      publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, null);
+      publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, null, null);
    }
 
    /**
@@ -369,16 +414,17 @@
     * @param resourceId name of resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>.
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
     *            {@link ResourceBinder#addResource(Object, MultivaluedMap)}
     * @throws CompilationFailedException if compilation fails from source errors
     */
    public final void publishSingleton(String source, ResourceId resourceId, MultivaluedMap<String, String> properties,
-      ClassPath classPath)
+      SourceFolder[] src, SourceFile[] files)
    {
-      publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, classPath);
+      publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, src, files);
    }
 
    /**
@@ -401,7 +447,7 @@
       MultivaluedMap<String, String> properties)
    {
       publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
-         null);
+         null, null);
    }
 
    /**
@@ -414,7 +460,8 @@
     * @param resourceId name of resource
     * @param properties optional resource properties. This parameter may be
     *           <code>null</code>.
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws UnsupportedCharsetException if <code>charset</code> is unsupported
     * @throws NullPointerException if <code>resourceId == null</code>
     * @throws ResourcePublicationException see
@@ -422,10 +469,10 @@
     * @throws CompilationFailedException if compilation fails from source errors
     */
    public final void publishSingleton(String source, String charset, ResourceId resourceId,
-      MultivaluedMap<String, String> properties, ClassPath classPath)
+      MultivaluedMap<String, String> properties, SourceFolder[] src, SourceFile[] files)
    {
       publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
-         classPath);
+         src, files);
    }
 
    /**
@@ -440,14 +487,10 @@
    {
       String path = resources.get(resourceId);
       if (path == null)
-      {
          return null;
-      }
       ObjectFactory<AbstractResourceDescriptor> resource = binder.removeResource(path);
       if (resource != null)
-      {
          resources.remove(resourceId);
-      }
       return resource;
    }
 
@@ -461,25 +504,26 @@
     *           message in compilation of Groovy fails. If this parameter is
     *           <code>null</code> then GroovyClassLoader will use automatically
     *           generated name
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws MalformedScriptException if source has errors or there is no
     *            required JAX-RS annotation
     */
-   public void validateResource(final InputStream in, final String name, final ClassPath classPath)
-      throws MalformedScriptException
+   public void validateResource(final InputStream in, final String name, final SourceFolder[] src,
+      final SourceFile[] files) throws MalformedScriptException
    {
-      Class<?> rc;
+      //Class<?> rc;
       try
       {
-         rc = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
+         //rc = 
+         AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>()
+         {
             public Class<?> run() throws MalformedURLException
             {
-               GroovyClassLoader cl =
-                  (classPath == null) ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider
-                     .getGroovyClassLoader(classPath);
-               if (name == null || name.length() == 0)
-                  return cl.parseClass(createCodeSource(in, cl.generateScriptName()));
-               return cl.parseClass(createCodeSource(in, name));
+               ExtendedGroovyClassLoader cl =
+                  (src == null) ? classLoaderProvider.getGroovyClassLoader() : classLoaderProvider
+                     .getGroovyClassLoader(src);
+               return cl.parseClass(in, (name != null && name.length() > 0) ? name : cl.generateScriptName(), files);
             }
          });
       }
@@ -521,7 +565,7 @@
     */
    public void validateResource(InputStream in, String name) throws MalformedScriptException
    {
-      validateResource(in, name, null);
+      validateResource(in, name, null, null);
    }
 
    /**
@@ -536,14 +580,15 @@
     *           message in compilation of Groovy fails. If this parameter is
     *           <code>null</code> then GroovyClassLoader will use automatically
     *           generated name
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws MalformedScriptException if source has errors or there is no
     *            required JAX-RS annotation
     */
-   public final void validateResource(String source, String charset, String name, ClassPath classPath)
+   public final void validateResource(String source, String charset, String name, SourceFolder[] src, SourceFile[] files)
       throws MalformedScriptException
    {
-      validateResource(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), name, classPath);
+      validateResource(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), name, src, files);
    }
 
    /**
@@ -556,13 +601,15 @@
     *           message in compilation of Groovy fails. If this parameter is
     *           <code>null</code> then GroovyClassLoader will use automatically
     *           generated name
-    * @param classPath additional path to Groovy sources
+    * @param src additional path to Groovy sources
+    * @param files Groovy source files to be added in build path directly
     * @throws MalformedScriptException if source has errors or there is no
     *            required JAX-RS annotation
     */
-   public final void validateResource(String source, String name, ClassPath classPath) throws MalformedScriptException
+   public final void validateResource(String source, String name, SourceFolder[] src, SourceFile[] files)
+      throws MalformedScriptException
    {
-      validateResource(source, DEFAULT_CHARSET, name, classPath);
+      validateResource(source, DEFAULT_CHARSET, name, src, files);
    }
 
    /**
@@ -580,28 +627,55 @@
     */
    public final void validateResource(String source, String name) throws MalformedScriptException
    {
-      validateResource(source, DEFAULT_CHARSET, name, null);
+      validateResource(source, DEFAULT_CHARSET, name, null, null);
    }
 
+   @SuppressWarnings("rawtypes")
+   private Object createInstance(Class clazz) throws IllegalArgumentException, InstantiationException,
+      IllegalAccessException, InvocationTargetException
+   {
+      ExoContainer container = ExoContainerContext.getCurrentContainer();
+      Constructor[] constructors = clazz.getConstructors();
+      //Sort constructors by number of parameters. With more parameters must be first.
+      Arrays.sort(constructors, constructorComparator);
+      l : for (Constructor<?> c : constructors)
+      {
+         Class<?>[] parameterTypes = c.getParameterTypes();
+         if (parameterTypes.length == 0)
+            return c.newInstance();
+         Object[] parameters = new Object[parameterTypes.length];
+         for (int i = 0; i < parameterTypes.length; i++)
+         {
+            Object param = container.getComponentInstanceOfType(parameterTypes[i]);
+            if (param == null)
+               continue l;
+            parameters[i] = param;
+         }
+         return c.newInstance(parameters);
+      }
+      throw new ResourcePublicationException("Unbale create instance of class " + clazz.getName()
+         + ". Required constructor's dependencies can't be resolved. ");
+   }
+
    private void publishPerRequest(String source, Charset charset, ResourceId resourceId,
-      MultivaluedMap<String, String> properties, ClassPath classPath)
+      MultivaluedMap<String, String> properties, SourceFolder[] src, SourceFile[] files)
    {
       byte[] bytes = source.getBytes(charset);
-      publishPerRequest(new ByteArrayInputStream(bytes), resourceId, properties, classPath);
+      publishPerRequest(new ByteArrayInputStream(bytes), resourceId, properties, src, files);
    }
 
    private void publishSingleton(String source, Charset charset, ResourceId resourceId,
-      MultivaluedMap<String, String> properties, ClassPath classPath)
+      MultivaluedMap<String, String> properties, SourceFolder[] src, SourceFile[] files)
    {
       byte[] bytes = source.getBytes(charset);
-      publishSingleton(new ByteArrayInputStream(bytes), resourceId, properties, classPath);
+      publishSingleton(new ByteArrayInputStream(bytes), resourceId, properties, src, files);
    }
 
-   private void validateResource(String source, Charset charset, String name, ClassPath classPath)
+   private void validateResource(String source, Charset charset, String name, SourceFolder[] src, SourceFile[] files)
       throws MalformedScriptException
    {
       byte[] bytes = source.getBytes(charset);
-      validateResource(new ByteArrayInputStream(bytes), name, classPath);
+      validateResource(new ByteArrayInputStream(bytes), name, src, files);
    }
 
    /**
@@ -614,13 +688,13 @@
     */
    protected GroovyCodeSource createCodeSource(final InputStream in, final String name)
    {
-      GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>() {
+      GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>()
+      {
          public GroovyCodeSource run()
          {
-            return new GroovyCodeSource(in, name, "/groovy/script/jaxrs");
+            return new GroovyCodeSource(in, name, ExtendedGroovyClassLoader.CODE_BASE);
          }
       });
-
       gcs.setCachable(false);
       return gcs;
    }

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFile.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFile.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFile.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.groovy;
+
+import java.net.URL;
+
+/**
+ * Describe location of Groovy source file.
+ * 
+ * @author <a href="andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class SourceFile extends ClassPathEntry
+{
+   public SourceFile(URL path)
+   {
+      super(path);
+   }
+}


Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFile.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFolder.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFolder.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFolder.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.groovy;
+
+import java.net.URL;
+
+/**
+ * Folder with groovy sources.
+ * 
+ * @author <a href="andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class SourceFolder extends ClassPathEntry
+{
+   public SourceFolder(URL path)
+   {
+      super(path);
+   }
+}


Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/SourceFolder.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/ExtendedClassLoaderTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/ExtendedClassLoaderTest.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/ExtendedClassLoaderTest.java	2010-12-27 13:35:46 UTC (rev 3731)
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.ext.groovy;
+
+import groovy.lang.GroovyObject;
+
+import org.exoplatform.services.rest.ext.BaseTest;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+ at SuppressWarnings("rawtypes")
+public class ExtendedClassLoaderTest extends BaseTest
+{
+   public void testParseClasses() throws Exception
+   {
+      ExtendedGroovyClassLoader loader = new GroovyClassLoaderProvider().getGroovyClassLoader();
+      SourceFile[] sources = new SourceFile[2];
+      sources[0] = new SourceFile(Thread.currentThread().getContextClassLoader().getResource("GMain1.groovy"));
+      sources[1] =
+         new SourceFile(Thread.currentThread().getContextClassLoader().getResource("repo/dependencies/Dep1.groovy"));
+      Class[] classes = loader.parseClasses(sources);
+      assertEquals(2, classes.length);
+      List<String> names = new ArrayList<String>(2);
+      for (Class c : classes)
+         names.add(c.getName());
+      assertTrue(names.contains("GMain1"));
+      assertTrue(names.contains("dependencies.Dep1"));
+   }
+
+   public void testParseClasses2() throws Exception
+   {
+      ExtendedGroovyClassLoader loader =
+         new GroovyClassLoaderProvider().getGroovyClassLoader(new SourceFolder[]{new SourceFolder(Thread
+            .currentThread().getContextClassLoader().getResource("repo"))});
+      SourceFile[] sources = new SourceFile[1];
+      sources[0] = new SourceFile(Thread.currentThread().getContextClassLoader().getResource("GMain1.groovy"));
+      Class[] classes = loader.parseClasses(sources);
+      assertEquals(1, classes.length);
+      List<String> names = new ArrayList<String>(1);
+      for (Class c : classes)
+         names.add(c.getName());
+      assertTrue(names.contains("GMain1"));
+   }
+
+   public void testParseClassWithDependency() throws Exception
+   {
+      ExtendedGroovyClassLoader loader = new GroovyClassLoaderProvider().getGroovyClassLoader();
+      SourceFile[] sources =
+         new SourceFile[]{new SourceFile(Thread.currentThread().getContextClassLoader()
+            .getResource("repo/dependencies/Dep1.groovy"))};
+      Class clazz =
+         loader.parseClass(Thread.currentThread().getContextClassLoader().getResourceAsStream("GMain1.groovy"),
+            "GMain1", sources);
+      assertEquals("GMain1", clazz.getName());
+      assertEquals("dependencies.Dep1", ((GroovyObject)clazz.newInstance()).invokeMethod("m0", new Object[0]));
+   }
+}


Property changes on: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/ExtendedClassLoaderTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy	2010-12-27 13:35:46 UTC (rev 3731)
@@ -11,4 +11,4 @@
       return new Dep1().getName()
    }
    
-}
\ No newline at end of file
+}

Modified: ws/trunk/exo.ws.rest.ext/src/test/resources/groovy1.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/groovy1.groovy	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/groovy1.groovy	2010-12-27 13:35:46 UTC (rev 3731)
@@ -4,10 +4,10 @@
 import javax.ws.rs.core.Context;
 
 @Path("a")
-class CroovyResource1
+class GroovyResource1
 {
    
-   CroovyResource1(@Context HttpServletRequest req1)
+   GroovyResource1(@Context HttpServletRequest req1)
    {
       this.req1 = req1;
    }
@@ -24,4 +24,4 @@
       return req.getMethod() + "\n" +req.getRequestURI().toString() 
    }
    
-}
\ No newline at end of file
+}

Modified: ws/trunk/exo.ws.rest.ext/src/test/resources/groovy2.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/groovy2.groovy	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/groovy2.groovy	2010-12-27 13:35:46 UTC (rev 3731)
@@ -4,11 +4,11 @@
 import javax.ws.rs.Path;
 
 @Path("a")
-class CroovyResource2
+class GroovyResource2
 {
    private Component1 component
    
-   CroovyResource2(Component1 component)
+   GroovyResource2(Component1 component)
    {
       this.component = component
    }

Modified: ws/trunk/exo.ws.rest.ext/src/test/resources/groovy3.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/groovy3.groovy	2010-12-27 13:22:15 UTC (rev 3730)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/groovy3.groovy	2010-12-27 13:35:46 UTC (rev 3731)
@@ -2,10 +2,10 @@
 import javax.ws.rs.Path;
 
 @Path("a")
-class CroovyResource3
+class GroovyResource3
 {
    
-   CroovyResource3()
+   GroovyResource3()
    {
    }
    



More information about the exo-jcr-commits mailing list