KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > groovy > buildernodes > BeanNode


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by James Strachan *
9  *****************************************************************************/

10
11 package org.nanocontainer.script.groovy.buildernodes;
12
13 import java.util.Map JavaDoc;
14
15 import org.nanocontainer.NanoContainer;
16 import org.picocontainer.MutablePicoContainer;
17 import org.nanocontainer.script.NanoContainerMarkupException;
18 import java.util.Iterator JavaDoc;
19 import org.codehaus.groovy.runtime.InvokerHelper;
20
21 /**
22  * Creates on-the-spot Javabeans configurations and registers the result with
23  * the container via pico.registerCompoenntInstance.
24  * @author James Strachan
25  * @author Paul Hammant
26  * @author Aslak Hellesøy
27  * @author Michael Rimov
28  * @author Mauro Talevi
29  * @version $Revision: 2695 $
30  */

31 public class BeanNode extends AbstractBuilderNode {
32
33     /**
34      * The name of the node we're handling.
35      */

36     public static final String JavaDoc NODE_NAME = "bean";
37
38     /**
39      * Bean class attribute.
40      */

41     public static final String JavaDoc BEAN_CLASS = "beanClass";
42
43
44     /**
45      * Default constructor.
46      */

47     public BeanNode() {
48         super(NODE_NAME);
49     }
50
51     public Object JavaDoc createNewNode(Object JavaDoc current, Map JavaDoc attributes) {
52         MutablePicoContainer pico = ((NanoContainer) current).getPico();
53         Object JavaDoc bean = createBean(attributes);
54         pico.registerComponentInstance(bean);
55         return bean;
56     }
57
58
59     /**
60      * Instantiates the bean and sets the appropriate attributes. It then
61      * @param attributes Map
62      * @return Object resulting JavaBean.
63      */

64     protected Object JavaDoc createBean(final Map JavaDoc attributes) {
65         Class JavaDoc type = (Class JavaDoc) attributes.remove(BEAN_CLASS);
66         if (type == null) {
67             throw new NanoContainerMarkupException("Bean must have a beanClass attribute");
68         }
69         try {
70             Object JavaDoc bean = type.newInstance();
71             // now let's set the properties on the bean
72
for (Iterator JavaDoc iter = attributes.entrySet().iterator(); iter.hasNext();) {
73                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
74                 String JavaDoc name = entry.getKey().toString();
75                 Object JavaDoc value = entry.getValue();
76                 InvokerHelper.setProperty(bean, name, value);
77             }
78             return bean;
79         } catch (IllegalAccessException JavaDoc e) {
80             throw new NanoContainerMarkupException("Failed to create bean of type '" + type + "'. Reason: " + e, e);
81         } catch (InstantiationException JavaDoc e) {
82             throw new NanoContainerMarkupException("Failed to create bean of type " + type + "'. Reason: " + e, e);
83         }
84     }
85
86     /**
87      * {@inheritDoc}
88      * <p>This version only checks for 'beanClass' and lets all other attributes
89      * through (since they become property values)</p>
90      * @param specifiedAttributes Map
91      * @throws NanoContainerMarkupException
92      */

93     public void validateScriptedAttributes(Map JavaDoc specifiedAttributes) throws NanoContainerMarkupException {
94         if (!specifiedAttributes.containsKey(BEAN_CLASS)) {
95             throw new NanoContainerMarkupException("Attribute " + BEAN_CLASS + " is required.");
96         }
97
98         //Assume all other attributes
99
}
100 }
101
Popular Tags