[CVS nano] update

Paul Hammant <paul-yCVjj/[email protected]> Mon, 17 Nov 2003 15:01:14 -0600
Newsgroups gmane.comp.java.nanocontainer.cvs
Message-ID <[email protected]>
Commit in nano/groovypico/src on MAIN

java/org/picoextras/groovy/PicoBuilder.java +133 added 1.1

/PicoBuilderException.java +29 added 1.1

test/org/picoextras/groovy/Xxx.java +45 added 1.1

+207

3 added files

update

----------

nano /groovypico /src /java /org /picoextras /groovy

PicoBuilder.java added at 1.1

diff -N PicoBuilder.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoBuilder.java 17 Nov 2003 21:01:13 -0000 1.1
@@ -0,0 +1,133 @@

+/*****************************************************************************
+ * 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 James Strachan *
+ *****************************************************************************/
+
+package org.picoextras.groovy;
+
+import groovy.util.BuilderSupport;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.codehaus.groovy.runtime.InvokerHelper;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.defaults.ComponentAdapterFactory;
+import org.picocontainer.defaults.DefaultPicoContainer;
+
+/**
+ * Builds trees of Pico containers and Pico components using GroovyMarkup
+ *
+ * @author <a href="mailto:[email protected]">James Strachan</a>
+ * @version $Revision: 1.1 $
+ */
+public class PicoBuilder extends BuilderSupport {
+
+ public PicoBuilder() {
+ }
+
+ protected void setParent(Object parent, Object child) {
+ if (parent instanceof MutablePicoContainer) {
+ MutablePicoContainer parentContainer = (MutablePicoContainer) parent;
+ if (child instanceof MutablePicoContainer) {
+ MutablePicoContainer childContainer = (MutablePicoContainer) child;
+ parentContainer.addChild(childContainer);
+ }
+ }
+ }
+
+ protected void nodeCompleted(Object parent, Object node) {
+ }
+
+ protected Object createNode(Object name) {
+ return createNode(name, Collections.EMPTY_MAP);
+ }
+
+ protected Object createNode(Object name, Object value) {
+ if (value instanceof Class) {
+ Map attributes = new HashMap();
+ attributes.put("componentClass", value);
+ return createNode(name, attributes);
+ }
+ return createNode(name);
+ }
+
+ protected Object createNode(Object name, Map attributes) {
+ if (name.equals("container")) {
+ return createContainer(attributes);
+ }
+ else {
+ Object parent = getCurrent();
+ if (parent instanceof MutablePicoContainer) {
+ MutablePicoContainer pico = (MutablePicoContainer) parent;
+
+ //System.out.println("Creating node: " + name + " with " + attributes);
+
+ if (name.equals("component")) {
+ Class type = (Class) attributes.remove("componentClass");
+ if (type != null) {
+ Object key = attributes.remove("key");
+ if (key != null) {
+ pico.registerComponentImplementation(key, type);
+ }
+ else {
+ pico.registerComponentImplementation(type);
+ }
+ return name;
+ }
+ else {
+ throw new PicoBuilderException("Must specify a componentClass attribute for a component");
+ }
+ }
+ else if (name.equals("bean")) {
+ // lets create a bean
+ Object answer = createBean(attributes);
+ pico.registerComponentInstance(answer);
+ return answer;
+ }
+ }
+ else {
+ throw new PicoBuilderException("method: " + name + " must be a child of a container element");
+ }
+ }
+ throw new PicoBuilderException("uknown method: " + name);
+ }
+
+ protected Object createContainer(Map attributes) {
+ ComponentAdapterFactory adapterFactory = (ComponentAdapterFactory) attributes.remove("adapterFactory");
+ if (adapterFactory != null) {
+ return new DefaultPicoContainer(adapterFactory);
+ }
+ else {
+ return new DefaultPicoContainer();
+ }
+ }
+
+ protected Object createBean(Map attributes) {
+ Class type = (Class) attributes.remove("beanClass");
+ if (type == null) {
+ throw new PicoBuilderException("bean must have a beanClass attribute");
+ }
+ try {
+ Object bean = type.newInstance();
+ // now lets set the properties on the bean
+ for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ String name = entry.getKey().toString();
+ Object value = entry.getValue();
+ InvokerHelper.setProperty(bean, name, value);
+ }
+ return bean;
+ }
+ catch (Exception e) {
+ throw new PicoBuilderException("Failed to create bean of type: " + type + ". Reason: " + e, e);
+ }
+ }
+}

----------

nano /groovypico /src /java /org /picoextras /groovy

PicoBuilderException.java added at 1.1

diff -N PicoBuilderException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PicoBuilderException.java 17 Nov 2003 21:01:13 -0000 1.1
@@ -0,0 +1,29 @@

+/*****************************************************************************
+ * 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 James Strachan *
+ *****************************************************************************/
+
+package org.picoextras.groovy;
+
+/**
+ * Exception thrown due to invalid GroovyMarkup when creating pico containers
+ * and components
+ *
+ * @author <a href="mailto:[email protected]">James Strachan</a>
+ * @version $Revision: 1.1 $
+ */
+public class PicoBuilderException extends RuntimeException {
+
+ public PicoBuilderException(String message) {
+ super(message);
+ }
+
+ public PicoBuilderException(String message, Exception e) {
+ super(message, e);
+ }
+}

----------

nano /groovypico /src /test /org /picoextras /groovy

Xxx.java added at 1.1

diff -N Xxx.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Xxx.java 17 Nov 2003 21:01:14 -0000 1.1
@@ -0,0 +1,45 @@

+/*****************************************************************************
+ * Copyright (C) NanoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ * *
+ *****************************************************************************/
+package org.picoextras.groovy;
+
+import org.picocontainer.lifecycle.Lifecycle;
+import junit.framework.Assert;
+
+/**
+ * An abstract component and three dependancies used for testing.
+ */
+public abstract class Xxx implements Lifecycle {
+
+ public static String componentRecorder = "";
+
+ public void start() {
+ componentRecorder += "<" + code();
+ }
+
+ public void stop() {
+ componentRecorder += code() + ">";
+ }
+
+ public void dispose() {
+ componentRecorder += "!" + code();
+ }
+
+ private String code() {
+ String name = getClass().getName();
+ return name.substring(name.indexOf('$')+1,name.length());
+ }
+
+ public static class A extends Xxx {}
+ public static class B extends Xxx {
+ public B(A a) {
+ Assert.assertNotNull(a);
+ }
+ }
+ public static class C extends Xxx {}
+}