[exo-jcr-commits] exo-jcr SVN: r2762 - 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 Jul 9 09:39:29 EDT 2010
Author: aparfonov
Date: 2010-07-09 09:39:29 -0400 (Fri, 09 Jul 2010)
New Revision: 2762
Modified:
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/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/GroovyDependenciesTest.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/GroovySecureRestrictionTest.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java
Log:
EXOJCR-823:
Modified: 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 2010-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -29,8 +29,9 @@
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Map.Entry;
/**
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
@@ -42,8 +43,11 @@
protected URL[] roots;
- protected Map<String, URL> resources = Collections.synchronizedMap(new HashMap<String, URL>());
+ // TODO need configurable ?
+ private int maxEntries = 512;
+ protected final Map<String, URL> resources;
+
public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
{
this.roots = new URL[roots.length];
@@ -59,6 +63,13 @@
this.roots[i] = roots[i];
}
}
+ resources = Collections.synchronizedMap(new LinkedHashMap<String, URL>()
+ {
+ protected boolean removeEldestEntry(Entry<String, URL> eldest)
+ {
+ return size() > maxEntries;
+ }
+ });
}
public DefaultGroovyResourceLoader(URL root) throws MalformedURLException
@@ -99,6 +110,7 @@
URL resource = null;
synchronized (filename)
{
+ System.out.println(">>>>>>>> "+resources.size());
resource = resources.get(filename);
boolean inCache = resource != null;
for (URL root : roots)
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-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -22,8 +22,12 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
+import org.exoplatform.services.rest.ObjectFactory;
import org.exoplatform.services.rest.PerRequestObjectFactory;
import org.exoplatform.services.rest.impl.ResourceBinder;
+import org.exoplatform.services.rest.impl.ResourcePublicationException;
+import org.exoplatform.services.rest.resource.AbstractResourceDescriptor;
+import org.exoplatform.services.rest.uri.UriPattern;
import org.exoplatform.services.script.groovy.GroovyScriptInstantiator;
import java.io.ByteArrayInputStream;
@@ -32,8 +36,12 @@
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.MultivaluedMap;
+
/**
* Manage via {@link ResourceBinder} Groovy based RESTful services.
*
@@ -55,8 +63,7 @@
protected GroovyClassLoader gcl;
- protected final Map<ResourceId, Class<?>> resources =
- Collections.synchronizedMap(new HashMap<ResourceId, Class<?>>());
+ protected final Map<ResourceId, String> resources = Collections.synchronizedMap(new HashMap<ResourceId, String>());
/**
* Create GroovyJaxrsPublisher which is able publish per-request and
@@ -84,6 +91,33 @@
}
/**
+ * Get resource corresponded to specified id <code>resourceId</code> .
+ *
+ * @param resourceId resource id
+ * @return resource or <code>null</code>
+ */
+ public ObjectFactory<AbstractResourceDescriptor> getResource(ResourceId resourceId)
+ {
+ String path = resources.get(resourceId);
+ if (path == null)
+ return null;
+
+ UriPattern pattern = new UriPattern(path);
+ List<ObjectFactory<AbstractResourceDescriptor>> rootResources = binder.getResources();
+ synchronized (rootResources)
+ {
+ for (ObjectFactory<AbstractResourceDescriptor> res : rootResources)
+ {
+ if (res.getObjectModel().getUriPattern().equals(pattern))
+ return res;
+ }
+ }
+ // If resource not exists any more but still in mapping.
+ resources.remove(resourceId);
+ return null;
+ }
+
+ /**
* Check is groovy resource with specified id is published or not
*
* @param resourceId id of resource to be checked
@@ -92,7 +126,7 @@
*/
public boolean isPublished(ResourceId resourceId)
{
- return resources.containsKey(resourceId);
+ return null != getResource(resourceId);
}
/**
@@ -100,19 +134,17 @@
*
* @param in stream which contains groovy source code of RESTful service
* @param resourceId id to be assigned to resource
- * @return <code>true</code> if resource was published and <code>false</code>
- * otherwise
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
- public boolean publishPerRequest(InputStream in, ResourceId resourceId)
+ public void publishPerRequest(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
Class<?> rc = gcl.parseClass(createCodeSource(in, resourceId.getId()));
- boolean answ = binder.bind(rc);
- if (answ)
- {
- resources.put(resourceId, rc);
- }
- return answ;
+ binder.addResource(rc, properties);
+ resources.put(resourceId, rc.getAnnotation(Path.class).value());
}
/**
@@ -121,13 +153,15 @@
*
* @param source groovy source code of RESTful service
* @param resourceId id to be assigned to resource
- * @return <code>true</code> if resource was published and <code>false</code>
- * otherwise
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
- public final boolean publishPerRequest(String source, ResourceId resourceId)
+ public final void publishPerRequest(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
- return publishPerRequest(source, DEFAULT_CHARSET, resourceId);
+ publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties);
}
/**
@@ -138,14 +172,17 @@
* @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
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
* @throws UnsupportedCharsetException if <code>charset</code> is unsupported
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
- public final boolean publishPerRequest(String source, String charset, ResourceId resourceId)
+ public final void publishPerRequest(String source, String charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties)
{
- return publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId);
+ publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties);
}
/**
@@ -153,19 +190,17 @@
*
* @param in stream which contains groovy source code of RESTful service
* @param resourceId id to be assigned to resource
- * @return <code>true</code> if resource was published and <code>false</code>
- * otherwise
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
- public boolean publishSingleton(InputStream in, ResourceId resourceId)
+ public void publishSingleton(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
Object r = instantiator.instantiateScript(createCodeSource(in, resourceId.getId()), gcl);
- boolean answ = binder.bind(r);
- if (answ)
- {
- resources.put(resourceId, r.getClass());
- }
- return answ;
+ binder.addResource(r, properties);
+ resources.put(resourceId, r.getClass().getAnnotation(Path.class).value());
}
/**
@@ -174,13 +209,15 @@
*
* @param source groovy source code of RESTful service
* @param resourceId name of resource
- * @return <code>true</code> if resource was published and <code>false</code>
- * otherwise
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
- public final boolean publishSingleton(String source, ResourceId resourceId)
+ public final void publishSingleton(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
- return publishSingleton(source, DEFAULT_CHARSET, resourceId);
+ publishSingleton(source, DEFAULT_CHARSET, resourceId, properties);
}
/**
@@ -191,14 +228,17 @@
* @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
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
* @throws UnsupportedCharsetException if <code>charset</code> is unsupported
* @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
- public final boolean publishSingleton(String source, String charset, ResourceId resourceId)
+ public final void publishSingleton(String source, String charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties)
{
- return publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId);
+ publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties);
}
/**
@@ -222,31 +262,33 @@
* otherwise, e.g. because there is not resource corresponded to
* supplied <code>resourceId</code>
*/
- public boolean unpublishResource(ResourceId resourceId)
+ public ObjectFactory<AbstractResourceDescriptor> unpublishResource(ResourceId resourceId)
{
- Class<?> clazz = resources.get(resourceId);
- boolean answ = false;
- if (clazz != null)
+ String path = resources.get(resourceId);
+ if (path == null)
{
- answ = binder.unbind(clazz);
+ return null;
}
- if (answ)
+ ObjectFactory<AbstractResourceDescriptor> resource = binder.removeResource(path);
+ if (resource != null)
{
resources.remove(resourceId);
}
- return answ;
+ return resource;
}
- private boolean publishPerRequest(String source, Charset charset, ResourceId resourceId)
+ private void publishPerRequest(String source, Charset charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties)
{
byte[] bytes = source.getBytes(charset);
- return publishPerRequest(new ByteArrayInputStream(bytes), resourceId);
+ publishPerRequest(new ByteArrayInputStream(bytes), resourceId, properties);
}
- private boolean publishSingleton(String source, Charset charset, ResourceId resourceId)
+ private void publishSingleton(String source, Charset charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties)
{
byte[] bytes = source.getBytes(charset);
- return publishSingleton(new ByteArrayInputStream(bytes), resourceId);
+ publishSingleton(new ByteArrayInputStream(bytes), resourceId, properties);
}
/**
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-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyContextParamTest.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -58,7 +58,7 @@
assertEquals(0, binder.getSize());
assertEquals(0, groovyPublisher.resources.size());
- groovyPublisher.publishPerRequest(script, new BaseResourceId("g1"));
+ groovyPublisher.publishPerRequest(script, new BaseResourceId("g1"), null);
assertEquals(1, binder.getSize());
assertEquals(1, groovyPublisher.resources.size());
Modified: 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 2010-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyDependenciesTest.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -48,7 +48,7 @@
public void testDependency() throws Exception
{
- groovyPublisher.publishPerRequest(script, new BaseResourceId("GMain1"));
+ groovyPublisher.publishPerRequest(script, new BaseResourceId("GMain1"), null);
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
ContainerResponse resp = launcher.service("GET", "/a", "", null, null, writer, null);
assertEquals(200, resp.getStatus());
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-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovyExoComponentTest.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -67,9 +67,9 @@
assertEquals(0, groovyPublisher.resources.size());
if (singleton)
- groovyPublisher.publishSingleton(script, resourceId);
+ groovyPublisher.publishSingleton(script, resourceId, null);
else
- groovyPublisher.publishPerRequest(script, resourceId);
+ groovyPublisher.publishPerRequest(script, resourceId, null);
assertEquals(1, binder.getSize());
assertEquals(1, groovyPublisher.resources.size());
Modified: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySecureRestrictionTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySecureRestrictionTest.java 2010-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySecureRestrictionTest.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -45,7 +45,7 @@
public void testReadSystemPropertyFail() throws Exception
{
- groovyPublisher.publishPerRequest(script, new BaseResourceId("g1"));
+ groovyPublisher.publishPerRequest(script, new BaseResourceId("g1"), null);
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
ContainerResponse resp = launcher.service("GET", "/a/b", "", null, null, writer, null);
assertEquals(500, 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-07-09 13:37:30 UTC (rev 2761)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/groovy/GroovySimpleTest.java 2010-07-09 13:39:29 UTC (rev 2762)
@@ -60,12 +60,13 @@
assertEquals(0, groovyPublisher.resources.size());
if (singleton)
- groovyPublisher.publishSingleton(script, resourceId);
+ groovyPublisher.publishSingleton(script, resourceId, null);
else
- groovyPublisher.publishPerRequest(script, resourceId);
+ groovyPublisher.publishPerRequest(script, resourceId, null);
assertEquals(1, binder.getSize());
assertEquals(1, groovyPublisher.resources.size());
+ assertTrue(groovyPublisher.isPublished(resourceId));
String cs =
binder.getResources().get(0).getObjectModel().getObjectClass().getProtectionDomain().getCodeSource()
More information about the exo-jcr-commits
mailing list