001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql;
024:
025: import java.io.IOException;
026: import java.io.StringReader;
027: import java.lang.reflect.Constructor;
028: import java.lang.reflect.InvocationTargetException;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031:
032: import javax.xml.parsers.FactoryConfigurationError;
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: import org.xml.sax.SAXException;
036:
037: import biz.hammurapi.config.ConfigurationException;
038: import biz.hammurapi.config.DomConfigurable;
039: import biz.hammurapi.config.GenericContainer;
040: import biz.hammurapi.convert.CompositeConverter;
041: import biz.hammurapi.xml.dom.DOMUtils;
042:
043: /**
044: * Container, which reads component definitions from rowset.
045: * @author Pavel Vlasov
046: * @revision $Revision$
047: */
048: public class RowsetConfigurableContainer extends GenericContainer
049: implements RowProcessor {
050: public RowsetConfigurableContainer() {
051: // Default constructor
052: }
053:
054: public boolean process(ResultSet rs) throws SQLException {
055: String value = rs.getString("PARAMETER_VALUE");
056: String componentName = rs.getString("NAME");
057: String componentClass = rs.getString("CLASS_NAME");
058: try {
059: // Default class is String
060: if (componentClass == null
061: || componentClass.trim().length() == 0) {
062: addComponent(componentName, value);
063: } else {
064: Class clazz = Class.forName(componentClass);
065: if (value == null || value.trim().length() == 0) {
066: addComponent(componentName, clazz.newInstance());
067: } else {
068: // Attempt to construct class from value
069: Constructor[] constructors = clazz
070: .getConstructors();
071: for (int i = 0; i < constructors.length; i++) {
072: if (constructors[i].getParameterTypes().length == 1
073: && constructors[i].getParameterTypes()[0]
074: .equals(String.class)) {
075: addComponent(
076: componentName,
077: constructors[i]
078: .newInstance(new Object[] { value }));
079: return true;
080: }
081: }
082:
083: Object instance = clazz.newInstance();
084:
085: if (DomConfigurable.class.isAssignableFrom(clazz)) {
086: ((DomConfigurable) instance).configure(DOMUtils
087: .parse(new StringReader(value))
088: .getDocumentElement(), this );
089: } else {
090: instance = CompositeConverter
091: .getDefaultConverter().convert(
092: instance, clazz, false);
093: }
094: addComponent(componentName, instance);
095: }
096: }
097: } catch (InstantiationException e) {
098: throw new SQLExceptionEx("Cannot instantiate parameter '"
099: + componentName + "' -> " + componentClass, e);
100: } catch (IllegalAccessException e) {
101: throw new SQLExceptionEx("Cannot instantiate parameter '"
102: + componentName + "' -> " + componentClass, e);
103: } catch (ClassNotFoundException e) {
104: throw new SQLExceptionEx("Cannot instantiate parameter '"
105: + componentName + "' -> " + componentClass, e);
106: } catch (SecurityException e) {
107: throw new SQLExceptionEx("Cannot instantiate parameter '"
108: + componentName + "' -> " + componentClass, e);
109: } catch (InvocationTargetException e) {
110: throw new SQLExceptionEx("Cannot instantiate parameter '"
111: + componentName + "' -> " + componentClass, e);
112: } catch (ConfigurationException e) {
113: throw new SQLExceptionEx("Cannot instantiate parameter '"
114: + componentName + "'", e);
115: } catch (SAXException e) {
116: throw new SQLExceptionEx("Cannot instantiate parameter '"
117: + componentName + "'", e);
118: } catch (IOException e) {
119: throw new SQLExceptionEx("Cannot instantiate parameter '"
120: + componentName + "'", e);
121: } catch (ParserConfigurationException e) {
122: throw new SQLExceptionEx("Cannot instantiate parameter '"
123: + componentName + "'", e);
124: } catch (FactoryConfigurationError e) {
125: throw new SQLExceptionEx("Cannot instantiate parameter '"
126: + componentName + "'", e);
127: }
128: return true;
129: }
130: }
|