Author: aparfonov
Date: 2010-10-26 11:08:22 -0400 (Tue, 26 Oct 2010)
New Revision: 3348
Added:
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/JcrGroovyResourceLoader.java
jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyCompilerTest.java
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyScript2RestLoader.java
Log:
EXOJCR-1020
Modified:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyScript2RestLoader.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyScript2RestLoader.java 2010-10-26
13:38:12 UTC (rev 3347)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyScript2RestLoader.java 2010-10-26
15:08:22 UTC (rev 3348)
@@ -32,10 +32,8 @@
import org.exoplatform.services.jcr.ext.common.SessionProvider;
import org.exoplatform.services.jcr.ext.registry.RegistryEntry;
import org.exoplatform.services.jcr.ext.registry.RegistryService;
-import org.exoplatform.services.jcr.ext.resource.JcrURLConnection;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
-import org.exoplatform.services.rest.ext.groovy.DefaultGroovyResourceLoader;
import org.exoplatform.services.rest.ext.groovy.GroovyJaxrsPublisher;
import org.exoplatform.services.rest.ext.groovy.ResourceId;
import org.exoplatform.services.rest.impl.ResourceBinder;
@@ -53,7 +51,6 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
-import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
@@ -98,7 +95,7 @@
{
/** Logger. */
- private static final Log LOG =
ExoLogger.getLogger("exo.jcr.component.ext.GroovyScript2RestLoader");
+ static final Log LOG =
ExoLogger.getLogger("exo.jcr.component.ext.GroovyScript2RestLoader");
/** Default node types for Groovy scripts. */
private static final String DEFAULT_NODETYPE =
"exo:groovyResourceContainer";
@@ -1275,7 +1272,6 @@
public Response list(@PathParam("repository") String repository,
@PathParam("workspace") String workspace,
@QueryParam("name") String name)
{
-
Session ses = null;
try
{
@@ -1335,7 +1331,6 @@
Collections.sort(scriptList);
return Response.status(Response.Status.OK).entity(new
ScriptList(scriptList)).type(MediaType.APPLICATION_JSON)
.build();
-
}
catch (Exception e)
{
@@ -1377,131 +1372,21 @@
}
/**
- * JCR groovy resource resolver.
- */
- protected class JcrGroovyResourceLoader extends DefaultGroovyResourceLoader
- {
-
- public JcrGroovyResourceLoader(URL[] roots) throws MalformedURLException
- {
- super(normalizeJcrURL(roots));
- }
-
- @Override
- protected URL getResource(String filename) throws MalformedURLException
- {
- filename = filename.intern();
- URL resource = null;
- synchronized (filename)
- {
- resource = resources.get(filename);
- boolean inCache = resource != null;
- for (URL root : roots)
- {
- if (resource == null)
- {
- if ("jcr".equals(root.getProtocol()))
- {
- // In JCR URL path represented by fragment
- // jcr://repository/workspace#/path
- String ref = root.getRef();
- resource = new URL(root, "#" + ref + filename);
- }
- else
- {
- resource = new URL(root, filename);
- }
- }
- URLConnection connection = null;
- try
- {
- if (LOG.isDebugEnabled())
- LOG.debug("Try to load resource from URL : " + resource);
-
- connection = resource.openConnection();
- connection.getInputStream().close();
-
- break;
- }
- catch (IOException e)
- {
- if (LOG.isDebugEnabled())
- LOG.debug("Can't open URL : " + resource);
-
- resource = null;
- }
- finally
- {
- if (connection != null && resource != null &&
"jcr".equals(resource.getProtocol()))
- {
- ((JcrURLConnection)connection).disconnect();
- }
- }
- }
- if (resource != null)
- {
- resources.put(filename, resource);
- }
- else if (inCache)
- {
- // Remove from map if resource is unreachable
- resources.remove(filename);
- }
- }
- return resource;
- }
- }
-
- private static URL[] normalizeJcrURL(URL[] src) throws MalformedURLException
- {
- URL[] res = new URL[src.length];
- for (int i = 0; i < src.length; i++)
- {
- if ("jcr".equals(src[i].getProtocol()))
- {
- String ref = src[i].getRef();
- if (ref == null)
- {
- ref = "/";
- }
- else if (ref.charAt(ref.length() - 1) != '/')
- {
- ref = ref + "/";
- }
- res[i] = new URL(src[i], "#" + ref);
- }
- else
- {
- res[i] = src[i];
- }
- }
- return res;
- }
-
- /**
* Script meta-data, used for pass script meta-data as JSON.
*/
public static class ScriptMetadata
{
- /**
- * Is script autoload.
- */
+ /** Is script autoload. */
private final boolean autoload;
- /**
- * Is script loaded.
- */
+ /** Is script loaded. */
private final boolean load;
- /**
- * Script media type (script/groovy).
- */
+ /** Script media type (script/groovy). */
private final String mediaType;
- /**
- * Last modified date.
- */
+ /** Last modified date. */
private final long lastModified;
public ScriptMetadata(boolean autoload, boolean load, String mediaType, long
lastModified)
@@ -1551,9 +1436,7 @@
public static class ScriptList
{
- /**
- * The list of scripts.
- */
+ /** The list of scripts. */
private List<String> list;
/**
Added:
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
(rev 0)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java 2010-10-26
15:08:22 UTC (rev 3348)
@@ -0,0 +1,111 @@
+/**
+ * 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 groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyCodeSource;
+
+import org.exoplatform.services.jcr.ext.resource.JcrURLConnection;
+import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * JcrGroovyCompiler can load source code of groovy script from JCR and parse it
+ * via GroovyClassLoader.
+ *
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class JcrGroovyCompiler
+{
+ protected GroovyClassLoader gcl;
+
+ public JcrGroovyCompiler()
+ {
+ ClassLoader cl = getClass().getClassLoader();
+ this.gcl = new GroovyClassLoader(cl);
+ }
+
+ /**
+ * @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 Class<?>[] compile(UnifiedNodeReference... sourceReferences) throws
IOException
+ {
+ GroovyClassLoader cl = gcl;
+ Class<?>[] classes = new Class<?>[sourceReferences.length];
+ for (int i = 0; i < sourceReferences.length; i++)
+ {
+ JcrURLConnection conn = null;
+ try
+ {
+ URL url = sourceReferences[i].getURL();
+ conn = (JcrURLConnection)url.openConnection();
+ Class<?> clazz = cl.parseClass(createCodeSource(conn.getInputStream(),
url.toString()));
+ classes[i] = clazz;
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.disconnect();
+ }
+ }
+ }
+ return classes;
+ }
+
+ /**
+ * Create {@link GroovyCodeSource} from given stream and name. Code base
+ * 'file:/groovy/script' (default code base used for all Groovy classes) will
+ * be used.
+ *
+ * @param in groovy source code stream
+ * @param name code source name
+ * @return GroovyCodeSource
+ */
+ // Override this method if need other behavior.
+ protected GroovyCodeSource createCodeSource(InputStream in, String name)
+ {
+ GroovyCodeSource gcs = new GroovyCodeSource(in, name, "/groovy/script");
+ gcs.setCachable(false);
+ return gcs;
+ }
+}
Property changes on:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyCompiler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyResourceLoader.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyResourceLoader.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyResourceLoader.java 2010-10-26
15:08:22 UTC (rev 3348)
@@ -0,0 +1,129 @@
+/*
+ * 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.jcr.ext.script.groovy;
+
+import org.exoplatform.services.jcr.ext.resource.JcrURLConnection;
+import org.exoplatform.services.rest.ext.groovy.DefaultGroovyResourceLoader;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * JCR groovy resource resolver.
+ */
+public class JcrGroovyResourceLoader extends DefaultGroovyResourceLoader
+{
+
+ private static URL[] normalizeJcrURL(URL[] src) throws MalformedURLException
+ {
+ URL[] res = new URL[src.length];
+ for (int i = 0; i < src.length; i++)
+ {
+ if ("jcr".equals(src[i].getProtocol()))
+ {
+ String ref = src[i].getRef();
+ if (ref == null)
+ {
+ ref = "/";
+ }
+ else if (ref.charAt(ref.length() - 1) != '/')
+ {
+ ref = ref + "/";
+ }
+ res[i] = new URL(src[i], "#" + ref);
+ }
+ else
+ {
+ res[i] = src[i];
+ }
+ }
+ return res;
+ }
+
+ public JcrGroovyResourceLoader(URL[] roots) throws MalformedURLException
+ {
+ super(normalizeJcrURL(roots));
+ }
+
+ @Override
+ protected URL getResource(String filename) throws MalformedURLException
+ {
+ filename = filename.intern();
+ URL resource = null;
+ synchronized (filename)
+ {
+ resource = resources.get(filename);
+ boolean inCache = resource != null;
+ for (URL root : roots)
+ {
+ if (resource == null)
+ {
+ if ("jcr".equals(root.getProtocol()))
+ {
+ // In JCR URL path represented by fragment
+ // jcr://repository/workspace#/path
+ String ref = root.getRef();
+ resource = new URL(root, "#" + ref + filename);
+ }
+ else
+ {
+ resource = new URL(root, filename);
+ }
+ }
+ URLConnection connection = null;
+ try
+ {
+ if (GroovyScript2RestLoader.LOG.isDebugEnabled())
+ GroovyScript2RestLoader.LOG.debug("Try to load resource from URL :
" + resource);
+
+ connection = resource.openConnection();
+ connection.getInputStream().close();
+
+ break;
+ }
+ catch (IOException e)
+ {
+ if (GroovyScript2RestLoader.LOG.isDebugEnabled())
+ GroovyScript2RestLoader.LOG.debug("Can't open URL : " +
resource);
+
+ resource = null;
+ }
+ finally
+ {
+ if (connection != null && resource != null &&
"jcr".equals(resource.getProtocol()))
+ {
+ ((JcrURLConnection)connection).disconnect();
+ }
+ }
+ }
+ if (resource != null)
+ {
+ resources.put(filename, resource);
+ }
+ else if (inCache)
+ {
+ // Remove from map if resource is unreachable
+ resources.remove(filename);
+ }
+ }
+ return resource;
+ }
+}
\ No newline at end of file
Property changes on:
jcr/trunk/exo.jcr.component.ext/src/main/java/org/exoplatform/services/jcr/ext/script/groovy/JcrGroovyResourceLoader.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyCompilerTest.java
===================================================================
---
jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyCompilerTest.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyCompilerTest.java 2010-10-26
15:08:22 UTC (rev 3348)
@@ -0,0 +1,76 @@
+/**
+ * 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 groovy.lang.GroovyObject;
+
+import org.exoplatform.services.jcr.ext.BaseStandaloneTest;
+import org.exoplatform.services.jcr.ext.resource.UnifiedNodeReference;
+
+import java.util.Calendar;
+
+import javax.jcr.Node;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class GroovyCompilerTest extends BaseStandaloneTest
+{
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ Node groovyRepo = root.addNode("groovyRepo", "nt:folder");
+ Node org = groovyRepo.addNode("org", "nt:folder");
+ Node exo = org.addNode("exoplatform", "nt:folder");
+ Node a = exo.addNode("A.groovy", "nt:file");
+ a = a.addNode("jcr:content", "nt:resource");
+ a.setProperty("jcr:mimeType", "script/groovy");
+ a.setProperty("jcr:lastModified", Calendar.getInstance());
+ a.setProperty("jcr:data", //
+ "package org.exoplatform\n" + //
+ " class A { String message = 'groovy compiler test' }");
+
+ Node test = exo.addNode("test", "nt:folder");
+ Node b = test.addNode("B.groovy", "nt:file");
+ b = b.addNode("jcr:content", "nt:resource");
+ b.setProperty("jcr:mimeType", "script/groovy");
+ b.setProperty("jcr:lastModified", Calendar.getInstance());
+ b.setProperty("jcr:data", //
+ "package org.exoplatform.test\n" + //
+ " import org.exoplatform.A\n" + //
+ " class B extends A {}");
+ session.save();
+ }
+
+ public void testGroovyDependency() throws Exception
+ {
+ JcrGroovyCompiler compiler = new JcrGroovyCompiler();
+ compiler.getGroovyClassLoader().setResourceLoader(
+ new JcrGroovyResourceLoader(new java.net.URL[]{new
java.net.URL("jcr://db1/ws#/groovyRepo")}));
+ Class<?>[] classes =
+ compiler.compile(new UnifiedNodeReference("db1", "ws",
"/groovyRepo/org/exoplatform/test/B.groovy"));
+ assertEquals(1, classes.length);
+ GroovyObject go = (GroovyObject)classes[0].newInstance();
+ assertEquals("groovy compiler test",
go.invokeMethod("getMessage", new Object[0]));
+ }
+
+}
Property changes on:
jcr/trunk/exo.jcr.component.ext/src/test/java/org/exoplatform/services/jcr/ext/script/groovy/GroovyCompilerTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native