001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql;
005    
006    import java.io.IOException;
007    import java.io.StringReader;
008    import java.lang.reflect.Constructor;
009    import java.lang.reflect.InvocationTargetException;
010    import java.sql.ResultSet;
011    import java.sql.SQLException;
012    
013    import javax.xml.parsers.FactoryConfigurationError;
014    import javax.xml.parsers.ParserConfigurationException;
015    
016    import org.xml.sax.SAXException;
017    
018    import biz.hammurapi.config.ConfigurationException;
019    import biz.hammurapi.config.DomConfigurable;
020    import biz.hammurapi.config.GenericContainer;
021    import biz.hammurapi.convert.CompositeConverter;
022    import biz.hammurapi.xml.dom.DOMUtils;
023    
024    
025    /**
026     * Container, which reads component definitions from rowset.
027     * @author Pavel Vlasov
028     * @revision $Revision$
029     */
030    public class RowsetConfigurableContainer extends GenericContainer implements RowProcessor {
031            public RowsetConfigurableContainer() {
032                    // Default constructor
033            }
034            
035            public boolean process(ResultSet rs) throws SQLException {
036                    String value=rs.getString("PARAMETER_VALUE");
037                    String componentName = rs.getString("NAME");
038                    String componentClass = rs.getString("CLASS_NAME");
039                    try {
040                            // Default class is String
041                            if (componentClass==null || componentClass.trim().length()==0) {
042                                    addComponent(componentName, value);
043                            } else {
044                                    Class clazz = Class.forName(componentClass);
045                                    if (value==null || value.trim().length()==0) {
046                                            addComponent(componentName,     clazz.newInstance());
047                                    } else {
048                                            // Attempt to construct class from value
049                                            Constructor[] constructors=clazz.getConstructors();
050                                            for (int i=0; i<constructors.length; i++) {
051                                                    if (constructors[i].getParameterTypes().length==1 && constructors[i].getParameterTypes()[0].equals(String.class)) {
052                                                            addComponent(componentName,     constructors[i].newInstance(new Object[] {value}));
053                                                            return true;
054                                                    }
055                                            }
056                                            
057                                            Object instance = clazz.newInstance();
058            
059                                            if (DomConfigurable.class.isAssignableFrom(clazz)) {
060                                                    ((DomConfigurable) instance).configure(DOMUtils.parse(new StringReader(value)).getDocumentElement(), this);
061                                            } else {
062                                                    instance = CompositeConverter.getDefaultConverter().convert(instance, clazz, false);
063                                            }
064                                            addComponent(componentName, instance);                                                                  
065                                    }
066                            }
067                    } catch (InstantiationException e) {
068                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
069                    } catch (IllegalAccessException e) {
070                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
071                    } catch (ClassNotFoundException e) {
072                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
073                    } catch (SecurityException e) {
074                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
075                    } catch (InvocationTargetException e) {
076                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"' -> "+componentClass, e);
077                    } catch (ConfigurationException e) {
078                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"'", e);
079                    } catch (SAXException e) {
080                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"'", e);
081                    } catch (IOException e) {
082                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"'", e);
083                    } catch (ParserConfigurationException e) {
084                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"'", e);
085                    } catch (FactoryConfigurationError e) {
086                            throw new SQLExceptionEx("Cannot instantiate parameter '"+componentName+"'", e);
087                    }
088                    return true;
089            }
090    }