weaver 2004/04/15 08:33:13
Added: components/cm/src/java/org/apache/jetspeed/components/pico/groovy
ParameterReader.java GroovyComponentAdapter.java
components/cm/src/test/org/apache/jetspeed/components
MockComponent.groovy
TestGroovyComponentAdapter.java MockComponent.java
Log:
Created a GroovyComponentAdapter for Pico. It works almost indentically to the
BeanShellComponentAdapter save
for the fact that it adds a new object, ParameterReader, to make reading out the
passed in parameters easier.
Revision Changes Path
1.1
jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/pico/groovy/ParameterReader.java
Index: ParameterReader.java
===================================================================
/*
* Created on Apr 15, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.apache.jetspeed.components.pico.groovy;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver</a>
*
*/
public class ParameterReader
{
private PicoContainer fieldContainer;
private Parameter[] fieldParameters;
public ParameterReader(Parameter[] inParameters, PicoContainer inContainer)
{
fieldContainer = inContainer;
fieldParameters = inParameters;
}
public Object getValue(int index, Class expectedType)
{
ComponentAdapter adp = fieldParameters[index].resolveAdapter(fieldContainer,
expectedType);
if(adp != null)
{
return adp.getComponentInstance();
}
else
{
return null;
}
}
}
1.1
jakarta-jetspeed-2/components/cm/src/java/org/apache/jetspeed/components/pico/groovy/GroovyComponentAdapter.java
Index: GroovyComponentAdapter.java
===================================================================
/*
* Created on Apr 13, 2004
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
package org.apache.jetspeed.components.pico.groovy;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.syntax.SyntaxException;
import org.picocontainer.Parameter;
import org.picocontainer.PicoInitializationException;
import org.picocontainer.PicoIntrospectionException;
import org.picocontainer.defaults.AbstractComponentAdapter;
import org.picocontainer.defaults.UnsatisfiableDependenciesException;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver </a>
*
*/
public class GroovyComponentAdapter extends AbstractComponentAdapter
{
private static final String GROOVY_EXTENSION = ".groovy";
private final Parameter[] parameters;
private Script groovyScript;
private boolean isSingleton;
private Object instance;
public GroovyComponentAdapter( Object componentKey, Class
componentImplementation, Parameter[] parameters, boolean isSingleton )
{
super(componentKey, componentImplementation);
this.parameters = parameters;
this.isSingleton = isSingleton;
}
/*
* (non-Javadoc)
*
* @see org.picocontainer.ComponentAdapter#verify()
*/
public void verify() throws UnsatisfiableDependenciesException
{
}
/*
* (non-Javadoc)
*
* @see org.picocontainer.ComponentAdapter#getComponentInstance()
*/
public Object getComponentInstance() throws PicoInitializationException,
PicoIntrospectionException
{
try
{
if(isSingleton && instance != null)
{
return instance;
}
Binding binding = new Binding();
binding.setVariable("adapter", this);
binding.setVariable("picoContainer", getContainer());
binding.setVariable("componentKey", getComponentKey());
binding.setVariable("componentImplementation",
getComponentImplementation());
binding.setVariable("parameters", parameters != null ?
Arrays.asList(parameters) : Collections.EMPTY_LIST);
binding.setVariable("parameterReader", parameters != null ? new
ParameterReader(parameters, getContainer()) : null);
String scriptPath = "/" +
getComponentImplementation().getName().replace('.', '/') + GROOVY_EXTENSION;
InputStream scriptIs =
getComponentImplementation().getResourceAsStream(scriptPath);
if (scriptIs == null)
{
throw new PicoInitializationException("Couldn't load script at path
" + scriptPath);
}
GroovyClassLoader loader = new
GroovyClassLoader(Thread.currentThread().getContextClassLoader());
Class scriptClass = loader.parseClass(scriptIs);
groovyScript = InvokerHelper.createScript(scriptClass, null);
groovyScript.setBinding(binding);
instance = groovyScript.run();
if(instance == null)
{
instance = groovyScript.getBinding().getVariable("instance");
}
return instance;
}
catch (SyntaxException e)
{
throw new PicoInitializationException(e);
}
catch (IOException e)
{
throw new PicoInitializationException(e);
}
}
}
1.1
jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/MockComponent.groovy
Index: MockComponent.groovy
===================================================================
import org.apache.jetspeed.components.MockComponent
v1 = parameterReader.getValue(0, Integer).intValue()
v2 = parameterReader.getValue(1, String)
return new MockComponent(v1, v2)
1.1
jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/TestGroovyComponentAdapter.java
Index: TestGroovyComponentAdapter.java
===================================================================
/*
* Created on Apr 15, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.apache.jetspeed.components;
import org.apache.jetspeed.components.pico.groovy.GroovyComponentAdapter;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.defaults.ConstantParameter;
import org.picocontainer.defaults.DefaultPicoContainer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver</a>
*
*/
public class TestGroovyComponentAdapter extends TestCase
{
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestGroovyComponentAdapter.class);
}
public void testGroovyAdapter() throws Exception
{
MutablePicoContainer container = new DefaultPicoContainer();
Parameter v1 = new ConstantParameter(new Integer(69));
Parameter v2 = new ConstantParameter("Some Value");
ComponentAdapter adp1 = new GroovyComponentAdapter("multipleObjects",
MockComponent.class, new Parameter[] {v1, v2}, false);
ComponentAdapter adp2 = new GroovyComponentAdapter("singletonObject",
MockComponent.class, new Parameter[] {v1, v2}, true);
container.registerComponent(adp1);
container.registerComponent(adp2);
container.start();
MockComponent comp1 = (MockComponent)
container.getComponentInstance("multipleObjects");
MockComponent comp2 = (MockComponent)
container.getComponentInstance("multipleObjects");
assertNotNull(comp1);
assertNotNull(comp2);
assertTrue(comp1 != comp2);
beanTest(comp1, 69, "Some Value");
beanTest(comp2, 69, "Some Value");
MockComponent comp3 = (MockComponent)
container.getComponentInstance("singletonObject");
MockComponent comp4 = (MockComponent)
container.getComponentInstance("singletonObject");
assertNotNull(comp3);
assertNotNull(comp4);
assertTrue(comp3 == comp4);
beanTest(comp3, 69, "Some Value");
beanTest(comp4, 69, "Some Value");
}
protected void beanTest(MockComponent comp, int v1, String v2)
{
assertEquals(comp.getValue1(), 69);
assertEquals(comp.getValue2(), "Some Value");
}
}
1.1
jakarta-jetspeed-2/components/cm/src/test/org/apache/jetspeed/components/MockComponent.java
Index: MockComponent.java
===================================================================
/*
* Created on Apr 15, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package org.apache.jetspeed.components;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver</a>
*
*/
public class MockComponent
{
private int fieldValue1;
private String fieldValue2;
public MockComponent(int inValue1, String inValue2)
{
fieldValue1 = inValue1;
fieldValue2 = inValue2;
}
/**
* @return Returns the value1.
*/
protected int getValue1()
{
return fieldValue1;
}
/**
* @param value1 The value1 to set.
*/
protected void setValue1( int value1 )
{
fieldValue1 = value1;
}
/**
* @return Returns the value2.
*/
protected String getValue2()
{
return fieldValue2;
}
/**
* @param value2 The value2 to set.
*/
protected void setValue2( String value2 )
{
fieldValue2 = value2;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]