[exo-jcr-commits] exo-jcr SVN: r3461 - jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Nov 12 08:39:52 EST 2010
Author: aparfonov
Date: 2010-11-12 08:39:50 -0500 (Fri, 12 Nov 2010)
New Revision: 3461
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java
Log:
EXOJCR-1020 : possibility to configure root resources for lookup groovy scripts
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 2010-11-12 13:07:42 UTC (rev 3460)
+++ jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java 2010-11-12 13:39:50 UTC (rev 3461)
@@ -24,15 +24,24 @@
import org.codehaus.groovy.control.CompilationFailedException;
import org.exoplatform.commons.utils.SecurityHelper;
+import org.exoplatform.container.component.ComponentPlugin;
import org.exoplatform.services.jcr.ext.resource.JcrURLConnection;
import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.picocontainer.Startable;
import java.io.IOException;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
/**
* JcrGroovyCompiler can load source code of groovy script from JCR and parse it
@@ -41,10 +50,15 @@
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id$
*/
-public class JcrGroovyCompiler
+public class JcrGroovyCompiler implements Startable
{
+ /** Logger. */
+ private static final Log LOG = ExoLogger.getLogger(JcrGroovyCompiler.class);
+
protected GroovyClassLoader gcl;
+ protected List<GroovyScriptAddRepoPlugin> addRepoPlugins;
+
public JcrGroovyCompiler()
{
this.gcl = SecurityHelper.doPriviledgedAction(new PrivilegedAction<GroovyClassLoader>()
@@ -56,30 +70,21 @@
});
}
- /**
- * @return get underling groovy class loader
- */
- public GroovyClassLoader getGroovyClassLoader()
+ public void addPlugin(ComponentPlugin cp)
{
- return gcl;
+ if (cp instanceof GroovyScriptAddRepoPlugin)
+ {
+ if (addRepoPlugins == null)
+ {
+ addRepoPlugins = new ArrayList<GroovyScriptAddRepoPlugin>();
+ }
+ addRepoPlugins.add((GroovyScriptAddRepoPlugin)cp);
+ }
}
- /**
- * Set groovy class loader.
- *
- * @param gcl groovy class loader
- * @throws NullPointerException if <code>gcl == null</code>
- */
- public void setGroovyClassLoader(GroovyClassLoader gcl)
- {
- if (gcl == null)
- throw new NullPointerException("GroovyClassLoader may not be null.");
- this.gcl = gcl;
- }
-
public Class<?>[] compile(UnifiedNodeReference... sourceReferences) throws IOException
{
- final GroovyClassLoader cl = gcl;
+ final GroovyClassLoader cl = getGroovyClassLoader();
Class<?>[] classes = new Class<?>[sourceReferences.length];
for (int i = 0; i < sourceReferences.length; i++)
{
@@ -135,6 +140,51 @@
}
/**
+ * @return get underling groovy class loader
+ */
+ public GroovyClassLoader getGroovyClassLoader()
+ {
+ return gcl;
+ }
+
+ /**
+ * Set groovy class loader.
+ *
+ * @param gcl groovy class loader
+ * @throws NullPointerException if <code>gcl == null</code>
+ */
+ public void setGroovyClassLoader(GroovyClassLoader gcl)
+ {
+ if (gcl == null)
+ throw new NullPointerException("GroovyClassLoader may not be null.");
+ this.gcl = gcl;
+ }
+
+ public void start()
+ {
+ if (addRepoPlugins != null && addRepoPlugins.size() > 0)
+ {
+ try
+ {
+ Set<URL> repos = new HashSet<URL>();
+ for (GroovyScriptAddRepoPlugin pl : addRepoPlugins)
+ {
+ repos.addAll(pl.getRepositories());
+ }
+ getGroovyClassLoader().setResourceLoader(new JcrGroovyResourceLoader(repos.toArray(new URL[repos.size()])));
+ }
+ catch (MalformedURLException e)
+ {
+ LOG.error("Unable add groovy script repository. ", e);
+ }
+ }
+ }
+
+ public void stop()
+ {
+ }
+
+ /**
* Create {@link GroovyCodeSource} from given stream and name. Code base
* 'file:/groovy/script' (default code base used for all Groovy classes) will
* be used.
More information about the exo-jcr-commits
mailing list