Author: aparfonov
Date: 2011-01-20 05:12:08 -0500 (Thu, 20 Jan 2011)
New Revision: 3820
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java
Log:
EXOJCR-1164
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java 2011-01-19
08:16:34 UTC (rev 3819)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java 2011-01-20
10:12:08 UTC (rev 3820)
@@ -23,11 +23,13 @@
import org.codehaus.groovy.control.CompilationUnit;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.ErrorCollector;
+import org.codehaus.groovy.control.Phases;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.io.ReaderSource;
import org.codehaus.groovy.control.io.URLReaderSource;
import org.exoplatform.services.rest.ext.groovy.ExtendedGroovyClassLoader;
import org.exoplatform.services.rest.ext.groovy.GroovyClassLoaderProvider;
+import org.exoplatform.services.rest.ext.groovy.SourceFile;
import org.exoplatform.services.rest.ext.groovy.SourceFolder;
import java.io.File;
@@ -36,6 +38,12 @@
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
/**
* @author <a href="mailto:andrey.parfonov@exoplatform.com">Andrey
Parfonov</a>
@@ -65,6 +73,58 @@
{
return new JcrCompilationUnit(config, cs, this);
}
+
+ @SuppressWarnings("rawtypes")
+ public Class[] parseClasses(SourceFile[] files, boolean includeLoadedClasses)
+ {
+ return doParseClasses(files, Phases.CLASS_GENERATION, null,
includeLoadedClasses);
+ }
+
+ @SuppressWarnings("rawtypes")
+ private Class[] doParseClasses(SourceFile[] sources, int phase,
CompilerConfiguration config,
+ boolean includeLoadedClasses)
+ {
+ 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();
+ String classname = clazz.getName();
+ int i = classname.lastIndexOf('.');
+ if (i != -1)
+ {
+ String pkgname = classname.substring(0, i);
+ Package pkg = getPackage(pkgname);
+ if (pkg == null)
+ definePackage(pkgname, null, null, null, null, null, null, null);
+ }
+ setClassCacheEntry(clazz);
+ }
+ List<Class> compiledClasses;
+ if (includeLoadedClasses)
+ {
+System.out.println("+++++++++++++++++++++++++ " + cunit.getClasses());
+ Collection loadedClasses = collector.getLoadedClasses();
+ compiledClasses = new ArrayList<Class>(loadedClasses.size());
+ for (Iterator iter = loadedClasses.iterator(); iter.hasNext();)
+ compiledClasses.add((Class)iter.next());
+ }
+ else
+ {
+ compiledClasses = collector.getCompiledClasses();
+ }
+ return compiledClasses.toArray(new Class[compiledClasses.size()]);
+ }
+ }
}
public static class JcrCompilationUnit extends CompilationUnit
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java 2011-01-19
08:16:34 UTC (rev 3819)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java 2011-01-20
10:12:08 UTC (rev 3820)
@@ -77,12 +77,30 @@
*/
public Class<?>[] compile(UnifiedNodeReference... sourceReferences) throws
IOException
{
- return compile(null, sourceReferences);
+ return compile(false, sourceReferences);
}
/**
* Compile Groovy source that located in <code>sourceReferences</code>.
* Compiled sources can be dependent to each other and dependent to Groovy
+ * sources that are accessible for this compiler.
+ *
+ * @param includeLoadedClasses if <code>true</code> then array of classes
+ * which method method return contains all loaded classes. If
+ * <code>false</code> then only classes created from
+ * <code>sourceReferences</code> included in result array
+ * @param sourceReferences references to Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class<?>[] compile(boolean includeLoadedClasses, UnifiedNodeReference...
sourceReferences) throws IOException
+ {
+ return compile(includeLoadedClasses, null, sourceReferences);
+ }
+
+ /**
+ * Compile Groovy source that located in <code>sourceReferences</code>.
+ * Compiled sources can be dependent to each other and dependent to Groovy
* sources that are accessible for this compiler and with additional Groovy
* sources <code>src</code>. <b>NOTE</b> To be able load
Groovy source files
* from specified folders the following rules must be observed:
@@ -94,7 +112,7 @@
* <li>Groovy source file must have extension '.groovy'</li>
* </ul>
* <br/>
- * Example: If source stream that we want validate contains the following
+ * Example: If source stream that we want compile contains the following
* code:
*
* <pre>
@@ -120,10 +138,58 @@
*/
public Class<?>[] compile(SourceFolder[] src, UnifiedNodeReference...
sourceReferences) throws IOException
{
+ return compile(false, src, sourceReferences);
+ }
+
+ /**
+ * Compile Groovy source that located in <code>sourceReferences</code>.
+ * Compiled sources can be dependent to each other and dependent to Groovy
+ * sources that are accessible for this compiler and with additional Groovy
+ * sources <code>src</code>. <b>NOTE</b> To be able load
Groovy source files
+ * from specified folders the following rules must be observed:
+ * <ul>
+ * <li>Groovy source files must be located in folder with respect to package
+ * structure</li>
+ * <li>Name of Groovy source files must be the same as name of class located
+ * in file</li>
+ * <li>Groovy source file must have extension '.groovy'</li>
+ * </ul>
+ * <br/>
+ * Example: If source stream that we want compile contains the following
+ * code:
+ *
+ * <pre>
+ * package c.b.a
+ *
+ * import a.b.c.A
+ *
+ * class B extends A {
+ * // Do something.
+ * }
+ * </pre>
+ *
+ * Assume we store dependencies in JCR then URL of folder with Groovy sources
+ * may be like this:
<code>jcr://repository/workspace#/groovy-library</code>.
+ * Then absolute path to JCR node that contains Groovy source must be as
+ * following: <code>/groovy-library/a/b/c/A.groovy</code>
+ *
+ * @param includeLoadedClasses if <code>true</code> then array of classes
+ * which method method return contains all loaded classes. If
+ * <code>false</code> then only classes created from
+ * <code>sourceReferences</code> included in result array
+ * @param src additional Groovy source location that should be added in
+ * class-path when compile <code>sourceReferences</code>
+ * @param sourceReferences references to Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class<?>[] compile(boolean includeLoadedClasses, SourceFolder[] src,
UnifiedNodeReference... sourceReferences)
+ throws IOException
+ {
SourceFile[] files = new SourceFile[sourceReferences.length];
for (int i = 0; i < sourceReferences.length; i++)
files[i] = new SourceFile(sourceReferences[i].getURL());
- return
doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(src), files);
+ return
doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(src), files,
includeLoadedClasses);
}
/**
@@ -140,7 +206,7 @@
* <li>Groovy source file must have extension '.groovy'</li>
* </ul>
*
- * @param srcadditional Groovy source location that should be added in
+ * @param src additional Groovy source location that should be added in
* class-path when compile <code>files</code>
* @param files Groovy sources to be compiled
* @return result of compilation
@@ -148,12 +214,41 @@
*/
public Class<?>[] compile(SourceFolder[] src, SourceFile[] files) throws
IOException
{
- return
doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(src), files);
+ return compile(false, src, files);
}
/**
* Compile Groovy source that located in <code>files</code>. Compiled
sources
* can be dependent to each other and dependent to Groovy sources that are
+ * accessible for this compiler and with additional Groovy sources
+ * <code>src</code>. <b>NOTE</b> To be able load Groovy source
files from
+ * specified folders the following rules must be observed:
+ * <ul>
+ * <li>Groovy source files must be located in folder with respect to package
+ * structure</li>
+ * <li>Name of Groovy source files must be the same as name of class located
+ * in file</li>
+ * <li>Groovy source file must have extension '.groovy'</li>
+ * </ul>
+ *
+ * @param includeLoadedClasses if <code>true</code> then array of classes
+ * which method method return contains all loaded classes. If
+ * <code>false</code> then only classes created from
+ * <code>sourceReferences</code> included in result array
+ * @param src additional Groovy source location that should be added in
+ * class-path when compile <code>files</code>
+ * @param files Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class<?>[] compile(boolean includeLoadedClasses, SourceFolder[] src,
SourceFile[] files) throws IOException
+ {
+ return
doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(src), files,
includeLoadedClasses);
+ }
+
+ /**
+ * Compile Groovy source that located in <code>files</code>. Compiled
sources
+ * can be dependent to each other and dependent to Groovy sources that are
* accessible for this compiler.
*
* @param files Groovy sources to be compiled
@@ -162,16 +257,35 @@
*/
public Class<?>[] compile(SourceFile[] files) throws IOException
{
- return doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(),
files);
+ return compile(false, files);
}
+ /**
+ * Compile Groovy source that located in <code>files</code>. Compiled
sources
+ * can be dependent to each other and dependent to Groovy sources that are
+ * accessible for this compiler.
+ *
+ * @param includeLoadedClasses if <code>true</code> then array of classes
+ * which method method return contains all loaded classes. If
+ * <code>false</code> then only classes created from
+ * <code>sourceReferences</code> included in result array
+ * @param files Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class<?>[] compile(boolean includeLoadedClasses, SourceFile[] files)
throws IOException
+ {
+ return doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovyClassLoader(),
files, includeLoadedClasses);
+ }
+
@SuppressWarnings("rawtypes")
- private Class<?>[] doCompile(final JcrGroovyClassLoader cl, final SourceFile[]
files) throws IOException
+ private Class<?>[] doCompile(final JcrGroovyClassLoader cl, final SourceFile[]
files,
+ final boolean includeLoadedClasses) throws IOException
{
Class[] classes = AccessController.doPrivileged(new
PrivilegedAction<Class[]>() {
public Class[] run()
{
- return cl.parseClasses(files);
+ return cl.parseClasses(files, includeLoadedClasses);
}
});
return classes;