KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > deployment > xml > BeanFactoryHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.kernel.plugins.deployment.xml;
23
24 import javax.xml.namespace.NamespaceContext JavaDoc;
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.jboss.beans.metadata.plugins.factory.GenericBeanFactoryMetaData;
28 import org.jboss.beans.metadata.spi.ConstructorMetaData;
29 import org.jboss.beans.metadata.spi.PropertyMetaData;
30 import org.jboss.beans.metadata.spi.ValueMetaData;
31 import org.jboss.dependency.spi.ControllerMode;
32 import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler;
33 import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
34 import org.xml.sax.Attributes JavaDoc;
35
36 /**
37  * BeanFactoryHandler.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 43013 $
41  */

42 public class BeanFactoryHandler extends DefaultElementHandler
43 {
44    /** The handler */
45    public static final BeanFactoryHandler HANDLER = new BeanFactoryHandler();
46    
47    public Object JavaDoc startElement(Object JavaDoc parent, QName JavaDoc name, ElementBinding element)
48    {
49       return new GenericBeanFactoryMetaData();
50    }
51
52    public void attributes(Object JavaDoc o, QName JavaDoc elementName, ElementBinding element, Attributes JavaDoc attrs, NamespaceContext JavaDoc nsCtx)
53    {
54       GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) o;
55       for (int i = 0; i < attrs.getLength(); ++i)
56       {
57          String JavaDoc localName = attrs.getLocalName(i);
58          if ("name".equals(localName))
59             bean.setName(attrs.getValue(i));
60          else if ("class".equals(localName))
61             bean.setBeanClass(attrs.getValue(i));
62          else if ("mode".equals(localName))
63             bean.setMode(new ControllerMode(attrs.getValue(i)));
64       }
65    }
66
67    public Object JavaDoc endElement(Object JavaDoc o, QName JavaDoc qName, ElementBinding element)
68    {
69       GenericBeanFactoryMetaData bean = (GenericBeanFactoryMetaData) o;
70       if (bean.getBeanClass() == null)
71       {
72          PropertyMetaData property = bean.getProperty("constructor");
73          if (property == null)
74             throw new IllegalArgumentException JavaDoc("BeanFactory should have a class attribute or a constructor element.");
75          ValueMetaData value = property.getValue();
76          if (value == null)
77             throw new IllegalArgumentException JavaDoc("BeanFactory should have a class attribute or a constructor element.");
78          ConstructorMetaData constructor = (ConstructorMetaData) value.getUnderlyingValue();
79          if (constructor == null)
80             throw new IllegalArgumentException JavaDoc("BeanFactory should have a class attribute or a constructor element.");
81          if (constructor.getFactoryMethod() == null)
82             throw new IllegalArgumentException JavaDoc("BeanFactory should have a class attribute or the constructor element should have a factoryMethod attribute.");
83          if (constructor.getFactory() == null && constructor.getFactoryClass() == null)
84             throw new IllegalArgumentException JavaDoc("BeanFactory should have a class attribute or the constructor element should have a either a factoryClass attribute or a factory element.");
85       }
86       return bean;
87    }
88 }
89
Popular Tags