Author: aparfonov
Date: 2010-05-12 02:25:23 -0400 (Wed, 12 May 2010)
New Revision: 2360
Modified:
core/trunk/exo.core.component.script.groovy/src/main/java/org/exoplatform/services/script/groovy/GroovyScriptInstantiator.java
Log:
EXOJCR-721: add method with GroovyClassLoader as parameter
Modified:
core/trunk/exo.core.component.script.groovy/src/main/java/org/exoplatform/services/script/groovy/GroovyScriptInstantiator.java
===================================================================
---
core/trunk/exo.core.component.script.groovy/src/main/java/org/exoplatform/services/script/groovy/GroovyScriptInstantiator.java 2010-05-11
11:19:08 UTC (rev 2359)
+++
core/trunk/exo.core.component.script.groovy/src/main/java/org/exoplatform/services/script/groovy/GroovyScriptInstantiator.java 2010-05-12
06:25:23 UTC (rev 2360)
@@ -50,7 +50,7 @@
{
/** Our logger. */
- private static final Log LOG =
ExoLogger.getLogger("exo.core.component.script.groovy.GroovyScriptInstantiator");
+ private static final Log LOG = ExoLogger.getLogger(GroovyScriptInstantiator.class);
/**
* eXo Container.
@@ -71,11 +71,11 @@
/**
* Load script from given address.
- *
+ *
* @param spec the resource's address.
* @return the object created from groovy script.
* @throws MalformedURLException if parameter <code>url</code> have wrong
- * format.
+ * format.
* @throws IOException if can't load script from given
<code>url</code>.
* @see GroovyScriptInstantiator#instantiateScript(URL)
* @see GroovyScriptInstantiator#instantiateScript(InputStream)
@@ -87,7 +87,7 @@
/**
* Load script from given address.
- *
+ *
* @param url the resource's address.
* @return the object created from groovy script.
* @throws IOException if can't load script from given
<code>url</code>.
@@ -101,10 +101,11 @@
/**
* Parse given stream, the stream must represents groovy script.
- *
+ *
* @param stream the stream represented groovy script.
* @return the object created from groovy script.
- * @throws IOException if stream can't be parsed or object can't be created.
+ * @throws IOException if stream can't be parsed or object can't be created
+ * cause to illegal content of stream
*/
public Object instantiateScript(InputStream stream) throws IOException
{
@@ -113,12 +114,13 @@
/**
* Parse given stream, the stream must represents groovy script.
- *
+ *
* @param stream the stream represented groovy script.
* @param name script name is null or empty string that groovy completer will
- * use default name
+ * use default name
* @return the object created from groovy script.
- * @throws IOException if stream can't be parsed or object can't be created.
+ * @throws IOException if stream can't be parsed or object can't be created
+ * cause to illegal content of stream
*/
public Object instantiateScript(InputStream stream, String name) throws IOException
{
@@ -133,13 +135,39 @@
{
loader = new GroovyClassLoader();
}
+ return instantiateScript(stream, name, loader);
+ }
+
+ /**
+ * Parse given stream, the stream must represents groovy script and use given
+ * class-loader. If <code>loader == null</code> then
+ * {@link groovy.lang.GroovyClassLoader} will be is use.
+ *
+ * @param stream the stream represented groovy script.
+ * @param name script name is null or empty string that groovy completer will
+ * use default name
+ * @param loader GroovyClassLoader or <code>null</code>
+ * @return the object created from groovy script.
+ * @throws IOException if stream can't be parsed or object can't be created
+ * cause to illegal content of stream
+ */
+ public Object instantiateScript(InputStream stream, String name, GroovyClassLoader
loader) throws IOException
+ {
+ if (loader == null)
+ {
+ loader = new GroovyClassLoader();
+ }
Class<?> clazz = null;
try
{
if (name != null && name.length() > 0)
+ {
clazz = loader.parseClass(stream, name);
+ }
else
+ {
clazz = loader.parseClass(stream);
+ }
}
catch (CompilationFailedException e)
{
@@ -162,7 +190,7 @@
/**
* Created object from given class, if class has parameters in constructor,
* then this parameters will be searched in container.
- *
+ *
* @param clazz java-groovy class
*/
private Object createObject(Class<?> clazz) throws Exception
@@ -180,7 +208,9 @@
{
Class<?>[] parameterTypes = c.getParameterTypes();
if (parameterTypes.length == 0)
+ {
return c.newInstance();
+ }
List<Object> parameters = new
ArrayList<Object>(parameterTypes.length);
@@ -188,7 +218,9 @@
{
Object param = container.getComponentInstanceOfType(parameterType);
if (param == null)
+ {
continue l;
+ }
parameters.add(param);
}
@@ -214,9 +246,13 @@
int c1 = constructor1.getParameterTypes().length;
int c2 = constructor2.getParameterTypes().length;
if (c1 < c2)
+ {
return 1;
+ }
if (c1 > c2)
+ {
return -1;
+ }
return 0;
}
@@ -227,7 +263,10 @@
if (plugin instanceof GroovyScriptJarJarPlugin)
{
GroovyScriptJarJarPlugin jarjarPlugin = (GroovyScriptJarJarPlugin)plugin;
- LOG.debug("Add mapping to groovy instantiator:" +
jarjarPlugin.getMapping());
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("Add mapping to groovy instantiator:" +
jarjarPlugin.getMapping());
+ }
mapping.putAll(jarjarPlugin.getMapping());
}
}
Show replies by date