[CVS nano] rhino and xml combined into script
Paul Hammant <paul-yCVjj/[email protected]> Mon, 17 Nov 2003 17:13:54 -0600
| Newsgroups | gmane.comp.java.nanocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit in nano on MAIN
nanocontainer/project.xml +1 -7 1.12 -> 1.13
nanocontainer/src/java/org/nanocontainer/JavaScriptCompositionNanoContainer.java +2 -2 1.2 -> 1.3
/NanoContainer.java +2 -2 1.11 -> 1.12
/XmlCompositionNanoContainer.java +3 -3 1.2 -> 1.3
nanocontainer/src/test/org/nanocontainer/BespokeNanoRhinoScriptable.java +1 -1 1.4 -> 1.5
/BespokeXmlFrontEnd.java +2 -2 1.6 -> 1.7
/BogusNanoRhinoScriptable.java +1 -1 1.3 -> 1.4
/XmlCompositionNanoContainerTestCase.java +3 -3 1.2 -> !
1.3
script/.cvsignore +6 added 1.1
/project.properties +6 added 1.1
/project.xml +35 added 1.1
script/src/java/org/picoextras/script/rhino/DefaultNanoRhinoScriptable.java +94 added 1.1
/NanoRhinoManager.java +54 added 1.1
/NanoRhinoScriptable.java +18 added 1.1
/NanoRhinoScriptableHolder.java +19 added 1.1
script/src/java/org/picoextras/script/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
script/src/test/org/picoextras/script/rhino/DefaultNanoRhinoScriptableTestCase.java +109 added 1.1
script/src/test/org/picoextras/script/xml/BeanXmlPseudoComponentFactoryTestCase.java +48 added 1.1
/DefaultXmlFrontEndTestCase.java +155 added 1.1
/ParameterXmlFrontEndTestCase.java +118 added 1.1
/TestPseudoComp.java +30 added 1.1
/XStreamXmlPseudoComponentFactoryTestCase.java +46 added 1.1
rhino/.cvsignore -6 1.2 removed
/project.properties -6 1.2 removed
/project.xml -30 1.4 removed
rhino/src/java/org/picoextras/rhino/DefaultNanoRhinoScriptable.java -94 1.1 removed
/NanoRhinoManager.java -54 1.1 removed
/NanoRhinoScriptable.java -18 1.1 removed
/NanoRhinoScriptableHolder.java -17 1.1 removed
rhino/src/test/org/picoextras/rhino/DefaultNanoRhinoScriptableTestCase.java -109 1.1 removed
xml/.cvsignore -6 1.1 removed
/project.properties -3 1.1 removed
/project.xml -30 1.6 removed
xml/src/java/org/picoextras/xml/BeanXmlPseudoComponentFactory.java -70 1.1 removed
/DefaultXmlFrontEnd.java -195 1.1 removed
/EmptyXmlCompositionException.java -19 1.1 removed
/ParameterXmlFrontEnd.java -223 1.1 removed
/XStreamXmlPseudoComponentFactory.java -27 1.1 removed
/XmlFrontEnd.java -36 1.1 removed
/XmlFrontEndException.java -37 1.1 removed
/XmlPseudoComponentFactory.java -18 1.1 removed
xml/src/test/org/picoextras/xml/BeanXmlPseudoComponentFactoryTestCase.java -47 1.1 removed
/DefaultXmlFrontEndTestCase.java -155 1.1 removed
/ParameterXmlFrontEndTestCase.java -122 1.1 removed
/TestPseudoComp.java -30 1.1 removed
/XStreamXmlPseudoComponentFactoryTestCase.java -48 1.1 removed
+1378 -1421
21 added + 24 removed + 8 modified, total 53 files
- TODO we need to have config elements parsing here, and register with params action...
rhino and xml combined into script
----------
nano /nanocontainer
project.xml 1.12 -> 1.13
diff -u -r1.12 -r1.13
--- project.xml 16 Nov 2003 16:19:10 -0000 1.12
+++ project.xml 17 Nov 2003 23:13:50 -0000 1.13
@@ -17,16 +17,10 @@
<artifactId>picocontainer-reflection</artifactId>
<version>${pom.currentVersion}</version>
</dependency>
-
- <dependency>
- <groupId>${pom.groupId}</groupId>
- <artifactId>picocontainer-xml</artifactId>
- <version>${pom.currentVersion}</version>
- </dependency>
<dependency>
<groupId>${pom.groupId}</groupId>
- <artifactId>picocontainer-rhino</artifactId>
+ <artifactId>picocontainer-script</artifactId>
<version>${pom.currentVersion}</version>
</dependency>
----------
nano /nanocontainer /src /java /org /nanocontainer
JavaScriptCompositionNanoContainer.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- JavaScriptCompositionNanoContainer.java 17 Nov 2003 10:03:16 -0000 1.2
+++ JavaScriptCompositionNanoContainer.java 17 Nov 2003 23:13:50 -0000 1.3
@@ -8,8 +8,8 @@
*****************************************************************************/
package org.nanocontainer;
-import org.picoextras.rhino.DefaultNanoRhinoScriptable;
-import org.picoextras.rhino.NanoRhinoManager;
+import org.picoextras.script.rhino.DefaultNanoRhinoScriptable;
+import org.picoextras.script.rhino.NanoRhinoManager;
import org.picocontainer.PicoCompositionException;
import java.io.IOException;
----------
nano /nanocontainer /src /java /org /nanocontainer
NanoContainer.java 1.11 -> 1.12
diff -u -r1.11 -r1.12
--- NanoContainer.java 17 Nov 2003 10:03:16 -0000 1.11
+++ NanoContainer.java 17 Nov 2003 23:13:50 -0000 1.12
@@ -8,7 +8,7 @@
*****************************************************************************/
package org.nanocontainer;
-import org.picoextras.xml.EmptyXmlCompositionException;
+import org.picoextras.script.xml.EmptyXmlCompositionException;
import org.picocontainer.PicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.extras.DefaultLifecyclePicoAdapter;
@@ -22,7 +22,7 @@
* @author Mauro Talevi
* @author Ward Cunningham
* @author Paul Hammant
- * @version $Revision: 1.11 $
+ * @version $Revision: 1.12 $
*/
public abstract class NanoContainer {
----------
nano /nanocontainer /src /java /org /nanocontainer
XmlCompositionNanoContainer.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- XmlCompositionNanoContainer.java 17 Nov 2003 10:03:16 -0000 1.2
+++ XmlCompositionNanoContainer.java 17 Nov 2003 23:13:50 -0000 1.3
@@ -8,8 +8,8 @@
*****************************************************************************/
package org.nanocontainer;
-import org.picoextras.xml.DefaultXmlFrontEnd;
-import org.picoextras.xml.XmlFrontEnd;
+import org.picoextras.script.xml.DefaultXmlFrontEnd;
+import org.picoextras.script.xml.XmlFrontEnd;
import org.picocontainer.PicoCompositionException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -27,7 +27,7 @@
* @author Aslak Hellesøy
* @author Paul Hammant
* @author Ward Cunningham
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class XmlCompositionNanoContainer extends NanoContainer {
----------
nano /nanocontainer /src /test /org /nanocontainer
BespokeNanoRhinoScriptable.java 1.4 -> 1.5
diff -u -r1.4 -r1.5
--- BespokeNanoRhinoScriptable.java 17 Nov 2003 10:03:16 -0000 1.4
+++ BespokeNanoRhinoScriptable.java 17 Nov 2003 23:13:50 -0000 1.5
@@ -12,7 +12,7 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
-import org.picoextras.rhino.DefaultNanoRhinoScriptable;
+import org.picoextras.script.rhino.DefaultNanoRhinoScriptable;
public class BespokeNanoRhinoScriptable extends DefaultNanoRhinoScriptable {
----------
nano /nanocontainer /src /test /org /nanocontainer
BespokeXmlFrontEnd.java 1.6 -> 1.7
diff -u -r1.6 -r1.7
--- BespokeXmlFrontEnd.java 17 Nov 2003 10:03:16 -0000 1.6
+++ BespokeXmlFrontEnd.java 17 Nov 2003 23:13:50 -0000 1.7
@@ -8,8 +8,8 @@
*****************************************************************************/
package org.nanocontainer;
-import org.picoextras.xml.DefaultXmlFrontEnd;
-import org.picoextras.xml.XmlFrontEnd;
+import org.picoextras.script.xml.DefaultXmlFrontEnd;
+import org.picoextras.script.xml.XmlFrontEnd;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoCompositionException;
import org.picocontainer.PicoContainer;
----------
nano /nanocontainer /src /test /org /nanocontainer
BogusNanoRhinoScriptable.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- BogusNanoRhinoScriptable.java 17 Nov 2003 10:03:16 -0000 1.3
+++ BogusNanoRhinoScriptable.java 17 Nov 2003 23:13:50 -0000 1.4
@@ -13,7 +13,7 @@
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
-import org.picoextras.rhino.DefaultNanoRhinoScriptable;
+import org.picoextras.script.rhino.DefaultNanoRhinoScriptable;
public class BogusNanoRhinoScriptable extends ScriptableObject {
----------
nano /nanocontainer /src /test /org /nanocontainer
XmlCompositionNanoContainerTestCase.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- XmlCompositionNanoContainerTestCase.java 17 Nov 2003 10:03:16 -0000 1.2
+++ XmlCompositionNanoContainerTestCase.java 17 Nov 2003 23:13:50 -0000 1.3
@@ -30,7 +30,7 @@
* @author Aslak Hellesøy
* @author Paul Hammant
* @author Ward Cunningham
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class XmlCompositionNanoContainerTestCase extends TestCase {
@@ -122,7 +122,7 @@
NanoContainer nano = null;
nano = new XmlCompositionNanoContainer(new StringReader("" +
"<container>" +
- " <pseudocomponent factory='org.picoextras.xml.XStreamXmlPseudoComponentFactory'>" +
+ " <pseudocomponent factory='org.picoextras.script.xml.XStreamXmlPseudoComponentFactory'>" +
" <org.picoextras.testmodel.WebServerConfigStub>" +
" <host>foobar.com</host> " +
" <port>4321</port> " +
@@ -143,7 +143,7 @@
NanoContainer nano = null;
nano = new XmlCompositionNanoContainer(new StringReader("" +
"<container>" +
- " <pseudocomponent factory='org.picoextras.xml.BeanXmlPseudoComponentFactory'>" +
+ " <pseudocomponent factory='org.picoextras.script.xml.BeanXmlPseudoComponentFactory'>" +
" <org.picoextras.testmodel.WebServerConfigBean>" +
" <host>foobar.com</host> " +
" <port>4321</port> " +
----------
nano /script
.cvsignore added at 1.1
diff -N .cvsignore
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ .cvsignore 17 Nov 2003 23:13:51 -0000 1.1
@@ -0,0 +1,6 @@
+target
+junit*
+*.log
+*.iws
+*.ipr
+*.iml
----------
nano /script
project.properties added at 1.1
diff -N project.properties
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ project.properties 17 Nov 2003 23:13:51 -0000 1.1
@@ -0,0 +1,6 @@
+maven.junit.fork = yes
+maven.junit.sysproperties = testcomp.jar
+testcomp.jar = ${basedir}/../reflection/src/test-comp/TestComp.jar
+
+maven.repo.remote = http://www.ibiblio.org/maven,http://dist.codehaus.org
+
----------
nano /script
project.xml added at 1.1
diff -N project.xml
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ project.xml 17 Nov 2003 23:13:51 -0000 1.1
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project>
+ <extend>${basedir}/../project.xml</extend>
+ <id>picoextras-script</id>
+ <name>picoextras-script</name>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>picoextras-testmodel</artifactId>
+ <version>${pom.currentVersion}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>picoextras-reflection</artifactId>
+ <version>${pom.currentVersion}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>rhino</groupId>
+ <artifactId>js</artifactId>
+ <version>1.5R4.1</version>
+ </dependency>
+
+ <dependency>
+ <id>xstream</id>
+ <version>0.1-SNAPSHOT</version>
+ </dependency>
+
+ </dependencies>
+
+</project>
----------
nano /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -0000 1.1
@@ -0,0 +1,54 @@
+package org.picoextras.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.rhino;
+
+import org.picocontainer.MutablePicoContainer;
+import org.mozilla.javascript.Scriptable;
+
+public interface NanoRhinoScriptable extends Scriptable {
+
+ MutablePicoContainer getPicoContainer();
+
+}
----------
nano /script /src /java /org /picoextras /script /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 23:13:51 -0000 1.1
@@ -0,0 +1,19 @@
+package org.picoextras.script.rhino;
+
+import org.picoextras.script.rhino.NanoRhinoScriptable;
+
+public class NanoRhinoScriptableHolder {
+
+ private NanoRhinoScriptable nanoRhinoScriptable;
+
+ public NanoRhinoScriptable getNanoRhinoScriptable() {
+ return nanoRhinoScriptable;
+ }
+
+ public void setNanoRhinoScriptable(NanoRhinoScriptable nanoRhinoScriptable) {
+ if (this.nanoRhinoScriptable == null) {
+ this.nanoRhinoScriptable = nanoRhinoScriptable;
+ }
+ }
+
+}
----------
nano /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.xml;
+
+public class EmptyXmlCompositionException extends XmlFrontEndException {
+ public EmptyXmlCompositionException() {
+ }
+
+ public String getMessage() {
+ return "No components in the XML composition";
+ }
+}
----------
nano /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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.script.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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.xml;
+
+import org.picoextras.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /java /org /picoextras /script /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 23:13:51 -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.script.xml;
+
+import org.xml.sax.SAXException;
+import org.w3c.dom.Element;
+
+public interface XmlPseudoComponentFactory {
+ Object makeInstance(Element elem) throws SAXException, ClassNotFoundException;
+}
----------
nano /script /src /test /org /picoextras /script /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 23:13:51 -0000 1.1
@@ -0,0 +1,109 @@
+package org.picoextras.script.rhino;
+
+import junit.framework.TestCase;
+import org.picocontainer.PicoCompositionException;
+import org.picocontainer.PicoContainer;
+import org.picoextras.testmodel.WebServer;
+import org.picoextras.testmodel.WebServerConfig;
+import org.picoextras.testmodel.WebServerImpl;
+
+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 /script /src /test /org /picoextras /script /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 23:13:51 -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.script.xml;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.picoextras.script.xml.BeanXmlPseudoComponentFactory;
+import org.picoextras.script.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 BeanXmlPseudoComponentFactoryTestCase extends TestCase {
+
+ public void testDeserialization() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException {
+ BeanXmlPseudoComponentFactory xsf = new BeanXmlPseudoComponentFactory();
+
+ StringReader sr = new StringReader("" +
+ "<org.picoextras.script.xml.TestPseudoComp>" +
+ "<foo>10</foo>" +
+ "<bar>hello</bar>" +
+ "</org.picoextras.script.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 /script /src /test /org /picoextras /script /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 23:13:51 -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.script.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.script.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 /script /src /test /org /picoextras /script /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 23:13:51 -0000 1.1
@@ -0,0 +1,118 @@
+/*****************************************************************************
+ * 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.script.xml;
+
+import junit.framework.TestCase;
+import org.picocontainer.PicoContainer;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.picoextras.testmodel.DefaultWebServerConfig;
+import org.picoextras.testmodel.WebServerConfigComp;
+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.IOException;
+import java.io.StringReader;
+
+/**
+ * @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 /script /src /test /org /picoextras /script /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 23:13:51 -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.script.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 /script /src /test /org /picoextras /script /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 23:13:51 -0000 1.1
@@ -0,0 +1,46 @@
+/*****************************************************************************
+ * 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.script.xml;
+
+import junit.framework.TestCase;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+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.script.xml.TestPseudoComp>" +
+ "<foo>10</foo>" +
+ "<bar>hello</bar>" +
+ "</org.picoextras.script.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 /rhino
.cvsignore removed after 1.2
diff -N .cvsignore
--- .cvsignore 4 Nov 2003 16:34:30 -0000 1.2
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,6 +0,0 @@
-target
-junit*
-*.log
-*.iws
-*.ipr
-*.iml
----------
nano /rhino
project.properties removed after 1.2
diff -N project.properties
--- project.properties 29 Oct 2003 08:03:49 -0000 1.2
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,6 +0,0 @@
-maven.junit.fork = yes
-maven.junit.sysproperties = testcomp.jar
-testcomp.jar = ${basedir}/../reflection/src/test-comp/TestComp.jar
-
-maven.repo.remote = http://www.ibiblio.org/maven,http://dist.codehaus.org
-
----------
nano /rhino
project.xml removed after 1.4
diff -N project.xml
--- project.xml 17 Nov 2003 22:34:38 -0000 1.4
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<project>
- <extend>${basedir}/../project.xml</extend>
- <id>picoextras-rhino</id>
- <name>picoextras-rhino</name>
-
- <dependencies>
-
- <dependency>
- <groupId>${pom.groupId}</groupId>
- <artifactId>picoextras-testmodel</artifactId>
- <version>${pom.currentVersion}</version>
- </dependency>
-
- <dependency>
- <groupId>${pom.groupId}</groupId>
- <artifactId>picoextras-reflection</artifactId>
- <version>${pom.currentVersion}</version>
- </dependency>
-
- <dependency>
- <groupId>rhino</groupId>
- <artifactId>js</artifactId>
- <version>1.5R4.1</version>
- </dependency>
-
- </dependencies>
-
-</project>
----------
nano /rhino /src /java /org /picoextras /rhino
DefaultNanoRhinoScriptable.java removed after 1.1
diff -N DefaultNanoRhinoScriptable.java
--- DefaultNanoRhinoScriptable.java 17 Nov 2003 21:05:16 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,94 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N NanoRhinoManager.java
--- NanoRhinoManager.java 17 Nov 2003 21:05:16 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,54 +0,0 @@
-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 removed after 1.1
diff -N NanoRhinoScriptable.java
--- NanoRhinoScriptable.java 17 Nov 2003 21:05:16 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,18 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N NanoRhinoScriptableHolder.java
--- NanoRhinoScriptableHolder.java 17 Nov 2003 21:05:16 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,17 +0,0 @@
-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 removed after 1.1
diff -N DefaultNanoRhinoScriptableTestCase.java
--- DefaultNanoRhinoScriptableTestCase.java 17 Nov 2003 21:05:16 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,109 +0,0 @@
-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 /xml
.cvsignore removed after 1.1
diff -N .cvsignore
--- .cvsignore 10 Sep 2003 15:24:19 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,6 +0,0 @@
-target
-*.log
-cvsspam_test
-*.iws
-*.ipr
-*.iml
----------
nano /xml
project.properties removed after 1.1
diff -N project.properties
--- project.properties 12 Oct 2003 23:43:09 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,3 +0,0 @@
-maven.junit.fork = yes
-maven.junit.sysproperties = testcomp.jar
-testcomp.jar = ${basedir}/../reflection/src/test-comp/TestComp.jar
----------
nano /xml
project.xml removed after 1.6
diff -N project.xml
--- project.xml 17 Nov 2003 22:34:39 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<project>
- <extend>${basedir}/../project.xml</extend>
- <id>picoextras-xml</id>
- <name>picoextras-xml</name>
-
- <dependencies>
- <dependency>
- <groupId>${pom.groupId}</groupId>
- <artifactId>picoextras-testmodel</artifactId>
- <version>${pom.currentVersion}</version>
- </dependency>
-
- <dependency>
- <groupId>${pom.groupId}</groupId>
- <artifactId>picoextras-reflection</artifactId>
- <version>${pom.currentVersion}</version>
- </dependency>
-
- <dependency>
- <id>xstream</id>
- <version>0.1-SNAPSHOT</version>
- </dependency>
-
- </dependencies>
-
-
-
-</project>
----------
nano /xml /src /java /org /picoextras /xml
BeanXmlPseudoComponentFactory.java removed after 1.1
diff -N BeanXmlPseudoComponentFactory.java
--- BeanXmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,70 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N DefaultXmlFrontEnd.java
--- DefaultXmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,195 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N EmptyXmlCompositionException.java
--- EmptyXmlCompositionException.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,19 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N ParameterXmlFrontEnd.java
--- ParameterXmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,223 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N XStreamXmlPseudoComponentFactory.java
--- XStreamXmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,27 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N XmlFrontEnd.java
--- XmlFrontEnd.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,36 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N XmlFrontEndException.java
--- XmlFrontEndException.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,37 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N XmlPseudoComponentFactory.java
--- XmlPseudoComponentFactory.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,18 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N BeanXmlPseudoComponentFactoryTestCase.java
--- BeanXmlPseudoComponentFactoryTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,47 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N DefaultXmlFrontEndTestCase.java
--- DefaultXmlFrontEndTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,155 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N ParameterXmlFrontEndTestCase.java
--- ParameterXmlFrontEndTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,122 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N TestPseudoComp.java
--- TestPseudoComp.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-/*****************************************************************************
- * 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 removed after 1.1
diff -N XStreamXmlPseudoComponentFactoryTestCase.java
--- XStreamXmlPseudoComponentFactoryTestCase.java 17 Nov 2003 21:05:17 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,48 +0,0 @@
-/*****************************************************************************
- * 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());
- }
-}