null
then GroovyClassLoader will use automati=
cally
* generated name
* @param script Groovy source stream
- * @param sources locations (URL) of source folders that should be add =
in
- * class path when compile Groovy script
- * @param files locations (URL) of source files that should be add in c=
lass
- * path when compile Groovy script
- * @param extensions extensions of files from source folders that shoul=
d be
- * added in class path. By default only files with .groovy
- * extension are processed. Extensions must be set in form
- * '.{extensions}'
+ * @param sources locations (string representation of URL) of source fo=
lders
+ * that should be add in class path when compile Groovy scrip=
t.
+ * NOTE To be able load Groovy source files from speci=
fied
+ * folders the following rules must be observed:
+ * + * package c.b.a + * = + * import a.b.c.A + * = + * class B extends A { + * // Do something. + * } + *+ * = + * Assume we store dependencies in JCR then URL of folder with + * Groovy sources may be like this: + *
jcr://repository/workspace#/groovy-library
. T=
hen
+ * absolute path to JCR node that contains Groovy source must=
be as
+ * following: /groovy-library/a/b/c/A.groovy
+ * @param files locations (string representation of URL) of source file=
s that
+ * should be add in class path when compile Groovy script. Ea=
ch
+ * location must point directly to file that contains Groovy
+ * source. Source file can have any name and extension
* @return Response with corresponded status. 200 if source code is val=
id
*/
@POST
@Consumes({"script/groovy"})
@Path("validate{name:.*}")
public Response validateScript(@PathParam("name") String name, final In=
putStream script,
- @QueryParam("sources") Listnull
then GroovyClassLoader will use automati=
cally
* generated name
* @param script Groovy source stream
- * @param classPath class path
+ * @param src set of folders that contains Groovy source files that sho=
uld be
+ * add in class-path when validate script
, see
+ * {@link SourceFolder#getPath()}. NOTE To be able load
+ * Groovy source files from specified folders the following r=
ules
+ * must be observed:
+ * script
. Each item must point di=
rectly
+ * to file that contains Groovy source, see
+ * {@link SourceFile#getPath()} . Source file can have any na=
me and
+ * extension
* @throws MalformedScriptException if script
contains not=
valid
* source code
*/
- public void validateScript(String name, InputStream script, ClassPath c=
lassPath) throws MalformedScriptException
+ public void validateScript(String name, InputStream script, SourceFolde=
r[] src, SourceFile[] files)
+ throws MalformedScriptException
{
if (name !=3D null && name.length() > 0 && name.startsWith("/"))
name =3D name.substring(1);
- groovyPublisher.validateResource(script, name, classPath);
+ groovyPublisher.validateResource(script, name, src, files);
}
=
/**
@@ -931,14 +981,40 @@
* @param state true
if resource should be loaded and
* false
otherwise. If this attribute is not pre=
sent
* in HTTP request then it will be considered as true=
code>
- * @param sources locations (URL) of source folders that should be add =
in
- * class path when compile Groovy script
- * @param files locations (URL) of source files that should be add in c=
lass
- * path when compile Groovy script
- * @param extensions extensions of files from source folders that shoul=
d be
- * added in class path. By default only files with .groovy
- * extension are processed. Extensions must be set in form
- * '.{extensions}'
+ * @param sources locations (string representation of URL) of source fo=
lders
+ * that should be add in class path when compile Groovy scrip=
t.
+ * NOTE To be able load Groovy source files from speci=
fied
+ * folders the following rules must be observed:
+ *
+ * - Groovy source files must be located in folder with res=
pect
+ * to package structure
+ * - Name of Groovy source files must be the same as name of
+ * class located in file
+ * - Groovy source file must have extension '.groovy'
+ *
+ *
+ * Example: If source stream that we want validate contains t=
he
+ * following code:
+ * =
+ *
+ * package c.b.a
+ * =
+ * import a.b.c.A
+ * =
+ * class B extends A {
+ * // Do something.
+ * }
+ *
+ * =
+ * Assume we store dependencies in JCR then URL of folder with
+ * Groovy sources may be like this:
+ * jcr://repository/workspace#/groovy-library
. T=
hen
+ * absolute path to JCR node that contains Groovy source must=
be as
+ * following: /groovy-library/a/b/c/A.groovy
+ * @param files locations (string representation of URL) of source file=
s that
+ * should be add in class path when compile Groovy script. Ea=
ch
+ * location must point directly to file that contains Groovy
+ * source. Source file can have any name and extension
* @param properties optional properties to be applied to loaded resour=
ce.
* Ignored if state
parameter is false
*/
@@ -948,11 +1024,12 @@
public Response load(@PathParam("repository") String repository, @PathP=
aram("workspace") String workspace,
@PathParam("path") String path, @DefaultValue("true") @QueryParam("s=
tate") boolean state,
@QueryParam("sources") List sources, @QueryParam("file") Lis=
t files,
- @QueryParam("extension") List extensions, MultivaluedMap properties)
+ MultivaluedMap properties)
{
try
{
- return load(repository, workspace, path, state, properties, creat=
eClassPath(sources, files, extensions));
+ return load(repository, workspace, path, state, properties, creat=
eSourceFolders(sources),
+ createSourceFiles(files));
}
catch (MalformedURLException e)
{
@@ -976,10 +1053,25 @@
* in HTTP request then it will be considered as true=
code>
* @param properties optional properties to be applied to loaded resour=
ce.
* Ignored if state
parameter is false
- * @param classPath class path
+ * @param src set of folders that contains Groovy source files that sho=
uld be
+ * add in class-path when compile file located at path<=
/code>
+ * . NOTE To be able load Groovy source files from spe=
cified
+ * folders the following rules must be observed:
+ *
+ * - Groovy source files must be located in folder with res=
pect
+ * to package structure
+ * - Name of Groovy source files must be the same as name of
+ * class located in file
+ * - Groovy source file must have extension '.groovy'
+ *
+ * @param files set of groovy source files that should be add in class-=
path
+ * when compile file located at path
. Each item =
must
+ * point directly to file that contains Groovy source, see
+ * {@link SourceFile#getPath()} . Source file can have any na=
me and
+ * extension
*/
public Response load(String repository, String workspace, String path, =
boolean state,
- MultivaluedMap properties, ClassPath classPath)
+ MultivaluedMap properties, SourceFolder[] src, Sourc=
eFile[] files)
{
Session ses =3D null;
try
@@ -992,7 +1084,7 @@
if (state)
{
groovyPublisher.unpublishResource(key);
- groovyPublisher.publishPerRequest(script.getProperty("jcr:data=
").getStream(), key, properties, classPath);
+ groovyPublisher.publishPerRequest(script.getProperty("jcr:data=
").getStream(), key, properties, src, files);
}
else
{
@@ -1314,46 +1406,44 @@
}
}
=
- ///////////////////////////////////////////////////////////////////////=
////////////////
-
- private ClassPath createClassPath(List sources, List fi=
les, List extensions)
- throws MalformedURLException
+ private SourceFolder[] createSourceFolders(List sources) throws=
MalformedURLException
{
- if ((sources =3D=3D null || sources.size() =3D=3D 0) && (files =3D=
=3D null || files.size() =3D=3D 0)
- && (extensions =3D=3D null || extensions.size() =3D=3D 0))
- return null;
-
- List classPathEntries =3D new ArrayList();
-
+ SourceFolder[] src =3D null;
if (sources !=3D null && sources.size() > 0)
{
- for (String src : sources)
+ src =3D new SourceFolder[sources.size()];
+ for (int i =3D 0; i < sources.size(); i++)
{
- if (src.startsWith("jcr://"))
- classPathEntries.add(new JcrClassPathEntry(EntryType.SRC_DI=
R, new URL(null, src, UnifiedNodeReference
- .getURLStreamHandler())));
+ String str =3D sources.get(i);
+ URL url =3D null;
+ if (str.startsWith("jcr://"))
+ url =3D new URL(null, str, UnifiedNodeReference.getURLStrea=
mHandler());
else
- classPathEntries.add(new JcrClassPathEntry(EntryType.SRC_DI=
R, new URL(src)));
+ url =3D new URL(str);
+ src[i] =3D new SourceFolder(url);
}
}
+ return src;
+ }
=
+ private SourceFile[] createSourceFiles(List files) throws Malfo=
rmedURLException
+ {
+ SourceFile[] srcFiles =3D null;
if (files !=3D null && files.size() > 0)
{
- for (String file : files)
+ srcFiles =3D new SourceFile[files.size()];
+ for (int i =3D 0; i < files.size(); i++)
{
- if (file.startsWith("jcr://"))
- classPathEntries.add(new JcrClassPathEntry(EntryType.FILE, =
new URL(null, file, UnifiedNodeReference
- .getURLStreamHandler())));
+ String str =3D files.get(i);
+ URL url =3D null;
+ if (str.startsWith("jcr://"))
+ url =3D new URL(null, str, UnifiedNodeReference.getURLStrea=
mHandler());
else
- classPathEntries.add(new JcrClassPathEntry(EntryType.FILE, =
new URL(file)));
+ url =3D new URL(str);
+ srcFiles[i] =3D new SourceFile(url);
}
}
-
- ClassPath classPath =3D
- new ClassPath(classPathEntries.toArray(new ClassPathEntry[classPa=
thEntries.size()]),
- (extensions !=3D null && extensions.size() > 0) ? extensions.t=
oArray(new String[extensions.size()]) : null);
-
- return classPath;
+ return srcFiles;
}
=
/**
Modified: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/GroovyScriptAddRepoPlugin.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyScriptAddRepoPlugin.java 2010-12-27 13:35:46 UT=
C (rev 3731)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyScriptAddRepoPlugin.java 2010-12-27 13:37:04 UT=
C (rev 3732)
@@ -19,7 +19,6 @@
=
package org.exoplatform.services.jcr.ext.script.groovy;
=
-import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.component.BaseComponentPlugin;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PropertiesParam;
@@ -29,6 +28,8 @@
=
import java.net.MalformedURLException;
import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.Collections;
@@ -69,18 +70,19 @@
final String path =3D p.getProperty("path");
try
{
- SecurityHelper.doPrivilegedMalformedURLExceptionAction(new Pri=
vilegedExceptionAction()
+ AccessController.doPrivileged(new PrivilegedExceptionAction()
{
- public Void run() throws Exception
+ public Void run() throws MalformedURLException
{
repos.add(new UnifiedNodeReference(repository, workspace=
, path).getURL());
return null;
}
});
}
- catch (MalformedURLException e)
+ catch (PrivilegedActionException e)
{
- LOG.error("Failed add groovy script repository. " + e.getMessa=
ge());
+ // MalformedURLException
+ LOG.error("Failed add groovy script repository. " + e.getCause=
().getMessage());
}
}
return repos;
Deleted: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/serv=
ices/jcr/ext/script/groovy/JcrClassPathEntry.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrClassPathEntry.java 2010-12-27 13:35:46 UTC (rev 3=
731)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrClassPathEntry.java 2010-12-27 13:37:04 UTC (rev 3=
732)
@@ -1,42 +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.jcr.ext.script.groovy;
-
-import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-
-/**
- * @author Andrey Par=
fonov
- * @version $Id$
- */
-public class JcrClassPathEntry extends ClassPathEntry
-{
- public JcrClassPathEntry(EntryType type, URL path)
- {
- super(type, path);
- }
-
- public JcrClassPathEntry(EntryType type, UnifiedNodeReference path) thr=
ows MalformedURLException
- {
- this(type, path.getURL());
- }
-}
Modified: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java 2010-12-27 13:35:46=
UTC (rev 3731)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyClassLoaderProvider.java 2010-12-27 13:37:04=
UTC (rev 3732)
@@ -20,17 +20,22 @@
=
import groovy.lang.GroovyClassLoader;
=
-import org.exoplatform.services.rest.ext.groovy.ClassPath;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry.EntryType;
+import org.codehaus.groovy.control.CompilationUnit;
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.control.ErrorCollector;
+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.SourceFolder;
=
+import java.io.File;
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.List;
=
/**
* @author Andrey Par=
fonov
@@ -39,39 +44,128 @@
*/
public class JcrGroovyClassLoaderProvider extends GroovyClassLoaderProvider
{
+
+ public static class JcrGroovyClassLoader extends ExtendedGroovyClassLoa=
der
+ {
+ public JcrGroovyClassLoader(ClassLoader classLoader)
+ {
+ super(classLoader);
+ }
+
+ public JcrGroovyClassLoader(GroovyClassLoader parent)
+ {
+ super(parent);
+ }
+
+ /**
+ * @see org.exoplatform.services.rest.ext.groovy.ExtendedGroovyClass=
Loader#createCompilationUnit(org.codehaus.groovy.control.CompilerConfigurat=
ion,
+ * java.security.CodeSource)
+ */
+ protected CompilationUnit createCompilationUnit(CompilerConfiguratio=
n config, CodeSource cs)
+ {
+ return new JcrCompilationUnit(config, cs, this);
+ }
+ }
+
+ public static class JcrCompilationUnit extends CompilationUnit
+ {
+
+ public JcrCompilationUnit()
+ {
+ super();
+ }
+
+ public JcrCompilationUnit(CompilerConfiguration configuration, CodeS=
ource security, GroovyClassLoader loader,
+ GroovyClassLoader transformLoader)
+ {
+ super(configuration, security, loader, transformLoader);
+ }
+
+ public JcrCompilationUnit(CompilerConfiguration configuration, CodeS=
ource security, GroovyClassLoader loader)
+ {
+ super(configuration, security, loader);
+ }
+
+ public JcrCompilationUnit(CompilerConfiguration configuration)
+ {
+ super(configuration);
+ }
+
+ public JcrCompilationUnit(GroovyClassLoader loader)
+ {
+ super(loader);
+ }
+
+ /**
+ * @see org.codehaus.groovy.control.CompilationUnit#addSource(java.n=
et.URL)
+ */
+ @Override
+ public SourceUnit addSource(URL url)
+ {
+ return addSource(new JcrSourceUnit(url, configuration, classLoade=
r, getErrorCollector()));
+ }
+ }
+
+ /** Adapter for JCR like URLs. */
+ public static class JcrSourceUnit extends SourceUnit
+ {
+ public JcrSourceUnit(File source, CompilerConfiguration configuratio=
n, GroovyClassLoader loader, ErrorCollector er)
+ {
+ super(source, configuration, loader, er);
+ }
+
+ public JcrSourceUnit(String name, ReaderSource source, CompilerConfi=
guration flags, GroovyClassLoader loader,
+ ErrorCollector er)
+ {
+ super(name, source, flags, loader, er);
+ }
+
+ public JcrSourceUnit(String name, String source, CompilerConfigurati=
on configuration, GroovyClassLoader loader,
+ ErrorCollector er)
+ {
+ super(name, source, configuration, loader, er);
+ }
+
+ public JcrSourceUnit(URL source, CompilerConfiguration configuration=
, GroovyClassLoader loader, ErrorCollector er)
+ {
+ /* jCR path is in fragment of URL:
+ * jcr://repository/workspace#/path */
+ super("jcr".equals(source.getProtocol()) ? source.getRef() : sour=
ce.getPath(), new URLReaderSource(source,
+ configuration), configuration, loader, er);
+ }
+ }
+
+ public JcrGroovyClassLoaderProvider()
+ {
+ super(AccessController.doPrivileged(new PrivilegedAction() {
+ public JcrGroovyClassLoader run()
+ {
+ return new JcrGroovyClassLoader(getClass().getClassLoader());
+ }
+ }));
+ }
+
/**
- * @see org.exoplatform.services.rest.ext.groovy.GroovyClassLoaderProvi=
der#getGroovyClassLoader(org.exoplatform.services.rest.ext.groovy.ClassPath)
+ * @see org.exoplatform.services.rest.ext.groovy.GroovyClassLoaderProvi=
der#getGroovyClassLoader(org.exoplatform.services.rest.ext.groovy.SourceFol=
der[])
*/
@Override
- public GroovyClassLoader getGroovyClassLoader(ClassPath classPath) thro=
ws MalformedURLException
+ public ExtendedGroovyClassLoader getGroovyClassLoader(SourceFolder[] so=
urces) throws MalformedURLException
{
- List files =3D new ArrayList();
- List roots =3D new ArrayList();
- ClassPathEntry[] classPathEntries =3D classPath.getEntries();
- if (classPathEntries !=3D null && classPathEntries.length > 0)
- {
- for (int i =3D 0; i < classPathEntries.length; i++)
- {
- ClassPathEntry classPathEntry =3D classPathEntries[i];
- if (EntryType.SRC_DIR =3D=3D classPathEntry.getType())
- {
- roots.add(classPathEntry.getPath());
- }
- else
- {
- files.add(classPathEntry.getPath());
- }
- }
- }
+ if (sources =3D=3D null || sources.length =3D=3D 0)
+ return getGroovyClassLoader();
+
+ URL[] roots =3D new URL[sources.length];
+ for (int i =3D 0; i < sources.length; i++)
+ roots[i] =3D sources[i].getPath();
+
final GroovyClassLoader parent =3D getGroovyClassLoader();
- GroovyClassLoader classLoader =3D AccessController.doPrivileged(new =
PrivilegedAction() {
- public GroovyClassLoader run()
+ JcrGroovyClassLoader classLoader =3D AccessController.doPrivileged(n=
ew PrivilegedAction() {
+ public JcrGroovyClassLoader run()
{
- return new GroovyClassLoader(parent);
+ return new JcrGroovyClassLoader(parent);
}
});
- classLoader.setResourceLoader(new JcrGroovyResourceLoader(roots.toAr=
ray(new URL[roots.size()]), files
- .toArray(new URL[files.size()]), classPath.getExtensions()));
+ classLoader.setResourceLoader(new JcrGroovyResourceLoader(roots));
return classLoader;
}
}
\ No newline at end of file
Modified: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/JcrGroovyCompiler.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyCompiler.java 2010-12-27 13:35:46 UTC (rev 3=
731)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyCompiler.java 2010-12-27 13:37:04 UTC (rev 3=
732)
@@ -22,15 +22,12 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
=
-import org.codehaus.groovy.control.CompilationFailedException;
-import org.exoplatform.services.jcr.ext.resource.JcrURLConnection;
import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
+import org.exoplatform.services.jcr.ext.script.groovy.JcrGroovyClassLoader=
Provider.JcrGroovyClassLoader;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
-import org.exoplatform.services.rest.ext.groovy.ClassPath;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry.EntryType;
-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 org.picocontainer.Startable;
=
import java.io.IOException;
@@ -39,8 +36,6 @@
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -57,88 +52,128 @@
/** Logger. */
private static final Log LOG =3D ExoLogger.getLogger(JcrGroovyCompiler.=
class);
=
- protected final GroovyClassLoaderProvider classLoaderProvider;
+ protected final JcrGroovyClassLoaderProvider classLoaderProvider;
=
protected List addRepoPlugins;
=
- protected JcrGroovyCompiler(GroovyClassLoaderProvider classLoaderProvid=
er)
+ protected JcrGroovyCompiler(JcrGroovyClassLoaderProvider classLoaderPro=
vider)
{
this.classLoaderProvider =3D classLoaderProvider;
}
=
public JcrGroovyCompiler()
{
- classLoaderProvider =3D new JcrGroovyClassLoaderProvider();
+ this(new JcrGroovyClassLoaderProvider());
}
=
+ /**
+ * Compile Groovy source that located in sourceReferences
.
+ * Compiled sources can be dependent to each other and dependent to Gro=
ovy
+ * sources that are accessible for this compiler.
+ * =
+ * @param sourceReferences references to Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
public Class>[] compile(UnifiedNodeReference... sourceReferences) thr=
ows IOException
{
- // Add all compiled entries in class-path. Need to do this to resolv=
e dependencies between compiled files.
- ClassPathEntry[] classPath =3D new ClassPathEntry[sourceReferences.l=
ength];
- for (int i =3D 0; i < classPath.length; i++)
- classPath[i] =3D new JcrClassPathEntry(EntryType.FILE, sourceRefe=
rences[i]);
- return doCompile(classLoaderProvider.getGroovyClassLoader(new ClassP=
ath(classPath, null)), sourceReferences);
+ return compile(null, sourceReferences);
}
=
- public Class>[] compile(ClassPath classPath, UnifiedNodeReference... =
sourceReferences) throws IOException
+ /**
+ * Compile Groovy source that located in sourceReferences
.
+ * Compiled sources can be dependent to each other and dependent to Gro=
ovy
+ * sources that are accessible for this compiler and with additional Gr=
oovy
+ * sources src
. NOTE To be able load Groovy source =
files
+ * from specified folders the following rules must be observed:
+ *
+ * - Groovy source files must be located in folder with respect to pa=
ckage
+ * structure
+ * - Name of Groovy source files must be the same as name of class lo=
cated
+ * in file
+ * - Groovy source file must have extension '.groovy'
+ *
+ *
+ * Example: If source stream that we want validate contains the followi=
ng
+ * code:
+ * =
+ *
+ * package c.b.a
+ * =
+ * import a.b.c.A
+ * =
+ * class B extends A {
+ * // Do something.
+ * }
+ *
+ * =
+ * Assume we store dependencies in JCR then URL of folder with Groovy s=
ources
+ * may be like this: jcr://repository/workspace#/groovy-library=
code>.
+ * Then absolute path to JCR node that contains Groovy source must be as
+ * following: /groovy-library/a/b/c/A.groovy
+ * =
+ * @param src additional Groovy source location that should be added in
+ * class-path when compile sourceReferences
+ * @param sourceReferences references to Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class>[] compile(SourceFolder[] src, UnifiedNodeReference... s=
ourceReferences) throws IOException
{
- ClassPathEntry[] compiled =3D new ClassPathEntry[sourceReferences.le=
ngth];
- for (int i =3D 0; i < compiled.length; i++)
- compiled[i] =3D new JcrClassPathEntry(EntryType.FILE, sourceRefer=
ences[i]);
- ClassPathEntry[] classPathEntries =3D classPath.getEntries();
- if (classPathEntries =3D=3D null)
- classPathEntries =3D new ClassPathEntry[0];
- ClassPathEntry[] fullClassPath =3D new ClassPathEntry[compiled.lengt=
h + classPathEntries.length];
- System.arraycopy(compiled, 0, fullClassPath, 0, compiled.length);
- System.arraycopy(classPathEntries, 0, fullClassPath, compiled.length=
, classPathEntries.length);
- return doCompile(
- classLoaderProvider.getGroovyClassLoader(new ClassPath(fullClassP=
ath, classPath.getExtensions())),
- sourceReferences);
+ SourceFile[] files =3D new SourceFile[sourceReferences.length];
+ for (int i =3D 0; i < sourceReferences.length; i++)
+ files[i] =3D new SourceFile(sourceReferences[i].getURL());
+ return doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovy=
ClassLoader(src), files);
}
=
- private Class>[] doCompile(final GroovyClassLoader cl, final UnifiedN=
odeReference... sourceReferences)
- throws IOException
+ /**
+ * Compile Groovy source that located in files
. Compiled s=
ources
+ * can be dependent to each other and dependent to Groovy sources that =
are
+ * accessible for this compiler and with additional Groovy sources
+ * src
. NOTE To be able load Groovy source files fr=
om
+ * specified folders the following rules must be observed:
+ *
+ * - Groovy source files must be located in folder with respect to pa=
ckage
+ * structure
+ * - Name of Groovy source files must be the same as name of class lo=
cated
+ * in file
+ * - Groovy source file must have extension '.groovy'
+ *
+ * =
+ * @param srcadditional Groovy source location that should be added in
+ * class-path when compile files
+ * @param files Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class>[] compile(SourceFolder[] src, SourceFile[] files) throw=
s IOException
{
- Class>[] classes =3D new Class>[sourceReferences.length];
- for (int i =3D 0; i < sourceReferences.length; i++)
- {
- JcrURLConnection conn =3D null;
- try
- {
- final URL url =3D sourceReferences[i].getURL();
- conn =3D (JcrURLConnection)url.openConnection();
+ return doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovy=
ClassLoader(src), files);
+ }
=
- final JcrURLConnection fConn =3D conn;
- Class> clazz;
- try
- {
- clazz =3D AccessController.doPrivileged(new PrivilegedExcep=
tionAction>() {
- public Class> run() throws Exception
- {
- return cl.parseClass(createCodeSource(fConn.getInputS=
tream(), url.toString()));
- }
- });
- }
- catch (PrivilegedActionException pae)
- {
- Throwable cause =3D pae.getCause();
- if (cause instanceof CompilationFailedException)
- throw (CompilationFailedException)cause;
- else if (cause instanceof IOException)
- throw (IOException)cause;
- else if (cause instanceof RuntimeException)
- throw (RuntimeException)cause;
- else
- throw new RuntimeException(cause);
- }
- classes[i] =3D clazz;
- }
- finally
+ /**
+ * Compile Groovy source that located in files
. Compiled s=
ources
+ * can be dependent to each other and dependent to Groovy sources that =
are
+ * accessible for this compiler.
+ * =
+ * @param files Groovy sources to be compiled
+ * @return result of compilation
+ * @throws IOException if any i/o errors occurs
+ */
+ public Class>[] compile(SourceFile[] files) throws IOException
+ {
+ return doCompile((JcrGroovyClassLoader)classLoaderProvider.getGroovy=
ClassLoader(), files);
+ }
+
+ @SuppressWarnings("rawtypes")
+ private Class>[] doCompile(final JcrGroovyClassLoader cl, final Sourc=
eFile[] files) throws IOException
+ {
+ Class[] classes =3D AccessController.doPrivileged(new PrivilegedActi=
on() {
+ public Class[] run()
{
- if (conn !=3D null)
- conn.disconnect();
+ return cl.parseClasses(files);
}
- }
+ });
return classes;
}
=
@@ -160,7 +195,7 @@
@Deprecated
public void setGroovyClassLoader(GroovyClassLoader gcl)
{
- classLoaderProvider.setGroovyClassLoader(gcl);
+ LOG.warn("Method setGroovyClassLoader is deprecated.");
}
=
/**
@@ -173,6 +208,7 @@
* @return GroovyCodeSource
*/
// Override this method if need other behavior.
+ @Deprecated
protected GroovyCodeSource createCodeSource(final InputStream in, final=
String name)
{
GroovyCodeSource gcs =3D AccessController.doPrivileged(new Privilege=
dAction() {
Modified: jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/JcrGroovyResourceLoader.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyResourceLoader.java 2010-12-27 13:35:46 UTC =
(rev 3731)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/=
jcr/ext/script/groovy/JcrGroovyResourceLoader.java 2010-12-27 13:37:04 UTC =
(rev 3732)
@@ -61,24 +61,14 @@
return res;
}
=
- public JcrGroovyResourceLoader(URL[] roots, URL[] files, String[] exten=
sions) throws MalformedURLException
- {
- super(normalizeJcrURL(roots), files, extensions);
- }
-
- public JcrGroovyResourceLoader(URL[] roots, URL[] files) throws Malform=
edURLException
- {
- this(roots, files, null);
- }
-
public JcrGroovyResourceLoader(URL[] roots) throws MalformedURLException
{
- this(roots, new URL[0]);
+ super(normalizeJcrURL(roots));
}
=
public JcrGroovyResourceLoader(URL root) throws MalformedURLException
{
- this(new URL[]{root}, new URL[0]);
+ this(new URL[]{root});
}
=
/**
@@ -89,7 +79,6 @@
{
if (LOG.isDebugEnabled())
LOG.debug("Process file: " + filename);
-
URL resource =3D null;
filename =3D filename.intern();
synchronized (filename)
@@ -98,12 +87,6 @@
boolean inCache =3D resource !=3D null;
if (inCache && !checkResource(resource))
resource =3D null;
- for (int i =3D 0; i < files.length && resource =3D=3D null; i++)
- {
- URL tmp =3D files[i];
- if (tmp.toString().endsWith(filename) && checkResource(tmp))
- resource =3D tmp;
- }
for (int i =3D 0; i < roots.length && resource =3D=3D null; i++)
{
// In JCR URL path represented by fragment jcr://repository/wo=
rkspace#/path
Modified: jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/GroovyCompilerTest.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyCompilerTest.java 2010-12-27 13:35:46 UTC (rev =
3731)
+++ jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyCompilerTest.java 2010-12-27 13:37:04 UTC (rev =
3732)
@@ -22,11 +22,11 @@
import groovy.lang.GroovyObject;
=
import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
-import org.exoplatform.services.rest.ext.groovy.ClassPath;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry;
-import org.exoplatform.services.rest.ext.groovy.GroovyClassLoaderProvider;
-import org.exoplatform.services.rest.ext.groovy.ClassPathEntry.EntryType;
+import org.exoplatform.services.rest.ext.groovy.SourceFolder;
=
+import java.util.ArrayList;
+import java.util.List;
+
import javax.jcr.Node;
=
/**
@@ -63,7 +63,7 @@
=
public void testSnaredDependencies() throws Exception
{
- GroovyClassLoaderProvider classLoaderProvider =3D new GroovyClassLoa=
derProvider();
+ JcrGroovyClassLoaderProvider classLoaderProvider =3D new JcrGroovyCl=
assLoaderProvider();
classLoaderProvider.getGroovyClassLoader().setResourceLoader(
new JcrGroovyResourceLoader(new java.net.URL[]{new java.net.URL("=
jcr://db1/ws#/groovyRepo")}));
JcrGroovyCompiler compiler =3D new JcrGroovyCompiler(classLoaderProv=
ider);
@@ -76,7 +76,7 @@
=
public void testDependenciesBetweenCompiled() throws Exception
{
- GroovyClassLoaderProvider classLoaderProvider =3D new GroovyClassLoa=
derProvider();
+ JcrGroovyClassLoaderProvider classLoaderProvider =3D new JcrGroovyCl=
assLoaderProvider();
JcrGroovyCompiler compiler =3D new JcrGroovyCompiler(classLoaderProv=
ider);
Class>[] classes =3D
compiler.compile(new UnifiedNodeReference("db1", "ws", scriptB),
@@ -90,13 +90,11 @@
=
public void testAddDependenciesInRuntime() throws Exception
{
- GroovyClassLoaderProvider classLoaderProvider =3D new GroovyClassLoa=
derProvider();
+ JcrGroovyClassLoaderProvider classLoaderProvider =3D new JcrGroovyCl=
assLoaderProvider();
JcrGroovyCompiler compiler =3D new JcrGroovyCompiler(classLoaderProv=
ider);
- ClassPathEntry[] classPathEntries =3D
- new ClassPathEntry[]{new JcrClassPathEntry(EntryType.FILE, new Un=
ifiedNodeReference("db1", "ws", scriptA))};
- Class>[] classes =3D compiler.compile( //
- new ClassPath(classPathEntries, null), //
- new UnifiedNodeReference("db1", "ws", scriptB));
+ SourceFolder[] src =3D
+ new SourceFolder[]{new SourceFolder(new UnifiedNodeReference("db1=
", "ws", groovyRepo.getPath()).getURL())};
+ Class>[] classes =3D compiler.compile(src, new UnifiedNodeReferenc=
e("db1", "ws", scriptB));
assertEquals(1, classes.length);
GroovyObject go =3D (GroovyObject)classes[0].newInstance();
assertEquals("groovy compiler test", go.invokeMethod("getMessage", n=
ew Object[0]));
@@ -104,7 +102,8 @@
=
public void testCombinedDependencies() throws Exception
{
- String scriptC =3D createScript(otherGroovyRepo, "org.exoplatform.te=
st", "C.groovy", //
+ // Add file without respect to package structure to be sure direct l=
ink to source files works any way.
+ String scriptC =3D createScript(otherGroovyRepo, "org.exoplatform", =
"C", //
"package org.exoplatform.test\n" + //
"import org.exoplatform.*\n" + //
"class C extends B {}");
@@ -114,7 +113,7 @@
"import org.exoplatform.test.C\n" + //
"class D extends C {}");
=
- GroovyClassLoaderProvider classLoaderProvider =3D new GroovyClassLoa=
derProvider();
+ JcrGroovyClassLoaderProvider classLoaderProvider =3D new JcrGroovyCl=
assLoaderProvider();
classLoaderProvider.getGroovyClassLoader().setResourceLoader(
new JcrGroovyResourceLoader(new java.net.URL[]{new java.net.URL("=
jcr://db1/ws#/groovyRepo")}));
=
@@ -123,5 +122,11 @@
compiler.compile(new UnifiedNodeReference("db1", "ws", scriptD),
new UnifiedNodeReference("db1", "ws", scriptC));
assertEquals(2, classes.length);
+ List names =3D new ArrayList(2);
+ for (Class c : classes)
+ names.add(c.getName());
+ assertTrue(names.contains("org.exoplatform.test.C"));
+ assertTrue(names.contains("org.exoplatform.test.other.D"));
+ //System.out.println(">>>>>>>>>>>>>>>>> "+names); =
}
}
Modified: jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/ser=
vices/jcr/ext/script/groovy/GroovyScript2RestLoaderTest.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyScript2RestLoaderTest.java 2010-12-27 13:35:46 =
UTC (rev 3731)
+++ jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/=
jcr/ext/script/groovy/GroovyScript2RestLoaderTest.java 2010-12-27 13:37:04 =
UTC (rev 3732)
@@ -193,7 +193,7 @@
String path =3D createScript(testRoot, "test.load", "Test000.groovy"=
, //
"import test.load.User000\n" + //
"@javax.ws.rs.Path('test/load') class Test000 {\n" + //
- "def user =3D new User(name:'test')\n" + //
+ "def user =3D new User000(name:'test')\n" + //
"@javax.ws.rs.GET def m() {user}" + //
"}\n");
EnvironmentContext ctx =3D new EnvironmentContext();
@@ -206,7 +206,7 @@
assertEquals(before, after);
}
=
- public void testLoadNoExtClassPath_File() throws Exception
+ public void testLoadExtClassPath_File() throws Exception
{
String scriptPath =3D createScript(testRoot, "test.load", "Test001.g=
roovy", //
"import test.load.User001\n" + //
@@ -232,7 +232,7 @@
assertEquals(before + 1, after);
}
=
- public void testLoadNoExtClassPath_SourceFolder() throws Exception
+ public void testLoadExtClassPath_SourceFolder() throws Exception
{
String scriptPath =3D createScript(testRoot, "test.load", "Test002.g=
roovy", //
"import test.load.User002\n" + //
@@ -258,33 +258,6 @@
assertEquals(before + 1, after);
}
=
- public void testLoadNoExtClassPath_CustomExtension() throws Exception
- {
- String scriptPath =3D createScript(testRoot, "test.load", "Test003.g=
roovy", //
- "import test.load.User003\n" + //
- "@javax.ws.rs.Path('test/load_003') class Test003 {\n" + //
- "def user =3D new User003(name:'test')\n" + //
- "@javax.ws.rs.GET def m() {user}" + //
- "}\n");
- createScript(testRoot, "test.load", "User003.otherGroovy",// =
- "package test.load\n" + //
- "class User003 {def name}");
-
- EnvironmentContext ctx =3D new EnvironmentContext();
- ctx.put(SecurityContext.class, adminSecurityContext);
- String path =3D
- "/script/groovy/load/db1/ws" + scriptPath //
- + "?sources=3D" //
- + URLEncoder.encode(new UnifiedNodeReference(repository.getNam=
e(), workspace.getName(), testRoot.getPath())
- .getURL().toString(), "UTF-8") + //
- "&extension=3D.otherGroovy";
- int before =3D binder.getSize();
- ContainerResponse cres =3D launcher.service("POST", path, "", null, =
null, ctx);
- assertEquals(204, cres.getStatus());
- int after =3D binder.getSize();
- assertEquals(before + 1, after);
- }
-
public void testDelete() throws Exception
{
ContainerResponse cres =3D
@@ -429,28 +402,6 @@
assertEquals(200, cres.getStatus());
}
=
- public void testValidateExtClassPath_CustomExtension() throws Exception
- {
- MultivaluedMap headers =3D new MultivaluedMapImpl();
- headers.putSingle("Content-Type", "script/groovy");
- String script =3D "import test.validate.User003\n" + //
- "@javax.ws.rs.Path('test/validate') class Test {\n" + //
- "def user =3D new User003(name:'test')\n" + //
- "@javax.ws.rs.GET def m() {user}" + //
- " }\n";
- createScript(testRoot, "test.validate", "User003.otherGroovy",// =
- "package test.validate\n" + //
- "class User003 {def name}");
- // Specify source folder location and customized extension '.otherGr=
oovy'.
- String path =3D
- "/script/groovy/validate/Test?sources=3D" //
- + URLEncoder.encode(new UnifiedNodeReference(repository.getNam=
e(), workspace.getName(), testRoot.getPath())
- .getURL().toString(), "UTF-8") + //
- "&extension=3D.otherGroovy";
- ContainerResponse cres =3D launcher.service("POST", path, "", header=
s, script.getBytes(), null);
- assertEquals(200, cres.getStatus());
- }
-
public void testGroovyDependency() throws Exception
{
// Add script in dependency repository
--===============1977002325676083424==--