001: package org.objectweb.celtix.bus.configuration.spring;
002:
003: import java.beans.PropertyEditor;
004: import java.math.BigInteger;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import javax.xml.namespace.QName;
009:
010: import org.objectweb.celtix.common.i18n.Message;
011: import org.objectweb.celtix.common.logging.LogUtils;
012: import org.objectweb.celtix.configuration.Configuration;
013: import org.objectweb.celtix.configuration.ConfigurationException;
014: import org.objectweb.celtix.configuration.ConfigurationItemMetadata;
015: import org.objectweb.celtix.jaxb.JAXBUtils;
016: import org.objectweb.celtix.tools.generators.spring.BeanGenerator;
017: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
018: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
019: import org.springframework.core.io.Resource;
020:
021: @SuppressWarnings("deprecation")
022: public class CeltixXmlBeanFactory extends DefaultListableBeanFactory {
023:
024: private static final Logger LOG = LogUtils
025: .getL7dLogger(CeltixXmlBeanFactory.class);
026: private static final Class DEFAULT_PARSER_CLASS = CeltixXmlBeanDefinitionParser.class;
027:
028: private final PropertyEditor editor;
029:
030: CeltixXmlBeanFactory(Resource res) {
031: XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(
032: this );
033: reader.setParserClass(DEFAULT_PARSER_CLASS);
034: reader.setEntityResolver(new CeltixBeansDtdResolver());
035: reader.setValidating(false);
036: reader.setNamespaceAware(true);
037: reader.loadBeanDefinitions(res);
038:
039: editor = new JaxbPropertyEditor();
040: registerCustomEditor(String.class, editor);
041:
042: PropertyEditor pe = null;
043:
044: pe = new JaxbBigIntegerEditor();
045: registerCustomEditor(BigInteger.class, pe);
046:
047: pe = new JaxbBooleanEditor();
048: registerCustomEditor(boolean.class, pe);
049: registerCustomEditor(Boolean.class, pe);
050:
051: pe = new JaxbNumberEditor(Byte.class);
052: registerCustomEditor(byte.class, pe);
053: registerCustomEditor(Byte.class, pe);
054:
055: pe = new JaxbNumberEditor(Short.class);
056: registerCustomEditor(short.class, pe);
057: registerCustomEditor(Short.class, pe);
058:
059: pe = new JaxbNumberEditor(Integer.class);
060: registerCustomEditor(int.class, pe);
061: registerCustomEditor(Integer.class, pe);
062:
063: pe = new JaxbNumberEditor(Long.class);
064: registerCustomEditor(long.class, pe);
065: registerCustomEditor(Long.class, pe);
066:
067: pe = new JaxbNumberEditor(Float.class);
068: registerCustomEditor(float.class, pe);
069: registerCustomEditor(Float.class, pe);
070:
071: pe = new JaxbNumberEditor(Double.class);
072: registerCustomEditor(double.class, pe);
073: registerCustomEditor(Double.class, pe);
074:
075: }
076:
077: void registerCustomEditors(Configuration c) {
078:
079: for (ConfigurationItemMetadata definition : c.getModel()
080: .getDefinitions()) {
081: QName qn = definition.getType();
082: String className = BeanGenerator.getClassName(qn, true);
083:
084: Class cl = JAXBUtils.holderClass(className);
085: if (null != cl) {
086: continue;
087: }
088:
089: try {
090: cl = Class.forName(className);
091: } catch (ClassCastException ex) {
092: throw new ConfigurationException(new Message(
093: "COULD_NOT_REGISTER_PROPERTY_EDITOR_EXC", LOG,
094: className), ex);
095: } catch (ClassNotFoundException ex) {
096: throw new ConfigurationException(new Message(
097: "COULD_NOT_REGISTER_PROPERTY_EDITOR_EXC", LOG,
098: className), ex);
099: }
100:
101: if (cl == String.class) {
102: continue;
103: }
104:
105: if (null == getCustomEditors().get(cl)) {
106: registerCustomEditor(cl, editor);
107: if (LOG.isLoggable(Level.FINE)) {
108: LOG
109: .fine("Registered JaxbPropertyEditor for class: "
110: + className);
111: }
112: }
113: }
114: }
115:
116: }
|