Author: aparfonov
Date: 2010-06-22 07:43:00 -0400 (Tue, 22 Jun 2010)
New Revision: 2680
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyDependenciesTest.java
ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy
ws/trunk/exo.ws.rest.ext/src/test/resources/repo/
ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/
ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groovy
Log:
EXOJCR-482 : URL based groovy resource resolver
Added:
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
(rev 0)
+++
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java 2010-06-22
11:43:00 UTC (rev 2680)
@@ -0,0 +1,109 @@
+/**
+ * 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;
+
+import groovy.lang.GroovyResourceLoader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class DefaultGroovyResourceLoader implements GroovyResourceLoader
+{
+
+ protected URL[] roots;
+
+ protected Map<String, URL> resources = Collections.synchronizedMap(new
HashMap<String, URL>());
+
+ public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
+ {
+ this.roots = new URL[roots.length];
+ for (int i = 0; i < roots.length; i++)
+ {
+ String str = roots[i].toString();
+ if (str.charAt(str.length() - 1) != '/')
+ {
+ this.roots[i] = new URL(str + '/');
+ }
+ else
+ {
+ this.roots[i] = roots[i];
+ }
+ }
+ }
+
+ public DefaultGroovyResourceLoader(URL root) throws MalformedURLException
+ {
+ this(new URL[]{root});
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public URL loadGroovySource(String filename) throws MalformedURLException
+ {
+ filename = filename.replace('.', File.separatorChar) +
".groovy";
+ filename = filename.intern();
+ URL resource = null;
+ synchronized (filename)
+ {
+ resource = resources.get(filename);
+ boolean inCache = resource != null;
+ for (URL root : roots)
+ {
+ if (resource == null)
+ {
+ resource = new URL(root, filename);
+ }
+ //System.out.println(resource);
+ try
+ {
+ InputStream script = resource.openStream();
+ script.close();
+ break;
+ }
+ catch (IOException e)
+ {
+ resource = null;
+ }
+ }
+ if (resource != null)
+ {
+ resources.put(filename, resource);
+ }
+ else if (inCache)
+ {
+ // Remove from map if resource is unreachable
+ resources.remove(filename);
+ }
+ }
+ return resource;
+ }
+
+}
Property changes on:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.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/GroovyDependenciesTest.java
===================================================================
---
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyDependenciesTest.java
(rev 0)
+++
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyDependenciesTest.java 2010-06-22
11:43:00 UTC (rev 2680)
@@ -0,0 +1,58 @@
+/**
+ * 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;
+
+import org.exoplatform.services.rest.ext.BaseTest;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
+ * @version $Id$
+ */
+public class GroovyDependenciesTest extends BaseTest
+{
+ private InputStream script;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ URL root =
Thread.currentThread().getContextClassLoader().getResource("repo");
+ DefaultGroovyResourceLoader groovyResourceLoader = new
DefaultGroovyResourceLoader(root);
+ groovyPublisher.getGroovyClassLoader().setResourceLoader(groovyResourceLoader);
+
+ script =
Thread.currentThread().getContextClassLoader().getResourceAsStream("GMain1.groovy");
+ assertNotNull(script);
+ }
+
+ public void testDependency() throws Exception
+ {
+ groovyPublisher.publishPerRequest(script, new BaseResourceId("GMain1"));
+ ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
+ ContainerResponse resp = service("GET", "/a", "",
null, null, writer);
+ assertEquals(200, resp.getStatus());
+ assertEquals("dependencies.Dep1", new String(writer.getBody()));
+ }
+
+}
Property changes on:
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyDependenciesTest.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/resources/GMain1.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy (rev
0)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/GMain1.groovy 2010-06-22 11:43:00 UTC (rev
2680)
@@ -0,0 +1,14 @@
+import javax.ws.rs.Path
+import javax.ws.rs.GET
+import dependencies.Dep1
+
+@Path("a")
+class GMain1
+{
+ @GET
+ def m0()
+ {
+ return new Dep1().getName()
+ }
+
+}
\ No newline at end of file
Added: ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groovy
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groovy
(rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groovy 2010-06-22
11:43:00 UTC (rev 2680)
@@ -0,0 +1,6 @@
+package dependencies
+
+class Dep1
+{
+ String name = getClass().getName()
+}
\ No newline at end of file