[exo-jcr-commits] exo-jcr SVN: r3701 - in ws/trunk/exo.ws.rest.ext/src: test/java/org/exoplatform/services/rest/ext and 2 other directories.
do-not-reply at jboss.org
do-not-reply at jboss.org
Wed Dec 22 05:15:37 EST 2010
Author: aparfonov
Date: 2010-12-22 05:15:37 -0500 (Wed, 22 Dec 2010)
New Revision: 3701
Added:
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java
ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/HierarchicalPropertyEntityProviderTest.java
Removed:
ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/filter/HierarchicalPropertyEntityProviderTest.java
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
Log:
EXOJCR-1105
Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -0,0 +1,64 @@
+/*
+ * 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 java.net.URL;
+
+/**
+ * Item of Groovy classpath. It may describe source file of folder that contains
+ * source files. If <code>ClassPathEntry</code> point to the folder then like
+ * Java package structure is expected.
+ *
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class ClassPathEntry
+{
+ /** Type of class-path entry. */
+ public enum EntryType {
+ SRC_DIR, FILE
+ }
+
+ /** Type of entry. */
+ private EntryType type;
+
+ /** URL. */
+ private URL path;
+
+ public ClassPathEntry(EntryType type, URL path)
+ {
+ this.type = type;
+ this.path = path;
+ }
+
+ public EntryType getType()
+ {
+ return type;
+ }
+
+ public URL getPath()
+ {
+ return path;
+ }
+
+ public void setPath(URL path)
+ {
+ this.path = path;
+ }
+}
Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/ClassPathEntry.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/DefaultGroovyResourceLoader.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java 2010-12-22 09:24:37 UTC (rev 3700)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/DefaultGroovyResourceLoader.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -22,7 +22,6 @@
import groovy.lang.GroovyResourceLoader;
import java.io.IOException;
-import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
@@ -40,28 +39,29 @@
*/
public class DefaultGroovyResourceLoader implements GroovyResourceLoader
{
+ private static final String[] DEFAULT_SCRIPT_EXTENSIONS = new String[]{".groovy"};
protected URL[] roots;
+ protected URL[] files;
+
// TODO need configurable ?
private int maxEntries = 512;
protected final Map<String, URL> resources;
- public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
+ @SuppressWarnings("serial")
+ public DefaultGroovyResourceLoader(URL[] roots, URL[] files) throws MalformedURLException
{
+ this.files = files;
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];
- }
}
resources = Collections.synchronizedMap(new LinkedHashMap<String, URL>()
{
@@ -72,6 +72,11 @@
});
}
+ public DefaultGroovyResourceLoader(URL[] roots) throws MalformedURLException
+ {
+ this(roots, new URL[0]);
+ }
+
public DefaultGroovyResourceLoader(URL root) throws MalformedURLException
{
this(new URL[]{root});
@@ -82,65 +87,81 @@
*/
public final URL loadGroovySource(String classname) throws MalformedURLException
{
- final String filename = classname.replace('.', '/') + ".groovy";
- try
+ URL resource = null;
+ final String baseName = classname.replace('.', '/');
+ String[] extensions = getScriptExtensions();
+ for (int i = 0; i < extensions.length && resource == null; i++)
{
- return AccessController.doPrivileged(new PrivilegedExceptionAction<URL>()
+ final String ext = extensions[i];
+ try
{
- public URL run() throws Exception
+ resource = AccessController.doPrivileged(new PrivilegedExceptionAction<URL>()
{
- return getResource(filename);
- }
- });
+ public URL run() throws Exception
+ {
+ return getResource(baseName + ext);
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ Throwable cause = e.getCause();
+ if (cause instanceof Error)
+ throw (Error)cause;
+ if (cause instanceof RuntimeException)
+ throw (RuntimeException)cause;
+ throw (MalformedURLException)cause;
+ }
}
- catch (PrivilegedActionException e)
- {
- Throwable cause = e.getCause();
- if (cause instanceof Error)
- throw (Error)cause;
- if (cause instanceof RuntimeException)
- throw (RuntimeException)cause;
- throw (MalformedURLException)cause;
- }
+ return resource;
}
protected URL getResource(String filename) throws MalformedURLException
{
+ URL resource = null;
filename = filename.intern();
- URL resource = null;
synchronized (filename)
{
resource = resources.get(filename);
boolean inCache = resource != null;
- for (URL root : roots)
+ if (inCache && !checkResource(resource))
+ resource = null; // Resource in cache is unreachable.
+ for (int i = 0; i < files.length && resource == null; i++)
{
- if (resource == null)
- {
- resource = new URL(root, filename);
- }
- try
- {
- InputStream script = resource.openStream();
- script.close();
- break;
- }
- catch (IOException e)
- {
- resource = null;
- }
+ URL tmp = files[i];
+ if (tmp.toString().endsWith(filename) && checkResource(tmp))
+ resource = tmp;
}
+ for (int i = 0; i < roots.length && resource == null; i++)
+ {
+ URL tmp = new URL(roots[i], filename);
+ if (checkResource(tmp))
+ resource = tmp;
+ }
if (resource != null)
- {
resources.put(filename, resource);
- }
else if (inCache)
- {
- // Remove from map if resource is unreachable
resources.remove(filename);
- }
}
return resource;
}
+ protected boolean checkResource(URL resource)
+ {
+ try
+ {
+ resource.openStream().close();
+ return true;
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ }
+
+ protected String[] getScriptExtensions()
+ {
+ return DEFAULT_SCRIPT_EXTENSIONS;
+ }
}
Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -0,0 +1,127 @@
+/*
+ * 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.GroovyClassLoader;
+
+import org.exoplatform.services.rest.ext.groovy.ClassPathEntry.EntryType;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Factory of Groovy class loader. It can provide preset GroovyClassLoader
+ * instance or customized instance of GroovyClassLoader able resolve additional
+ * Groovy source files.
+ *
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class GroovyClassLoaderProvider
+{
+ /** Preset default GroovyClassLoader. */
+ private GroovyClassLoader defaultClassLoader;
+
+ /**
+ * Create GroovyClassLoaderProvider that will use specified GroovyClassLoader
+ * as default.
+ *
+ * @param defaultClassLoader GroovyClassLoader
+ */
+ public GroovyClassLoaderProvider(GroovyClassLoader defaultClassLoader)
+ {
+ this.defaultClassLoader = defaultClassLoader;
+ }
+
+ public GroovyClassLoaderProvider()
+ {
+ defaultClassLoader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>()
+ {
+ public GroovyClassLoader run()
+ {
+ return new GroovyClassLoader(getClass().getClassLoader());
+ }
+ });
+ }
+
+ /**
+ * Get default GroovyClassLoader.
+ *
+ * @return default GroovyClassLoader
+ */
+ public GroovyClassLoader getGroovyClassLoader()
+ {
+ return defaultClassLoader;
+ }
+
+ /**
+ * Get customized instance of GroovyClassLoader that able to resolve
+ * additional Groovy source files.
+ *
+ * @param classPath additional Groovy sources
+ * @return GroovyClassLoader
+ * @throws MalformedURLException if any of entries in <code>classPath</code>
+ * has invalid URL.
+ */
+ public GroovyClassLoader getGroovyClassLoader(ClassPathEntry[] classPath) throws MalformedURLException
+ {
+ List<URL> files = new ArrayList<URL>();
+ List<URL> roots = new ArrayList<URL>();
+ for (int i = 0; i < classPath.length; i++)
+ {
+ ClassPathEntry classPathEntry = classPath[i];
+ if (EntryType.SRC_DIR == classPathEntry.getType())
+ {
+ roots.add(classPathEntry.getPath());
+ }
+ else
+ {
+ files.add(classPathEntry.getPath());
+ }
+ }
+ final GroovyClassLoader parent = getGroovyClassLoader();
+ GroovyClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>()
+ {
+ public GroovyClassLoader run()
+ {
+ return new GroovyClassLoader(parent);
+ }
+ });
+ classLoader.setResourceLoader(new DefaultGroovyResourceLoader(roots.toArray(new URL[roots.size()]), files
+ .toArray(new URL[files.size()])));
+ return classLoader;
+ }
+
+ /**
+ * Set default Groovy class loader.
+ *
+ * @param defaultClassLoader default Groovy class loader
+ * @throws NullPointerException if <code>defaultClassLoader == null</code>
+ */
+ public void setGroovyClassLoader(GroovyClassLoader defaultClassLoader)
+ {
+ if (defaultClassLoader == null)
+ throw new NullPointerException("GroovyClassLoader may not be null. ");
+ this.defaultClassLoader = defaultClassLoader;
+ }
+}
Property changes on: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyClassLoaderProvider.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-12-22 09:24:37 UTC (rev 3700)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/groovy/GroovyJaxrsPublisher.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -33,6 +33,7 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.security.PrivilegedAction;
@@ -46,7 +47,7 @@
/**
* Manage via {@link ResourceBinder} Groovy based RESTful services.
- *
+ *
* @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
* @version $Id$
*/
@@ -63,44 +64,58 @@
protected final GroovyScriptInstantiator instantiator;
- protected GroovyClassLoader gcl;
+ protected final GroovyClassLoaderProvider classLoaderProvider;
protected final Map<ResourceId, String> resources = Collections.synchronizedMap(new HashMap<ResourceId, String>());
+ protected GroovyJaxrsPublisher(ResourceBinder binder, GroovyScriptInstantiator instantiator,
+ GroovyClassLoaderProvider classLoaderProvider)
+ {
+ this.binder = binder;
+ this.instantiator = instantiator;
+ this.classLoaderProvider = classLoaderProvider;
+ }
+
/**
* Create GroovyJaxrsPublisher which is able publish per-request and
* singleton resources. Any required dependencies for per-request resource
* injected by {@link PerRequestObjectFactory}, instance of singleton
* resources will be created by {@link GroovyScriptInstantiator}.
- *
+ *
* @param binder resource binder
* @param instantiator instantiate java object from given groovy source
*/
public GroovyJaxrsPublisher(ResourceBinder binder, GroovyScriptInstantiator instantiator)
{
- this.binder = binder;
- this.instantiator = instantiator;
- final ClassLoader cl = getClass().getClassLoader();
- this.gcl = SecurityHelper.doPriviledgedAction(new PrivilegedAction<GroovyClassLoader>()
- {
- public GroovyClassLoader run()
- {
- return new GroovyClassLoader(cl);
- }
- });
+ this(binder, instantiator, new GroovyClassLoaderProvider());
}
/**
* @return get underling groovy class loader
*/
+ @Deprecated
public GroovyClassLoader getGroovyClassLoader()
{
- return gcl;
+ return classLoaderProvider.getGroovyClassLoader();
}
/**
+ * Set groovy class loader.
+ *
+ * @param gcl groovy class loader
+ * @throws NullPointerException if <code>gcl == null</code>
+ */
+ @Deprecated
+ public void setGroovyClassLoader(GroovyClassLoader gcl)
+ {
+ if (gcl == null)
+ throw new NullPointerException("GroovyClassLoader may not be null. ");
+ classLoaderProvider.setGroovyClassLoader(gcl);
+ }
+
+ /**
* Get resource corresponded to specified id <code>resourceId</code> .
- *
+ *
* @param resourceId resource id
* @return resource or <code>null</code>
*/
@@ -127,7 +142,7 @@
/**
* Check is groovy resource with specified id is published or not
- *
+ *
* @param resourceId id of resource to be checked
* @return <code>true</code> if resource is published and <code>false</code>
* otherwise
@@ -139,23 +154,50 @@
/**
* Parse given stream and publish result as per-request RESTful service.
- *
+ *
* @param in stream which contains groovy source code of RESTful service
* @param resourceId id to be assigned to resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
* @throws ResourcePublicationException see
- * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
public void publishPerRequest(final InputStream in, final ResourceId resourceId,
MultivaluedMap<String, String> properties)
{
- Class<?> rc = SecurityHelper.doPriviledgedAction(new PrivilegedAction<Class<?>>()
- {
+ publishPerRequest(in, resourceId, properties, null);
+ }
+
+ /**
+ * Parse given stream and publish result as per-request RESTful service.
+ *
+ * @param in stream which contains groovy source code of RESTful service
+ * @param resourceId id to be assigned to resource
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
+ * @param classPath additional path to Groovy sources
+ * @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
+ */
+ public void publishPerRequest(final InputStream in, final ResourceId resourceId,
+ final MultivaluedMap<String, String> properties, final ClassPathEntry[] classPath)
+ {
+ Class<?> rc = SecurityHelper.doPriviledgedAction(new PrivilegedAction<Class<?>>() {
public Class<?> run()
{
- return gcl.parseClass(createCodeSource(in, resourceId.getId()));
+ try
+ {
+ GroovyClassLoader cl = (classPath == null || classPath.length == 0) //
+ ? classLoaderProvider.getGroovyClassLoader() //
+ : classLoaderProvider.getGroovyClassLoader(classPath);
+ return cl.parseClass(createCodeSource(in, resourceId.getId()));
+ }
+ catch (MalformedURLException e)
+ {
+ throw new ResourcePublicationException(e.getMessage());
+ }
}
});
@@ -166,113 +208,216 @@
/**
* Parse given <code>source</code> and publish result as per-request RESTful
* service.
- *
+ *
* @param source groovy source code of RESTful service
* @param resourceId id to be assigned to resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
* @throws ResourcePublicationException see
- * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
public final void publishPerRequest(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
- publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties);
+ publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, null);
}
/**
* Parse given <code>source</code> and publish result as per-request RESTful
* service.
- *
+ *
* @param source groovy source code of RESTful service
+ * @param resourceId id to be assigned to resource
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
+ * @param classPath additional path to Groovy sources
+ * @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
+ */
+ public final void publishPerRequest(String source, ResourceId resourceId, MultivaluedMap<String, String> properties,
+ ClassPathEntry[] classPath)
+ {
+ publishPerRequest(source, DEFAULT_CHARSET, resourceId, properties, classPath);
+ }
+
+ /**
+ * Parse given <code>source</code> and publish result as per-request 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
+ * default charset will be in use
* @param resourceId id to be assigned to resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>.
+ * <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)}
+ * {@link ResourceBinder#addResource(Class, MultivaluedMap)}
*/
public final void publishPerRequest(String source, String charset, ResourceId resourceId,
MultivaluedMap<String, String> properties)
{
- publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties);
+ publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
+ null);
}
/**
+ * Parse given <code>source</code> and publish result as per-request 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
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
+ * @param classPath additional path to Groovy sources
+ * @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 void publishPerRequest(String source, String charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties, ClassPathEntry[] classPath)
+ {
+ publishPerRequest(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
+ classPath);
+ }
+
+ /**
* Parse given stream and publish result as singleton RESTful service.
- *
+ *
* @param in stream which contains groovy source code of RESTful service
* @param resourceId id to be assigned to resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>
+ * <code>null</code>
* @throws NullPointerException if <code>resourceId == null</code>
* @throws ResourcePublicationException see
- * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
public void publishSingleton(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
- Object r = instantiator.instantiateScript(createCodeSource(in, resourceId.getId()), gcl);
- binder.addResource(r, properties);
- resources.put(resourceId, r.getClass().getAnnotation(Path.class).value());
+ publishSingleton(in, resourceId, properties, null);
}
/**
+ * Parse given stream and publish result as singleton RESTful service.
+ *
+ * @param in stream which contains groovy source code of RESTful service
+ * @param resourceId id to be assigned to resource
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>
+ * @param classPath additional path to Groovy sources
+ * @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
+ */
+ public void publishSingleton(InputStream in, ResourceId resourceId, MultivaluedMap<String, String> properties,
+ ClassPathEntry[] classPath)
+ {
+ Object resource;
+ try
+ {
+ resource =
+ instantiator.instantiateScript(createCodeSource(in, resourceId.getId()),
+ (classPath == null || classPath.length == 0) //
+ ? classLoaderProvider.getGroovyClassLoader() //
+ : classLoaderProvider.getGroovyClassLoader(classPath));
+ }
+ catch (MalformedURLException e)
+ {
+ throw new ResourcePublicationException(e.getMessage());
+ }
+ binder.addResource(resource, properties);
+ resources.put(resourceId, resource.getClass().getAnnotation(Path.class).value());
+ }
+
+ /**
* Parse given <code>source</code> and publish result as singleton RESTful
* service.
- *
+ *
* @param source groovy source code of RESTful service
* @param resourceId name of resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>.
+ * <code>null</code>.
* @throws NullPointerException if <code>resourceId == null</code>
* @throws ResourcePublicationException see
- * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
public final void publishSingleton(String source, ResourceId resourceId, MultivaluedMap<String, String> properties)
{
- publishSingleton(source, DEFAULT_CHARSET, resourceId, properties);
+ publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, null);
}
/**
* Parse given <code>source</code> and publish result as singleton RESTful
* service.
- *
+ *
* @param source groovy source code of RESTful service
+ * @param resourceId name of resource
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
+ * @param classPath additional path to Groovy sources
+ * @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
+ */
+ public final void publishSingleton(String source, ResourceId resourceId, MultivaluedMap<String, String> properties,
+ ClassPathEntry[] classPath)
+ {
+ publishSingleton(source, DEFAULT_CHARSET, resourceId, properties, classPath);
+ }
+
+ /**
+ * Parse given <code>source</code> and publish result as singleton 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
+ * default charset will be in use
* @param resourceId name of resource
* @param properties optional resource properties. This parameter may be
- * <code>null</code>.
+ * <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)}
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
public final void publishSingleton(String source, String charset, ResourceId resourceId,
MultivaluedMap<String, String> properties)
{
- publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties);
+ publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
+ null);
}
/**
- * Set groovy class loader.
- *
- * @param gcl groovy class loader
- * @throws NullPointerException if <code>gcl == null</code>
+ * Parse given <code>source</code> and publish result as singleton 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
+ * @param properties optional resource properties. This parameter may be
+ * <code>null</code>.
+ * @param classPath additional path to Groovy sources
+ * @throws UnsupportedCharsetException if <code>charset</code> is unsupported
+ * @throws NullPointerException if <code>resourceId == null</code>
+ * @throws ResourcePublicationException see
+ * {@link ResourceBinder#addResource(Object, MultivaluedMap)}
*/
- public void setGroovyClassLoader(GroovyClassLoader gcl)
+ public final void publishSingleton(String source, String charset, ResourceId resourceId,
+ MultivaluedMap<String, String> properties, ClassPathEntry[] classPath)
{
- if (gcl == null)
- throw new NullPointerException("GroovyClassLoader may not be null.");
- this.gcl = gcl;
+ publishSingleton(source, charset == null ? DEFAULT_CHARSET : Charset.forName(charset), resourceId, properties,
+ classPath);
}
/**
* 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
@@ -294,31 +439,30 @@
}
private void publishPerRequest(String source, Charset charset, ResourceId resourceId,
- MultivaluedMap<String, String> properties)
+ MultivaluedMap<String, String> properties, ClassPathEntry[] classPath)
{
byte[] bytes = source.getBytes(charset);
publishPerRequest(new ByteArrayInputStream(bytes), resourceId, properties);
}
private void publishSingleton(String source, Charset charset, ResourceId resourceId,
- MultivaluedMap<String, String> properties)
+ MultivaluedMap<String, String> properties, ClassPathEntry[] classPath)
{
byte[] bytes = source.getBytes(charset);
- publishSingleton(new ByteArrayInputStream(bytes), resourceId, properties);
+ publishSingleton(new ByteArrayInputStream(bytes), resourceId, properties, classPath);
}
/**
* Create {@link GroovyCodeSource} from given stream and name. Code base
* 'file:/groovy/script/jaxrs' will be used.
- *
+ *
* @param in groovy source code stream
* @param name code source name
* @return GroovyCodeSource
*/
protected GroovyCodeSource createCodeSource(final InputStream in, final String name)
{
- GroovyCodeSource gcs = SecurityHelper.doPriviledgedAction(new PrivilegedAction<GroovyCodeSource>()
- {
+ GroovyCodeSource gcs = SecurityHelper.doPriviledgedAction(new PrivilegedAction<GroovyCodeSource>() {
public GroovyCodeSource run()
{
return new GroovyCodeSource(in, name, "/groovy/script/jaxrs");
Deleted: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/filter/HierarchicalPropertyEntityProviderTest.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/filter/HierarchicalPropertyEntityProviderTest.java 2010-12-22 09:24:37 UTC (rev 3700)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/filter/HierarchicalPropertyEntityProviderTest.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -1,21 +0,0 @@
-package org.exoplatform.services.rest.ext.filter;
-
-import junit.framework.TestCase;
-
-import org.exoplatform.common.util.HierarchicalProperty;
-import org.exoplatform.services.rest.ext.provider.HierarchicalPropertyEntityProvider;
-
-import java.io.ByteArrayInputStream;
-
-public class HierarchicalPropertyEntityProviderTest extends TestCase
-{
- public void testRequestBodyXMLParsing() throws Exception
- {
- String s = "\n<root>\n <l1>\n\t<l2>hel\nlo</l2>\n </l1> \n</root>\n";
- System.out.println(s);
- HierarchicalProperty hp =
- new HierarchicalPropertyEntityProvider().readFrom(HierarchicalProperty.class, null, null, null, null,
- new ByteArrayInputStream(s.getBytes()));
- assertTrue(hp.getChild(0).getChild(0).getValue().equals("hel\nlo"));
- }
-}
\ No newline at end of file
Copied: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/HierarchicalPropertyEntityProviderTest.java (from rev 3678, ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/filter/HierarchicalPropertyEntityProviderTest.java)
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/HierarchicalPropertyEntityProviderTest.java (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/HierarchicalPropertyEntityProviderTest.java 2010-12-22 10:15:37 UTC (rev 3701)
@@ -0,0 +1,21 @@
+package org.exoplatform.services.rest.ext.provider;
+
+import junit.framework.TestCase;
+
+import org.exoplatform.common.util.HierarchicalProperty;
+import org.exoplatform.services.rest.ext.provider.HierarchicalPropertyEntityProvider;
+
+import java.io.ByteArrayInputStream;
+
+public class HierarchicalPropertyEntityProviderTest extends TestCase
+{
+ public void testRequestBodyXMLParsing() throws Exception
+ {
+ String s = "\n<root>\n <l1>\n\t<l2>hel\nlo</l2>\n </l1> \n</root>\n";
+ //System.out.println(s);
+ HierarchicalProperty hp =
+ new HierarchicalPropertyEntityProvider().readFrom(HierarchicalProperty.class, null, null, null, null,
+ new ByteArrayInputStream(s.getBytes()));
+ assertTrue(hp.getChild(0).getChild(0).getValue().equals("hel\nlo"));
+ }
+}
\ No newline at end of file
Property changes on: ws/trunk/exo.ws.rest.ext/src/test/java/org/exoplatform/services/rest/ext/provider/HierarchicalPropertyEntityProviderTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the exo-jcr-commits
mailing list