    001    /*****************************************************************************
    002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by James Strachan                                           *
    009     *****************************************************************************/
    010
    011    package org.nanocontainer.script.groovy.buildernodes;
    012
    013    import java.util.Map;
    014
    015    import org.nanocontainer.NanoContainer;
    016    import org.nanocontainer.script.NanoContainerMarkupException;
    017    import groovy.lang.GroovyObject;
    018    import org.nanocontainer.DefaultNanoContainer;
    019    import org.picocontainer.MutablePicoContainer;
    020
    021    /**
    022     * Handles the child of container 'newBuilder' node.
    023     * @author James Strachan
    024     * @author Paul Hammant
    025     * @author Aslak Helles&oslash;y
    026     * @author Michael Rimov
    027     * @author Mauro Talevi
    028     * @version $Revision: 2695 $
    029     */
    030    public class NewBuilderNode extends AbstractBuilderNode {
    031
    032        /**
    033         * Node name we're handling: 'newBuilder'.
    034         */
    035        public static final String NODE_NAME = "newBuilder";
    036
    037        /**
    038         * Supported attribute: 'class'.
    039         */
    040        public static final String CLASS_ATTRIBUTE = "class";
    041
    042        /**
    043         * Suppoerted attribute 'validating'.  Indicates that attributes should
    044         * be validated and NanoContainerMarkupException should be thrown
    045         * if invalid attributes are found.
    046         * @todo Not yet implemented. How do we get NanoContainer to register
    047         * a component instance?  -MR
    048         */
    049        public static final String VALIDATE_ATTRIBUTE = "validating";
    050
    051
    052        public NewBuilderNode() {
    053            super(NODE_NAME);
    054
    055            addAttribute(CLASS_ATTRIBUTE);
    056            addAttribute(VALIDATE_ATTRIBUTE);
    057        }
    058
    059        public Object createNewNode(final Object current, final Map attributes) {
    060            Object builderClass = attributes.remove(CLASS_ATTRIBUTE);
    061
    062
    063            NanoContainer factory = new DefaultNanoContainer();
    064            MutablePicoContainer parentPico = ((NanoContainer) current).getPico();
    065            factory.getPico().registerComponentInstance(MutablePicoContainer.class, parentPico);
    066            try {
    067                if (builderClass instanceof String) {
    068                    factory.registerComponentImplementation(GroovyObject.class, (String) builderClass);
    069                } else {
    070                    factory.getPico().registerComponentImplementation(GroovyObject.class, (Class) builderClass);
    071                }
    072            } catch (ClassNotFoundException e) {
    073                throw new NanoContainerMarkupException("ClassNotFoundException " + builderClass);
    074            }
    075            Object componentInstance = factory.getPico().getComponentInstance(GroovyObject.class);
    076            return componentInstance;
    077        }
    078
    079    }