[exo-jcr-commits] exo-jcr SVN: r2663 - in ws/trunk/exo.ws.rest.ext/src: test/java/org/exoplatform/services/rest/ext/groovy and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Jun 18 09:50:28 EDT 2010
Author: aparfonov
Date: 2010-06-18 09:50:27 -0400 (Fri, 18 Jun 2010)
New Revision: 2663
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/BaseResourceId.java
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ResourceId.java
Modified:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyContextParamTest.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyExoComponentTest.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java
Log:
EXOJCR-793 :
Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/BaseResourceId.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/BaseResourceId.java (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/BaseResourceId.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -0,0 +1,75 @@
+/**
+ * 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;
+
+/**
+ * Base implementation of ResourceId.
+ *
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class BaseResourceId implements ResourceId
+{
+
+ protected final String id;
+
+ public BaseResourceId(String id)
+ {
+ this.id = id;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getId()
+ {
+ return id;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ return id.equals(((BaseResourceId)obj).id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode()
+ {
+ return id.hashCode();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String toString()
+ {
+ return getClass().getSimpleName() + '(' + id + ')';
+ }
+}
Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/BaseResourceId.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java 2010-06-18 13:22:59 UTC (rev 2662)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -29,8 +29,14 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
/**
+ * Manage via {@link ResourceBinder} Groovy based RESTful services.
+ *
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id$
*/
@@ -38,10 +44,10 @@
{
/** Default character set name. */
- static final String DEFAULT_CHARSET_NAME = "UTF-8";
+ protected static final String DEFAULT_CHARSET_NAME = "UTF-8";
/** Default character set. */
- static final Charset DEFAULT_CHARSET = Charset.forName(DEFAULT_CHARSET_NAME);
+ protected static final Charset DEFAULT_CHARSET = Charset.forName(DEFAULT_CHARSET_NAME);
protected final ResourceBinder binder;
@@ -49,6 +55,9 @@
protected GroovyClassLoader gcl;
+ protected final Map<ResourceId, Class<?>> resources =
+ Collections.synchronizedMap(new HashMap<ResourceId, Class<?>>());
+
/**
* Create GroovyJaxrsPublisher which is able publish per-request and
* singleton resources. Any required dependencies for per-request resource
@@ -67,32 +76,43 @@
}
/**
- * Create GroovyJaxrsPublisher which is able public per-request resources
- * only. This is default behavior for RESTful services. Any required
- * dependencies for resource injected by {@link PerRequestObjectFactory} in
- * runtime.
+ * @return get underling groovy class loader
+ */
+ public GroovyClassLoader getGroovyClassLoader()
+ {
+ return gcl;
+ }
+
+ /**
+ * Check is groovy resource with specified id is published or not
*
- * @param binder resource binder
+ * @param resourceId id of resource to be checked
+ * @return <code>true</code> if resource is published and <code>false</code>
+ * otherwise
*/
- public GroovyJaxrsPublisher(ResourceBinder binder)
+ public boolean isPublished(ResourceId resourceId)
{
- ClassLoader cl = getClass().getClassLoader();
- this.gcl = new GroovyClassLoader(cl);
- this.binder = binder;
- this.instantiator = null;
+ return resources.containsKey(resourceId);
}
/**
* Parse given stream and publish result as per-request RESTful service.
*
* @param in stream which contains groovy source code of RESTful service
- * @param name name of resource
+ * @param resourceId id to be assigned to resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishPerRequest(InputStream in, String name)
+ public boolean publishPerRequest(InputStream in, ResourceId resourceId)
{
- return publishPerRequest(createCodeSource(in, name));
+ Class<?> rc = gcl.parseClass(createCodeSource(in, resourceId.getId()));
+ boolean answ = binder.bind(rc);
+ if (answ)
+ {
+ resources.put(resourceId, rc);
+ }
+ return answ;
}
/**
@@ -100,48 +120,52 @@
* service.
*
* @param source groovy source code of RESTful service
- * @param name name of resource
+ * @param resourceId id to be assigned to resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishPerRequest(String source, String name)
+ public final boolean publishPerRequest(String source, ResourceId resourceId)
{
- byte[] bytes = source.getBytes(DEFAULT_CHARSET);
- return publishPerRequest(new ByteArrayInputStream(bytes), name);
+ return publishPerRequest(source, DEFAULT_CHARSET, resourceId);
}
/**
- * Parse given {@link GroovyCodeSource} and publish result as per-request
- * RESTful service.
+ * Parse given <code>source</code> and publish result as per-request RESTful
+ * service.
*
- * @param gcs groovy code source which contains source code of RESTful
- * service
+ * @param source groovy source code of RESTful service
+ * @param charset source string charset. May be <code>null</code> than
+ * default charset will be in use
+ * @param resourceId id to be assigned to resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
+ * @throws UnsupportedCharsetException if <code>charset</code> is unsupported
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishPerRequest(GroovyCodeSource gcs)
+ public final boolean publishPerRequest(String source, String charset, ResourceId resourceId)
{
- Class<?> rc = gcl.parseClass(gcs);
- return binder.bind(rc);
+ return publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId);
}
/**
* Parse given stream and publish result as singleton RESTful service.
*
* @param in stream which contains groovy source code of RESTful service
- * @param name name of resource
+ * @param resourceId id to be assigned to resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
- * @throws UnsupportedOperationException if publisher was created without
- * support of singleton resource, see
- * {@link #GroovyJaxrsPublisher(ResourceBinder)}
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishSingleton(InputStream in, String name)
+ public boolean publishSingleton(InputStream in, ResourceId resourceId)
{
- if (instantiator == null)
- throw new UnsupportedOperationException(
- "Can't instantiate groovy script. GroovyScriptInstantiator is not set.");
- return publishSingleton(createCodeSource(in, name));
+ Object r = instantiator.instantiateScript(createCodeSource(in, resourceId.getId()), gcl);
+ boolean answ = binder.bind(r);
+ if (answ)
+ {
+ resources.put(resourceId, r.getClass());
+ }
+ return answ;
}
/**
@@ -149,41 +173,32 @@
* service.
*
* @param source groovy source code of RESTful service
- * @param name name of resource
+ * @param resourceId name of resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
- * @throws UnsupportedOperationException if publisher was created without
- * support of singleton resource, see
- * {@link #GroovyJaxrsPublisher(ResourceBinder)}
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishSingleton(String source, String name)
+ public final boolean publishSingleton(String source, ResourceId resourceId)
{
- if (instantiator == null)
- throw new UnsupportedOperationException(
- "Can't instantiate groovy script. GroovyScriptInstantiator is not set.");
- byte[] bytes = source.getBytes(DEFAULT_CHARSET);
- return publishSingleton(new ByteArrayInputStream(bytes), name);
+ return publishSingleton(source, DEFAULT_CHARSET, resourceId);
}
/**
- * Parse given {@link GroovyCodeSource} and publish result as singleton
- * RESTful service.
+ * Parse given <code>source</code> and publish result as singleton RESTful
+ * service.
*
- * @param gcs groovy code source which contains source code of RESTful
- * service
+ * @param source groovy source code of RESTful service
+ * @param charset source string charset. May be <code>null</code> than
+ * default charset will be in use
+ * @param resourceId name of resource
* @return <code>true</code> if resource was published and <code>false</code>
* otherwise
- * @throws UnsupportedOperationException if publisher was created without
- * support of singleton resource, see
- * {@link #GroovyJaxrsPublisher(ResourceBinder)}
+ * @throws UnsupportedCharsetException if <code>charset</code> is unsupported
+ * @throws NullPointerException if <code>resourceId == null</code>
*/
- public boolean publishSingleton(GroovyCodeSource gcs)
+ public final boolean publishSingleton(String source, String charset, ResourceId resourceId)
{
- if (instantiator == null)
- throw new UnsupportedOperationException(
- "Can't instantiate groovy script. GroovyScriptInstantiator is not set.");
- Object r = instantiator.instantiateScript(gcs, gcl);
- return binder.bind(r);
+ return publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId);
}
/**
@@ -200,6 +215,41 @@
}
/**
+ * Unpublish resource with specified id.
+ *
+ * @param resourceId id of resource to be unpublished
+ * @return <code>true</code> if resource was published and <code>false</code>
+ * otherwise, e.g. because there is not resource corresponded to
+ * supplied <code>resourceId</code>
+ */
+ public boolean unpublishResource(ResourceId resourceId)
+ {
+ Class<?> clazz = resources.get(resourceId);
+ boolean answ = false;
+ if (clazz != null)
+ {
+ answ = binder.unbind(clazz);
+ }
+ if (answ)
+ {
+ resources.remove(resourceId);
+ }
+ return answ;
+ }
+
+ private boolean publishPerRequest(String source, Charset charset, ResourceId resourceId)
+ {
+ byte[] bytes = source.getBytes(charset);
+ return publishPerRequest(new ByteArrayInputStream(bytes), resourceId);
+ }
+
+ private boolean publishSingleton(String source, Charset charset, ResourceId resourceId)
+ {
+ byte[] bytes = source.getBytes(charset);
+ return publishSingleton(new ByteArrayInputStream(bytes), resourceId);
+ }
+
+ /**
* Create {@link GroovyCodeSource} from given stream and name. Code base
* 'file:/groovy/script/jaxrs' will be used.
*
@@ -209,8 +259,7 @@
*/
protected GroovyCodeSource createCodeSource(InputStream in, String name)
{
- GroovyCodeSource gcs =
- new GroovyCodeSource(in, name == null ? gcl.generateScriptName() : name, "/groovy/script/jaxrs");
+ GroovyCodeSource gcs = new GroovyCodeSource(in, name, "/groovy/script/jaxrs");
gcs.setCachable(false);
return gcs;
}
Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ResourceId.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ResourceId.java (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ResourceId.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -0,0 +1,36 @@
+/**
+ * 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;
+
+/**
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public interface ResourceId
+{
+
+ /**
+ * Gets the id of resource as string
+ *
+ * @return the resource id
+ */
+ String getId();
+
+}
Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ResourceId.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyContextParamTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyContextParamTest.java 2010-06-18 13:22:59 UTC (rev 2662)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyContextParamTest.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -27,7 +27,8 @@
/**
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
- * @version $Id$
+ * @version $Id: GroovyContextParamTest.java 2647 2010-06-17 08:39:29Z aparfonov
+ * $
*/
public class GroovyContextParamTest extends BaseTest
{
@@ -41,11 +42,23 @@
assertNotNull(script);
}
+ @Override
+ public void tearDown() throws Exception
+ {
+ groovyPublisher.resources.clear();
+ super.tearDown();
+ }
+
public void testPerRequest() throws Exception
{
assertEquals(0, binder.getSize());
- groovyPublisher.publishPerRequest(script, "g1");
+ assertEquals(0, groovyPublisher.resources.size());
+
+ groovyPublisher.publishPerRequest(script, new BaseResourceId("g1"));
+
assertEquals(1, binder.getSize());
+ assertEquals(1, groovyPublisher.resources.size());
+
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
ContainerResponse resp =
service("GET", "http://localhost:8080/context/a/b", "http://localhost:8080/context", null, null, writer);
Modified: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyExoComponentTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyExoComponentTest.java 2010-06-18 13:22:59 UTC (rev 2662)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyExoComponentTest.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -47,27 +47,33 @@
public void tearDown() throws Exception
{
container.unregisterComponent(Component1.class.getName());
+ groovyPublisher.resources.clear();
super.tearDown();
}
public void testExoComponentPerRequest() throws Exception
{
- containerComponentTest(false, "g1");
+ containerComponentTest(false, new BaseResourceId("g1"));
}
public void testExoComponentSingleton() throws Exception
{
- containerComponentTest(true, "g1");
+ containerComponentTest(true, new BaseResourceId("g2"));
}
- private void containerComponentTest(boolean singleton, String name) throws Exception
+ private void containerComponentTest(boolean singleton, ResourceId resourceId) throws Exception
{
assertEquals(0, binder.getSize());
+ assertEquals(0, groovyPublisher.resources.size());
+
if (singleton)
- groovyPublisher.publishSingleton(script, name);
+ groovyPublisher.publishSingleton(script, resourceId);
else
- groovyPublisher.publishPerRequest(script, name);
+ groovyPublisher.publishPerRequest(script, resourceId);
+
assertEquals(1, binder.getSize());
+ assertEquals(1, groovyPublisher.resources.size());
+
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
ContainerResponse resp = service("GET", "/a/b", "", null, null, writer);
assertEquals(200, resp.getStatus());
Modified: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java 2010-06-18 13:22:59 UTC (rev 2662)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java 2010-06-18 13:50:27 UTC (rev 2663)
@@ -23,8 +23,6 @@
import org.exoplatform.services.rest.impl.ContainerResponse;
import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
-import java.io.ByteArrayInputStream;
-
/**
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id$
@@ -32,17 +30,24 @@
public class GroovySimpleTest extends BaseTest
{
+ @Override
+ public void tearDown() throws Exception
+ {
+ groovyPublisher.resources.clear();
+ super.tearDown();
+ }
+
public void testPerRequest() throws Exception
{
- publicationTest(false, "g1");
+ publicationTest(false, new BaseResourceId("g1"));
}
public void testSingleton() throws Exception
{
- publicationTest(true, "g2");
+ publicationTest(true, new BaseResourceId("g2"));
}
- private void publicationTest(boolean singleton, String name) throws Exception
+ private void publicationTest(boolean singleton, ResourceId resourceId) throws Exception
{
String script = //
"@javax.ws.rs.Path(\"a\")" //
@@ -52,13 +57,15 @@
+ "}";
assertEquals(0, binder.getSize());
+ assertEquals(0, groovyPublisher.resources.size());
if (singleton)
- groovyPublisher.publishSingleton(new ByteArrayInputStream(script.getBytes()), name);
+ groovyPublisher.publishSingleton(script, resourceId);
else
- groovyPublisher.publishPerRequest(new ByteArrayInputStream(script.getBytes()), name);
+ groovyPublisher.publishPerRequest(script, resourceId);
assertEquals(1, binder.getSize());
+ assertEquals(1, groovyPublisher.resources.size());
String cs =
binder.getResources().get(0).getObjectModel().getObjectClass().getProtectionDomain().getCodeSource()
@@ -69,6 +76,11 @@
ContainerResponse resp = service("GET", "/a/groovy", "", null, null, writer);
assertEquals(200, resp.getStatus());
assertEquals("hello groovy", new String(writer.getBody()));
+
+ groovyPublisher.unpublishResource(resourceId);
+
+ assertEquals(0, binder.getSize());
+ assertEquals(0, groovyPublisher.resources.size());
}
}
More information about the exo-jcr-commits
mailing list