From do-not-reply at jboss.org Tue Jun 22 07:43:00 2010 Content-Type: multipart/mixed; boundary="===============4597564267937406445==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: exo-jcr-commits at lists.jboss.org Subject: [exo-jcr-commits] exo-jcr SVN: r2680 - in ws/trunk/exo.ws.rest.ext/src: test/java/org/exoplatform/services/rest/ext/groovy and 3 other directories. Date: Tue, 22 Jun 2010 07:43:00 -0400 Message-ID: <201006221143.o5MBh0UM023239@svn01.web.mwc.hst.phx2.redhat.com> --===============4597564267937406445== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ex= t/groovy/DefaultGroovyResourceLoader.java (rev 0) +++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ex= t/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 Andrey Parfonov + * @version $Id$ + */ +public class DefaultGroovyResourceLoader implements GroovyResourceLoader +{ + + protected URL[] roots; + + protected Map resources =3D Collections.synchronizedMap(ne= w HashMap()); + + public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLExce= ption + { + this.roots =3D new URL[roots.length]; + for (int i =3D 0; i < roots.length; i++) + { + String str =3D roots[i].toString(); + if (str.charAt(str.length() - 1) !=3D '/') + { + this.roots[i] =3D new URL(str + '/'); + } + else + { + this.roots[i] =3D roots[i]; + } + } + } + + public DefaultGroovyResourceLoader(URL root) throws MalformedURLExcepti= on + { + this(new URL[]{root}); + } + + /** + * {@inheritDoc} + */ + public URL loadGroovySource(String filename) throws MalformedURLExcepti= on + { + filename =3D filename.replace('.', File.separatorChar) + ".groovy"; + filename =3D filename.intern(); + URL resource =3D null; + synchronized (filename) + { + resource =3D resources.get(filename); + boolean inCache =3D resource !=3D null; + for (URL root : roots) + { + if (resource =3D=3D null) + { + resource =3D new URL(root, filename); + } + //System.out.println(resource); + try + { + InputStream script =3D resource.openStream(); + script.close(); + break; + } + catch (IOException e) + { + resource =3D null; + } + } + if (resource !=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ex= t/groovy/GroovyDependenciesTest.java (rev 0) +++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ex= t/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.ByteArrayContainerResponseWrite= r; + +import java.io.InputStream; +import java.net.URL; + +/** + * @author Andrey Parfonov + * @version $Id$ + */ +public class GroovyDependenciesTest extends BaseTest +{ + private InputStream script; + + @Override + public void setUp() throws Exception + { + super.setUp(); + URL root =3D Thread.currentThread().getContextClassLoader().getResou= rce("repo"); + DefaultGroovyResourceLoader groovyResourceLoader =3D new DefaultGroo= vyResourceLoader(root); + groovyPublisher.getGroovyClassLoader().setResourceLoader(groovyResou= rceLoader); + + script =3D Thread.currentThread().getContextClassLoader().getResourc= eAsStream("GMain1.groovy"); + assertNotNull(script); + } + + public void testDependency() throws Exception + { + groovyPublisher.publishPerRequest(script, new BaseResourceId("GMain1= ")); + ByteArrayContainerResponseWriter writer =3D new ByteArrayContainerRe= sponseWriter(); + ContainerResponse resp =3D service("GET", "/a", "", null, null, writ= er); + 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 + +(a)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.g= roovy =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groo= vy (rev 0) +++ ws/trunk/exo.ws.rest.ext/src/test/resources/repo/dependencies/Dep1.groo= vy 2010-06-22 11:43:00 UTC (rev 2680) @@ -0,0 +1,6 @@ +package dependencies + +class Dep1 +{ + String name =3D getClass().getName() +} \ No newline at end of file --===============4597564267937406445==--