[CVS nano] update
Paul Hammant <paul-yCVjj/[email protected]> Mon, 17 Nov 2003 15:05:19 -0600
| Newsgroups | gmane.comp.java.nanocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit in nano on MAIN
rhino/src/java/org/picoextras/rhino/DefaultNanoRhinoScriptable.java +94 added 1.1
/NanoRhinoManager.java +54 added 1.1
/NanoRhinoScriptable.java +18 added 1.1
/NanoRhinoScriptableHolder.java +17 added 1.1
rhino/src/test/org/picoextras/rhino/DefaultNanoRhinoScriptableTestCase.java +109 added 1.1
servlet/src/java/org/picoextras/servlet/ApplicationScopeObjectReference.java +39 added 1.1
/KeyConstants.java +20 added 1.1
/RequestScopeObjectReference.java +39 added 1.1
/ServletContainerListener.java +93 added 1.1
/ServletRequestContainerLauncher.java +62 added 1.1
/SessionScopeObjectReference.java +39 added 1.1
testmodel/src/java/org/picoextras/testmodel/DefaultWebServerConfig.java +23 added 1.1
/FlintstonesImpl.java +20 added 1.1
/FredImpl.java +20 added 1.1
/MockComponent.java +28 added 1.1
/MockComponentImpl.java +49 added 1.1
/ResourceBundleWebServerConfig.java +43 added 1.1
/ThingThatTakesParamsInConstructor.java +15 added 1.1
/WebServer.java +14 added 1.1
/WebServerConfig.java +25 added 1.1
/WebServerConfigBean.java +36 added 1.1
/WebServerConfigComp.java +31 added 1.1
/WebServerConfigStub.java +28 added 1.1
/WebServerImpl.java +23 added 1.1
/Wilma.java +15 added 1.1
/WilmaImpl.java +26 added 1.1
type3msg/src/java/org/picoextras/type3msg/MulticastingPicoException.java +16 added 1.1
/MulticastingProxy.java +48 added 1.1
/RoundRobinMulticastingProxy.java +27 added 1.1
/Type3MsgPicoContainer.java +35 added 1.1
type3msg/src/java/org/picoextras/type3msg/sample/Controller.java +29 added 1.1
/Database.java +14 added 1.1
/PretendDatabase.java +22 added 1.1
/UnitOfWork.java +20 added 1.1
/Worker.java +12 added 1.1
/WorkerDoer.java +29 added 1.1
/WorkerLogger.java +20 added 1.1
type3msg/src/test/org/picoextras/typ3msg/Kissable.java +5 added 1.1
/TestBoy.java +20 added 1.1
/TestGirl.java +20 added 1.1
/Type3MsgPicoContainerTest.java +132 added 1.1
webwork2/src/java/org/picoextras/webwork2/PicoActionInvocation.java +60 added 1.1
/PicoActionProxyFactory.java +34 added 1.1
/PicoServletDispatcher.java +43 added 1.1
/WebWork2ContainerListener.java +25 added 1.1
webwork/src/java/org/picoextras/webwork/PicoActionFactory.java +78 added 1.1
/PicoServletDispatcher.java +42 added 1.1
/WebWorkActionFactory.java +45 added 1.1
xml/src/java/org/picoextras/xml/BeanXmlPseudoComponentFactory.java +70 added 1.1
/DefaultXmlFrontEnd.java +195 added 1.1
/EmptyXmlCompositionException.java +19 added 1.1
/ParameterXmlFrontEnd.java +223 added 1.1
/XStreamXmlPseudoComponentFactory.java +27 added 1.1
/XmlFrontEnd.java +36 added 1.1
/XmlFrontEndException.java +37 added 1.1
/XmlPseudoComponentFactory.java +18 added 1.1
xml/src/test/org/picoextras/xml/BeanXmlPseudoComponentFactoryTestCase.java +47 added 1.1
/DefaultXmlFrontEndTestCase.java +155 added 1.1
/ParameterXmlFrontEndTestCase.java +122 added 1.1
/TestPseudoComp.java +30 added 1.1
/XStreamXmlPseudoComponentFactoryTestCase.java +48 added 1.1
+2783
61 added files
- TODO we need to have config elements parsing here, and register with params action...
update
----------
nano /rhino /src /java /org /picoextras /rhino
DefaultNanoRhinoScriptable.java added at 1.1
diff -N DefaultNanoRhinoScriptable.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DefaultNanoRhinoScriptable.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,94 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.rhino;
+
+import org.mozilla.javascript.*;
+import org.picoextras.reflection.DefaultReflectionFrontEnd;
+import org.picoextras.reflection.ReflectionFrontEnd;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.picocontainer.defaults.ComponentAdapterFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+public class DefaultNanoRhinoScriptable extends ScriptableObject implements NanoRhinoScriptable {
+
+ private ReflectionFrontEnd reflectionFrontEnd;
+
+ public MutablePicoContainer getPicoContainer() {
+ return reflectionFrontEnd.getPicoContainer();
+ }
+
+ public String getClassName() {
+ return "NanoRhinoScriptable";
+ }
+
+ public static Object jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) {
+ ReflectionFrontEnd reflectionFrontEnd = null;
+ if (args.length == 1) {
+ Object arg = ((NativeJavaObject) args[0]).unwrap();
+ if (arg instanceof ComponentAdapterFactory) {
+ reflectionFrontEnd = new DefaultReflectionFrontEnd(new DefaultPicoContainer((ComponentAdapterFactory) arg));
+ } else if (arg instanceof MutablePicoContainer) {
+ reflectionFrontEnd = new DefaultReflectionFrontEnd((MutablePicoContainer) arg);
+ } else if (arg instanceof ReflectionFrontEnd) {
+ reflectionFrontEnd = (ReflectionFrontEnd) arg;
+ } else {
+ throw new IllegalArgumentException("Argument passed in should be a ComponentAdaptorFactory or a MutablePicoContainer");
+ }
+ } else {
+ reflectionFrontEnd = new DefaultReflectionFrontEnd();
+ }
+ DefaultNanoRhinoScriptable rhino = new DefaultNanoRhinoScriptable();
+ rhino.reflectionFrontEnd = reflectionFrontEnd;
+ return rhino;
+ }
+
+ public static void jsFunction_addComponent(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ClassNotFoundException {
+ DefaultNanoRhinoScriptable rhino = (DefaultNanoRhinoScriptable) thisObj;
+ if (args.length == 1) {
+ rhino.reflectionFrontEnd.registerComponentImplementation((String) args[0]);
+ } else if (args.length == 2) {
+ rhino.reflectionFrontEnd.registerComponent((String) args[0], (String) args[1]);
+ }
+ }
+
+ public static void jsFunction_addComponentWithClassKey(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ClassNotFoundException {
+ DefaultNanoRhinoScriptable rhino = (DefaultNanoRhinoScriptable) thisObj;
+ rhino.reflectionFrontEnd.registerComponentWithClassKey((String) args[0], (String) args[1]);
+ }
+
+ public static void jsFunction_addComponentInstance(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ClassNotFoundException {
+ DefaultNanoRhinoScriptable rhino = (DefaultNanoRhinoScriptable) thisObj;
+ MutablePicoContainer picoContainer = rhino.reflectionFrontEnd.getPicoContainer();
+ picoContainer.registerComponentInstance(((NativeJavaObject) args[0]).unwrap());
+ }
+
+ public static void jsFunction_addContainer(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
+ DefaultNanoRhinoScriptable parent = (DefaultNanoRhinoScriptable) thisObj;
+ DefaultNanoRhinoScriptable child = (DefaultNanoRhinoScriptable) args[0];
+ MutablePicoContainer childContainer = child.reflectionFrontEnd.getPicoContainer();
+ MutablePicoContainer parentContainer = parent.reflectionFrontEnd.getPicoContainer();
+ parentContainer.addChild(childContainer);
+ }
+
+ public static void jsFunction_addFileClassPathJar(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException {
+ DefaultNanoRhinoScriptable rhino = (DefaultNanoRhinoScriptable) thisObj;
+ ReflectionFrontEnd rfe = rhino.reflectionFrontEnd;
+ String path = (String) args[0];
+ File file = new File(path);
+ if (!file.exists()) {
+ throw new IOException(file.getAbsolutePath() + " doesn't exist");
+ }
+ rfe.addClassLoaderURL(file.toURL());
+ }
+
+}
----------
nano /rhino /src /java /org /picoextras /rhino
NanoRhinoManager.java added at 1.1
diff -N NanoRhinoManager.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NanoRhinoManager.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,54 @@
+package org.picoextras.rhino;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.JavaScriptException;
+import org.mozilla.javascript.ScriptableObject;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.PicoContainer;
+
+import java.io.Reader;
+import java.io.IOException;
+
+public class NanoRhinoManager {
+ public PicoContainer execute(Class scriptableClass, Reader script) throws PicoCompositionException, IOException {
+ Context cx = Context.enter();
+ Scriptable scriptable;
+ try {
+ scriptable = cx.initStandardObjects(null);
+
+ defineClass(scriptable, scriptableClass);
+
+ NanoRhinoScriptableHolder nanoHolder = new NanoRhinoScriptableHolder();
+ Scriptable jsArgs = Context.toObject(nanoHolder, scriptable);
+ scriptable.put("nano", scriptable, jsArgs);
+
+ cx.evaluateReader(scriptable, script, "<cmd>", 1, null);
+
+ return nanoHolder.getNanoRhinoScriptable().getPicoContainer();
+
+ } catch (final JavaScriptException e) {
+ e.printStackTrace();
+ throw new PicoCompositionException() {
+ public String getMessage() {
+ return "JavaScriptException : " + e.getMessage();
+ }
+ };
+ } finally {
+ Context.exit();
+ }
+
+ }
+
+ private void defineClass(Scriptable scriptable, Class rhinoClass) throws PicoCompositionException {
+ try {
+ ScriptableObject.defineClass(scriptable, rhinoClass);
+ } catch (final Exception e) {
+ throw new PicoCompositionException() {
+ public String getMessage() {
+ return "JavaScriptException : " + e.getMessage();
+ }
+ };
+ }
+ }
+}
----------
nano /rhino /src /java /org /picoextras /rhino
NanoRhinoScriptable.java added at 1.1
diff -N NanoRhinoScriptable.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NanoRhinoScriptable.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,18 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.rhino;
+
+import org.picocontainer.MutablePicoContainer;
+import org.mozilla.javascript.Scriptable;
+
+public interface NanoRhinoScriptable extends Scriptable {
+
+ MutablePicoContainer getPicoContainer();
+
+}
----------
nano /rhino /src /java /org /picoextras /rhino
NanoRhinoScriptableHolder.java added at 1.1
diff -N NanoRhinoScriptableHolder.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ NanoRhinoScriptableHolder.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,17 @@
+package org.picoextras.rhino;
+
+public class NanoRhinoScriptableHolder {
+
+ private NanoRhinoScriptable nanoRhinoScriptable;
+
+ public NanoRhinoScriptable getNanoRhinoScriptable() {
+ return nanoRhinoScriptable;
+ }
+
+ public void setNanoRhinoScriptable(NanoRhinoScriptable nanoRhinoScriptable) {
+ if (this.nanoRhinoScriptable == null) {
+ this.nanoRhinoScriptable = nanoRhinoScriptable;
+ }
+ }
+
+}
----------
nano /rhino /src /test /org /picoextras /rhino
DefaultNanoRhinoScriptableTestCase.java added at 1.1
diff -N DefaultNanoRhinoScriptableTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DefaultNanoRhinoScriptableTestCase.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,109 @@
+package org.picoextras.rhino;
+
+import junit.framework.TestCase;
+import org.picoextras.testmodel.WebServer;
+import org.picoextras.testmodel.WebServerImpl;
+import org.picoextras.testmodel.WebServerConfig;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.PicoContainer;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Collection;
+
+public class DefaultNanoRhinoScriptableTestCase extends TestCase {
+
+ public void testInstantiateBasicRhinoScriptable() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ Reader script = new StringReader("" +
+ "var parentContainer = new NanoRhinoScriptable();\n" +
+ "with (parentContainer) {\n" +
+ " addComponent('"+ FooTestComp.class.getName() +"');\n" +
+ "}\n" +
+ "nano.setNanoRhinoScriptable(parentContainer)\n"
+ );
+
+ PicoContainer rootContainer = new NanoRhinoManager().execute(DefaultNanoRhinoScriptable.class, script);
+
+ assertEquals("Instantiated Object should be a FooTestComp", FooTestComp.class,
+ rootContainer.getComponentInstances().get(0).getClass());
+ }
+
+ public void testInstantiateWithBespokeComponentAdaptor() throws IOException, ClassNotFoundException, PicoCompositionException {
+
+ Reader script = new StringReader("" +
+ "var caf = new Packages.org.picocontainer.extras.ImplementationHidingComponentAdapterFactory();\n" +
+ "var parentContainer = new NanoRhinoScriptable(caf);\n" +
+ "with (parentContainer) {\n" +
+ " addComponentWithClassKey('org.picoextras.testmodel.WebServerConfig','org.picoextras.testmodel.DefaultWebServerConfig');\n" +
+ " addComponentWithClassKey('org.picoextras.testmodel.WebServer','org.picoextras.testmodel.WebServerImpl');\n" +
+ "}\n" +
+ "nano.setNanoRhinoScriptable(parentContainer)\n"
+ );
+
+ PicoContainer rootContainer = new NanoRhinoManager().execute(DefaultNanoRhinoScriptable.class, script);
+
+ Object wsc = rootContainer.getComponentInstance(WebServerConfig.class);
+ Object ws = rootContainer.getComponentInstance(WebServer.class);
+
+ assertTrue(ws instanceof WebServer);
+ assertFalse(ws instanceof WebServerImpl);
+
+ ws = rootContainer.getComponentInstances().get(1);
+
+ assertTrue(ws instanceof WebServer);
+
+ assertFalse(ws instanceof WebServerImpl);
+
+ assertEquals("ClassLoader should be the same for both components",ws.getClass().getClassLoader(),wsc.getClass().getClassLoader());
+ }
+
+ public void testClassLoaderHierarchy() throws ClassNotFoundException, IOException, PicoCompositionException {
+
+ String testcompJarFileName = System.getProperty("testcomp.jar");
+ // Paul's path to TestComp. PLEASE do not take out.
+ //testcompJarFileName = "D:/DEV/nano/reflection/src/test-comp/TestComp.jar";
+
+ assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName);
+ File testCompJar = new File(testcompJarFileName);
+ assertTrue(testCompJar.isFile());
+ String canonicalPath = testCompJar.getCanonicalPath();
+ canonicalPath = canonicalPath.replace('\\','/');
+
+ Reader script = new StringReader(
+ "var parentContainer = new NanoRhinoScriptable();\n" +
+ "with (parentContainer) {\n" +
+ " addComponent('foo','" + FooTestComp.class.getName() + "');\n" +
+ " var childContainer = new NanoRhinoScriptable();\n" +
+ " addContainer(childContainer);\n" +
+ " with (childContainer) {\n" +
+ " addFileClassPathJar(\"" + canonicalPath + "\");\n" +
+ " addComponent('bar','TestComp');\n" +
+ " }\n" +
+ "}\n" +
+ "nano.setNanoRhinoScriptable(parentContainer)\n");
+
+ PicoContainer rootContainer = new NanoRhinoManager().execute(DefaultNanoRhinoScriptable.class, script);
+
+ Object fooTestComp = rootContainer.getComponentInstance("foo");
+ assertNotNull("Container should have a 'foo' component", fooTestComp);
+
+ Collection childContainers = rootContainer.getChildContainers();
+ PicoContainer childContainer = (PicoContainer) childContainers.iterator().next();
+ Object barTestComp = childContainer.getComponentInstance("bar");
+ assertNotNull("Container should have a 'bar' component", barTestComp);
+
+ assertFalse("ClassLoader should not be the same for both components",fooTestComp.getClass().getClassLoader() == barTestComp.getClass().getClassLoader());
+
+ assertTrue("ClassLoader for FooTestComp should not the parent of Bar's",fooTestComp.getClass().getClassLoader() == barTestComp.getClass().getClassLoader().getParent());
+
+
+ }
+
+ public static class FooTestComp {
+
+ }
+
+}
----------
nano /servlet /src /java /org /picoextras /servlet
ApplicationScopeObjectReference.java added at 1.1
diff -N ApplicationScopeObjectReference.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ApplicationScopeObjectReference.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,39 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+import org.picoextras.integrationkit.ObjectReference;
+
+import javax.servlet.ServletContext;
+
+/**
+ * References an object that lives as an attribute of the
+ * ServletContext (application scope)
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class ApplicationScopeObjectReference implements ObjectReference {
+
+ private ServletContext context;
+ private String key;
+
+ public ApplicationScopeObjectReference(ServletContext context, String key) {
+ this.context = context;
+ this.key = key;
+ }
+
+ public void set(Object item) {
+ context.setAttribute(key, item);
+ }
+
+ public Object get() {
+ return context.getAttribute(key);
+ }
+
+}
----------
nano /servlet /src /java /org /picoextras /servlet
KeyConstants.java added at 1.1
diff -N KeyConstants.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ KeyConstants.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,20 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+/**
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public interface KeyConstants {
+ String APPLICATION_CONTAINER = "picocontainer.application";
+ String SESSION_CONTAINER = "picocontainer.session";
+ String REQUEST_CONTAINER = "picocontainer.request";
+ String ASSEMBLER = "picocontainer.assembler";
+ String BUILDER = "picocontainer.builder";
+}
----------
nano /servlet /src /java /org /picoextras /servlet
RequestScopeObjectReference.java added at 1.1
diff -N RequestScopeObjectReference.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ RequestScopeObjectReference.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,39 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+import org.picoextras.integrationkit.ObjectReference;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * References an object that lives as an attribute of the
+ * ServletRequest.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class RequestScopeObjectReference implements ObjectReference {
+
+ private HttpServletRequest request;
+ private String key;
+
+ public RequestScopeObjectReference(HttpServletRequest request, String key) {
+ this.request = request;
+ this.key = key;
+ }
+
+ public void set(Object item) {
+ request.setAttribute(key, item);
+ }
+
+ public Object get() {
+ return request.getAttribute(key);
+ }
+
+}
----------
nano /servlet /src /java /org /picoextras /servlet
ServletContainerListener.java added at 1.1
diff -N ServletContainerListener.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ServletContainerListener.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,93 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+import org.picoextras.integrationkit.ContainerAssembler;
+import org.picoextras.integrationkit.ContainerBuilder;
+import org.picoextras.integrationkit.DefaultLifecycleContainerBuilder;
+import org.picoextras.integrationkit.ObjectReference;
+import org.picoextras.servlet.ApplicationScopeObjectReference;
+import org.picoextras.servlet.KeyConstants;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+/**
+ * Servlet listener class that hooks into the underlying servlet
+ * container and instantiates, assembles, starts, stores and
+ * disposes the appropriate pico containers when
+ * applications/sessions start/stop.
+ *
+ * To use, add this as a listener-class to web.xml.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class ServletContainerListener implements ServletContextListener, HttpSessionListener, KeyConstants {
+
+ private ContainerBuilder containerBuilder;
+
+ public void contextInitialized(ServletContextEvent event) {
+
+ ServletContext context = event.getServletContext();
+ ContainerAssembler assembler = loadAssembler(context);
+
+ ObjectReference containerRef = new ApplicationScopeObjectReference(context, APPLICATION_CONTAINER);
+ ObjectReference builderRef = new ApplicationScopeObjectReference(context, BUILDER);
+
+ containerBuilder = new DefaultLifecycleContainerBuilder();
+ builderRef.set(containerBuilder);
+
+ containerBuilder.buildContainer(containerRef, null, assembler, "application");
+ }
+
+ public void contextDestroyed(ServletContextEvent event) {
+ ServletContext context = event.getServletContext();
+ ObjectReference containerRef = new ApplicationScopeObjectReference(context, APPLICATION_CONTAINER);
+
+ containerBuilder.killContainer(containerRef);
+ }
+
+ public void sessionCreated(HttpSessionEvent event) {
+ HttpSession session = event.getSession();
+ ServletContext context = session.getServletContext();
+ ContainerAssembler assembler = getAssembler(context);
+ ObjectReference containerRef = new SessionScopeObjectReference(session, SESSION_CONTAINER);
+ ObjectReference parentContainerRef = new ApplicationScopeObjectReference(context, APPLICATION_CONTAINER);
+
+ containerBuilder.buildContainer(containerRef, parentContainerRef, assembler, "session");
+ }
+
+ public void sessionDestroyed(HttpSessionEvent event) {
+ HttpSession session = event.getSession();
+ ObjectReference containerRef = new SessionScopeObjectReference(session, SESSION_CONTAINER);
+
+ containerBuilder.killContainer(containerRef);
+ }
+
+ private ContainerAssembler loadAssembler(ServletContext context) {
+ try {
+ ObjectReference assemblerRef = new ApplicationScopeObjectReference(context, ASSEMBLER);
+ String className = context.getInitParameter("assembler");
+ ContainerAssembler result = (ContainerAssembler) Class.forName(className).newInstance();
+ assemblerRef.set(result);
+ return result;
+ } catch (Exception e) {
+ throw new RuntimeException(e); // todo: what should be thrown?
+ }
+ }
+
+ private ContainerAssembler getAssembler(ServletContext context) {
+ ObjectReference assemblerRef = new ApplicationScopeObjectReference(context, ASSEMBLER);
+ return (ContainerAssembler) assemblerRef.get();
+ }
+}
----------
nano /servlet /src /java /org /picoextras /servlet
ServletRequestContainerLauncher.java added at 1.1
diff -N ServletRequestContainerLauncher.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ServletRequestContainerLauncher.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,62 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+import org.picoextras.integrationkit.ContainerAssembler;
+import org.picoextras.integrationkit.ContainerBuilder;
+import org.picoextras.integrationkit.ObjectReference;
+import org.picoextras.servlet.ApplicationScopeObjectReference;
+import org.picoextras.servlet.KeyConstants;
+import org.picoextras.servlet.RequestScopeObjectReference;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+/**
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class ServletRequestContainerLauncher {
+
+ private ContainerBuilder containerBuilder;
+ private ContainerAssembler assembler;
+ private ObjectReference containerRef;
+ private HttpServletRequest request;
+
+ public ServletRequestContainerLauncher(ServletContext context, HttpServletRequest request) {
+
+ ObjectReference builderRef = new ApplicationScopeObjectReference(context, KeyConstants.BUILDER);
+ ObjectReference assemblerRef = new ApplicationScopeObjectReference(context, KeyConstants.ASSEMBLER);
+
+ assembler = (ContainerAssembler) assemblerRef.get();
+ containerRef = new RequestScopeObjectReference(request, KeyConstants.REQUEST_CONTAINER);
+ containerBuilder = (ContainerBuilder) builderRef.get();
+ this.request = request;
+
+ }
+
+ public void startContainer() throws ServletException {
+
+ HttpSession session = request.getSession(true);
+
+ ObjectReference parentContainerRef = new SessionScopeObjectReference(session, KeyConstants.SESSION_CONTAINER);
+
+ if (assembler == null || containerBuilder == null) {
+ throw new ServletException("org.picoextras.servlet.ServletContainerListener not deployed");
+ }
+
+ containerBuilder.buildContainer(containerRef, parentContainerRef, assembler, "request");
+
+ }
+
+ public void killContainer() {
+ containerBuilder.killContainer(containerRef);
+ }
+}
----------
nano /servlet /src /java /org /picoextras /servlet
SessionScopeObjectReference.java added at 1.1
diff -N SessionScopeObjectReference.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SessionScopeObjectReference.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,39 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.servlet;
+
+import org.picoextras.integrationkit.ObjectReference;
+
+import javax.servlet.http.HttpSession;
+
+/**
+ * References an object that lives as an attribute of the
+ * HttpSession.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class SessionScopeObjectReference implements ObjectReference {
+
+ private HttpSession session;
+ private String key;
+
+ public SessionScopeObjectReference(HttpSession session, String key) {
+ this.session = session;
+ this.key = key;
+ }
+
+ public void set(Object item) {
+ session.setAttribute(key, item);
+ }
+
+ public Object get() {
+ return session.getAttribute(key);
+ }
+
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
DefaultWebServerConfig.java added at 1.1
diff -N DefaultWebServerConfig.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DefaultWebServerConfig.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,23 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+public class DefaultWebServerConfig implements WebServerConfig {
+
+ public String getHost() {
+ return "*";
+ }
+
+ public int getPort() {
+ return 80;
+ }
+
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
FlintstonesImpl.java added at 1.1
diff -N FlintstonesImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ FlintstonesImpl.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,20 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import junit.framework.Assert;
+
+public class FlintstonesImpl {
+ public FlintstonesImpl(Wilma wilma, FredImpl fred) {
+ Assert.assertNotNull("Wilma cannot be passed in as null", wilma);
+ Assert.assertNotNull("FredImpl cannot be passed in as null", fred);
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
FredImpl.java added at 1.1
diff -N FredImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ FredImpl.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,20 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import junit.framework.Assert;
+
+public class FredImpl {
+ public FredImpl(Wilma wilma) {
+ Assert.assertNotNull("Wilma cannot be passed in as null", wilma);
+ wilma.hello();
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
MockComponent.java added at 1.1
diff -N MockComponent.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MockComponent.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,28 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Mike Hogan *
+ *****************************************************************************/
+
+
+package org.picoextras.testmodel;
+
+public interface MockComponent {
+ int getPort();
+
+ void setPort(int port);
+
+ String getServer();
+
+ void setServer(String server);
+
+ void addRegister(Integer i);
+
+ int getNumRegisters();
+
+ boolean hasRegister(int i);
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
MockComponentImpl.java added at 1.1
diff -N MockComponentImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MockComponentImpl.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,49 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Mike Hogan *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import org.picoextras.testmodel.MockComponent;
+import java.util.ArrayList;
+import java.util.Collection;
+
+public class MockComponentImpl implements MockComponent {
+ private int port = 0;
+ private String server = null;
+ private Collection registers = new ArrayList();
+
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getServer() {
+ return server;
+ }
+
+ public void setServer(String server) {
+ this.server = server;
+ }
+
+ public void addRegister(Integer i) {
+ registers.add(i);
+ }
+
+ public int getNumRegisters() {
+ return registers.size();
+ }
+
+ public boolean hasRegister(int i) {
+ return registers.contains(new Integer(i));
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
ResourceBundleWebServerConfig.java added at 1.1
diff -N ResourceBundleWebServerConfig.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ResourceBundleWebServerConfig.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,43 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import java.util.ResourceBundle;
+import java.util.PropertyResourceBundle;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.File;
+
+public class ResourceBundleWebServerConfig implements WebServerConfig {
+
+ String host = "localhost";
+ int port = 8080;
+
+ public ResourceBundleWebServerConfig() throws IOException {
+
+ File file = new File(System.getProperty("basedir"), "ResourceBundleWebServerConfig.properties");
+ // how do you get this working in intellij, maven and eclipse ?
+ if (file.exists()) {
+ ResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(file));
+ host = bundle.getString("host");
+ port = Integer.parseInt(bundle.getString("port"));
+ }
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
ThingThatTakesParamsInConstructor.java added at 1.1
diff -N ThingThatTakesParamsInConstructor.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ThingThatTakesParamsInConstructor.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,15 @@
+package org.picoextras.testmodel;
+
+public class ThingThatTakesParamsInConstructor {
+ private String value;
+ private Integer intValue;
+
+ public ThingThatTakesParamsInConstructor(String value, Integer intValue) {
+ this.value = value;
+ this.intValue = intValue;
+ }
+
+ public String getValue() {
+ return value + intValue;
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServer.java added at 1.1
diff -N WebServer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServer.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,14 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+public interface WebServer {
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServerConfig.java added at 1.1
diff -N WebServerConfig.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServerConfig.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,25 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+/**
+ * For a two parameter confiuration, you might prefer to use
+ * addParameterForComponent on OldRegistrationPicoContainer. This example
+ * that uses a separate configuration object would be better
+ * for cases where there were say fifty configurable things
+ * about a particilar component.
+ */
+public interface WebServerConfig {
+
+ String getHost();
+
+ int getPort();
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServerConfigBean.java added at 1.1
diff -N WebServerConfigBean.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServerConfigBean.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,36 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import org.picoextras.testmodel.WebServerConfig;
+
+public class WebServerConfigBean implements WebServerConfig {
+
+ private int port;
+ private String host;
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServerConfigComp.java added at 1.1
diff -N WebServerConfigComp.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServerConfigComp.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,31 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import org.picoextras.testmodel.WebServerConfig;
+
+public class WebServerConfigComp implements WebServerConfig {
+
+ private String host;
+ private int port;
+
+ public WebServerConfigComp(String host, int port) {
+ this.host = host;
+ this.port = port;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServerConfigStub.java added at 1.1
diff -N WebServerConfigStub.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServerConfigStub.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,28 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import org.picoextras.testmodel.WebServerConfig;
+
+public class WebServerConfigStub implements WebServerConfig {
+
+ private int port;
+ private String host;
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WebServerImpl.java added at 1.1
diff -N WebServerImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebServerImpl.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,23 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import junit.framework.Assert;
+import org.picoextras.testmodel.WebServer;
+import org.picoextras.testmodel.WebServerConfig;
+
+public class WebServerImpl implements WebServer {
+
+ public WebServerImpl(WebServerConfig wsc) {
+ Assert.assertTrue("No port number specified", wsc.getPort() > 0);
+ Assert.assertNotNull("No host name specified", wsc.getHost());
+ }
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
Wilma.java added at 1.1
diff -N Wilma.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Wilma.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,15 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+public interface Wilma {
+ void hello();
+}
----------
nano /testmodel /src /java /org /picoextras /testmodel
WilmaImpl.java added at 1.1
diff -N WilmaImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WilmaImpl.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,26 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.testmodel;
+
+import org.picoextras.testmodel.Wilma;
+
+public class WilmaImpl implements Wilma {
+
+ private boolean helloCalled;
+
+ public boolean helloCalled() {
+ return helloCalled;
+ }
+
+ public void hello() {
+ helloCalled = true;
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg
MulticastingPicoException.java added at 1.1
diff -N MulticastingPicoException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MulticastingPicoException.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,16 @@
+package org.picoextras.type3msg;
+
+import org.picocontainer.PicoException;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 8:58:44 PM
+ * To change this template use Options | File Templates.
+ */
+public class MulticastingPicoException extends PicoException {
+ public MulticastingPicoException(String string) {
+ super(string);
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg
MulticastingProxy.java added at 1.1
diff -N MulticastingProxy.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MulticastingProxy.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,48 @@
+package org.picoextras.type3msg;
+
+import org.picocontainer.PicoException;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.*;
+
+public class MulticastingProxy implements InvocationHandler {
+
+ private Class theInterface;
+ private List items;
+
+ public MulticastingProxy(Class theInterface) {
+ this.theInterface = theInterface;
+ items = new ArrayList();
+ }
+
+ public void add(Object item) throws PicoException {
+ if ( !theInterface.isAssignableFrom(item.getClass())) {
+ throw new MulticastingPicoException(
+ "The item " + item.getClass().getName() + " is not an instance of " + theInterface.getName());
+ }
+ items.add(item);
+ }
+
+ public Object getProxy() {
+ return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {theInterface}, this);
+ }
+
+ public Object invoke(Object object, Method method, Object[] args) throws Throwable {
+ Iterator iterator = items.iterator();
+ while (iterator.hasNext()) {
+ Object item = iterator.next();
+ method.invoke(item, args);
+ }
+ return null;
+ }
+
+ protected Object getItem(int index) {
+ return items.get(index);
+ }
+
+ protected int getItemCount() {
+ return items.size();
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg
RoundRobinMulticastingProxy.java added at 1.1
diff -N RoundRobinMulticastingProxy.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ RoundRobinMulticastingProxy.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,27 @@
+package org.picoextras.type3msg;
+
+import java.lang.reflect.Method;
+import java.util.Iterator;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 9:03:36 PM
+ * To change this template use Options | File Templates.
+ */
+public class RoundRobinMulticastingProxy extends MulticastingProxy {
+
+ int next = 0;
+
+ public RoundRobinMulticastingProxy(Class theInterface) {
+ super(theInterface);
+ }
+
+ public Object invoke(Object object, Method method, Object[] args) throws Throwable {
+ Object item = getItem(next % getItemCount());
+ next++;
+ return method.invoke(item, args);
+ }
+
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg
Type3MsgPicoContainer.java added at 1.1
diff -N Type3MsgPicoContainer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Type3MsgPicoContainer.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,35 @@
+package org.picoextras.type3msg;
+
+import org.picocontainer.defaults.DefaultPicoContainer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ */
+public class Type3MsgPicoContainer extends DefaultPicoContainer {
+ private Map impls = new HashMap();
+
+ public void registerMulticasted(Class theInterface, Object theImpl) {
+ MulticastingProxy proxy = (MulticastingProxy) impls.get(theInterface);
+ if (proxy==null) {
+ proxy = new MulticastingProxy(theInterface);
+ impls.put(theInterface, proxy);
+ registerComponentInstance(theInterface, proxy.getProxy());
+ }
+ proxy.add(theImpl);
+ }
+
+ public void registerRoundRobin(Class theInterface, Object theImpl) {
+ RoundRobinMulticastingProxy proxy = (RoundRobinMulticastingProxy) impls.get(theInterface);
+ if (proxy==null) {
+ proxy = new RoundRobinMulticastingProxy(theInterface);
+ impls.put(theInterface, proxy);
+ registerComponentInstance(theInterface, proxy.getProxy());
+ }
+ proxy.add(theImpl);
+ }
+
+
+ //proxy = new MulticastingProxy(theInterface, theCreationStratgy, theDelegationStrategy)
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
Controller.java added at 1.1
diff -N Controller.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Controller.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,29 @@
+package org.picoextras.type3msg.sample;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:18:15 PM
+ * To change this template use Options | File Templates.
+ */
+public class Controller {
+
+ Worker worker;
+ private Database database;
+
+ public Controller(Worker worker, Database database) {
+ this.worker = worker;
+ this.database = database;
+ }
+
+ public void fireOffAUnitOfWork(String name)
+ {
+ UnitOfWork task = new UnitOfWork(name);
+ worker.work(task);
+ }
+
+ public void storeObject(String anObject) {
+ database.storeObject(anObject);
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
Database.java added at 1.1
diff -N Database.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Database.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,14 @@
+package org.picoextras.type3msg.sample;
+
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:30:13 PM
+ * To change this template use Options | File Templates.
+ */
+public interface Database {
+ public void storeObject(String anObject);
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
PretendDatabase.java added at 1.1
diff -N PretendDatabase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PretendDatabase.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,22 @@
+package org.picoextras.type3msg.sample;
+
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:31:29 PM
+ * To change this template use Options | File Templates.
+ */
+public class PretendDatabase implements Database {
+ private List objects;
+
+ public PretendDatabase(List objects) {
+ this.objects = objects;
+ }
+
+ public void storeObject(String anObject) {
+ objects.add(anObject);
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
UnitOfWork.java added at 1.1
diff -N UnitOfWork.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ UnitOfWork.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,20 @@
+package org.picoextras.type3msg.sample;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:13:18 PM
+ * To change this template use Options | File Templates.
+ */
+public class UnitOfWork {
+ String name;
+
+ public UnitOfWork(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
Worker.java added at 1.1
diff -N Worker.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Worker.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,12 @@
+package org.picoextras.type3msg.sample;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:12:45 PM
+ * To change this template use Options | File Templates.
+ */
+public interface Worker {
+ public void work(UnitOfWork task);
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
WorkerDoer.java added at 1.1
diff -N WorkerDoer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WorkerDoer.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,29 @@
+package org.picoextras.type3msg.sample;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:15:14 PM
+ * To change this template use Options | File Templates.
+ */
+public class WorkerDoer implements Worker{
+ private List doneTasks;
+
+ public WorkerDoer() {
+ this.doneTasks = new ArrayList();
+ }
+
+ public void work(UnitOfWork task) {
+ doneTasks.add(task.getName());
+ System.out.println("Doing task " + task.getName());
+ }
+
+ public Collection getDoneTasks() {
+ return doneTasks;
+ }
+}
----------
nano /type3msg /src /java /org /picoextras /type3msg /sample
WorkerLogger.java added at 1.1
diff -N WorkerLogger.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WorkerLogger.java 17 Nov 2003 21:05:16 -0000 1.1
@@ -0,0 +1,20 @@
+package org.picoextras.type3msg.sample;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 10:15:03 PM
+ * To change this template use Options | File Templates.
+ */
+public class WorkerLogger implements Worker {
+ private String logged;
+
+ public void work(UnitOfWork task) {
+ logged = task.getName();
+ }
+
+ public String getLogged() {
+ return logged;
+ }
+}
----------
nano /type3msg /src /test /org /picoextras /typ3msg
Kissable.java added at 1.1
diff -N Kissable.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Kissable.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,5 @@
+package org.picoextras.typ3msg;
+
+public interface Kissable {
+ void kiss();
+}
----------
nano /type3msg /src /test /org /picoextras /typ3msg
TestBoy.java added at 1.1
diff -N TestBoy.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ TestBoy.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,20 @@
+package org.picoextras.typ3msg;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: skizz
+ * Date: Sep 10, 2003
+ * Time: 9:52:20 PM
+ * To change this template use Options | File Templates.
+ */
+public class TestBoy {
+ private Kissable kissable;
+
+ public TestBoy(Kissable kissable) {
+ this.kissable = kissable;
+ }
+
+ public void doKiss() {
+ kissable.kiss();
+ }
+}
----------
nano /type3msg /src /test /org /picoextras /typ3msg
TestGirl.java added at 1.1
diff -N TestGirl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ TestGirl.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,20 @@
+package org.picoextras.typ3msg;
+
+public class TestGirl implements Kissable {
+ private boolean kissed = false;
+
+ public TestGirl(int id) {
+ }
+
+ public boolean wasKissed() {
+ return kissed;
+ }
+
+ public void kiss() {
+ kissed = true;
+ }
+
+ public void wipeFace() {
+ kissed = false;
+ }
+}
----------
nano /type3msg /src /test /org /picoextras /typ3msg
Type3MsgPicoContainerTest.java added at 1.1
diff -N Type3MsgPicoContainerTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Type3MsgPicoContainerTest.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,132 @@
+package org.picoextras.typ3msg;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.picoextras.type3msg.MulticastingProxy;
+import org.picoextras.type3msg.MulticastingPicoException;
+import org.picoextras.type3msg.RoundRobinMulticastingProxy;
+import org.picoextras.type3msg.Type3MsgPicoContainer;
+import org.picoextras.type3msg.sample.Controller;
+import org.picoextras.type3msg.sample.WorkerLogger;
+import org.picoextras.type3msg.sample.WorkerDoer;
+import org.picoextras.type3msg.sample.Worker;
+import org.picoextras.type3msg.sample.Database;
+import org.picoextras.type3msg.sample.PretendDatabase;
+
+/**
+ * @author Chris Stevenson, Stacy Curl
+ */
+public class Type3MsgPicoContainerTest extends TestCase {
+ private TestGirl testGirl1;
+ private TestGirl testGirl2;
+ private TestGirl testGirl3;
+
+ protected void setUp() throws Exception {
+ testGirl1 = new TestGirl(1);
+ testGirl2 = new TestGirl(2);
+ testGirl3 = new TestGirl(3);
+ }
+
+ public void testCannotAddAComponentWithWrongInterface()
+ {
+ MulticastingProxy multicastingProxy = new MulticastingProxy(Kissable.class);
+ try {
+ multicastingProxy.add("foo");
+ fail("Should throw an Exception");
+ }
+ catch(MulticastingPicoException e) {
+ //expected
+ }
+ }
+
+ public void testMulticastingProxySendsKissToAllComponents()
+ {
+ MulticastingProxy multicastingProxy = new MulticastingProxy(Kissable.class);
+
+ multicastingProxy.add(testGirl1);
+ multicastingProxy.add(testGirl2);
+ multicastingProxy.add(testGirl3);
+
+ Kissable kissable = (Kissable) multicastingProxy.getProxy();
+
+ kissable.kiss();
+
+ assertTrue(testGirl1.wasKissed());
+ assertTrue(testGirl2.wasKissed());
+ assertTrue(testGirl3.wasKissed());
+
+ }
+
+ public void testRoundRobinProxySendsKissToOneOfTheComponents()
+ {
+ RoundRobinMulticastingProxy multicastingProxy = new RoundRobinMulticastingProxy(Kissable.class);
+
+ multicastingProxy.add(testGirl1);
+ multicastingProxy.add(testGirl2);
+
+ Kissable kissable = (Kissable) multicastingProxy.getProxy();
+
+ kissable.kiss();
+ assertTrue(testGirl1.wasKissed());
+ assertFalse(testGirl2.wasKissed());
+
+ testGirl1.wipeFace();
+ testGirl2.wipeFace();
+
+ kissable.kiss();
+ assertFalse(testGirl1.wasKissed());
+ assertTrue(testGirl2.wasKissed());
+ }
+
+ public void testPicoContainerWithKnowledgeOfCasting()
+ {
+ Type3MsgPicoContainer c = new Type3MsgPicoContainer();
+
+ c.registerComponentImplementation(TestBoy.class);
+
+ c.registerMulticasted(Kissable.class, testGirl1);
+ c.registerMulticasted(Kissable.class, testGirl2);
+
+ TestBoy testBoy = (TestBoy) c.getComponentInstance(TestBoy.class);
+ testBoy.doKiss();
+
+ assertTrue(testGirl1.wasKissed());
+ assertTrue(testGirl2.wasKissed());
+
+ }
+
+ public void testWhyWouldYouUseMulticasting()
+ {
+ Type3MsgPicoContainer c = new Type3MsgPicoContainer();
+
+ c.registerComponentImplementation(Controller.class);
+// c.registerRoundRobinMulticaster(Database.class, db2);
+
+ //smelly - how do we do this
+ //c.registerMulticasted(Worker.class, new RoundRobin(UnitOfWorkDoer.class, UnitOfWorkDoer.class));
+ WorkerLogger workerLogger = new WorkerLogger();
+ WorkerDoer workerDoer = new WorkerDoer();
+
+ c.registerMulticasted(Worker.class, workerLogger);
+ c.registerMulticasted(Worker.class, workerDoer);
+
+ List objects = new ArrayList();
+
+ c.registerRoundRobin(Database.class, new PretendDatabase(objects));
+ c.registerRoundRobin(Database.class, new PretendDatabase(objects));
+
+ Controller controller = (Controller) c.getComponentInstance(Controller.class);
+
+ controller.fireOffAUnitOfWork("foo");
+
+ assertEquals("foo", workerLogger.getLogged());
+ assertTrue(workerDoer.getDoneTasks().contains("foo"));
+
+ controller.storeObject("bar");
+ assertTrue(objects.contains("bar"));
+ }
+
+}
----------
nano /webwork2 /src /java /org /picoextras /webwork2
PicoActionInvocation.java added at 1.1
diff -N PicoActionInvocation.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoActionInvocation.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,60 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork2;
+
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.DefaultActionInvocation;
+import com.opensymphony.webwork.WebWorkStatics;
+import org.picoextras.integrationkit.ObjectReference;
+import org.picoextras.servlet.KeyConstants;
+import org.picoextras.servlet.RequestScopeObjectReference;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
+
+/**
+ * @author Chris Sturm
+ */
+public class PicoActionInvocation extends DefaultActionInvocation implements KeyConstants {
+
+ public PicoActionInvocation(ActionProxy proxy) throws Exception {
+ super(proxy);
+ }
+
+ public PicoActionInvocation(ActionProxy proxy, Map extraContext) throws Exception {
+ super(proxy, extraContext);
+ }
+
+ public PicoActionInvocation(ActionProxy proxy, Map extraContext, boolean pushAction) throws Exception {
+ super(proxy, extraContext, pushAction);
+ }
+
+ protected void createAction() {
+ // load action
+ try {
+ MutablePicoContainer container = new DefaultPicoContainer();
+ container.addParent(getParentContainer());
+
+ Class actionClass = proxy.getConfig().getClazz();
+ container.registerComponentImplementation(actionClass);
+ action = (Action) container.getComponentInstance(actionClass);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unknown action name: " + e.getMessage());
+ }
+ }
+
+ private MutablePicoContainer getParentContainer() {
+ HttpServletRequest request = (HttpServletRequest) getStack().getContext().get(WebWorkStatics.HTTP_REQUEST);
+ ObjectReference ref = new RequestScopeObjectReference(request, REQUEST_CONTAINER);
+ return (MutablePicoContainer) ref.get();
+ }
+}
----------
nano /webwork2 /src /java /org /picoextras /webwork2
PicoActionProxyFactory.java added at 1.1
diff -N PicoActionProxyFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoActionProxyFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,34 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork2;
+
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.DefaultActionProxyFactory;
+
+import java.util.Map;
+
+import org.picoextras.webwork2.PicoActionInvocation;
+
+/**
+ * @author Chris Sturm
+ */
+public class PicoActionProxyFactory extends DefaultActionProxyFactory {
+ public ActionInvocation createActionInvocation(ActionProxy actionProxy) throws Exception {
+ return new PicoActionInvocation(actionProxy);
+ }
+
+ public ActionInvocation createActionInvocation(ActionProxy actionProxy, Map extraContext) throws Exception {
+ return new PicoActionInvocation(actionProxy, extraContext);
+ }
+
+ public ActionInvocation createActionInvocation(ActionProxy actionProxy, Map extraContext, boolean pushAction) throws Exception {
+ return new PicoActionInvocation(actionProxy, extraContext, pushAction);
+ }
+}
----------
nano /webwork2 /src /java /org /picoextras /webwork2
PicoServletDispatcher.java added at 1.1
diff -N PicoServletDispatcher.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoServletDispatcher.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,43 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork2;
+
+import org.picoextras.servlet.KeyConstants;
+import org.picoextras.servlet.ServletRequestContainerLauncher;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.opensymphony.webwork.dispatcher.ServletDispatcher;
+
+/**
+ * Extension to the standard WebWork2 ServletDispatcher that instantiates a new container in the request
+ * scope for each request and disposes of it correctly at the end of the request.
+ *
+ * To use, replace the WebWork ServletDispatcher in web.xml with this.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class PicoServletDispatcher extends ServletDispatcher implements KeyConstants {
+
+
+ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
+ ServletRequestContainerLauncher containerLauncher = new ServletRequestContainerLauncher(getServletContext(), request);
+ try {
+ containerLauncher.startContainer();
+ // process the servlet using webwork2
+ super.service(request, response);
+ } finally {
+ containerLauncher.killContainer();
+ }
+
+ }
+
+}
----------
nano /webwork2 /src /java /org /picoextras /webwork2
WebWork2ContainerListener.java added at 1.1
diff -N WebWork2ContainerListener.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebWork2ContainerListener.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,25 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork2;
+
+import com.opensymphony.xwork.ActionProxyFactory;
+import org.picoextras.servlet.ServletContainerListener;
+import org.picoextras.webwork2.PicoActionProxyFactory;
+
+import javax.servlet.ServletContextEvent;
+
+/**
+ * @author Chris Sturm
+ */
+public class WebWork2ContainerListener extends ServletContainerListener {
+ public void contextInitialized(ServletContextEvent event) {
+ ActionProxyFactory.setFactory(new PicoActionProxyFactory());
+ super.contextInitialized(event);
+ }
+}
----------
nano /webwork /src /java /org /picoextras /webwork
PicoActionFactory.java added at 1.1
diff -N PicoActionFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoActionFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,78 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork;
+
+import org.picoextras.integrationkit.ObjectReference;
+import org.picoextras.servlet.KeyConstants;
+import org.picoextras.servlet.RequestScopeObjectReference;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import webwork.action.Action;
+import webwork.action.ServletActionContext;
+import webwork.action.factory.ActionFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Replacement for the standard WebWork JavaActionFactory that resolves all of the
+ * dependencies an Action may have in the constructor.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class PicoActionFactory extends ActionFactory implements KeyConstants {
+
+ private Map classCache = new HashMap();
+
+ public Action getActionImpl(String className) throws Exception {
+ Class actionClass = loadClass(className);
+ if (actionClass == null) {
+ return null;
+ } else {
+ Action action = null;
+ try {
+ action = instantiateAction(actionClass);
+ } catch (Exception e) {
+ // todo: what? webwork seems to swallow these exceptions - I think.
+ e.printStackTrace();
+ }
+ return action;
+ }
+ }
+
+ protected Class loadClass(String className) {
+ if (classCache.containsKey(className)) {
+ return (Class) classCache.get(className);
+ } else {
+ try {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ Class result = classLoader.loadClass(className);
+ classCache.put(className, result);
+ return result;
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+ }
+
+ protected Action instantiateAction(Class actionClass) {
+ MutablePicoContainer container = new DefaultPicoContainer();
+ container.addParent(getParentContainer());
+ container.registerComponentImplementation(actionClass);
+ return (Action) container.getComponentInstance(actionClass);
+ }
+
+ private MutablePicoContainer getParentContainer() {
+ HttpServletRequest request = ServletActionContext.getRequest();
+ ObjectReference ref = new RequestScopeObjectReference(request, REQUEST_CONTAINER);
+ return (MutablePicoContainer) ref.get();
+ }
+
+}
----------
nano /webwork /src /java /org /picoextras /webwork
PicoServletDispatcher.java added at 1.1
diff -N PicoServletDispatcher.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoServletDispatcher.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,42 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork;
+
+import org.picoextras.servlet.KeyConstants;
+import org.picoextras.servlet.ServletRequestContainerLauncher;
+import webwork.dispatcher.ServletDispatcher;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Extension to the standard WebWork DispatcherServlet that instantiates a new container in the request
+ * scope for each request and disposes of it correctly at the end of the request.
+ *
+ * To use, replace the WebWork ServletDispatcher in web.xml with this.
+ *
+ * @author <a href="mailto:[email protected]">Joe Walnes</a>
+ */
+public class PicoServletDispatcher extends ServletDispatcher implements KeyConstants {
+
+
+ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
+ ServletRequestContainerLauncher containerLauncher = new ServletRequestContainerLauncher(getServletContext(), request);
+ try {
+ containerLauncher.startContainer();
+ // process the servlet using webwork
+ super.service(request, response);
+ } finally {
+ containerLauncher.killContainer();
+ }
+
+ }
+
+}
----------
nano /webwork /src /java /org /picoextras /webwork
WebWorkActionFactory.java added at 1.1
diff -N WebWorkActionFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WebWorkActionFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,45 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.webwork;
+
+import webwork.action.Action;
+import webwork.action.factory.*;
+import org.picoextras.webwork.PicoActionFactory;
+
+/**
+ * Custom webwork action lifecycle that ensures actions are treated as pico components.
+ *
+ * <p>To use, add to webwork.properties:
+ * <pre>
+ * webwork.action.factory=org.picoextras.webwork.WebWorkActionFactory
+ * </pre></p>
+ */
+public class WebWorkActionFactory extends ActionFactory {
+
+ private ActionFactory factory;
+
+ public WebWorkActionFactory() {
+ // replace standard JavaActionFactory with PicoActionFactory
+ factory = new PicoActionFactory();
+ // the rest are the standard webwork ActionFactoryProxies
+ factory = new PrefixActionFactoryProxy(factory);
+ factory = new CommandActionFactoryProxy(factory);
+ factory = new AliasingActionFactoryProxy(factory);
+ factory = new CommandActionFactoryProxy(factory);
+ factory = new ContextActionFactoryProxy(factory);
+ factory = new PrepareActionFactoryProxy(factory);
+ factory = new ParametersActionFactoryProxy(factory);
+ factory = new ChainingActionFactoryProxy(factory);
+ }
+
+ public Action getActionImpl(String actionName) throws Exception {
+ return factory.getActionImpl(actionName);
+ }
+
+}
----------
nano /xml /src /java /org /picoextras /xml
BeanXmlPseudoComponentFactory.java added at 1.1
diff -N BeanXmlPseudoComponentFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BeanXmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,70 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.xml;
+
+import org.picoextras.reflection.DefaultReflectionFrontEnd;
+import org.picoextras.reflection.ReflectionFrontEnd;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * @author Paul Hammant
+ * @author Marcos Tarruella
+ */
+public class BeanXmlPseudoComponentFactory implements XmlPseudoComponentFactory {
+
+ public Object makeInstance(Element elem) throws SAXException, ClassNotFoundException {
+ String className = elem.getNodeName();
+ ReflectionFrontEnd rfe = new DefaultReflectionFrontEnd();
+ rfe.registerComponentImplementation(className);
+ Object o = rfe.getPicoContainer().getComponentInstances().get(0);
+
+ NodeList nl = elem.getChildNodes();
+ for (int i=0; i < nl.getLength(); i++) {
+ Node n = nl.item(i);
+ if (n instanceof Element) {
+ Element e = (Element) n;
+ setBeanProperty(o, e.getNodeName(), e.getChildNodes().item(0).getNodeValue());
+ }
+ }
+ return o;
+ }
+
+ private void setBeanProperty(Object o, String nodeName, String nodeValue) {
+ String methodName = "set" + nodeName.substring(0,1).toUpperCase() + nodeName.substring(1);
+ Method[] methods = o.getClass().getMethods();
+ for (int i=0; i < methods.length; i++) {
+ Method method = methods[i];
+ if (method.getName().equals(methodName)) {
+ Class paramType = method.getParameterTypes()[0];
+ try {
+ if (paramType == String.class) {
+ method.invoke(o, new Object[] {nodeValue});
+ return;
+ } else if (paramType == Integer.TYPE) {
+ method.invoke(o, new Object[] { new Integer(nodeValue)});
+ return;
+ }
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ throw new UnsupportedOperationException("Whoa - bad type!!");
+ }
+}
----------
nano /xml /src /java /org /picoextras /xml
DefaultXmlFrontEnd.java added at 1.1
diff -N DefaultXmlFrontEnd.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DefaultXmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,195 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import org.picoextras.reflection.ReflectionFrontEnd;
+import org.picoextras.reflection.DefaultReflectionFrontEnd;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.picocontainer.defaults.ComponentAdapterFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+
+/**
+ * This class builds up a hierarchy of PicoContainers from an XML configuration file.
+ *
+ * @author Paul Hammant
+ * @author Aslak Hellesøy
+ * @version $Revision: 1.1 $
+ */
+public class DefaultXmlFrontEnd implements XmlFrontEnd {
+
+ public PicoContainer createPicoContainer(Element rootElement) throws IOException, SAXException, ClassNotFoundException, PicoCompositionException {
+ MutablePicoContainer mutablePicoContainer = getMutablePicoContainerFromContainerAttribute(rootElement);
+ return createPicoContainer(rootElement, mutablePicoContainer);
+ }
+
+ public PicoContainer createPicoContainer(Element rootElement, MutablePicoContainer mutablePicoContainer)
+ throws IOException, SAXException, ClassNotFoundException, PicoCompositionException {
+
+ ReflectionFrontEnd rootReflectionFrontEnd = new DefaultReflectionFrontEnd(mutablePicoContainer);
+ registerComponentsAndChildContainers(rootReflectionFrontEnd, rootElement);
+
+ return rootReflectionFrontEnd.getPicoContainer();
+ }
+
+ private void registerComponentsAndChildContainers(ReflectionFrontEnd reflectionFrontEnd, Element containerElement) throws ClassNotFoundException, IOException, PicoCompositionException {
+
+ NodeList children = containerElement.getChildNodes();
+ // register classpath first, regardless of order in the document.
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ short type = child.getNodeType();
+ if (type == Document.ELEMENT_NODE) {
+ String name = child.getNodeName();
+ if (name.equals("classpath")) {
+ registerClasspath(reflectionFrontEnd, (Element) child);
+ }
+ }
+ }
+ int componentCount = 0;
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ short type = child.getNodeType();
+ if (type == Document.ELEMENT_NODE) {
+ String name = child.getNodeName();
+ if (name.equals("pseudocomponent")) {
+ registerPseudoComponent(reflectionFrontEnd, (Element) child);
+ componentCount++;
+ } if (name.equals("component")) {
+ registerComponent(reflectionFrontEnd, (Element) child);
+ componentCount++;
+ } else if (name.equals("container")) {
+
+ MutablePicoContainer mutablePicoContainer = getMutablePicoContainerFromContainerAttribute(child);
+
+ ReflectionFrontEnd childFrontEnd = new DefaultReflectionFrontEnd(reflectionFrontEnd, mutablePicoContainer);
+ registerComponentsAndChildContainers(childFrontEnd, (Element) child);
+ }
+ }
+ }
+ if (componentCount == 0) {
+ throw new EmptyXmlCompositionException();
+ }
+ }
+
+ private MutablePicoContainer getMutablePicoContainerFromContainerAttribute(Node child) throws ClassNotFoundException {
+ String picoContainerClassName = ((Element) child).getAttribute("container");
+ String componentAdaptorClassName = ((Element) child).getAttribute("componentadaptor");
+
+ ReflectionFrontEnd tempContainer = new DefaultReflectionFrontEnd();
+
+ if (componentAdaptorClassName != null && !componentAdaptorClassName.equals("")) {
+ tempContainer.registerComponent(ComponentAdapterFactory.class, componentAdaptorClassName);
+ }
+ if (picoContainerClassName == null || picoContainerClassName.equals("")) {
+ tempContainer.registerComponent(PicoContainer.class, DefaultPicoContainer.class.getName());
+ } else {
+ tempContainer.registerComponentImplementation(picoContainerClassName);
+ }
+ return (MutablePicoContainer) tempContainer.getPicoContainer().getComponentInstance(PicoContainer.class);
+ }
+
+ private void registerClasspath(ReflectionFrontEnd reflectionFrontEnd, Element classpathElement) throws IOException {
+ NodeList children = classpathElement.getChildNodes();
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ short type = child.getNodeType();
+ if (type == Document.ELEMENT_NODE) {
+ String name = child.getNodeName();
+ if (name.equals("element")) {
+ String fileName = ((Element) child).getAttribute("file");
+ String urlSpec = ((Element) child).getAttribute("url");
+ URL url = null;
+ if (urlSpec != null && !"".equals(urlSpec)) {
+ url = new URL(urlSpec);
+ } else {
+ File file = new File(fileName);
+ if (!file.exists()) {
+ throw new IOException(file.getAbsolutePath() + " doesn't exist");
+ }
+ url = file.toURL();
+ }
+ reflectionFrontEnd.addClassLoaderURL(url);
+ }
+ }
+ }
+ }
+
+ private void registerComponent(ReflectionFrontEnd pico, Element componentElement) throws ClassNotFoundException {
+ String className = componentElement.getAttribute("impl");
+ String stringKey = componentElement.getAttribute("stringkey");
+ String typeKey = componentElement.getAttribute("typekey");
+
+ ArrayList hints = new ArrayList();
+ NodeList children = componentElement.getChildNodes();
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if (child.getNodeName() == "hint") {
+ hints.add(((Element) child).getAttribute("stringkey"));
+ }
+ }
+
+
+ //TODO we need to have config elements parsing here, and register with params action...
+ if (stringKey == null || stringKey.equals("")) {
+ stringKey = className;
+ }
+ if (typeKey == null || typeKey.equals("")) {
+ pico.registerComponent(stringKey, className);
+ } else {
+ pico.registerComponentWithClassKey(typeKey,className);
+ }
+ }
+
+ private void registerPseudoComponent(ReflectionFrontEnd pico, Element componentElement) throws ClassNotFoundException, PicoCompositionException {
+ String factoryClass = componentElement.getAttribute("factory");
+
+ if (factoryClass == null || factoryClass.equals("")) {
+ throw new java.lang.IllegalArgumentException("factory attribute should be specified for pseudocomponent element");
+ // unless we provide a default.
+ }
+
+ ReflectionFrontEnd tempContainer = new DefaultReflectionFrontEnd();
+ tempContainer.registerComponentWithClassKey(XmlPseudoComponentFactory.class.getName(), factoryClass);
+ XmlPseudoComponentFactory factory = (XmlPseudoComponentFactory) tempContainer.getPicoContainer().getComponentInstances().get(0);
+
+ NodeList nl = componentElement.getChildNodes();
+ Element childElement = null;
+ for (int i = 0; i < nl.getLength(); i++) {
+ if (nl.item(i) instanceof Element) {
+ childElement = (Element) nl.item(i);
+ break;
+ }
+ }
+
+ try {
+ Object pseudoComp = factory.makeInstance(childElement);
+ pico.getPicoContainer().registerComponentInstance(pseudoComp);
+ } catch (final SAXException e) {
+ throw new PicoCompositionException() {
+ public String getMessage() {
+ return "SAXException during creation of PseudoComponent :" + e.getMessage();
+ }
+ };
+ }
+ }
+}
----------
nano /xml /src /java /org /picoextras /xml
EmptyXmlCompositionException.java added at 1.1
diff -N EmptyXmlCompositionException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ EmptyXmlCompositionException.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,19 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+public class EmptyXmlCompositionException extends XmlFrontEndException {
+ public EmptyXmlCompositionException() {
+ }
+
+ public String getMessage() {
+ return "No components in the XML composition";
+ }
+}
----------
nano /xml /src /java /org /picoextras /xml
ParameterXmlFrontEnd.java added at 1.1
diff -N ParameterXmlFrontEnd.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ParameterXmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,223 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Jeppe Cramon *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.picoextras.reflection.ReflectionFrontEnd;
+import org.picoextras.reflection.DefaultReflectionFrontEnd;
+import org.picoextras.xml.EmptyXmlCompositionException;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * Handles XML configuration files which includes specification of container implementation (default is DefaultPicoContainer) + plus constant
+ * parameters (including primitive types) for components.
+ * Requirement for Containers is that they have a default constructor which initializes it with the necessary
+ * requirements.<br>
+ * <br>
+ * Sample XML document:
+ * <pre>
+ * <container classname="org.picoextras.jmx.NanoMXContainer">
+ * <component key="testClass" classname="org.test.TestClass">
+ * <parameter classname="java.lang.String">Test</parameter>
+ * <parameter classname="int">23</parameter>
+ * </component>
+ * <container>
+ * <component classname="org.test.TestClass2"/>
+ * <component key="class23" classname="org.test.TestClass3"/>
+ * </container>
+ * </container>
+ * </pre>
+ * @author Jeppe Cramon
+ */
+public class ParameterXmlFrontEnd implements XmlFrontEnd {
+ private int componentCount = 0;
+
+ /**
+ *
+ */
+ public ParameterXmlFrontEnd() {
+ super();
+ }
+
+ /* (non-Javadoc)
+ * @see org.picoextras.xml.XmlFrontEnd#createPicoContainer(org.w3c.dom.Element)
+ */
+ public PicoContainer createPicoContainer(Element rootElement)
+ throws
+ IOException,
+ SAXException,
+ ClassNotFoundException,
+ XmlFrontEndException {
+ componentCount = 0;
+ PicoContainer container = registerContainer(null, rootElement);
+ if (componentCount == 0) {
+ throw new EmptyXmlCompositionException();
+ }
+ return container;
+ }
+
+ public PicoContainer createPicoContainer(Element rootElement, MutablePicoContainer mutablePicoContainer) throws IOException, SAXException, ClassNotFoundException, PicoCompositionException {
+ // ignore mutablePicoContainer param for now.
+ return createPicoContainer(rootElement);
+ }
+
+ /**
+ * Accepts elements:<br>
+ * <container classname=""/> where classname is optional. If left out the type of container is the DefaultPicoContainer
+ * @param element
+ * @return
+ */
+ private PicoContainer registerContainer(ReflectionFrontEnd parent, Element element) throws XmlFrontEndException {
+ if (!element.getNodeName().equals("container")) {
+ throw new XmlFrontEndException("Excepted 'container' element, but found '" + element.getNodeName() + "'");
+ }
+ // Check which type of container to register
+ String containerClass = DefaultPicoContainer.class.getName();
+ if (element.getAttribute("classname") != null && element.getAttribute("classname").length() != 0) {
+ containerClass = element.getAttribute("classname");
+ }
+
+ try {
+ MutablePicoContainer container = (MutablePicoContainer) Class.forName(containerClass).newInstance();
+ ReflectionFrontEnd frontEnd = new DefaultReflectionFrontEnd(container);
+ if (parent != null) {
+ MutablePicoContainer parentContainer = (MutablePicoContainer)parent.getPicoContainer();
+ container.addParent(parentContainer);
+ }
+ // Loop through components and child containers
+ NodeList elemList = element.getChildNodes();
+ for (int index = 0; index < elemList.getLength(); index++) {
+ if(elemList.item(index).getNodeType() == Document.ELEMENT_NODE) {
+ registerComponentsAndChildContainers(frontEnd, (Element)elemList.item(index));
+ }
+ }
+ return container;
+ } catch (InstantiationException e) {
+ throw MakeXmlFrontEndException(containerClass, e);
+ } catch (IllegalAccessException e) {
+ throw MakeXmlFrontEndException(containerClass, e);
+ } catch (ClassNotFoundException e) {
+ throw MakeXmlFrontEndException(containerClass, e);
+ }
+ }
+
+ private XmlFrontEndException MakeXmlFrontEndException(final String containerClass, final Exception e) {
+ return new XmlFrontEndException("Failed to create an instance of container '" + containerClass + "', due to " + e.getMessage(), e);
+ }
+
+ /**
+ * @param frontEnd
+ * @param element
+ */
+ private void registerComponentsAndChildContainers(ReflectionFrontEnd frontEnd, Element element) throws ClassNotFoundException, XmlFrontEndException {
+ if (element.getNodeName().equals("container")) {
+ registerContainer(frontEnd, element);
+ } else if (element.getNodeName().equals("component")) {
+ registerComponent(frontEnd, element);
+ } else {
+ throw new XmlFrontEndException("Unknown element type: '" + element.getNodeName() + "'");
+ }
+ }
+
+ /**
+ * Accepts elements of the following type:<br>
+ * <component key="" classname=""/> where key is optional. If key is left out the key value default to the classname<br>
+ * It also accepts parameter childelements :<br>
+ * <parameter classname="">[Value]</parameter><br>
+ * Full classname for type is needed, but shorthands for most primitives and wrapper classes exists.
+ *
+ * @param pico
+ * @param element
+ */
+ private void registerComponent(ReflectionFrontEnd pico, Element element) throws ClassNotFoundException, XmlFrontEndException {
+ String className = element.getAttribute("classname");
+ String key = element.getAttribute("key");
+ if(key == null || key.equals("")) {
+ key = className;
+ }
+ // Check for initialization parameters
+ if (element.getChildNodes().getLength() == 0) {
+ pico.registerComponent(key, className);
+ } else {
+ List parameterTypes = new ArrayList();
+ List parameterValues = new ArrayList();
+ NodeList elemList = element.getChildNodes();
+ int parameterIndex = 0;
+ for (int index = 0; index < elemList.getLength(); index++) {
+ if(elemList.item(index).getNodeType() == Document.ELEMENT_NODE) {
+ Element paramElement = (Element) elemList.item(index);
+ if (paramElement.getNodeName().equals("parameter")) {
+ parameterTypes.add(handleShortHands(paramElement.getAttribute("classname")));
+ if (paramElement.hasChildNodes() && paramElement.getChildNodes().item(0).getNodeType() == Document.TEXT_NODE) {
+ parameterValues.add(paramElement.getChildNodes().item(0).getNodeValue());
+ } else {
+ throw new XmlFrontEndException("No value specified for parameter " + (String)parameterTypes.get(parameterIndex) + " at order " + parameterIndex + 1);
+ }
+ parameterIndex++;
+ } else {
+ throw new XmlFrontEndException("Unkown component parameter element '" + paramElement.getNodeName() + "'");
+ }
+ }
+ }
+ pico.registerComponent(key, className, (String[])parameterTypes.toArray(new String[parameterTypes.size()]), (String[])parameterValues.toArray(new String[parameterValues.size()]));
+ }
+ componentCount++;
+ }
+
+ /**
+ * Handles shorthands for most of the simple type wrappers (translates from a shorthand for to the full
+ * form, e.g. from "int" to "java.lang.Integer" og "Short" to "java.lang.Short"
+ * @param name Shorthand
+ * @return The translated shorthand or the parameter value in case no shorthand was found
+ */
+ private String handleShortHands(String name) {
+ if (name.equals("String")) {
+ return "java.lang.String";
+ } else if (name.equals("")) {
+ return "java.lang.String";
+ } else if (name.equals("Integer")) {
+ return "java.lang.Integer";
+ } else if (name.equals("Float")) {
+ return "java.lang.Float";
+ } else if (name.equals("Double")) {
+ return "java.lang.Double";
+ } else if (name.equals("Boolean")) {
+ return "java.lang.Boolean";
+ } else if (name.equals("Short")) {
+ return "java.lang.Short";
+ } else if (name.equals("int")) {
+ return "java.lang.Integer";
+ } else if (name.equals("long")) {
+ return "java.lang.Long";
+ } else if (name.equals("float")) {
+ return "java.lang.Float";
+ } else if (name.equals("double")) {
+ return "java.lang.Double";
+ } else if (name.equals("boolean")) {
+ return "java.lang.Boolean";
+ } else if (name.equals("short")) {
+ return "java.lang.Short";
+ } else {
+ return name;
+ }
+ }
+
+}
----------
nano /xml /src /java /org /picoextras /xml
XStreamXmlPseudoComponentFactory.java added at 1.1
diff -N XStreamXmlPseudoComponentFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ XStreamXmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,27 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.xml;
+
+import org.picoextras.xml.XmlPseudoComponentFactory;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+import com.thoughtworks.xstream.xml.dom.DomXMLReader;
+import com.thoughtworks.xstream.XStream;
+
+/**
+ * @author Paul Hammant
+ * @author Marcos Tarruella
+ */
+public class XStreamXmlPseudoComponentFactory implements XmlPseudoComponentFactory {
+ public Object makeInstance(Element elem) throws SAXException, ClassNotFoundException {
+ XStream xs = new XStream();
+ Object result = xs.fromXML(new DomXMLReader(elem));
+ return result;
+ }
+}
----------
nano /xml /src /java /org /picoextras /xml
XmlFrontEnd.java added at 1.1
diff -N XmlFrontEnd.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ XmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,36 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.PicoContainer;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+import java.io.IOException;
+
+/**
+ * This interface builds up a hierarchy of PicoContainers from an XML configuration file.
+ *
+ * @author Paul Hammant
+ * @author Aslak Hellesøy
+ * @version $Revision: 1.1 $
+ */
+
+public interface XmlFrontEnd {
+
+ public PicoContainer createPicoContainer(Element rootElement, MutablePicoContainer mutablePicoContainer)
+ throws IOException, SAXException, ClassNotFoundException, PicoCompositionException;
+
+ public PicoContainer createPicoContainer(Element rootElement)
+ throws IOException, SAXException, ClassNotFoundException, PicoCompositionException;
+}
----------
nano /xml /src /java /org /picoextras /xml
XmlFrontEndException.java added at 1.1
diff -N XmlFrontEndException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ XmlFrontEndException.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,37 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import org.picocontainer.PicoCompositionException;
+
+/**
+ * @author Jeppe Cramon
+ */
+public class XmlFrontEndException extends PicoCompositionException {
+
+ public XmlFrontEndException() {
+ }
+
+ /**
+ * @param cause
+ */
+ public XmlFrontEndException(Throwable cause) {
+ super(cause);
+ }
+
+ public XmlFrontEndException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public XmlFrontEndException(String message) {
+ super(message);
+ }
+
+}
----------
nano /xml /src /java /org /picoextras /xml
XmlPseudoComponentFactory.java added at 1.1
diff -N XmlPseudoComponentFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ XmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,18 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import org.xml.sax.SAXException;
+import org.w3c.dom.Element;
+
+public interface XmlPseudoComponentFactory {
+ Object makeInstance(Element elem) throws SAXException, ClassNotFoundException;
+}
----------
nano /xml /src /test /org /picoextras /xml
BeanXmlPseudoComponentFactoryTestCase.java added at 1.1
diff -N BeanXmlPseudoComponentFactoryTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BeanXmlPseudoComponentFactoryTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,47 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.xml;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.picoextras.xml.BeanXmlPseudoComponentFactory;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * @author Paul Hammant
+ * @author Marcos Tarruella
+ */
+public class BeanXmlPseudoComponentFactoryTestCase extends TestCase {
+
+ public void testDeserialization() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
+ BeanXmlPseudoComponentFactory xsf = new BeanXmlPseudoComponentFactory();
+
+ StringReader sr = new StringReader("" +
+ "<org.picoextras.xml.TestPseudoComp>" +
+ "<foo>10</foo>" +
+ "<bar>hello</bar>" +
+ "</org.picoextras.xml.TestPseudoComp>"
+ );
+ InputSource is = new InputSource(sr);
+ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document doc = db.parse(is);
+
+ Object o = xsf.makeInstance(doc.getDocumentElement());
+ TestPseudoComp tsc = (TestPseudoComp) o;
+ assertEquals("hello",tsc.getBar());
+ assertEquals(10,tsc.getFoo());
+ }
+}
----------
nano /xml /src /test /org /picoextras /xml
DefaultXmlFrontEndTestCase.java added at 1.1
diff -N DefaultXmlFrontEndTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DefaultXmlFrontEndTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,155 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Aslak Hellesoy and Paul Hammant *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import junit.framework.TestCase;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.PicoContainer;
+import org.picoextras.testmodel.DefaultWebServerConfig;
+import org.picoextras.testmodel.WebServer;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * @author Aslak Hellesøy
+ * @version $Revision: 1.1 $
+ */
+public class DefaultXmlFrontEndTestCase extends TestCase {
+
+ private Element getRootElement(InputSource is) throws ParserConfigurationException, IOException, SAXException {
+ return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is).getDocumentElement();
+ }
+
+ public void testCreateSimpleContainer() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, PicoCompositionException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component impl='org.picoextras.testmodel.DefaultWebServerConfig'/>" +
+ " <component typekey='org.picoextras.testmodel.WebServer' impl='org.picoextras.testmodel.WebServerImpl'/>" +
+ "</container>"));
+
+ XmlFrontEnd inputSourceContainerFactory = new DefaultXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstance(DefaultWebServerConfig.class));
+ }
+
+ public void testPicoInPico() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, PicoCompositionException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component impl='org.picoextras.testmodel.DefaultWebServerConfig'/>" +
+ " <container>" +
+ " <component typekey='org.picoextras.testmodel.WebServer' impl='org.picoextras.testmodel.WebServerImpl'/>" +
+ " </container>" +
+ "</container>"));
+
+ XmlFrontEnd inputSourceFrontEnd = new DefaultXmlFrontEnd();
+ PicoContainer rootContainer = inputSourceFrontEnd.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(rootContainer.getComponentInstance(DefaultWebServerConfig.class));
+
+ PicoContainer childContainer = (PicoContainer) rootContainer.getChildContainers().iterator().next();
+ assertNotNull(childContainer.getComponentInstance(WebServer.class));
+ }
+
+ public void testClassLoaderHierarchy() throws ParserConfigurationException, ClassNotFoundException, SAXException, IOException, PicoCompositionException {
+
+ String testcompJarFileName = System.getProperty("testcomp.jar");
+ // Paul's path to TestComp. PLEASE do not take out.
+ //testcompJarFileName = "D:/DEV/nano/reflection/src/test-comp/TestComp.jar";
+
+ assertNotNull("The testcomp.jar system property should point to nano/reflection/src/test-comp/TestComp.jar", testcompJarFileName);
+ File testCompJar = new File(testcompJarFileName);
+ File testCompJar2 = new File(testCompJar.getParentFile(), "TestComp2.jar");
+ assertTrue(testCompJar.isFile());
+ assertTrue(testCompJar2.isFile());
+
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <classpath>" +
+ " <element file='" + testCompJar.getCanonicalPath() + "'/>" +
+ " </classpath>" +
+ " <component stringkey='foo' impl='TestComp'/>" +
+ " <container>" +
+ " <classpath>" +
+ " <element file='" + testCompJar2.getCanonicalPath() + "'/>" +
+ " </classpath>" +
+ " <component stringkey='bar' impl='TestComp2'/>" +
+ " </container>" +
+ "</container>"));
+
+ XmlFrontEnd inputSourceFrontEnd = new DefaultXmlFrontEnd();
+ PicoContainer rootContainer = inputSourceFrontEnd.createPicoContainer(getRootElement(inputSource));
+
+ Object fooTestComp = rootContainer.getComponentInstance("foo");
+ assertNotNull("Container should have a 'foo' component", fooTestComp);
+
+ PicoContainer childContainer = (PicoContainer) rootContainer.getChildContainers().iterator().next();
+ Object barTestComp = childContainer.getComponentInstance("bar");
+ assertNotNull("Container should have a 'bar' component", barTestComp);
+
+ assertEquals("foo classloader should be parent of bar",fooTestComp.getClass().getClassLoader(),
+ barTestComp.getClass().getClassLoader().getParent());
+
+ }
+
+ public void testInstantiateXmlWithMissingComponent() throws Exception, SAXException, ParserConfigurationException, IOException {
+
+ try {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component impl='Foo'/>" +
+ "</container>"));
+ PicoContainer rootContainer = new DefaultXmlFrontEnd().createPicoContainer(getRootElement(inputSource));
+ fail("Should have thrown a ClassNotFoundException");
+ } catch (ClassNotFoundException cnfe) {
+ }
+
+ }
+
+ public void testInstantiateEmptyXml() throws Exception, SAXException, ParserConfigurationException, IOException {
+
+ try {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ "</container>"));
+ PicoContainer rootContainer = new DefaultXmlFrontEnd().createPicoContainer(getRootElement(inputSource));
+ fail("Should have thrown a EmptyXmlCompositionException");
+ } catch (EmptyXmlCompositionException cnfe) {
+ }
+ }
+
+ public void testPseudoComponentCreation() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, PicoCompositionException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <pseudocomponent factory='org.picoextras.xml.DefaultXmlFrontEndTestCase$TestFactory'>" +
+ " <config-or-whatever/>"+
+ " </pseudocomponent>" +
+ "</container>"));
+
+ XmlFrontEnd inputSourceContainerFactory = new DefaultXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstances().get(0));
+ assertTrue(picoContainer.getComponentInstances().get(0) instanceof String);
+ assertEquals("Hello", picoContainer.getComponentInstances().get(0).toString());
+
+ }
+ public static class TestFactory implements XmlPseudoComponentFactory {
+ public Object makeInstance(Element elem) throws SAXException, ClassNotFoundException {
+ return "Hello";
+ }
+ }
+
+}
----------
nano /xml /src /test /org /picoextras /xml
ParameterXmlFrontEndTestCase.java added at 1.1
diff -N ParameterXmlFrontEndTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ParameterXmlFrontEndTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,122 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ * Original code by Jeppe Cramon *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import junit.framework.TestCase;
+
+import org.picoextras.testmodel.DefaultWebServerConfig;
+import org.picoextras.testmodel.WebServerConfigComp;
+import org.picoextras.xml.ParameterXmlFrontEnd;
+import org.picoextras.xml.XmlFrontEndException;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * @author Jeppe Cramon
+ * @author Paul Hammant
+ */
+public class ParameterXmlFrontEndTestCase extends TestCase {
+
+ private Element getRootElement(InputSource is) throws ParserConfigurationException, IOException, SAXException {
+ return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is).getDocumentElement();
+ }
+
+ public void testSingleDefaultContainer() throws XmlFrontEndException, IOException, SAXException, ClassNotFoundException, ParserConfigurationException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component classname='org.picoextras.testmodel.DefaultWebServerConfig'/>" +
+ " <component key='org.picoextras.testmodel.WebServer' classname='org.picoextras.testmodel.WebServerImpl'/>" +
+ "</container>"));
+ ParameterXmlFrontEnd inputSourceContainerFactory = new ParameterXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstance(DefaultWebServerConfig.class));
+ }
+
+ public void testContainerOverride() throws XmlFrontEndException, IOException, SAXException, ClassNotFoundException, ParserConfigurationException {
+ OverriddenPicoContainer.used = false;
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container classname='"+OverriddenPicoContainer.class.getName()+"'>" +
+ " <component classname='org.picoextras.testmodel.DefaultWebServerConfig'/>" +
+ " <component key='org.picoextras.testmodel.WebServer' classname='org.picoextras.testmodel.WebServerImpl'/>" +
+ "</container>"));
+ ParameterXmlFrontEnd inputSourceContainerFactory = new ParameterXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstance(DefaultWebServerConfig.class));
+ assertTrue("OverriddenPicoContainer should have been used", OverriddenPicoContainer.used);
+ }
+
+ public void testPicoInPico() throws XmlFrontEndException, IOException, SAXException, ClassNotFoundException, ParserConfigurationException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component classname='org.picoextras.testmodel.DefaultWebServerConfig'/>" +
+ " <container>" +
+ " <component key='org.picoextras.testmodel.WebServer' classname='org.picoextras.testmodel.WebServerImpl'/>" +
+ " </container>" +
+ "</container>"));
+
+ ParameterXmlFrontEnd inputSourceContainerFactory = new ParameterXmlFrontEnd();
+ PicoContainer rootContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(rootContainer.getComponentInstance(DefaultWebServerConfig.class));
+
+ PicoContainer childContainer = (PicoContainer) rootContainer.getChildContainers().iterator().next();
+ assertNotNull(childContainer.getComponentInstance("org.picoextras.testmodel.WebServer"));
+ }
+
+ public void testInstantiationOfComponentsWithParams() throws XmlFrontEndException, IOException, SAXException, ClassNotFoundException, ParserConfigurationException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component classname='org.picoextras.testmodel.WebServerConfigComp'>" +
+ " <parameter classname='String'>localhost</parameter>" +
+ " <parameter classname='int'>8080</parameter>" +
+ " </component>" +
+ " <component key='org.picoextras.testmodel.WebServer' classname='org.picoextras.testmodel.WebServerImpl'/>" +
+ "</container>"));
+ ParameterXmlFrontEnd inputSourceContainerFactory = new ParameterXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstance(WebServerConfigComp.class));
+ WebServerConfigComp config = (WebServerConfigComp) picoContainer.getComponentInstance(WebServerConfigComp.class);
+ assertEquals("localhost", config.getHost());
+ assertEquals(8080, config.getPort());
+ }
+
+ public void testInstantiationOfComponentsWithDefaultParamHandling() throws XmlFrontEndException, IOException, SAXException, ClassNotFoundException, ParserConfigurationException {
+ InputSource inputSource = new InputSource(new StringReader(
+ "<container>" +
+ " <component classname='org.picoextras.testmodel.WebServerConfigComp'>" +
+ " <parameter>localhost</parameter>" +
+ " <parameter classname='int'>8080</parameter>" +
+ " </component>" +
+ " <component key='org.picoextras.testmodel.WebServer' classname='org.picoextras.testmodel.WebServerImpl'/>" +
+ "</container>"));
+ ParameterXmlFrontEnd inputSourceContainerFactory = new ParameterXmlFrontEnd();
+ PicoContainer picoContainer = inputSourceContainerFactory.createPicoContainer(getRootElement(inputSource));
+ assertNotNull(picoContainer.getComponentInstance(WebServerConfigComp.class));
+ WebServerConfigComp config = (WebServerConfigComp) picoContainer.getComponentInstance(WebServerConfigComp.class);
+ assertEquals("localhost", config.getHost());
+ assertEquals(8080, config.getPort());
+ }
+
+ public static class OverriddenPicoContainer extends DefaultPicoContainer {
+ public static boolean used;
+ public OverriddenPicoContainer() {
+ used = true;
+ }
+ }
+}
----------
nano /xml /src /test /org /picoextras /xml
TestPseudoComp.java added at 1.1
diff -N TestPseudoComp.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ TestPseudoComp.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,30 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+
+package org.picoextras.xml;
+
+public class TestPseudoComp {
+ private int foo;
+ private String bar;
+
+ public int getFoo() {
+ return foo;
+ }
+ public String getBar() {
+ return bar;
+ }
+
+ public void setFoo(int foo) {
+ this.foo = foo;
+ }
+
+ public void setBar(String bar) {
+ this.bar = bar;
+ }
+}
----------
nano /xml /src /test /org /picoextras /xml
XStreamXmlPseudoComponentFactoryTestCase.java added at 1.1
diff -N XStreamXmlPseudoComponentFactoryTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ XStreamXmlPseudoComponentFactoryTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
@@ -0,0 +1,48 @@
+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.xml;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.picoextras.xml.XStreamXmlPseudoComponentFactory;
+import org.picoextras.xml.TestPseudoComp;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * @author Paul Hammant
+ * @author Marcos Tarruella
+ */
+public class XStreamXmlPseudoComponentFactoryTestCase extends TestCase {
+
+ public void testDeserialization() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
+ XStreamXmlPseudoComponentFactory xsf = new XStreamXmlPseudoComponentFactory();
+
+ StringReader sr = new StringReader("" +
+ "<org.picoextras.xml.TestPseudoComp>" +
+ "<foo>10</foo>" +
+ "<bar>hello</bar>" +
+ "</org.picoextras.xml.TestPseudoComp>"
+ );
+ InputSource is = new InputSource(sr);
+ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document doc = db.parse(is);
+
+ Object o = xsf.makeInstance(doc.getDocumentElement());
+ TestPseudoComp tsc = (TestPseudoComp) o;
+ assertEquals("hello",tsc.getBar());
+ assertEquals(10,tsc.getFoo());
+ }
+}